Showing results for 
Search instead for 
Do you mean 
Reply
Solved! Go to solution

NXOpen programming in visual studio 2015

There is a requirement to create an external windows application which will create a model based on entered user parameters. Here are the steps i have taken:-

1. A windows application is created and added references to NXOpen, NXOpen.Utilities

2. Opened an NX 10 session so that my program can identify NXOpen.session

3. Added the following code,:-

 

Dim theSession As NXOpen.Session = NXOpen.Session.GetSession()
Dim workPart As NXOpen.Part = theSession.Parts.Work

Dim displayPart As NXOpen.Part = theSession.Parts.Display

' ----------------------------------------------
' Menu: Insert->Sketch...
' ----------------------------------------------
Dim markId1 As NXOpen.Session.UndoMarkId
markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Start")

Dim nullNXOpen_Sketch As NXOpen.Sketch = Nothing

Dim sketchInPlaceBuilder1 As NXOpen.SketchInPlaceBuilder
sketchInPlaceBuilder1 = workPart.Sketches.CreateNewSketchInPlaceBuilder(nullNXOpen_Sketch)

3 REPLIES

Re: NXOpen programming in visual studio 2015

When you run an external NXOpen program, it has no connection to your running session.  When it gets the Session object, it is effectively running an NX process with no user interface. 

 

Your program needs to either create a part, or open a part, before you try to obtain the work part or the display part.

 

If you specifically need to run an external program and have it connect to a running session, then you will need to use "remoting".  This requires that you first run a .DLL in the interactive session to launch a listener process to be ready for the external connection, and then your external program requires code that will make that connection.  There is an example of this in the .Net folder in your NX installation directories under ...UGOPEN\SampleNXOpenApplications\.NET\RemotingExample.

 

Also please not that the supported version of the Studio for use with NX10 is the 2012 version, also known as V11.

Re: NXOpen programming in visual studio 2015

Hi,
Thanks so much for the useful insights on the session object:-
I was going through the list of functions in the session.parts object and could come up with the following code.

1. Presumption is that C:\Model1.prt is created and is basically an empty NX .prt file
2. Per the hierarchy mentioned in the SNAP guide basepart is the root object and in this case I have casted the my_basepart (type basepart) into workpart
3. While I was debugging the application I can see all NX objects like workpart,sketch etc successfully created. But the issue is that I don't see the circle/cylinder or any other geometry in the file. Can you please suggest where I am wrong here ?

Dim theSession As Session = Session.GetSession()
Dim mypartsta As PartLoadStatus

Dim my_basepart As BasePart = theSession.Parts.OpenBaseDisplay("C:\model1.prt", mypartsta)
Dim workpart As NXOpen.Part = my_basepart
Dim displayPart As NXOpen.Part = theSession.Parts.Display







' ----------------------------------------------
' below mentioned code is from a recorded journal which is extruding a circle -> a cylinder
' got the reference of workpart so I can proceed with using the same object as a journal would have used
' --------------------------- -------------------
Dim markId1 As NXOpen.Session.UndoMarkId
markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Start")

Dim nullNXOpen_Sketch As NXOpen.Sketch = Nothing

Dim sketchInPlaceBuilder1 As NXOpen.SketchInPlaceBuilder
sketchInPlaceBuilder1 = workPart.Sketches.CreateNewSketchInPlaceBuilder(nullNXOpen_Sketch)




Dim unit1 As NXOpen.Unit = CType(workpart.UnitCollection.FindObject("MilliMeter"), NXOpen.Unit)

Dim expression1 As NXOpen.Expression
expression1 = workpart.Expressions.CreateSystemExpressionWithUnits("0", unit1)

Dim expression2 As NXOpen.Expression
expression2 = workpart.Expressions.CreateSystemExpressionWithUnits("0", unit1)

