Journal to move objects to layers

I have a pretty basic question.

I am trying to write a journal to move things to layers. Ex. Datums to layer 61, sketch to layer 21, etc.. I can write the journal but it only works for that file. The Stickiness comes into play and I do not know how to get around it. Being that I am new to this programming thing.

Any help would be greatly appreciated.

' NX 9.0.3.4
' Journal created by rhensley on Thu Oct 01 07:47:07 2015 Eastern Daylight Time
'
Option Strict Off
Imports System
Imports NXOpen

Module NXJournal
Sub Main (ByVal args() As String)

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

Dim displayPart As Part = theSession.Parts.Display

' ----------------------------------------------
' Menu: Format->Move to Layer...
' ----------------------------------------------
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Move Layer")

Dim markId2 As Session.UndoMarkId
markId2 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Start")

theSession.SetUndoMarkName(markId2, "Class Selection Dialog")

' Refer to the sample NXOpen application, Selection for "Select All" alternatives.
Dim markId3 As Session.UndoMarkId
markId3 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Class Selection")

theSession.DeleteUndoMark(markId3, Nothing)

Dim markId4 As Session.UndoMarkId
markId4 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Class Selection")

theSession.DeleteUndoMark(markId4, Nothing)

theSession.SetUndoMarkName(markId2, "Class Selection")

theSession.DeleteUndoMark(markId2, Nothing)

' ----------------------------------------------
' Dialog Begin Layer Move
' ----------------------------------------------
Dim objectArray1(4) As DisplayableObject
Dim datumPlane1 As DatumPlane = CType(workPart.Datums.FindObject("DATUM_PLANE(8)"), DatumPlane)

objectArray1(0) = datumPlane1
Dim datumPlane2 As DatumPlane = CType(workPart.Datums.FindObject("DATUM_PLANE(5)"), DatumPlane)

objectArray1(1) = datumPlane2
Dim datumPlane3 As DatumPlane = CType(workPart.Datums.FindObject("DATUM_PLANE(9)"), DatumPlane)

objectArray1(2) = datumPlane3
Dim datumPlane4 As DatumPlane = CType(workPart.Datums.FindObject("DATUM_PLANE(4)"), DatumPlane)

objectArray1(3) = datumPlane4
Dim datumPlane5 As DatumPlane = CType(workPart.Datums.FindObject("DATUM_PLANE(6)"), DatumPlane)

objectArray1(4) = datumPlane5
workPart.Layers.MoveDisplayableObjects(61, objectArray1)

' ----------------------------------------------
' Menu: Format->Move to Layer...
' ----------------------------------------------
Dim markId5 As Session.UndoMarkId
markId5 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Move Layer")

Dim markId6 As Session.UndoMarkId
markId6 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Start")

theSession.SetUndoMarkName(markId6, "Class Selection Dialog")

' Refer to the sample NXOpen application, Selection for "Select All" alternatives.
Dim markId7 As Session.UndoMarkId
markId7 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Class Selection")

theSession.DeleteUndoMark(markId7, Nothing)

Dim markId8 As Session.UndoMarkId
markId8 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Class Selection")

theSession.DeleteUndoMark(markId8, Nothing)

theSession.SetUndoMarkName(markId6, "Class Selection")

theSession.DeleteUndoMark(markId6, Nothing)

' ----------------------------------------------
' Dialog Begin Layer Move
' ----------------------------------------------
Dim objectArray2(1) As DisplayableObject
Dim sketch1 As Sketch = CType(workPart.Sketches.FindObject("SKETCH_004"), Sketch)

objectArray2(0) = sketch1
Dim sketch2 As Sketch = CType(workPart.Sketches.FindObject("SKETCH_002"), Sketch)

objectArray2(1) = sketch2
workPart.Layers.MoveDisplayableObjects(21, objectArray2)

' ----------------------------------------------
' Menu: Format->Move to Layer...
' ----------------------------------------------
Dim markId9 As Session.UndoMarkId
markId9 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Move Layer")

Dim markId10 As Session.UndoMarkId
markId10 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Start")

theSession.SetUndoMarkName(markId10, "Class Selection Dialog")

' Refer to the sample NXOpen application, Selection for "Select All" alternatives.
Dim markId11 As Session.UndoMarkId
markId11 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Class Selection")

theSession.DeleteUndoMark(markId11, Nothing)

Dim markId12 As Session.UndoMarkId
markId12 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Class Selection")

theSession.DeleteUndoMark(markId12, Nothing)

theSession.SetUndoMarkName(markId10, "Class Selection")

theSession.DeleteUndoMark(markId10, Nothing)

' ----------------------------------------------
' Dialog Begin Layer Move
' ----------------------------------------------
Dim objectArray3(1) As DisplayableObject
Dim intersectionCurve1 As Features.IntersectionCurve = CType(workPart.Features.FindObject("INTERSECTION_CURVES(3)"), Features.IntersectionCurve)

Dim ellipse1 As Ellipse = CType(intersectionCurve1.FindObject("CURVE 2 {6 (0.6352611466206,0,-4.5681319232951)}"), Ellipse)

objectArray3(0) = ellipse1
Dim arc1 As Arc = CType(intersectionCurve1.FindObject("CURVE 1 {5 (-1.723390553531,0,-0.995)}"), Arc)

objectArray3(1) = arc1
workPart.Layers.MoveDisplayableObjects(41, objectArray3)

' ----------------------------------------------
' Menu: Format->Move to Layer...
' ----------------------------------------------
Dim markId13 As Session.UndoMarkId
markId13 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Move Layer")

Dim markId14 As Session.UndoMarkId
markId14 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Start")

theSession.SetUndoMarkName(markId14, "Class Selection Dialog")

' Refer to the sample NXOpen application, Selection for "Select All" alternatives.
Dim markId15 As Session.UndoMarkId
markId15 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Class Selection")

theSession.DeleteUndoMark(markId15, Nothing)

Dim markId16 As Session.UndoMarkId
markId16 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Class Selection")

theSession.DeleteUndoMark(markId16, Nothing)

theSession.SetUndoMarkName(markId14, "Class Selection")

theSession.DeleteUndoMark(markId14, Nothing)

' ----------------------------------------------
' Dialog Begin Layer Move
' ----------------------------------------------
Dim objectArray4(1) As DisplayableObject
Dim body1 As Body = CType(workPart.Bodies.FindObject("LINKED_FACE(2)"), Body)

objectArray4(0) = body1
Dim body2 As Body = CType(workPart.Bodies.FindObject("LINKED_FACE(1)"), Body)

objectArray4(1) = body2
workPart.Layers.MoveDisplayableObjects(11, objectArray4)

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

End Sub
End Module

The code below shows one way to move all the sketches in the work part to a particular layer. A similar strategy can be used for other object types.

Option Strict Off
Imports System
Imports NXOpen

Module Module9

Sub Main()

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow

Const sketchLayer As Integer = 21

Dim displayModification1 As DisplayModification
displayModification1 = theSession.DisplayManager.NewDisplayModification()

displayModification1.NewLayer = sketchLayer

displayModification1.Apply(workPart.Sketches.ToArray)

displayModification1.Dispose()

End Sub

End Module

This works awesome. Thanks.

One more question. What would the callout for a sheet body be in NX.open?

ryan hensley

All of the bodies in a part are contained in the .Bodies collection. You can use the Body object's .IsSolidBody (or .IsSheetBody) property to differentiate between solids and sheets. If you want to move sheets to a separate layer from solids, I'd suggest creating two list objects (one for solids, one for sheets), iterating through the body collection and adding each body to the correct list. You can then use the list objects to move the body types separately.

Hello,

I am trying to rewrite the code so that it does the same thing for planes but I am not succeeding. Can you help?

My code:

Option Strict Off
Imports System
Imports NXOpen

Module Module9

Sub Main()

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow

Const DatumsLayer As Integer = 251

Dim displayModification1 As DisplayModification
displayModification1 = theSession.DisplayManager.NewDisplayModification()

displayModification1.NewLayer = DatumsLayer

displayModification1.Apply(workPart.DatumPlanes.ToArray)

displayModification1.Dispose()

End Sub

End Module

Thanks

