How to get curve resulting from trimming one curve against another

As part of a much larger program, I'm trying to develop a function that will trim one curve against another. I need to obtain a copy of the trimmed curve to use in later constructions.

I have attached my trim code which works well. It prompts the user to pick two curves which it has no problems trimming. However, I cannot figure out how to obtain a copy of the trimmed object.

Later, I will change the NXOpen.Features.TrimCurve2Builder.Operation.trim option to Divide and try to obtain a list of trimmed segments. But... baby steps first.

As you can see near the bottom of the TrimOneCrvByAnother function, I do a test to see if the object output by trimCurve2Builder1.Commit is nothing. It appears to be nothing but the curves are created in NX and appear in the part browser.

I am attempting this in NX 1988

Any ideas how I can obtain the resulting trimmed curve?

' NX 1988

Imports System
Imports NXOpen
Imports NXOpen.UF

Module NXJournal

dim m_oSession As Session = Session.GetSession()
dim m_ufs As UFSession = UFSession.GetUFSession()
dim m_oWorkPart As Part = m_oSession.Parts.Work
dim m_lw As ListingWindow = m_oSession.ListingWindow

Sub Main (ByVal args() As String)

'--------------------------------------------------------
'--- Begin Test for open part file
'--------------------------------------------------------
m_lw.Close()
m_lw.Open()

'If m_oSession.Parts.Work Is Nothing Then
' MsgBox("Work part required")
' m_lw.WriteLine("Part file argument expected or work part required")
' Return
'End If

''// select the curves
Dim crv1 As Curve = nothing
dim pt3dPickPt1 as point3d
SelectACurve("Select 1st curve:", crv1, pt3dPickPt1)
If crv1 Is Nothing Then
msgbox("curve is nothing")
exit sub
end if

'm_lw.WriteLine("crv1 name: " & crv1.Name)
dim px as point = m_oworkpart.points.createpoint(pt3dPickPt1)
px.SetVisibility(smartobject.visibilityoption.visible)
Px.setname("xxxx")

Dim crv2 As Curve = nothing
dim pt3dPickPt2 as point3d
SelectACurve("Select 1st curve:", crv2, pt3dPickPt2)
If crv2 Is Nothing Then Return
'm_lw.WriteLine("crv name: " & crv2.Name)

'// call trim method
dim TrimCrv as NXOpen.Features.TrimCurve2 = nothing
TrimOneCrvByAnother(crv1, pt3dPickPt1, crv2, pt3dPickPt2)

End Sub

Public Function TrimOneCrvByAnother(ByVal crvToTrim As NXOpen.Curve, byval pt3dPickPt1 as point3d,
ByVal crvBoundry as nxopen.curve, byval pt3dPickPt2 as point3d) As NXOpen.Features.TrimCurve2

'----------------------------------------------------------------------------
'// Create points at the input point locations for diagnostic purposes
'// can be commented out with no problems
'----------------------------------------------------------------------------
dim p1 as point = m_oworkpart.points.createpoint(pt3dPickPt1)
p1.SetVisibility(smartobject.visibilityoption.visible)
p1.setname("pt3dPickPt1")

dim p2 as point = m_oworkpart.points.createpoint(pt3dPickPt2)
p2.SetVisibility(smartobject.visibilityoption.visible)
p2.setname("pt3dPickPt2")

Dim nullNXOpen_Features_TrimCurve2 As NXOpen.Features.TrimCurve2 = Nothing

'// create a direction object
Dim origin1 As NXOpen.Point3d = New NXOpen.Point3d(0.0, 0.0, 0.0)
Dim vector1 As NXOpen.Vector3d = New NXOpen.Vector3d(0.0, 0.0, 1.0)
Dim direction1 As NXOpen.Direction = Nothing
direction1 = m_oWorkPart.Directions.CreateDirection(origin1, vector1, NXOpen.SmartObject.UpdateOption.WithinModeling)

'-------------------------------------------------------------------------------------
'// create selection intent for curve to be trimmed
'-------------------------------------------------------------------------------------
Dim selectionIntentRuleOptions1 As NXOpen.SelectionIntentRuleOptions = Nothing
selectionIntentRuleOptions1 = m_oWorkPart.ScRuleFactory.CreateRuleOptions()
selectionIntentRuleOptions1.SetSelectedFromInactive(False)

Dim curves1(0) As NXOpen.IBaseCurve
curves1(0) = CType(crvToTrim, NXOpen.IBaseCurve)

Dim curveDumbRule1 As NXOpen.CurveDumbRule = Nothing
curveDumbRule1 = m_oWorkPart.ScRuleFactory.CreateRuleBaseCurveDumb(curves1, selectionIntentRuleOptions1)

selectionIntentRuleOptions1.Dispose()

Dim rules1(0) As NXOpen.SelectionIntentRule
rules1(0) = curveDumbRule1
Dim nullNXOpen_NXObject As NXOpen.NXObject = Nothing

Dim evaluator As IntPtr = Nothing
Dim limits(1) As Double
Dim spt(2) As Double
Dim ept(2) As Double
Dim junk(2) As Double

m_ufs.Eval.Initialize(crvToTrim.Tag, evaluator)
m_ufs.Eval.AskLimits(evaluator, limits)
m_ufs.Eval.Evaluate(evaluator, 0, limits(0), spt, junk)
m_ufs.Eval.Evaluate(evaluator, 0, limits(1), ept, junk)
m_ufs.Eval.Free(evaluator)

'------------------------------------------------------------------------
'// create selection intent rules object for bounding element
'------------------------------------------------------------------------
Dim selectionIntentRuleOptions2 As NXOpen.SelectionIntentRuleOptions = Nothing
selectionIntentRuleOptions2 = m_oWorkPart.ScRuleFactory.CreateRuleOptions()
selectionIntentRuleOptions2.SetSelectedFromInactive(False)

Dim curves2(0) As NXOpen.IBaseCurve
curves2(0) = CType(crvBoundry, NXOpen.IBaseCurve)

Dim curveDumbRule2 As NXOpen.CurveDumbRule = Nothing
curveDumbRule2 = m_oWorkPart.ScRuleFactory.CreateRuleBaseCurveDumb(curves2, selectionIntentRuleOptions2)

selectionIntentRuleOptions2.Dispose()

Dim rules2(0) As NXOpen.SelectionIntentRule
rules2(0) = curveDumbRule2

'-------------------------------------------------------
'// make the TrimCurve2Builder object
'-------------------------------------------------------
Dim trimCurve2Builder1 As NXOpen.Features.TrimCurve2Builder = Nothing
trimCurve2Builder1 = m_oWorkPart.Features.CreateTrimCurve2FeatureBuilder(nullNXOpen_Features_TrimCurve2)

trimCurve2Builder1.MakeInputCurvesDashed = True
trimCurve2Builder1.OperationOption = NXOpen.Features.TrimCurve2Builder.Operation.trim
trimCurve2Builder1.CurveOptions.Associative = False
trimCurve2Builder1.CurveOptions.InputCurveOption = NXOpen.GeometricUtilities.CurveOptions.InputCurve.Replace
trimCurve2Builder1.CurveExtensionOption = NXOpen.Features.TrimCurve2Builder.CurveExtension.Natural
trimCurve2Builder1.PerformExtendedIntersectionCalculation = True
trimCurve2Builder1.keepordiscard = nxopen.features.TrimCurve2Builder.KeepDiscard.Discard
trimCurve2Builder1.Vector = direction1
trimCurve2Builder1.CurveToTrim.SetAllowedEntityTypes(NXOpen.Section.AllowTypes.OnlyCurves)
trimCurve2Builder1.CurveToTrim.AllowSelfIntersection(True)
trimCurve2Builder1.CurveToTrim.AllowDegenerateCurves(False)
trimCurve2Builder1.CurveToTrim.AddToSection(rules1, crvToTrim, nullNXOpen_NXObject, nullNXOpen_NXObject, pt3dPickPt1, NXOpen.Section.Mode.Create, False)

'----------------------------------------------------------------
'// setup the bounding objects
'----------------------------------------------------------------
Dim nullNXOpen_Plane As NXOpen.Plane = Nothing

Dim trimCurveBoundingObjectBuilder1 As NXOpen.GeometricUtilities.TrimCurveBoundingObjectBuilder = Nothing
trimCurveBoundingObjectBuilder1 = trimCurve2Builder1.CreateTrimCurveBoundingObjectBuilder()
trimCurveBoundingObjectBuilder1.BoundingPlane = nullNXOpen_Plane
trimCurve2Builder1.BoundingObjectList.Append(trimCurveBoundingObjectBuilder1)

