Cancel
Showing results for 
Search instead for 
Did you mean: 

NX 10 Combining Journal and UI Block Styler Code

Experimenter
Experimenter

Hi all

 

I am posting here to reach out to any kind soul out here in the forums whom can help me clarify my doubts about my code. To add on , i am quite new to NX coding.

 

I have created a simple journal program that would help me create a plane on a curve. But what i would wish to accomplish is to allow the user to select the curve they want to create the plane on. 

 

I've done some research online and thus ended up creating a dialog box using the UI Block Styler. I am now trying to combine the code of my journal into my block styler but i do not have an idea how to change the this line of code : 

 

" NXOpen.Features.SectionCurve sectionCurve1 = (NXOpen.Features.SectionCurve)workPart.Features.FindObject("SECTION_CURVES(10)"); " 

 

In such a way that the "SECTION_CURVE(10)" is replaced by the curve that is selected by the user when prompted by the UI Block Styler.

 

Below here is my code : 

 

//==============================================================================
// WARNING!! This file is overwritten by the Block UI Styler while generating
// the automation code. Any modifications to this file will be lost after
// generating the code again.
//
// Filename: C:\Users\MTS PG 6\Desktop\test.cs
//
// This file was generated by the NX Block UI Styler
// Created by: MTS PG 6
// Version: NX 10
// Date: 04-17-2017 (Format: mm-dd-yyyy)
// Time: 13:54 (Format: hh-mm)
//
//==============================================================================

//==============================================================================
// Purpose: This TEMPLATE file contains C# source to guide you in the
// construction of your Block application dialog. The generation of your
// dialog file (.dlx extension) is the first step towards dialog construction
// within NX. You must now create a NX Open application that
// utilizes this file (.dlx).
//
// The information in this file provides you with the following:
//
// 1. Help on how to load and display your Block UI Styler dialog in NX
// using APIs provided in NXOpen.BlockStyler namespace
// 2. The empty callback methods (stubs) associated with your dialog items
// have also been placed in this file. These empty methods have been
// created simply to start you along with your coding requirements.
// The method name, argument list and possible return values have already
// been provided for you.
//==============================================================================

//------------------------------------------------------------------------------
//These imports are needed for the following template code
//------------------------------------------------------------------------------
using System;
using NXOpen;
using NXOpen.BlockStyler;