Dim sketchAlongPathBuilder1 As NXOpen.SketchAlongPathBuilder
sketchAlongPathBuilder1 = workpart.Sketches.CreateSketchAlongPathBuilder(nullNXOpen_Sketch)

sketchAlongPathBuilder1.PlaneLocation.Expression.RightHandSide = "0"

theSession.SetUndoMarkName(markId1, "Create Sketch Dialog")

Dim markId2 As NXOpen.Session.UndoMarkId
markId2 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "Create Sketch")

theSession.DeleteUndoMark(markId2, Nothing)

Dim markId3 As NXOpen.Session.UndoMarkId
markId3 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "Create Sketch")

theSession.Preferences.Sketch.CreateInferredConstraints = True

theSession.Preferences.Sketch.ContinuousAutoDimensioning = True

theSession.Preferences.Sketch.DimensionLabel = NXOpen.Preferences.SketchPreferences.DimensionLabelType.Expression

theSession.Preferences.Sketch.TextSizeFixed = True

theSession.Preferences.Sketch.FixedTextSize = 3.0

theSession.Preferences.Sketch.ConstraintSymbolSize = 3.0

theSession.Preferences.Sketch.DisplayObjectColor = False

theSession.Preferences.Sketch.DisplayObjectName = True

Dim nXObject1 As NXOpen.NXObject
nXObject1 = sketchInPlaceBuilder1.Commit()

Dim sketch1 As NXOpen.Sketch = CType(nXObject1, NXOpen.Sketch)

Dim feature1 As NXOpen.Features.Feature
feature1 = sketch1.Feature

Dim markId4 As NXOpen.Session.UndoMarkId
markId4 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "update")

Dim nErrs1 As Integer
nErrs1 = theSession.UpdateManager.DoUpdate(markId4)

sketch1.Activate(NXOpen.Sketch.ViewReorient.True)

theSession.DeleteUndoMark(markId3, Nothing)

theSession.SetUndoMarkName(markId1, "Create Sketch")

sketchInPlaceBuilder1.Destroy()

sketchAlongPathBuilder1.Destroy()

Try
' Expression is still in use.
workpart.Expressions.Delete(expression2)
Catch ex As NXException
ex.AssertErrorCode(1050029)
End Try

Try
' Expression is still in use.
workpart.Expressions.Delete(expression1)
Catch ex As NXException
ex.AssertErrorCode(1050029)
End Try

' ----------------------------------------------
' Menu: Insert->Sketch Curve->Circle...
' ----------------------------------------------
Dim markId5 As NXOpen.Session.UndoMarkId
markId5 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "Profile short list")

Dim markId6 As NXOpen.Session.UndoMarkId
markId6 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "Curve")

theSession.SetUndoMarkVisibility(markId6, "Curve", NXOpen.Session.MarkVisibility.Visible)

Dim nXMatrix1 As NXOpen.NXMatrix
nXMatrix1 = theSession.ActiveSketch.Orientation

Dim center1 As NXOpen.Point3d = New NXOpen.Point3d(-21.0813369651572, 3.29395890080582, 0.0)
Dim arc1 As NXOpen.Arc
arc1 = workpart.Curves.CreateArc(center1, nXMatrix1, 10.9996090006582, 0.0, (360.0 * Math.Pi / 180.0))

theSession.ActiveSketch.AddGeometry(arc1, NXOpen.Sketch.InferConstraintsOption.InferNoConstraints)

Dim dimObject1_1 As NXOpen.Sketch.DimensionGeometry
dimObject1_1.Geometry = arc1
dimObject1_1.AssocType = NXOpen.Sketch.AssocType.Midpoint
dimObject1_1.AssocValue = 0
dimObject1_1.HelpPoint.X = 0.0
dimObject1_1.HelpPoint.Y = 0.0
dimObject1_1.HelpPoint.Z = 0.0
Dim nullNXOpen_NXObject As NXOpen.NXObject = Nothing

