Selecting all objects on a layer as part of a journal
Selecting all objects on a layer as part of a journal
(OP)
I am looking to automate the process of changing the color of a point cloud (imported from an iges file) located on a specific layer. When I step through a journal of this processes, each point is individually selected with its coordinates displayed, which is a problem because every file will have different point cloud data.
I'm sure there is some function that I can include in a journal file to select all objects on a layer, but I have not been able to find this. Any help would be appreciated.
I'm sure there is some function that I can include in a journal file to select all objects on a layer, but I have not been able to find this. Any help would be appreciated.





RE: Selecting all objects on a layer as part of a journal
CODE
Imports System
Imports NXOpen
Module NXJournal
Sub Main
Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim displayPart As Part = theSession.Parts.Display
Dim lw As ListingWindow = theSession.ListingWindow
Dim allObjects as NXObject()
allObjects = workPart.Layers.GetAllObjectsOnLayer(1)
lw.Open
for each someObject as NXObject in allObjects
lw.WriteLine(someObject.GetType.ToString)
next
lw.Close
End Sub
End Module
RE: Selecting all objects on a layer as part of a journal
When I create a journal file to do this, there is a seperate line for each individual point, like this:
CODE
objects1(0) = point1
Dim point2 As Point = CType(workPart.Points.FindObject("HANDLE R-203300"), Point)
objects1(1) = point2
Dim point3 As Point = CType(workPart.Points.FindObject("HANDLE R-203136"), Point)
objects1(2) = point3
Dim point4 As Point = CType(workPart.Points.FindObject("HANDLE R-203439"), Point)
objects1(3) = point4
Dim point5 As Point = CType(workPart.Points.FindObject("HANDLE R-203296"), Point)
objects1(4) = point5
This code won't be very repeatable when I have a new file with a new point cloud that will have a different number of point coordinates. It seems like there would be some function that would allow the selection of everything on a layer so that you could modify it all at once (changing the color, moving the objects to a different layer, etc.)
We are currently using NX5, but will be transitioning to NX8 within a few weeks, so all the automation tasks I am working on have been in NX8.
RE: Selecting all objects on a layer as part of a journal
CODE
Imports System
Imports NXOpen
Imports NXOpenUI
Module NXJournal
Sub Main
Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim displayPart As Part = theSession.Parts.Display
Dim answer as String
dim layerNumber as Integer = 2
dim i as Integer = 0
Dim lw As ListingWindow = theSession.ListingWindow
Dim allObjects as NXObject()
Dim pointObject as Point
allObjects = workPart.Layers.GetAllObjectsOnLayer(layerNumber)
lw.Open
for each someObject as NXObject in allObjects
' lw.WriteLine(someObject.GetType.ToString)
if someObject.GetType.ToString = "NXOpen.Point" then
pointObject = someObject
pointObject.Color = 216
pointObject.RedisplayObject
i += 1
end if
next
lw.WriteLine(i & " points processed on layer: " & layerNumber)
lw.Close
End Sub
End Module
RE: Selecting all objects on a layer as part of a journal
I can also see how I can use this to change the colors of bodies as well by changing the lines:
CODE
allObjects = workPart.Layers.GetAllObjectsOnLayer(layerNumber)
lw.Open
for each someObject as NXObject in allObjects
' lw.WriteLine(someObject.GetType.ToString)
if someObject.GetType.ToString = "NXOpen.Point" then
to
CODE
allObjects = workPart.Layers.GetAllObjectsOnLayer(layerNumber)
lw.Open
for each someObject as NXObject in allObjects
' lw.WriteLine(someObject.GetType.ToString)
if someObject.GetType.ToString = "NXOpen.Body" then
I had two more questions:
1. How would I use this method to also make a body translucent? I hoped that "pointObject.Translucency = 60" would work, but no such luck. How do you know these commands anyway? Just experience? Is there a reference somewhere?
2. How would I move or copy objects from one layer to another once selected?
I really appreciate the help.
RE: Selecting all objects on a layer as part of a journal
CODE
Imports System
Imports NXOpen
Imports NXOpenUI
Module NXJournal
Sub Main
Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim displayPart As Part = theSession.Parts.Display
Dim answer as String
dim layerNumber as Integer = 2
dim i as Integer = 0
Dim lw As ListingWindow = theSession.ListingWindow
Dim allObjects as NXObject()
Dim pointObject as Point
Dim myPoints as DisplayableObject()
Dim displayModification1 As DisplayModification
displayModification1 = theSession.DisplayManager.NewDisplayModification()
allObjects = workPart.Layers.GetAllObjectsOnLayer(layerNumber)
lw.Open
for each someObject as NXObject in allObjects
' lw.WriteLine(someObject.GetType.ToString)
if someObject.GetType.ToString = "NXOpen.Point" then
redim preserve myPoints(i)
myPoints(i) = someObject
i += 1
end if
next
'move or copy object to another layer using the layer manager
'workPart.Layers.MoveDisplayableObjects(4, myPoints)
workPart.Layers.CopyObjects(5, myPoints)
With displayModification1
.NewColor = 216
'move layers with a display modification
.NewLayer = 3
'.NewTranslucency = 60 'only applies to faces; you can set this for points, but it will have no effect
.Apply(myPoints)
.Dispose
End With
lw.WriteLine(myPoints.Length & " points processed on layer: " & layerNumber)
lw.WriteLine("points copied to layer 5 (original color)")
lw.WriteLine("points moved to layer 3 and color changed to black")
lw.Close
End Sub
End Module
How do I know about this stuff? I have some past experience with Visual basic (VB 5&6, VBA in MSOffice - mostly Excel) which helps with the programming/debugging aspect. For the NX specific stuff, I mostly record journals and look at the output code; or look at code that other people have written (some sample .NET code is installed along with NX in <NX install directory path>\UGOPEN\SampleNXOpenApplications\.NET). When I see something new that looks interesting, I'll look it up in the .NET reference guide (NX Help -> Automation -> NX Open -> Open for .NET -> NX Open for .NET reference guide). Also in Automation -> NX Open is the NX Open Programmer's Guide which gives some background and theory.
RE: Selecting all objects on a layer as part of a journal