//------------------------------------------------------------------------------
//Represents Block Styler application class
//------------------------------------------------------------------------------
public class test
{
//class members
private static Session theSession = null;
private static UI theUI = null;
private string theDlxFileName;
private NXOpen.BlockStyler.BlockDialog theDialog;
private NXOpen.BlockStyler.Group group0;// Block type: Group
private NXOpen.BlockStyler.CurveCollector edge_select0;// Block type: Curve Collector
//------------------------------------------------------------------------------
//Bit Option for Property: EntityType
//------------------------------------------------------------------------------
public static readonly int EntityType_AllowEdges = (1 << 0);
public static readonly int EntityType_AllowCurves = (1 << 2);
public static readonly int EntityType_AllowPoint = (1 << 3);
public static readonly int EntityType_AllowBodies = (1 << 6);
//------------------------------------------------------------------------------
//Bit Option for Property: CurveRules
//------------------------------------------------------------------------------
public static readonly int CurveRules_SingleCurve = (1 << 0);
public static readonly int CurveRules_ConnectedCurves = (1 << 1);
public static readonly int CurveRules_TangentCurves = (1 << 2);
public static readonly int CurveRules_FaceEdges = (1 << 3);
public static readonly int CurveRules_BodyEdges = (1 << 4);
public static readonly int CurveRules_SheetEdges = (1 << 5);
public static readonly int CurveRules_FeatureCurves = (1 << 6);
public static readonly int CurveRules_VertexEdges = (1 << 8);
public static readonly int CurveRules_VertexTangentEdges = (1 << 9);
public static readonly int CurveRules_RegionBoundaryCurves = (1 <<11);
public static readonly int CurveRules_OuterEdgesofFaces = (1 <<13);
public static readonly int CurveRules_RibTopFaceEdges = (1 <<14);
public static readonly int CurveRules_FeatureIntersectionEdges = (1 <<15);

//------------------------------------------------------------------------------
//Constructor for NX Styler class
//------------------------------------------------------------------------------
public test()
{
try
{
theSession = Session.GetSession();
theUI = UI.GetUI();
theDlxFileName = "test.dlx";
theDialog = theUI.CreateDialog(theDlxFileName);
theDialog.AddApplyHandler(new NXOpen.BlockStyler.BlockDialog.Apply(apply_cb));
theDialog.AddOkHandler(new NXOpen.BlockStyler.BlockDialog.Ok(ok_cb));
theDialog.AddUpdateHandler(new NXOpen.BlockStyler.BlockDialog.Update(update_cb));
theDialog.AddInitializeHandler(new NXOpen.BlockStyler.BlockDialog.Initialize(initialize_cb));
theDialog.AddDialogShownHandler(new NXOpen.BlockStyler.BlockDialog.DialogShown(dialogShown_cb));
}
catch (Exception ex)
{
//---- Enter your exception handling code here -----
throw ex;
}
}
//------------------------------- DIALOG LAUNCHING ---------------------------------
//
// Before invoking this application one needs to open any part/empty part in NX
// because of the behavior of the blocks.
//
// Make sure the dlx file is in one of the following locations:
// 1.) From where NX session is launched
// 2.) $UGII_USER_DIR/application
// 3.) For released applications, using UGII_CUSTOM_DIRECTORY_FILE is highly
// recommended. This variable is set to a full directory path to a file
// containing a list of root directories for all custom applications.
// e.g., UGII_CUSTOM_DIRECTORY_FILE=$UGII_ROOT_DIR\menus\custom_dirs.dat
//
// You can create the dialog using one of the following way:
//
// 1. Journal Replay
//
// 1) Replay this file through Tool->Journal->Play Menu.
//
// 2. USER EXIT
//
// 1) Create the Shared Library -- Refer "Block UI Styler programmer's guide"
// 2) Invoke the Shared Library through File->Execute->NX Open menu.
//
//------------------------------------------------------------------------------
public static void Main()
{
test thetest = null;
try
{
thetest = new test();
// The following method shows the dialog immediately
thetest.Show();
}
catch (Exception ex)
{
//---- Enter your exception handling code here -----
theUI.NXMessageBox.Show("Block Styler", NXMessageBox.DialogType.Error, ex.ToString());
}
finally
{
if(thetest != null)
thetest.Dispose();
thetest = null;
}
}
//------------------------------------------------------------------------------
// This method specifies how a shared image is unloaded from memory
// within NX. This method gives you the capability to unload an
// internal NX Open application or user exit from NX. Specify any
// one of the three constants as a return value to determine the type
// of unload to perform:
//
//
// Immediately : unload the library as soon as the automation program has completed
// Explicitly : unload the library from the "Unload Shared Image" dialog
// AtTermination : unload the library when the NX session terminates
//
//
// NOTE: A program which associates NX Open applications with the menubar
// MUST NOT use this option since it will UNLOAD your NX Open application image
// from the menubar.
//------------------------------------------------------------------------------
public static int GetUnloadOption(string arg)
{
//return System.Convert.ToInt32(Session.LibraryUnloadOption.Explicitly);
return System.Convert.ToInt32(Session.LibraryUnloadOption.Immediately);
// return System.Convert.ToInt32(Session.LibraryUnloadOption.AtTermination);
}

//------------------------------------------------------------------------------
// Following method cleanup any housekeeping chores that may be needed.
// This method is automatically called by NX.
//------------------------------------------------------------------------------
public static void UnloadLibrary(string arg)
{
try
{
//---- Enter your code here -----
}
catch (Exception ex)
{
//---- Enter your exception handling code here -----
theUI.NXMessageBox.Show("Block Styler", NXMessageBox.DialogType.Error, ex.ToString());
}
}

//------------------------------------------------------------------------------
//This method shows the dialog on the screen
//------------------------------------------------------------------------------
public NXOpen.UIStyler.DialogResponse Show()
{
try
{
theDialog.Show();
}
catch (Exception ex)
{
//---- Enter your exception handling code here -----
theUI.NXMessageBox.Show("Block Styler", NXMessageBox.DialogType.Error, ex.ToString());
}
return 0;
}

//------------------------------------------------------------------------------
//Method Name: Dispose
//------------------------------------------------------------------------------
public void Dispose()
{
if(theDialog != null)
{
theDialog.Dispose();
theDialog = null;
}
}

//------------------------------------------------------------------------------
//---------------------Block UI Styler Callback Functions--------------------------
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
//Callback Name: initialize_cb
//------------------------------------------------------------------------------
public void initialize_cb()
{
try
{
group0 = (NXOpen.BlockStyler.Group)theDialog.TopBlock.FindBlock("group0");
edge_select0 = (NXOpen.BlockStyler.CurveCollector)theDialog.TopBlock.FindBlock("edge_select0");
}
catch (Exception ex)
{
//---- Enter your exception handling code here -----
theUI.NXMessageBox.Show("Block Styler", NXMessageBox.DialogType.Error, ex.ToString());
}
}

//------------------------------------------------------------------------------
//Callback Name: dialogShown_cb
//This callback is executed just before the dialog launch. Thus any value set
//here will take precedence and dialog will be launched showing that value.
//------------------------------------------------------------------------------
public void dialogShown_cb()
{
try
{
//---- Enter your callback code here -----
}
catch (Exception ex)
{
//---- Enter your exception handling code here -----
theUI.NXMessageBox.Show("Block Styler", NXMessageBox.DialogType.Error, ex.ToString());
}
}

//------------------------------------------------------------------------------
//Callback Name: apply_cb
//------------------------------------------------------------------------------
public int apply_cb()
{
int errorCode = 0;
try
{
// --------------------------------------------------------------------------------------------------------------------

NXOpen.Session theSession = NXOpen.Session.GetSession();
NXOpen.Part workPart = theSession.Parts.Work;
NXOpen.Part displayPart = theSession.Parts.Display;
NXOpen.Features.Feature nullNXOpen_Features_Feature = null;

// --------------------------------------------------------------------------------------------------------------------
NXOpen.Features.DatumPlaneBuilder datumPlaneBuilder1;
datumPlaneBuilder1 = workPart.Features.CreateDatumPlaneBuilder(nullNXOpen_Features_Feature);
NXOpen.Plane plane1;
plane1 = datumPlaneBuilder1.GetPlane();

// --------------------------------------------------------------------------------------------------------------------
NXOpen.Section section1;
section1 = workPart.Sections.CreateSection(0.0095, 0.01, 0.5);

// --------------------------------------------------------------------------------------------------------------------

NXOpen.Features.Feature[] features1 = new NXOpen.Features.Feature[1];
NXOpen.Features.SectionCurve sectionCurve1 = (NXOpen.Features.SectionCurve)workPart.Features.FindObject("SECTION_CURVES(10)");
features1[0] = sectionCurve1;
NXOpen.Line line1 = (NXOpen.Line)sectionCurve1.FindObject("CURVE 5 {3 (-100,-20,182.843154612058)}");
NXOpen.Curve nullNXOpen_Curve = null;
NXOpen.CurveFeatureChainRule curveFeatureChainRule1;
curveFeatureChainRule1 = workPart.ScRuleFactory.CreateRuleCurveFeatureChain(features1, line1, nullNXOpen_Curve, false, 0.0095);

// --------------------------------------------------------------------------------------------------------------------
NXOpen.SelectionIntentRule[] rules1 = new NXOpen.SelectionIntentRule[1];
rules1[0] = curveFeatureChainRule1;
NXOpen.NXObject nullNXOpen_NXObject = null;
NXOpen.Point3d helpPoint1 = new NXOpen.Point3d(-19.1115306887493, -20.0, 182.843154612058);
section1.AddToSection(rules1, line1, nullNXOpen_NXObject, nullNXOpen_NXObject, helpPoint1, NXOpen.Section.Mode.Create, false);

// --------------------------------------------------------------------------------------------------------------------

NXOpen.Features.Extrude extrude1 = (NXOpen.Features.Extrude)workPart.Features.FindObject("EXTRUDE(2)");
NXOpen.Face face1 = (NXOpen.Face)extrude1.FindObject("FACE 140 {(-100,10.0000000000001,185.3596624388273) EXTRUDE(2)}");

// --------------------------------------------------------------------------------------------------------------------

plane1.SetMethod(NXOpen.PlaneTypes.MethodType.Frenet);
NXOpen.NXObject[] geom1 = new NXOpen.NXObject[2];
geom1[0] = section1;
geom1[1] = face1;
plane1.SetGeometry(geom1);
plane1.SetFrenetSubtype(NXOpen.PlaneTypes.FrenetSubtype.Project);
plane1.Evaluate();

// --------------------------------------------------------------------------------------------------------------------

NXOpen.Point3d corner1_1 = new NXOpen.Point3d(-18.0945811370649, -37.8417169074719, 179.828688768918);
NXOpen.Point3d corner2_1 = new NXOpen.Point3d(18.0945811370649, -37.841769074719, 179.828688768918);
NXOpen.Point3d corner3_1 = new NXOpen.Point3d(18.0945811370649, -2.15828309252811, 185.857620455198);
NXOpen.Point3d corner4_1 = new NXOpen.Point3d(-18.0945811370649, -2.1582830925281, 185.857620455198);
datumPlaneBuilder1.SetCornerPoints(corner1_1, corner2_1, corner3_1, corner4_1);

// --------------------------------------------------------------------------------------------------------------------

NXOpen.Features.Feature feature1;
feature1 = datumPlaneBuilder1.CommitFeature();

// --------------------------------------------------------------------------------------------------------------------
}
catch (Exception ex)
{
//---- Enter your exception handling code here -----
errorCode = 1;
theUI.NXMessageBox.Show("Block Styler", NXMessageBox.DialogType.Error, ex.ToString());
}
return errorCode;
}

//------------------------------------------------------------------------------
//Callback Name: update_cb
//------------------------------------------------------------------------------
public int update_cb( NXOpen.BlockStyler.UIBlock block)
{
try
{
if(block == edge_select0)
{
//---------Enter your code here-----------
}
}
catch (Exception ex)
{
//---- Enter your exception handling code here -----
theUI.NXMessageBox.Show("Block Styler", NXMessageBox.DialogType.Error, ex.ToString());
}
return 0;
}

//------------------------------------------------------------------------------
//Callback Name: ok_cb
//------------------------------------------------------------------------------
public int ok_cb()
{
int errorCode = 0;
try
{
errorCode = apply_cb();
//---- Enter your callback code here -----
}
catch (Exception ex)
{
//---- Enter your exception handling code here -----
errorCode = 1;
theUI.NXMessageBox.Show("Block Styler", NXMessageBox.DialogType.Error, ex.ToString());
}
return errorCode;
}

//------------------------------------------------------------------------------
//Function Name: GetBlockProperties
//Returns the propertylist of the specified BlockID
//------------------------------------------------------------------------------
public PropertyList GetBlockProperties(string blockID)
{
PropertyList plist =null;
try
{
plist = theDialog.GetBlockProperties(blockID);
}
catch (Exception ex)
{
//---- Enter your exception handling code here -----
theUI.NXMessageBox.Show("Block Styler", NXMessageBox.DialogType.Error, ex.ToString());
}
return plist;
}

}

 

Also, i understand that the code of the journal already has a prefixed co-ordinate to create the plane on, my next question would be how do i get the plane to be created based off the new co-ordinates of the plane ?

 

Attached is the code file.

 

Thank you in advance ! Much appreciated !

1 REPLY

Re: NX 10 Combining Journal and UI Block Styler Code

Siemens Phenom Siemens Phenom
Siemens Phenom

This a classic problem -- replacing calls to "FindObject". It's sometimes referred to as the "selection stickiness" problem.

 

Briefly, what you need to do is retrieve the selected object from the selection block on your dialog, and use this object instead of the one found by "FindObject". There are lots of examples of this in the usual places (the GTAC site, the NX example folders, NXJournaling.com, and here).

 

There is an entire section of the "Getting Started with SNAP" guide that's devoted to this issue. It doesn't matter if you're not using SNAP -- the principles are the same in NX/Open.

山田
yamada