dimObject1_1.View = nullNXOpen_NXObject
Dim dimOrigin1 As NXOpen.Point3d = New NXOpen.Point3d(-21.0813369651572, 5.78388058960392, 0.0)
Dim nullNXOpen_Expression As NXOpen.Expression = Nothing

Dim sketchDimensionalConstraint1 As NXOpen.SketchDimensionalConstraint
sketchDimensionalConstraint1 = theSession.ActiveSketch.CreateDiameterDimension(dimObject1_1, dimOrigin1, nullNXOpen_Expression, NXOpen.Sketch.DimensionOption.CreateAsAutomatic)

Dim dimension1 As NXOpen.Annotations.Dimension
dimension1 = sketchDimensionalConstraint1.AssociatedDimension

Dim expression3 As NXOpen.Expression
expression3 = sketchDimensionalConstraint1.AssociatedExpression

theSession.ActiveSketch.Update()

' ----------------------------------------------
' Dialog Begin Circle
' ----------------------------------------------
' ----------------------------------------------
' Menu: Insert->Design Feature->Extrude...
' ----------------------------------------------
Dim markId7 As NXOpen.Session.UndoMarkId
markId7 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Start")

Dim nullNXOpen_Features_Feature As NXOpen.Features.Feature = Nothing


If Not workpart.Preferences.Modeling.GetHistoryMode Then
Throw (New Exception("Create or edit of a Feature was recorded in History Mode but playback is in History-Free Mode."))
End If

Dim extrudeBuilder1 As NXOpen.Features.ExtrudeBuilder
extrudeBuilder1 = workpart.Features.CreateExtrudeBuilder(nullNXOpen_Features_Feature)

Dim section1 As NXOpen.Section
section1 = workpart.Sections.CreateSection(0.0095, 0.01, 0.5)

extrudeBuilder1.Section = section1

extrudeBuilder1.AllowSelfIntersectingSection(True)

Dim unit2 As NXOpen.Unit
unit2 = extrudeBuilder1.Draft.FrontDraftAngle.Units

Dim expression4 As NXOpen.Expression
expression4 = workpart.Expressions.CreateSystemExpressionWithUnits("2.00", unit2)

extrudeBuilder1.DistanceTolerance = 0.01

extrudeBuilder1.BooleanOperation.Type = NXOpen.GeometricUtilities.BooleanOperation.BooleanType.Create

Dim targetBodies1(0) As NXOpen.Body
Dim nullNXOpen_Body As NXOpen.Body = Nothing

targetBodies1(0) = nullNXOpen_Body
extrudeBuilder1.BooleanOperation.SetTargetBodies(targetBodies1)

extrudeBuilder1.Limits.StartExtend.Value.RightHandSide = "0"

extrudeBuilder1.Limits.EndExtend.Value.RightHandSide = "25"

extrudeBuilder1.Offset.StartOffset.RightHandSide = "0"

extrudeBuilder1.Offset.EndOffset.RightHandSide = "5"

Dim smartVolumeProfileBuilder1 As NXOpen.GeometricUtilities.SmartVolumeProfileBuilder
smartVolumeProfileBuilder1 = extrudeBuilder1.SmartVolumeProfile

smartVolumeProfileBuilder1.OpenProfileSmartVolumeOption = False

smartVolumeProfileBuilder1.CloseProfileRule = NXOpen.GeometricUtilities.SmartVolumeProfileBuilder.CloseProfileRuleType.Fci

theSession.SetUndoMarkName(markId7, "Extrude Dialog")

extrudeBuilder1.Limits.StartExtend.Value.RightHandSide = "0"

extrudeBuilder1.BooleanOperation.Type = NXOpen.GeometricUtilities.BooleanOperation.BooleanType.Create

Dim targetBodies2(0) As NXOpen.Body
targetBodies2(0) = nullNXOpen_Body
extrudeBuilder1.BooleanOperation.SetTargetBodies(targetBodies2)

