Intersection curves using python

Hello there great people.
I am trying to create an intersection curve using Python and I am running into some issues that I can't figure out.

I have a given datum and a list of faces to create the curve from.
I am creating an intersection curve builder like the following:

intersectionCurveBuilder1 = workPart.Features.CreateIntersectionCurveBuilder(NXOpen.Features.Feature.Null)

Then I am converting the list of faces to tagged objects (I've red somewhere that it is required) and add them to the first set.

objects1 = [NXOpen.TaggedObject(face) for face in faces]
intersectionCurveBuilder1.FirstSet.Add(objects1)

I do the same for the datum

objects2 = NXOpen.TaggedObject(datum)
intersectionCurveBuilder1.SecondSet.Add(objects2)

Then I try to commit and destroy the builder

intersectionCurveBuilder1.Commit()
intersectionCurveBuilder1.Destroy()

Then an exception is raised at the line we are adding the faces to the first set, that saying:
Element number 0 no longer exists.
I couldn't find any information on this exception whatsoever, I could really use an advice.
Thanks in advance.

Are you adding a list (or array) of objects? If so, the .Add method requires 2 parameters: the number of objects (integer) and the list of objects. Something like: .Add(objList.Count, objList)

https://docs.plm.automation.siemens.com/data_services/resources/nx/1872/...

Also, both the "face" and "datumplane" object type inherit from taggedobject, so I don't think it is necessary to convert them before passing them to the builder. Try passing in the faces and datum planes as-is.

Thanks for the answer, I gave it a shot but there is not overload matches for there argument.
Which make some sense because if I would to pass only the list I didn't got any type error.
Also the docu I am following didn't mention anything about passing the length of the list.

https://docs.plm.automation.siemens.com/data_services/resources/nx/10/nx...

Any other suggestions?

From the documentation link it looks like you are using NX 10, is this correct?

Can you post all the code you have so far?

In fact I am using NX 12, I just found the documention for 10 being better on my mobile phone.
I'll try type in the code but since it's on a closed network(I actually have no internet access) there can be some typos.

def main():
#here is the session and the UI definition, I'll skip that to save myself some time
number_of_selected = theUI.SelectionManger.GetNumSelectedObjects()

#Split all selected to datums and faces
faces = []
datums = []
for index in range(number_of_selected):
feature = theUI.SelectionManger.GetSelectedTaggedObject(index)
if "DatumPlane" in str(feature):
datums.append(feature)
else:
faces.append(feature)

# loop for each datum
for datum in datums:
intersectionCurveBuilder1 = workPart.Features.CreateIntersectionCurveBuilder(NXOpen.Features.Feature.Null)
# might be worth mentioning, I've also tried to create ruledumbs and setting them before adding it with no aucsses
intersectionCurveBuilder1.FirstSet.Add(faces)
intersectionCurveBuilder1.SecondSet.Add(datum)

intersectionCurveBuilder1.Commit()
intersectionCurveBuilder1.Destroy()

Also, I figured out that faces and datum is inherited from taggedobject but when I am passing them as is I get a No objects for intersection curve exception on the line when we commit.
I surely tell that there is an intersection between them

Hi, is there any chance you could post working code that uses intersectionCurveBuilder?

I am trying to run a python code that draws intersection curves between all components in an assembly, but I am new to programming and haven't gotten the intersectionCurveBuilder to work yet.

Would be greatly appreciated!

Sarah

I recorded a journal in NX 10 and cleaned up the output a bit. If you replay the journal, it should create a block, create a datum plane in the middle of the block, then create intersection curves between the block body faces and the datum plane. (journal tested on NX 10, metric file). The intersection curve code starts around line 111.

import math
import NXOpen
import NXOpen.Features
import NXOpen.GeometricUtilities
def main() :

theSession = NXOpen.Session.GetSession()
workPart = theSession.Parts.Work
displayPart = theSession.Parts.Display
# ----------------------------------------------
# Menu: Insert->Design Feature->Block...
# ----------------------------------------------
markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Start")

if not workPart.Preferences.Modeling.GetHistoryMode() :
raise NXOpen.NXException("Create or edit of a Feature was recorded in History Mode but playback is in History-Free Mode.")
blockFeatureBuilder1 = workPart.Features.CreateBlockFeatureBuilder(NXOpen.Features.Feature.Null)

blockFeatureBuilder1.BooleanOption.Type = NXOpen.GeometricUtilities.BooleanOperation.BooleanType.Create

targetBodies1 = [NXOpen.Body.Null] * 1
targetBodies1[0] = NXOpen.Body.Null
blockFeatureBuilder1.BooleanOption.SetTargetBodies(targetBodies1)

blockFeatureBuilder1.BooleanOption.Type = NXOpen.GeometricUtilities.BooleanOperation.BooleanType.Create

theSession.SetUndoMarkName(markId1, "Block Dialog")

coordinates1 = NXOpen.Point3d(0.0, 0.0, 0.0)
point1 = workPart.Points.CreatePoint(coordinates1)

markId3 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "Block")

