Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Selecting all objects on a layer as part of a journal 1

Status
Not open for further replies.

kfraysur

Bioengineer
Feb 4, 2010
42
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.
 
Replies continue below

Recommended for you

Here's a quick journal that collects all the objects on layer 1 (can be changed in the journal).
Code:
Option Strict Off
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
 
Thanks for the reply. I'm not sure I was very clear in the op. I would like to be able to call a function or something that would allow for manipulation of all objects on a certain layer. In one example, I might want to make all the points in a point cloud imported from an iges file be black instead of the default green on layer 2, while on layer 3 they should all be turned red.

When I create a journal file to do this, there is a seperate line for each individual point, like this:
Code:
Dim point1 As Point = CType(workPart.Points.FindObject("HANDLE R-203321"), Point)

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.
 
Building on the previous example, here is one way to do it (not necessarily the best method).
Code:
Option Strict Off
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
 
Thanks for the help cowski.

I can also see how I can use this to change the colors of bodies as well by changing the lines:

Code:
Dim pointObject as [b]Point[/b]

allObjects = workPart.Layers.GetAllObjectsOnLayer(layerNumber)
lw.Open
for each someObject as NXObject in allObjects
'    lw.WriteLine(someObject.GetType.ToString)
    if someObject.GetType.ToString = "NXOpen.[b]Point[/b]" then

to

Code:
Dim pointObject as [b]Body[/b]

allObjects = workPart.Layers.GetAllObjectsOnLayer(layerNumber)
lw.Open
for each someObject as NXObject in allObjects
'    lw.WriteLine(someObject.GetType.ToString)
    if someObject.GetType.ToString = "NXOpen.[b]Body[/b]" 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.
 
The following code shows how to move/copy object to another layer and another way (probably more efficient) to change the display properties of objects.
Code:
Option Strict Off
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
[COLOR=green]'	lw.WriteLine(someObject.GetType.ToString)[/color]
	if someObject.GetType.ToString = "NXOpen.Point" then
		redim preserve myPoints(i)
		myPoints(i) = someObject
		i += 1
	end if
next

[COLOR=green]'move or copy object to another layer using the layer manager
'workPart.Layers.MoveDisplayableObjects(4, myPoints)[/color]
workPart.Layers.CopyObjects(5, myPoints)

With displayModification1
	.NewColor = 216
	[COLOR=green]'move layers with a display modification[/color]
	.NewLayer = 3
	[COLOR=green]'.NewTranslucency = 60		'only applies to faces; you can set this for points, but it will have no effect[/color]
	.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.
 
This has been extremely instructive cowski. Thank you very much for taking the time.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor