I want to write a journal that, among other things, moves all extrusions that have a name that starts with "_" to a give layer. The problem is, the code I have now only grabs one body associated with a given extrusion and leaves the rest (if the user creates more than one body with a given extrusion operation). I'm looking for a solution that runs thru the feature tree, like my example, as I'm doing other things too. I got this method of moving layers from recording a journal, I'm sure there are better ways, but I haven't learned them.
Thanks for any help you can give, below is my code as it stands:
Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpen.UF
Module Module2
Sub Main()
Const ExtLayer As Integer = 111
Dim theSession As Session = Session.GetSession()
Dim theUfSession As UFSession = UFSession.GetUFSession
If IsNothing(theSession.Parts.Work) Then
'active part required
Return
End If
Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()
Const undoMarkName As String = "NXJ feature group"
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)
For Each tempFeat As Features.Feature In workPart.Features
If tempFeat.FeatureType.ToString="EXTRUDE"
If left(tempFeat.name,1)="_" 'If this is a tagged feature
Dim objectArray1(0) As NXOpen.DisplayableObject 'DEFINES AN ARRAY FOR A TEMP OBJECT, THAT WILL HAVE NEW LAYER
Dim STRINGNAME As String =tempFeat.JournalIdentifier 'FINDS THE JOURNAL NAME OF THE CURRENT OBJECT. LIKLEY AN BETTER WAY TO DO THIS
Dim body1 As NXOpen.Body = CType(workPart.Bodies.FindObject(STRINGNAME), NXOpen.Body) 'CREATES A TEMP OBJECT TO CHANGE LAYER
objectArray1(0) = body1 'THE FUNCTION NEEDS AN ARRAY, NOT JUST ONE BODY, SO MUST CONVERT body1 to an array.
workPart.Layers.MoveDisplayableObjects(ExtLayer,objectArray1) 'THIS ACTUALLY CHANGES THE LAYER
End IF
End If
Next
lw.Close()
End Sub
Public Function GetUnloadOption(ByVal dummy As String) As Integer
'Unloads the image immediately after execution within NX
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately
End Function
End Module
re: move bodies of extrusion to layer
An extrude feature has a .GetBodies method that will return all the bodies created by the feature. See the example code below:
'NXJournaling.com
'December 30, 2020
'If the name of an extrude feature begins with an underscore, move the bodies it creates to a specific layer.
Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpen.UF
Module Module2
Dim theSession As Session = Session.GetSession()
Dim theUfSession As UFSession = UFSession.GetUFSession()
Dim lw As ListingWindow = theSession.ListingWindow
Sub Main()
If IsNothing(theSession.Parts.Work) Then
'active part required
Return
End If
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "move extruded bodies to layer")
lw.Open()
Const ExtLayer As Integer = 111
Dim theBodies As New List(Of Body)
For Each tempFeat As Features.Feature In theSession.Parts.Work.Features
If Not TypeOf (tempFeat) Is Features.Extrude Then
'skip features that are not extrudes
Continue For
End If
If Left(tempFeat.Name, 1) = "_" Then
'we want to move the bodies of this extrude feature
Dim tempExtrude As Features.Extrude = tempFeat
'add the bodies created by the feature to our collection
theBodies.AddRange(tempExtrude.GetBodies)
End If
Next
'use a display modification object to move all the bodies we found to the given layer
Dim displayModification1 As DisplayModification = theSession.DisplayManager.NewDisplayModification()
With displayModification1
.NewLayer = ExtLayer
.Apply(theBodies.ToArray)
.Dispose()
End With
End Sub
End Module