Hello to everyone,
I'm trying to understand how to work with Builder features in NXOpen.
For example, if I want to make a circle with a diameter of 100.00mm, I usually have two ways:
1. Using the following journal VB code:
Option Strict Off
Imports System
Imports NXOpen
Module NXJournal
Sub Main (ByVal args() As String)
Dim theSession As NXOpen.Session = NXOpen.Session.GetSession()
Dim workPart As NXOpen.Part = theSession.Parts.Work
Dim displayPart As NXOpen.Part = theSession.Parts.Display
Dim origin3d As NXOpen.Point3d = New NXOpen.Point3d(0, 0, 0)
Dim arc_test As NXOpen.Arc
Dim Y As NXOpen.Vector3d = New NXOpen.Vector3d(0, 1, 0)
Dim Z As NXOpen.Vector3d = New NXOpen.Vector3d(0, 0, 1)
arc_test = workPart.Curves.CreateArc(origin3d, Y, Z, 50, 0, 2*System.Math.Pi)
MsgBox("Radius value is " & arc_test.Radius)
End Sub
End Module
2. Recording a Journal, then editing the code.
If I choose the second option, after tuning the output code of the recorded journal I obtain this result:
Option Strict Off
Imports System
Imports NXOpen
Module NXJournal
Sub Main (ByVal args() As String)
Dim theSession As NXOpen.Session = NXOpen.Session.GetSession()
Dim workPart As NXOpen.Part = theSession.Parts.Work
Dim displayPart As NXOpen.Part = theSession.Parts.Display
Dim nullNXOpen_Features_AssociativeArc As NXOpen.Features.AssociativeArc = Nothing
Dim associativeArcBuilder1 As NXOpen.Features.AssociativeArcBuilder
associativeArcBuilder1 = workPart.BaseFeatures.CreateAssociativeArcBuilder(nullNXOpen_Features_AssociativeArc)
associativeArcBuilder1.Associative = False
associativeArcBuilder1.Type = NXOpen.Features.AssociativeArcBuilder.Types.ArcFromCenter
associativeArcBuilder1.CenterPointReference = NXOpen.Features.AssociativeArcBuilder.CenterReference.Absolute
Dim origin3d As NXOpen.Point3d = New NXOpen.Point3d(0, 0, 0)
Dim origin As NXOpen.Point = workPart.Points.CreatePoint(origin3d)
associativeArcBuilder1.CenterPoint.Value = origin
associativeArcBuilder1.EndPointOptions = NXOpen.Features.AssociativeArcBuilder.EndOption.Radius
associativeArcBuilder1.Radius.RightHandSide = "50"
associativeArcBuilder1.Limits.FullCircle = True
Dim arc_test As NXOpen.Arc
arc_test = associativeArcBuilder1.Commit()
associativeArcBuilder1.Destroy()
MsgBox("Radius value is " & arc_test.Radius)
End Sub
End Module
To test the code, I simply ask for the radius value of the arc through a message box, which does not work in the second case.
If I use the first method, the radius value is correctly shown on a message box.
Now the question is: how to work with builders?
In the second method, I declare "arc_test" as an Arc which is the result of a Commit() of a builder. This should be true. But in the reality I cannot do anythig with that arc (like an extrusion, discover the radius value, etc, etc). I can only simply see the result on the part model.
Is there anyone who can help me?
Thank You in advance, best regards,
kalo86
re: arc object from builder
To get the newly created arc object, call the builder's .GetCommittedObjects method after the call to .Commit and before the call to .Destroy.
Hi,
Hi,
Since I'm creating only one arc object, I get the following error:
Value of 'NXOpen.NXObject' cannot be converted to '1-dimensional array of NXOpen.
I just tried again declaring arc(0) as array, but it does not work :(
If I declare this:
If I declare this:
Dim arc As NXOpen.Arc
arc = associativeArcBuilder1.GetObject()
MsgBox("Radius value is " & arc.Radius)
I get this error:
"System.NullReferenceException: object reference not set to an instance of an object."
Do I have a bug?
re: get arc
"Do I have a bug?"
No.
You need to call the .GetCommittedObjects method, not the .GetObject method.
Sorry if I'm bothering you,
Sorry if I'm bothering you, but declaring this:
Dim nullNXOpen_Features_AssociativeArc As NXOpen.Features.AssociativeArc = Nothing
Dim associativeArcBuilder1 As NXOpen.Features.AssociativeArcBuilder
associativeArcBuilder1 = workPart.BaseFeatures.CreateAssociativeArcBuilder(nullNXOpen_Features_AssociativeArc)
associativeArcBuilder1.Associative = False
associativeArcBuilder1.Type = NXOpen.Features.AssociativeArcBuilder.Types.ArcFromCenter
associativeArcBuilder1.CenterPointReference = NXOpen.Features.AssociativeArcBuilder.CenterReference.Absolute
Dim origin3d As NXOpen.Point3d = New NXOpen.Point3d(0, 0, 0)
Dim origin As NXOpen.Point = workPart.Points.CreatePoint(origin3d)
associativeArcBuilder1.CenterPoint.Value = origin
associativeArcBuilder1.EndPointOptions = NXOpen.Features.AssociativeArcBuilder.EndOption.Radius
associativeArcBuilder1.Radius.RightHandSide = "50"
associativeArcBuilder1.Limits.FullCircle = True
Dim arc_test As NXOpen.Arc
arc_test = associativeArcBuilder1.Commit()
Dim arc As NXOpen.Arc
arc = associativeArcBuilder1.GetCommittedObjects()
associativeArcBuilder1.Destroy()
MsgBox("Radius value is " & arc.Radius)
I get the following error: Value of 'NXOpen.NXObject' cannot be converted to '1-dimensional array of NXOpen
I have no hopes :(
re: return arc
Try changing it to this:
Dim arc() As NXOpen.Arc
arc = associativeArcBuilder1.GetCommittedObjects()
Your arc will be the first (and only) item in the resulting array (arc(0)).
Error again
Declaring arc() with your last suggestion, I get this error:
'Radius' is not a member of 'System.Array'.
re: arc radius
Your code attempts to read the .Radius property of the array object; the array object does not have a .Radius property, thus the error. You need to query the individual arc object instead of the array itself. Hence, my previous comment about arc(0).
msgbox("radius: " & arc(0).Radius.ToString)
Again error: System
Again error: System.InvalidCastException: Unable to cast object of type 'NXOpen.NXObject' on 'NXOpen.Arc'.
Are you running the journal on NX10?
Solved!
Thank You for your help!
This is the solution I just found after 10 tentatives:
Dim arc() As NXOpen.Arc
arc = associativeArcBuilder1.GetCommittedObjects()
Dim arc_finally As NXOpen.Arc = CType(arc(0), NXOpen.Arc)
MsgBox("Radius value is " & arc_finally.Radius)
Fantastic!
Best regards,
kalo86