×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Uniting All Objects on a Layer

Uniting All Objects on a Layer

Uniting All Objects on a Layer

(OP)
Is there a function that can be used in a journal to unite all bodies on a given layer? We often have scenarios where we copy all bodies in view to a single layer and then unite all and I am looking to automate this process. I already know how to automate the copying portion, and am hoping that the uniting is similarly simple.

We run NX8. Thanks in advance for any help.

RE: Uniting All Objects on a Layer

Do you care which solid is the "target"?

www.nxjournaling.com

RE: Uniting All Objects on a Layer

(OP)
No, it makes no difference as long as all the bodies are united in the end.

RE: Uniting All Objects on a Layer

Do all the bodies always properly intersect the "target"? There might not be a logical way to do all the unites in a single operation.

Proud Member of the Reality-Based Community..

To the Toolmaker, your nice little cartoon drawing of your glass looks cool, but your solid model sucks. Do you want me to fix it, or are you going to take all week to get it back to me so I can get some work done?

RE: Uniting All Objects on a Layer

Here's a quick journal. It doesn't do any error checking, we'll have to add code to fix problems as you find them. If one of the solids doesn't intersect the others (and it is not designated as the target), it will disappear and there will be a warning message on the unite feature. This behavior is no different than interactive NX and you can roll back and edit the unite to fix it, so I didn't add any checks/fixes for this. Currently, it unites all the solids on layer 1. Change the value of the myLayer variable to change this.

CODE

'Unite all the solid bodies on a given layer

Option Strict Off  
Imports System  
Imports System.Collections.Generic  
Imports NXOpen  

Module Module1  

    Sub Main()  

        Dim theSession As Session = Session.GetSession()  
        Dim workPart As Part = theSession.Parts.Work  
        Dim myLayer As Integer = 1  

        Dim bodiesOnLayer As New List(Of Body)  

 'get list of solid bodies on given layer
        For Each temp As Body In workPart.Bodies  
            If temp.IsSolidBody And temp.Layer = myLayer Then  
                bodiesOnLayer.Add(temp)  
            End If  
        Next  

 'MsgBox("there are " & bodiesOnLayer.Count & " solid bodies on layer " & myLayer.ToString)

        Dim markId1 As Session.UndoMarkId  
        markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Unite solids on layer " & myLayer.ToString)  

        Dim nullFeatures_BooleanFeature As Features.BooleanFeature = Nothing  

        Dim booleanBuilder1 As Features.BooleanBuilder  
        booleanBuilder1 = workPart.Features.CreateBooleanBuilderUsingCollector(nullFeatures_BooleanFeature)  

        Dim scCollector1 As ScCollector  
        scCollector1 = booleanBuilder1.ToolBodyCollector  

        booleanBuilder1.Tolerance = 0.001  

        booleanBuilder1.Operation = Features.Feature.BooleanType.Unite  

        Dim added1 As Boolean  
        added1 = booleanBuilder1.Targets.Add(bodiesOnLayer.Item(0))  
bodiesOnLayer.  Remove(bodiesOnLayer.Item(0))

        Dim scCollector2 As ScCollector  
        scCollector2 = workPart.ScCollectors.CreateCollector()  

        Dim bodies1() As Body = bodiesOnLayer.ToArray  
        Dim bodyDumbRule1 As BodyDumbRule  
        bodyDumbRule1 = workPart.ScRuleFactory.CreateRuleBodyDumb(bodies1)  

        Dim rules1(0) As SelectionIntentRule  
        rules1(0) = bodyDumbRule1  
        scCollector2.ReplaceRules(rules1, False)  

        booleanBuilder1.ToolBodyCollector = scCollector2  

        Dim nXObject1 As NXObject  
        nXObject1 = booleanBuilder1.Commit()  

        booleanBuilder1.Destroy()  


    End Sub  


    Public Function GetUnloadOption(ByVal dummy As String) As Integer  

 'Unloads the image when the NX session terminates
        GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination  

    End Function  

End Module 

www.nxjournaling.com

RE: Uniting All Objects on a Layer

(OP)
Cowski that is really excellent. Thank you very much.

All the bodies should be intersecting any time we perform this function, so it should be fine. As you say, the unite function is always something we can edit.

As this worked so well, would it then be possible to subtract all the bodies from another layer from the bodies on the layer that just got united. So, let's say we used what you have already written to unite everything on layer 200. I would want to then subtract some bodies on layer 205 from the united bodies on layer 200. It would be preferable if the bodies used for the subtraction on layer 205 could be kept (like choosing the 'Keep Tool' box in the 'Subtract' interface). Would this work?

RE: Uniting All Objects on a Layer

