// Invoked when the SetColor button is clicked private void SetColorClicked(object sender, System.EventArgs e) { if (selectedComponent != null) { // Get the color ID from the colorTable, using the currently selected name // as the key. The find the corresponding color index in the displayed part UFDisp.ColorName colorID = (UFDisp.ColorName)colorTable[colorBox.SelectedItem]; int colorIndex; theUFSession.Disp.AskClosestColorInDisplayedPart(colorID, out colorIndex); // Set an undo mark before modifying the color; theSession.SetUndoMark(Session.MarkVisibility.Visible, "Set color " + colorBox.SelectedItem); // Create a DisplayModification object to set the color DisplayModification displayMod = theSession.DisplayManager.NewDisplayModification(); displayMod.ApplyToAllFaces = false; displayMod.NewColor = colorIndex; DisplayableObject[] objArray = new DisplayableObject[1]; objArray[0] = selectedComponent; displayMod.Apply(objArray); } }
//------------------------------------------------------------------------------ //Callback Name: apply_cb //------------------------------------------------------------------------------ public int apply_cb() { int errorCode = 0; NXOpen.Features.BlockFeatureBuilder blockFeatureBuilder1 = null; try { Part workPart = theSession.Parts.Work; //Get the values from UI Blocks PropertyList blockHeightProps = blockHeight.GetProperties(); double theHeight = blockHeightProps.GetDouble("Value"); blockHeightProps.Dispose(); blockHeightProps = null; PropertyList blockWidthProps = blockWidth.GetProperties(); double theWidth = blockWidthProps.GetDouble("Value"); blockWidthProps.Dispose(); blockWidthProps = null; PropertyList blockLengthProps = blockLength.GetProperties(); double theLength = blockLengthProps.GetDouble("Value"); blockLengthProps.Dispose(); blockLengthProps = null; PropertyList blockOriginProps = blockOrigin.GetProperties(); Point3d originPoint = blockOriginProps.GetPoint("Point"); blockOriginProps.Dispose(); blockOriginProps = null; PropertyList blockColorProps = blockColor.GetProperties(); int[] color = blockColorProps.GetIntegerVector("Value"); blockColorProps.Dispose(); blockColorProps = null; //Create the NX block NXOpen.Features.Feature nullFeatures_Feature = null; blockFeatureBuilder1 = workPart.Features.CreateBlockFeatureBuilder(nullFeatures_Feature); blockFeatureBuilder1.SetOriginAndLengths(originPoint, theLength.ToString(), theHeight.ToString(), theWidth.ToString()); NXOpen.Features.Feature feature1; feature1 = blockFeatureBuilder1.CommitFeature(); // Get the body from Feature NXOpen.Features.BodyFeature bodyFeat = (NXOpen.Features.BodyFeature)feature1; Body[] bodies = new Body[1]; bodies = bodyFeat.GetBodies(); // Apply the color to feature body DisplayModification displayModification1 = theSession.DisplayManager.NewDisplayModification(); displayModification1.ApplyToAllFaces = true; displayModification1.NewColor = color[0]; DisplayableObject[] objects1 = new DisplayableObject[1]; objects1[0] = bodies[0]; displayModification1.Apply(objects1); displayModification1.Dispose(); } catch (Exception ex) { //---- Enter your exception handling code here ----- errorCode = 1; theUI.NXMessageBox.Show("Block Styler", NXMessageBox.DialogType.Error, ex.Message); } finally { if (blockFeatureBuilder1 != null) { blockFeatureBuilder1.Destroy(); blockFeatureBuilder1 = null; } } return(errorCode); }