extrudeBuilder1.Limits.EndExtend.Value.RightHandSide = "25"

extrudeBuilder1.BooleanOperation.Type = NXOpen.GeometricUtilities.BooleanOperation.BooleanType.Create

Dim targetBodies3(0) As NXOpen.Body
targetBodies3(0) = nullNXOpen_Body
extrudeBuilder1.BooleanOperation.SetTargetBodies(targetBodies3)

extrudeBuilder1.Draft.FrontDraftAngle.RightHandSide = "2"

Dim expression5 As NXOpen.Expression
expression5 = extrudeBuilder1.Draft.FrontDraftAngle

expression5.RightHandSide = "2"

extrudeBuilder1.BooleanOperation.Type = NXOpen.GeometricUtilities.BooleanOperation.BooleanType.Create

Dim targetBodies4(0) As NXOpen.Body
targetBodies4(0) = nullNXOpen_Body
extrudeBuilder1.BooleanOperation.SetTargetBodies(targetBodies4)

extrudeBuilder1.Draft.BackDraftAngle.RightHandSide = "2"

Dim expression6 As NXOpen.Expression
expression6 = extrudeBuilder1.Draft.BackDraftAngle

expression6.RightHandSide = "2"

extrudeBuilder1.BooleanOperation.Type = NXOpen.GeometricUtilities.BooleanOperation.BooleanType.Create

Dim targetBodies5(0) As NXOpen.Body
targetBodies5(0) = nullNXOpen_Body
extrudeBuilder1.BooleanOperation.SetTargetBodies(targetBodies5)

extrudeBuilder1.BooleanOperation.Type = NXOpen.GeometricUtilities.BooleanOperation.BooleanType.Create

Dim targetBodies6(0) As NXOpen.Body
targetBodies6(0) = nullNXOpen_Body
extrudeBuilder1.BooleanOperation.SetTargetBodies(targetBodies6)

extrudeBuilder1.Offset.StartOffset.RightHandSide = "0"

Dim expression7 As NXOpen.Expression
expression7 = extrudeBuilder1.Offset.StartOffset

expression7.RightHandSide = "0"

extrudeBuilder1.BooleanOperation.Type = NXOpen.GeometricUtilities.BooleanOperation.BooleanType.Create

Dim targetBodies7(0) As NXOpen.Body
targetBodies7(0) = nullNXOpen_Body
extrudeBuilder1.BooleanOperation.SetTargetBodies(targetBodies7)

extrudeBuilder1.Offset.EndOffset.RightHandSide = "5"

Dim expression8 As NXOpen.Expression
expression8 = extrudeBuilder1.Offset.EndOffset

expression8.RightHandSide = "5"

extrudeBuilder1.BooleanOperation.Type = NXOpen.GeometricUtilities.BooleanOperation.BooleanType.Create

Dim targetBodies8(0) As NXOpen.Body
targetBodies8(0) = nullNXOpen_Body
extrudeBuilder1.BooleanOperation.SetTargetBodies(targetBodies8)

section1.DistanceTolerance = 0.01

section1.ChainingTolerance = 0.0095

section1.SetAllowedEntityTypes(NXOpen.Section.AllowTypes.OnlyCurves)

Dim markId8 As NXOpen.Session.UndoMarkId
markId8 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, Nothing)

Dim curves1(0) As NXOpen.ICurve
curves1(0) = arc1
Dim seedPoint1 As NXOpen.Point3d = New NXOpen.Point3d(-17.4148006316045, 3.29395890080582, 0.0)
Dim regionBoundaryRule1 As NXOpen.RegionBoundaryRule
regionBoundaryRule1 = workpart.ScRuleFactory.CreateRuleRegionBoundary(theSession.ActiveSketch, curves1, seedPoint1, 0.01)

section1.AllowSelfIntersection(True)