An NX part does not have a .DatumPlanes collection, but it does have a .Datums collection. The .Datums collection contains datum planes and datum axes; we'll need to process each object to see if it is a plane or axis. The link below leads to some code that shows one way of doing this.

http://www.nxjournaling.com/comment/1381#comment-1381

Thanks that works,

Now I am trying to do the same thing but with Text but I don't know what is the name of the function, Can you help?

Thanks

Text created in modeling with the "Text" command? If so, one way to do it is to iterate through the feature collection looking for the text features. When a text feature is found, use .GetEntities to get the splines created by the text feature and move them to your desired layer.

Yes, I am referring to the text features. Basically I want to move all Text feature to layer 252 but I try the same way and NX don't recognize

Option Strict Off
Imports System
Imports NXOpen

Module Module9

Sub Main()

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim markId1 As NXOpen.Session.UndoMarkId
markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Start")
Dim lw As ListingWindow = theSession.ListingWindow

Const TextLayer As Integer = 253

Dim displayModification1 As DisplayModification
displayModification1 = theSession.DisplayManager.NewDisplayModification()

displayModification1.NewLayer = TextLayer

displayModification1.Apply(workPart.Texts.ToArray)

displayModification1.Dispose()

End Sub

End Module

Unfortunately, there is no ".Texts" collection in a part; we will need to look through the features, find the text features, and move the text entities to your desired layer.

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

Module MoveTextToLayer
Sub Main()
Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow
Dim Layno As Integer
Layno = 253
Dim textObjs As New List(Of DisplayableObject)

For Each temp As Features.Feature In theSession.Parts.Work.Features
If TypeOf (temp) Is Features.Text Then
Dim myText As Features.Text = temp
For Each textObj As DisplayableObject In myText.GetEntities
textObjs.Add(textObj)
Next
End If
Next
Dim displayModification1 As DisplayModification
displayModification1 = theSession.DisplayManager.NewDisplayModification()

displayModification1.NewLayer = Layno

displayModification1.Apply(textObjs.ToArray)

displayModification1.Dispose()

End Sub

End Module

Thank very much that works very well

Here's how you move solid and sheet bodies using SNAP functions. Doing the same thing with NX/Open functions is conceptually the same,

Public Class MyProgram

Public Shared Sub Main()

For Each body In Snap.Globals.WorkPart.Bodies

If body.IsSheetBody Then body.Layer = 60 ' Change 60 to whatever you want
If body.IsSolidBody Then body.Layer = 70 ' Change 70 to whatever you want

Next

End Sub

End Class

I want to learn to write journal file.
i need move datums, sketch, curves, solid models to specific layers
someone can help me with this ?
and someone can give me a training online ?
moca8708@gmail.com
regards

Hello and sorry, english is not my native language :)

i used some snips of your codes, combine with some recordings, to craft a Journal. I think its the frankenstein of all journals but it actually works :D
The Journal moves all Datumplanes and Axis from the koordinate system to Layer 61, all sketches to Layer 21 and all Text-features to Layer 22. After that the Journal switches all layers invisible and shows the isometric view of the Part. Thats a pretty nice function.

Now i need one more detail. some of my workmates use special layers, for example to moves a special needed sketch manually to an other layer as layer 21.
the problem is, that the journal would overvrite the manually choosed Layer back to layer 21. The journal would only have to consider objects that are on layer 1 and ignore objects wich are unequal 1.

Can somebody help me to create that funktion?

Best Regards!

2 strategies immediately come to mind:

1) If you are working with a collection of objects, such as workPart.Bodies, you can check the layer of each object before moving it to a new layer. The code might look something like below:

for each tempBody as body in workpart.bodies
if tempBody.Layer = 1 then
'code to move body to desired layer
end if
next

2) The .GetAllObjectsOnLayer method will return all the objects on a given layer. In this way, you could get access to all objects on layer 1 and then check the object type and move them to the desired layer. Note that this method will return objects that you may not be interested in. For example, if there is a solid body on the layer, it will return the body along with each face and edge individually. Only the body needs to be moved to a new layer, the faces and edges will follow. You will need to filter out the objects that you are not interested in.