'****************************************************
'// section to hold the objects to trim
'****************************************************
Dim section1 As NXOpen.Section = Nothing
section1 = m_oWorkPart.Sections.CreateSection(0.0094999999999999998, 0.01, 0.5)
'section1.SetAllowedEntityTypes(NXOpen.Section.AllowTypes.CurvesAndPoints)
section1.AllowSelfIntersection(True)
section1.AllowDegenerateCurves(False)
section1.SetAllowedEntityTypes(NXOpen.Section.AllowTypes.OnlyCurves)

dim added1 as boolean = nothing
added1 = trimCurveBoundingObjectBuilder1.BoundingObjectList.Add(section1)

section1.AddToSection(rules2, crvBoundry, nullNXOpen_NXObject, nullNXOpen_NXObject, pt3dPickPt2, NXOpen.Section.Mode.Create, False)

trimCurve2Builder1.UpdateTrimRegionsAndDivideLocations()
trimCurve2Builder1.ResetTrimRegions()
trimCurve2Builder1.SelectTrimRegion(pt3dPickPt1)

'---------------------------------------
'// commit the trim
'---------------------------------------

Dim nXObject1 As NXOpen.NXObject = Nothing
nXObject1 = trimCurve2Builder1.Commit

'// this is where I'm having problems. The object returned by the commit
'// seems to still be nothing.
m_lw.WriteLine("about to check for something")
if nXObject1 is nothing then
m_lw.WriteLine("i am nothing")
else
m_lw.WriteLine("i am something")
m_lw.WriteLine("count: " & nXObject1.GetType.ToString)
end if

'// since the nxObject1 is nothing, the function is not returning a valid value.
Return DirectCast(nXObject1, NXOpen.Features.TrimCurve2)

End Function

Public Sub SelectACurve(ByVal prompt As String, ByRef curve As Curve, byref pt3dPointOnCurve as point3d)
Dim selobj As TaggedObject = Nothing
Dim pt3dCursor As Point3d = Nothing
Dim resp As Selection.Response
Dim scope As Selection.SelectionScope = NXOpen.Selection.SelectionScope.WorkPart
Dim TypeArray() As Selection.SelectionType = New NXOpen.Selection.SelectionType() {Selection.SelectionType.Curves}
Dim theUI As NXOpen.UI = NXOpen.UI.GetUI

resp = theUI.SelectionManager.SelectTaggedObject(
prompt, prompt,
scope, False,
TypeArray,
selobj, pt3dCursor)
curve = CType(selobj, Curve)

'// get the coordinates of the cursor pos in the curve
dim tagCurve as nxopen.tag
tagCurve = curve.tag
dim cp(2) as double
cp(0) = pt3dCursor.x
cp(1) = pt3dCursor.y
cp(2) = pt3dCursor.z

'm_lw.WriteLine("pre cp: " & cp(0) & ", " & cp(1) & ", " & cp(2))
ask_pos_on_obj(tagCurve, cp)
' m_ufs.Disp.SetHighlight(tagCurve, 0)
'm_lw.WriteLine("post cp: " & cp(0) & ", " & cp(1) & ", " & cp(2))

pt3dPointOnCurve.x = cp(0)
pt3dPointOnCurve.y = cp(1)
pt3dPointOnCurve.z = cp(2)

End Sub

'***********************************************************************
'// somehow the dblLocation is acting like it is being passed byref
'***********************************************************************
Public Sub ask_pos_on_obj(ByVal tagInputObj As NXOpen.Tag, ByVal dblLocation() As Double)
Dim cp() As Double = {0, 0, 0}
Dim dblDist As Double = 0
Dim lnPierceLine As UFCurve.Line
Dim tagPierceLine As NXOpen.Tag = NXOpen.Tag.Null
Dim dblStartCoords(2) As Double
Dim dblEndCoords(2) As Double
lnPierceLine.start_point = dblStartCoords
lnPierceLine.end_point = dblEndCoords
map_abs2view(dblLocation)
lnPierceLine.start_point(0) = dblLocation(0)
lnPierceLine.start_point(1) = dblLocation(1)
lnPierceLine.start_point(2) = dblLocation(2) + 10000
lnPierceLine.end_point(0) = dblLocation(0)
lnPierceLine.end_point(1) = dblLocation(1)
lnPierceLine.end_point(2) = dblLocation(2) - 10000
map_view2abs(lnPierceLine.start_point)
map_view2abs(lnPierceLine.end_point)
m_ufs.Curve.CreateLine(lnPierceLine, tagPierceLine)
m_ufs.Modl.AskMinimumDist(tagInputObj, tagPierceLine, 0, cp, 0, cp, dblDist, dblLocation, cp)
m_ufs.Obj.DeleteObject(tagPierceLine)
End Sub