Change the layerUnite and layerSubtract variables as needed.
This is a lot of cut & paste from the journal recorder, so it could probably stand a little more cleanup but seems to do the job.

CODE

'Unite all the solid bodies on a given layer
' then subtract all the solid bodies on a given layer from the resulting solid

Option Strict Off  
Imports System  
Imports System.Collections.Generic  
Imports NXOpen  

Module Module1  

    Sub Main()  

        Dim theSession As Session = Session.GetSession()  
        Dim workPart As Part = theSession.Parts.Work  
        Dim layerUnite As Integer = 1  
        Dim layerSubtract As Integer = 2  

        Dim bodiesOnUniteLayer As New List(Of Body)  
        Dim bodiesOnSubtractLayer As New List(Of Body)  

 'get list of solid bodies on given layer
        For Each temp As Body In workPart.Bodies  
            If temp.IsSolidBody Then  
                If temp.Layer = layerUnite Then  
                    bodiesOnUniteLayer.Add(temp)  
                ElseIf temp.Layer = layerSubtract Then  
                    bodiesOnSubtractLayer.Add(temp)  
                End If  
            End If  
        Next  

 'Unite solids on specified layer
        Dim markId1 As Session.UndoMarkId  
        markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Unite solids on layer " & layerUnite.ToString)  

        Dim nullFeatures_BooleanFeature As Features.BooleanFeature = Nothing  

        Dim booleanBuilder1 As Features.BooleanBuilder  
        booleanBuilder1 = workPart.Features.CreateBooleanBuilderUsingCollector(nullFeatures_BooleanFeature)  

        booleanBuilder1.Tolerance = 0.001  

        booleanBuilder1.Operation = Features.Feature.BooleanType.Unite  

        Dim added1 As Boolean  
        added1 = booleanBuilder1.Targets.Add(bodiesOnUniteLayer.Item(0))  
bodiesOnUniteLayer.  Remove(bodiesOnUniteLayer.Item(0))

        Dim scCollector1 As ScCollector  
        scCollector1 = workPart.ScCollectors.CreateCollector()  

        Dim bodies1() As Body = bodiesOnUniteLayer.ToArray  
        Dim bodyDumbRule1 As BodyDumbRule  
        bodyDumbRule1 = workPart.ScRuleFactory.CreateRuleBodyDumb(bodies1)  

        Dim rules1(0) As SelectionIntentRule  
        rules1(0) = bodyDumbRule1  
        scCollector1.ReplaceRules(rules1, False)  

        booleanBuilder1.ToolBodyCollector = scCollector1  

        Dim uniteFeature As Features.BooleanFeature  
        uniteFeature = booleanBuilder1.Commit()  
        booleanBuilder1.Destroy()  

        MsgBox("united bodies: " & uniteFeature.GetBodies.Length.ToString)  
        Dim unitedBody As Body = uniteFeature.GetBodies(0)  


 'subtract solids from specified layer
        Dim markId2 As Session.UndoMarkId  
        markId2 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Subtract solids on layer " & layerSubtract.ToString)  


        Dim booleanBuilder2 As Features.BooleanBuilder  
        booleanBuilder2 = workPart.Features.CreateBooleanBuilderUsingCollector(nullFeatures_BooleanFeature)  

        booleanBuilder2.Tolerance = 0.001  

        booleanBuilder2.Operation = Features.Feature.BooleanType.Subtract  

        booleanBuilder2.CopyTools = True  

        Dim added2 As Boolean  
        added2 = booleanBuilder2.Targets.Add(unitedBody)  

        Dim scCollector2 As ScCollector  
        scCollector2 = workPart.ScCollectors.CreateCollector()  

        Dim bodies2() As Body = bodiesOnSubtractLayer.ToArray  

        Dim bodyDumbRule2 As BodyDumbRule  
        bodyDumbRule2 = workPart.ScRuleFactory.CreateRuleBodyDumb(bodies2)  

        Dim rules2(0) As SelectionIntentRule  
        rules2(0) = bodyDumbRule2  
        scCollector2.ReplaceRules(rules2, False)  

        booleanBuilder2.ToolBodyCollector = scCollector2  

        Dim nXObject1 As NXObject  
        nXObject1 = booleanBuilder2.Commit()  

        booleanBuilder2.Destroy()  



    End Sub  


    Public Function GetUnloadOption(ByVal dummy As String) As Integer  

 'Unloads the image when the NX session terminates
        GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination  

    End Function  

End Module 

www.nxjournaling.com

RE: Uniting All Objects on a Layer

(OP)
Thanks again cowski. I probably won't have too much time to mess around with this today, but I wanted to thank you while the thread was still on the front page.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources