cancel
Showing results for 
Search instead for 
Did you mean: 

for loop with boolean

Genius
Genius

I'm trying to write a for loop with a boolean operation specifically intersect. In the first iteration, I run the intersect boolean and then I delete it so I can add a new one. Without the for loop at the top, the rest of the code works fine. When I add that for loop though, I get a "Can't run Boolean" error message.

 

        #for index in range(0, max, step):


        # create instance of Intersect/Boolean builder class
        booleanBuilder = workPart.Features.CreateBooleanBuilderUsingCollector(NXOpen.Features.BooleanFeature.Null)
        booleanBuilder.Tolerance = 0.00001

        scCollector1 = booleanBuilder.ToolBodyCollector
        booleanRegionSelect = booleanBuilder.BooleanRegionSelect

        #declare type of Boolean operation
        booleanBuilder.Operation = NXOpen.Features.Feature.BooleanType.Intersect

        #Find target body ie: the 4 point plane, instance of NX Open Body class
        target_body = workPart.Bodies.FindObject(str(planes[3]))
        added1 = booleanBuilder.Targets.Add(target_body)

        # need to use list of NXOpen.TaggedObject type, can't use NXOpen.Body type
        targets = [NXOpen.TaggedObject.Null] * 1
        targets[0] = target_body
        booleanRegionSelect.AssignTargets(targets)

        #not sure what this is for
        scCollector2 = workPart.ScCollectors.CreateCollector()

        #find tool body, ie the solid body
        tool_features = [NXOpen.Features.Feature.Null] * 1
        tool_body = workPart.Features.FindObject(str(body[0]))
        tool_features[0] = tool_body
        bodyFeatureRule = workPart.ScRuleFactory.CreateRuleBodyFeature(tool_features, False)
        #not sure what this is for
        rules = [None] * 1
        rules[0] = bodyFeatureRule
        scCollector2.ReplaceRules(rules, False)
        #need this line for some reason
        booleanBuilder.ToolBodyCollector = scCollector2
        #call methods
        booleanBuilder.Commit()
        #booleanBuilder.Destroy()

        temp_list=[]
        features= workPart.Features
        #need to pop Intersect body and add to new list
        for feature in features:
            theLw.Open()
            theLw.WriteLine("Feature name is " + feature.JournalIdentifier)
            temp_list.append(feature.JournalIdentifier)

        temp_obj= temp_list.pop()
        theLw.WriteLine("Intersect is " + str(temp_obj))
        #delete intersect
        # objects1 = [NXOpen.NXObject.Null] * 1
        # booleanFeature1 = workPart.Features.FindObject(str(temp_obj))
        # objects1[0] = booleanFeature1
        # nErrs1 = theSession.UpdateManager.AddToDeleteList(objects1)
9 REPLIES

Re: for loop with boolean

Siemens Phenom Siemens Phenom
Siemens Phenom

 

You should always Destroy a Builder after you use it.  If you are creating it in the loop, then Destroy it in the loop.  Leaving it around can cause a variety of odd issues.

 

Re: for loop with boolean

Genius
Genius

thanks for the reply. i uncommented my code from above. I call a booleanBuilder.Destroy() method and I also have 4 lines of code that delete the previous intersect.

 

My other for loops work fine (with datum planes and 4 point body planes) but this for loop keeps giving me a "Unable to perform Boolean" error.

for index in range(start, stop):
.....
#call methods booleanBuilder.Commit() booleanBuilder.Destroy() temp_list=[] features= workPart.Features #need to pop Intersect body and add to new list for feature in features: theLw.Open() theLw.WriteLine("Feature name is " + feature.JournalIdentifier) temp_list.append(feature.JournalIdentifier) temp_obj= temp_list.pop() theLw.WriteLine("Intersect is " + str(temp_obj))

################delete intersect objects1 = [NXOpen.NXObject.Null] * 1 booleanFeature1=workPart.Features.FindObject(str(temp_obj)) objects1[0] = booleanFeature1 nErrs1 = theSession.UpdateManager.AddToDeleteList(objects1)
#########increment for loop index index+=1

Re: for loop with boolean

Siemens Phenom Siemens Phenom
Siemens Phenom

 

Have you verified that you can perform the same boolean operation interactively?  You might need to have your program stop at some point and let you try it.

Re: for loop with boolean

Phenom
Phenom

Rather than deleting the boolean after creating it have you tried creating an Undo mark prior to creating the boolean, then undoing to that mark afterwards?

Pseudo code:

loop
   create undo mark
   create boolean
   destroy boolean builder etc
   interrogate boolean as required
   undo to undo mark
endloop
Graham Inchley Snr R&D Engineer (Systems Development), Sandvik Coromant
Lenovo ThinkPad W540, Win7, 16GB. Developing in: Java | C | C# | KF
Production: NX8.5.3.3 MP11 64bit Testing: NX9.0.2.5

Re: for loop with boolean

Phenom
Phenom

You create a boolean intersect then immediately delete it?

 

I don't understand what you are trying to accomplish...

Re: for loop with boolean

Genius
Genius

I'm trying to create an intersect at different heights. If I create an interesect with a 4 point plane at z=5mm  then I need to delete that intersect before creating a new one at z=6mm.

 

It works fine without the for loop. I just discovered something though, "Edit with rollback." Will post a solution if I figure this out.

Re: for loop with boolean

Phenom
Phenom

If I run a journal that creates a note that says "3Dartist is a totally radical dude" then it immediately deletes the note, the net effect is the same as if I had never run the journal.

 

From your description so far, you seem to be in the same situation. You must be doing something with the intersect body before deleting it, or else what's the point of running the code?

Re: for loop with boolean

Genius
Genius

The code below seems to work. The key method is booleanBuilder.Targets.Clear() right after booleanBuilder.Commit()

 

Is there a way for me to add that method to the NX Open python documentation? https://docs.plm.automation.siemens.com/data_services/resources/nx/10/nx_api/en_US/custom/nxopen_pyt...

 

        booleanBuilder = workPart.Features.CreateBooleanBuilderUsingCollector(NXOpen.Features.BooleanFeature.Null)
        booleanBuilder.Tolerance = 0.00001

        scCollector1 = booleanBuilder.ToolBodyCollector
        booleanRegionSelect = booleanBuilder.BooleanRegionSelect

        #declare type of Boolean operation
        booleanBuilder.Operation = NXOpen.Features.Feature.BooleanType.Intersect

        for index in range(2, 20):
            #Find target body ie: the 4 point plane, instance of NX Open Body class
            target_body = workPart.Bodies.FindObject(str(section_curves[index]))
            added1 = booleanBuilder.Targets.Add(target_body)

            # need to use list of NXOpen.TaggedObject type, can't use NXOpen.Body class
            targets = [NXOpen.TaggedObject.Null] * 1
            targets[0] = target_body
            booleanRegionSelect.AssignTargets(targets)

            #not sure what this is for
            scCollector2 = workPart.ScCollectors.CreateCollector()

            #find tool body, ie the solid body
            tool_features = [NXOpen.Features.Feature.Null] * 1
            tool_body = workPart.Features.FindObject(str(section_curves[0]))
            tool_features[0] = tool_body
            bodyFeatureRule = workPart.ScRuleFactory.CreateRuleBodyFeature(tool_features, False)
            #not sure what this is for
            rules = [None] * 1
            rules[0] = bodyFeatureRule
            scCollector2.ReplaceRules(rules, False)
            #need this line for some reason
            booleanBuilder.ToolBodyCollector = scCollector2
            #call methods
            booleanBuilder.Commit()

            ##clear targets
            booleanBuilder.Targets.Clear()

            ##end of loop
        booleanBuilder.Destroy()

 

Re: for loop with boolean

Phenom
Phenom

3Dartist wrote:

The code below seems to work. The key method is booleanBuilder.Targets.Clear() right after booleanBuilder.Commit()

 

Is there a way for me to add that method to the NX Open python documentation? https://docs.plm.automation.siemens.com/data_services/resources/nx/10/nx_api/en_US/custom/nxopen_pyt...

 

No need to add it, it is already there. The documentation lists the .Targets return type as NXOpen.SelectBodyList (which is a link). When you click that link, it will take you to the .SelectBodyList type which will list all of its properties and methods. All of the properties and methods listed here can be used with the booleanBuilder.Targets object.