blockFeatureBuilder1.Type = NXOpen.Features.BlockFeatureBuilder.Types.OriginAndEdgeLengths

blockFeatureBuilder1.OriginPoint = point1

originPoint1 = NXOpen.Point3d(0.0, 0.0, 0.0)
blockFeatureBuilder1.SetOriginAndLengths(originPoint1, "100", "50", "20")

blockFeatureBuilder1.SetBooleanOperationAndTarget(NXOpen.Features.Feature.BooleanType.Create, NXOpen.Body.Null)

feature1 = blockFeatureBuilder1.CommitFeature()

theSession.DeleteUndoMark(markId3, None)

theSession.SetUndoMarkName(markId1, "Block")

blockFeatureBuilder1.Destroy()

# ----------------------------------------------
# Menu: Insert->Datum/Point->Datum Plane...
# ----------------------------------------------
markId4 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Start")

datumPlaneBuilder1 = workPart.Features.CreateDatumPlaneBuilder(NXOpen.Features.Feature.Null)

plane1 = datumPlaneBuilder1.GetPlane()

theSession.SetUndoMarkName(markId4, "Datum Plane Dialog")

plane1.SetUpdateOption(NXOpen.SmartObject.UpdateOption.WithinModeling)

plane1.SetMethod(NXOpen.PlaneTypes.MethodType.Distance)

block1 = feature1
face1 = block1.FindObject("FACE 3 {(0,25,10) BLOCK(3)}")
face2 = block1.FindObject("FACE 6 {(100,25,10) BLOCK(3)}")

plane1.SetMethod(NXOpen.PlaneTypes.MethodType.Center)

plane1.SetGeometry([face1, face2])

plane1.SetAlternate(NXOpen.PlaneTypes.AlternateType.One)

plane1.Evaluate()

markId6 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "Datum Plane")

plane1.RemoveOffsetData()

plane1.Evaluate()

corner1_1 = NXOpen.Point3d(50.0, -1.25, -1.25)
corner2_1 = NXOpen.Point3d(50.0, -1.25, 21.25)
corner3_1 = NXOpen.Point3d(50.0, 51.25, 21.25)
corner4_1 = NXOpen.Point3d(50.0, 51.25, -1.25)
datumPlaneBuilder1.SetCornerPoints(corner1_1, corner2_1, corner3_1, corner4_1)

datumPlaneBuilder1.ResizeDuringUpdate = True

feature2 = datumPlaneBuilder1.CommitFeature()

datumPlaneFeature1 = feature2
datumPlane1 = datumPlaneFeature1.DatumPlane

datumPlane1.SetReverseSection(False)

theSession.DeleteUndoMark(markId6, None)

theSession.SetUndoMarkName(markId4, "Datum Plane")

datumPlaneBuilder1.Destroy()

# ----------------------------------------------
# Menu: Insert->Derived Curve->Intersect...
# ----------------------------------------------
markId7 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Start")

intersectionCurveBuilder1 = workPart.Features.CreateIntersectionCurveBuilder(NXOpen.Features.Feature.Null)

origin2 = NXOpen.Point3d(0.0, 0.0, 0.0)
normal2 = NXOpen.Vector3d(0.0, 0.0, 1.0)
plane3 = workPart.Planes.CreatePlane(origin2, normal2, NXOpen.SmartObject.UpdateOption.WithinModeling)

intersectionCurveBuilder1.CurveFitData.Tolerance = 0.01

intersectionCurveBuilder1.CurveFitData.AngleTolerance = 0.5

theSession.SetUndoMarkName(markId7, "Intersection Curve Dialog")

body1 = feature1.GetBodies()[0]
faceBodyRule1 = workPart.ScRuleFactory.CreateRuleFaceBody(body1)

rules1 = [None] * 1
rules1[0] = faceBodyRule1
intersectionCurveBuilder1.FirstFace.ReplaceRules(rules1, False)

objects1 = body1.GetFaces()
added1 = intersectionCurveBuilder1.FirstSet.Add(objects1)

plane3.SetMethod(NXOpen.PlaneTypes.MethodType.Distance)

plane3.SetGeometry([datumPlane1])

plane3.Evaluate()

intersectionCurveBuilder1.SecondPlane = plane3

markId9 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "Intersection Curve")

nXObject1 = intersectionCurveBuilder1.Commit()

theSession.DeleteUndoMark(markId9, None)

theSession.SetUndoMarkName(markId7, "Intersection Curve")

intersectionCurveBuilder1.Destroy()

# ----------------------------------------------
# Menu: Tools->Journal->Stop Recording
# ----------------------------------------------

if __name__ == '__main__':
main()

The code ended up pretty much like the the above one but with the "dumbrules" added, I would gladly add the final code but it have to wait until tomorrow. (The machine the code is wrote at is an offline one so I'll type tommorow)

If you don't mind sending your final code that would be awesome. But the above code helps, a lot more understandable than what I got from my journal file. Thanks again!

Sarah