Learning NXOpen JAVA: Automate features painting

By | 07/20/2016

    Hello! My name is Alex and today our tutorial would be dedicated to the next question: How to automate the task of feature painting in Siemens NX using NXOpen Java?


    First of all, let’s find out in which cases this functionality would be helpful. If you’re creating a part, which utilizes a lot of features, it’s sometimes necessary to visually separate features by assigning them a different colors.

 The default NX behaviour (concerning a color assigning) divides into three basic branches: using random colors (for faces or bodies), assigning face colors based on the boolean target and tool body, or assigning colors according to feature or feature group. In this sample we will enhance the basic NX functionality by developing an NXOpen application, which would paint features using random color.

    The main idea is that we have to iterate over all the elements of the workPart.features() collection and define them a random color. To assign a new color to a feature we will use the ColorFeatureBuilder. It is also important to note, that NX has some features which are not suitable for the color re-assign, they are: Datum Coordinate Systems, Datum Vectors, Sketches and other (which also would be preserved in workPart.features() collection).


import java.util.Iterator;
import java.util.Random;
import nxopen.BaseSession;
import nxopen.Part;
import nxopen.Session;
import nxopen.SessionFactory;
import nxopen.UI;
import nxopen.features.Feature;
public class featurePaint {
@SuppressWarnings({ "null", "unused" })
public static void main(String[] args) throws Exception
//Define the NX session
Session theSession;

//Define the Work Part
Part workPart;

//Define the Display Part
Part displayPart;

//Define the UI
UI theUI;

//Current NX Session
theSession = (Session)SessionFactory.get("Session");

//Current Work Part
workPart = theSession.parts().work();

//Current UI
theUI = (UI)SessionFactory.get("UI");

//Iterator for the features collection
Iterator iterFeat = workPart.features().iterator();
nxopen.features.ColorFeatureBuilder colorFeatureBuilder = workPart.features().createColorFeatureBuilder();
Random colorID = new Random();
//Processed objects counter
int featCounter = 0;
//Walk through the feature collection
while (iterFeat.hasNext()){
int color = colorID.nextInt(215);
//Assigning a random color
Feature feat = (Feature) iterFeat.next();
//Send current feature to the CFB (colorFeatureBuilder)
try {
//Commit feature
catch (Exception ex) {
//In case you need an information about the features which are not suitable for re-paint
//theUI.nxmessageBox().show("Error", nxopen.NXMessageBox.DialogType.ERROR, ex.getMessage());
//Clear feature

theUI.nxmessageBox().show("Done", nxopen.NXMessageBox.DialogType.INFORMATION, featCounter+" objects were painted");

catch (Exception ex)

StringWriter s = new StringWriter();
PrintWriter p = new PrintWriter(s);
p.println("Caught exception " + ex );
theUI.nxmessageBox().show("Error", nxopen.NXMessageBox.DialogType.ERROR, ex.toString());

public static int getUnloadOption() {

return BaseSession.LibraryUnloadOption.IMMEDIATELY;



See the video of how it works:

Related article.

Author: Alex Sergeenko
Special thanks to

Leave a Reply