Dim rules1(0) As NXOpen.SelectionIntentRule
rules1(0) = regionBoundaryRule1
Dim helpPoint1 As NXOpen.Point3d = New NXOpen.Point3d(0.0, 0.0, 0.0)
section1.AddToSection(rules1, nullNXOpen_NXObject, nullNXOpen_NXObject, nullNXOpen_NXObject, helpPoint1, NXOpen.Section.Mode.Create, False)

theSession.DeleteUndoMark(markId8, Nothing)

Dim markId9 As NXOpen.Session.UndoMarkId
markId9 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "section mark")

Dim markId10 As NXOpen.Session.UndoMarkId
markId10 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, Nothing)

theSession.DeleteUndoMark(markId10, Nothing)

Dim direction1 As NXOpen.Direction
direction1 = workpart.Directions.CreateDirection(theSession.ActiveSketch, NXOpen.Sense.Forward, NXOpen.SmartObject.UpdateOption.WithinModeling)

extrudeBuilder1.Direction = direction1

theSession.DeleteUndoMark(markId9, Nothing)

Dim markId11 As NXOpen.Session.UndoMarkId
markId11 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "Extrude")

theSession.DeleteUndoMark(markId11, Nothing)

Dim markId12 As NXOpen.Session.UndoMarkId
markId12 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "Extrude")

extrudeBuilder1.ParentFeatureInternal = False

Dim markId13 As NXOpen.Session.UndoMarkId
markId13 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "Deactivate Sketch")

theSession.ActiveSketch.Deactivate(NXOpen.Sketch.ViewReorient.False, NXOpen.Sketch.UpdateLevel.SketchOnly)

Dim feature2 As NXOpen.Features.Feature
feature2 = extrudeBuilder1.CommitFeature()

theSession.DeleteUndoMark(markId12, Nothing)

theSession.SetUndoMarkName(markId7, "Extrude")

Dim expression9 As NXOpen.Expression = extrudeBuilder1.Limits.StartExtend.Value

Dim expression10 As NXOpen.Expression = extrudeBuilder1.Limits.EndExtend.Value

extrudeBuilder1.Destroy()

workpart.Expressions.Delete(expression4)

Dim scaleAboutPoint1 As NXOpen.Point3d = New NXOpen.Point3d(33.3787835281655, 23.4969068257481, 0.0)
Dim viewCenter1 As NXOpen.Point3d = New NXOpen.Point3d(-33.3787835281655, -23.4969068257481, 0.0)
workpart.ModelingViews.WorkView.ZoomAboutPoint(1.25, scaleAboutPoint1, viewCenter1)

Dim rotMatrix1 As NXOpen.Matrix3x3
rotMatrix1.Xx = 0.814575952050337
rotMatrix1.Xy = 0.0
rotMatrix1.Xz = -0.580056909571197
rotMatrix1.Yx = -0.0
rotMatrix1.Yy = 1.0
rotMatrix1.Yz = 0.0
rotMatrix1.Zx = 0.580056909571197
rotMatrix1.Zy = 0.0
rotMatrix1.Zz = 0.814575952050337
Dim translation1 As NXOpen.Point3d = New NXOpen.Point3d(-2.39933476751111, -4.69938136514963, 11.6221877858965)
workpart.ModelingViews.WorkView.SetRotationTranslationScale(rotMatrix1, translation1, 1.50607146275759)

' ----------------------------------------------
' Menu: Tools->Journal->Stop Recording
' ----------------------------------------------
Solution
Solution
Accepted by topic author cabhishekin
‎08-30-2016 11:02 AM

Re: NXOpen programming in visual studio 2015

When I added your code to a template and ran it in an interactive session of NX, I saw both the circle and the cylinder.

 

Your code never files the part - at least the code you shared - so if you are running that code and then opening the part in NX, it will still be an empty part.  Just as your external program needs to open a part to work on, it needs to save the part after it makes changes, if you want to be able to see the effects of those changes in the part.