Public Sub map_view2abs(ByRef dblCoords() As Double)
Dim strViewName As String = ""
Dim dblAbsCsys() As Double = {0, 0, 0, 1, 0, 0, 0, 1, 0}
Dim dblLastViewCsys() As Double = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
Dim dblMappingMatrix() As Double = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
Dim intStatus As Integer = 0
m_ufs.Ui.AskLastPickedView(strViewName)
'Dim wp As Part = oSession.Parts.Work
Dim lastViewPicked As View = CType(m_oWorkPart.ModelingViews.FindObject(strViewName), ModelingView)
Dim mat33LastViewPicked As Matrix3x3 = lastViewPicked.Matrix()
dblLastViewCsys(3) = mat33LastViewPicked.Xx
dblLastViewCsys(4) = mat33LastViewPicked.Xy
dblLastViewCsys(5) = mat33LastViewPicked.Xz
dblLastViewCsys(6) = mat33LastViewPicked.Yx
dblLastViewCsys(7) = mat33LastViewPicked.Yy
dblLastViewCsys(8) = mat33LastViewPicked.Yz
dblLastViewCsys(9) = mat33LastViewPicked.Zx
dblLastViewCsys(10) = mat33LastViewPicked.Zy
dblLastViewCsys(11) = mat33LastViewPicked.Zz
m_ufs.Trns.CreateCsysMappingMatrix(dblLastViewCsys, dblAbsCsys, dblMappingMatrix, intStatus)
m_ufs.Trns.MapPosition(dblCoords, dblMappingMatrix)
End Sub

Public Sub map_abs2view(ByRef dblCoords() As Double)
Dim strViewName As String = ""
Dim dblAbsCsys() As Double = {0, 0, 0, 1, 0, 0, 0, 1, 0}
Dim dblLastViewCsys() As Double = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
Dim dblMappingMatrix() As Double = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
Dim intStatus As Integer = 0
m_ufs.Ui.AskLastPickedView(strViewName)
'Dim wp As Part = oSession.Parts.Work
Dim lastViewPicked As View =
CType(m_oWorkPart.ModelingViews.FindObject(strViewName), ModelingView)
Dim mat33LastViewPicked As Matrix3x3 = lastViewPicked.Matrix()
dblLastViewCsys(3) = mat33LastViewPicked.Xx
dblLastViewCsys(4) = mat33LastViewPicked.Xy
dblLastViewCsys(5) = mat33LastViewPicked.Xz
dblLastViewCsys(6) = mat33LastViewPicked.Yx
dblLastViewCsys(7) = mat33LastViewPicked.Yy
dblLastViewCsys(8) = mat33LastViewPicked.Yz
dblLastViewCsys(9) = mat33LastViewPicked.Zx
dblLastViewCsys(10) = mat33LastViewPicked.Zy
dblLastViewCsys(11) = mat33LastViewPicked.Zz
m_ufs.Trns.CreateCsysMappingMatrix(dblAbsCsys, dblLastViewCsys, dblMappingMatrix, intStatus)
m_ufs.Trns.MapPosition(dblCoords, dblMappingMatrix)
End Sub

End Module

I created a new file and made 2 unassociative lines that intersected and ran your code. The code completed without error, the first line that I chose was trimmed to the 2nd.

In your code, you have the following options set:

trimCurve2Builder1.CurveOptions.Associative = False
trimCurve2Builder1.CurveOptions.InputCurveOption = NXOpen.GeometricUtilities.CurveOptions.InputCurve.Replace

The first option (associative = false) means that no feature will be created, you just want to trim an existing curve. The second option means that the input curve will be replaced by the trimmed curve.

Builder objects in NXOpen generally return the feature that they created. In this case no feature is being created, so the builder returns nothing.

At the end of Sub Main (after the trimming operation and before end sub) add this line of code:

crv1.Highlight()

You will see that the trimmed curve will highlight. The original line object stored in crv1 has been replaced by the trimmed version of the line.

Edit: I should add that I also tried creating an associative line feature to see what would happen. The code ran without error or complaint, but the line was not trimmed. I don't think NX will let you do an "unassociative/replace" type trim on a feature curve.

That did it! I'm now able to get the newly created TrimCurve2 object back. That make a lot more sense the way you explained it.
Thanks a ton!