Move each objects on specified layer to another layer
Move each objects on specified layer to another layer
(OP)
Hello everyone,
I have imported spep file into NX, and now I have each body of each parts in assembly on layer 10. I don't wanna manually place it on layer 1 because there are more than 200 parts in this assembly. So I started to write some code.
But I get some errors. Any one can help me to solve my problem?
I have imported spep file into NX, and now I have each body of each parts in assembly on layer 10. I don't wanna manually place it on layer 1 because there are more than 200 parts in this assembly. So I started to write some code.
CODE
Option Strict Off
Imports System
Imports System.Collections
Imports NXOpen
Imports NXOpen.Utilities
Imports NXOpen.Assemblies
Imports NXOpen.UF
Imports NXOpenUI
Module MoveComponents
'Dim s As Session = Session.GetSession()
Dim theSession As Session = Session.GetSession()
'Dim ufs As UFSession = UFSession.GetUFSession()
Public ufs As UFSession = UFSession.GetUFSession()
Dim lw As ListingWindow = theSession.ListingWindow
Dim blnCancel As Boolean = False
Dim answer as String
dim layerNumber as Integer = 10
dim newlayer as Integer = 1
Dim allObjects as NXObject()
Sub Main()
Dim workPart As Part = theSession.Parts.Work
Dim dispPart As Part = theSession.Parts.Display
Dim HRC1 as string
Dim stock as string
lw.Open
Try
Dim c As ComponentAssembly = dispPart.ComponentAssembly
if not IsNothing(c.RootComponent) then
ReportComponentChildren(c.RootComponent, 0)
end if
Catch e As Exception
theSession.ListingWindow.WriteLine("Failed: " & e.ToString)
End Try
lw.Close
End Sub
'**********************************************************
Sub reportComponentChildren( ByVal comp As Component, ByVal indent As Integer)
Dim workPart As Part = theSession.Parts.Work
Dim dispPart As Part = theSession.Parts.Display
For Each child As Component In comp.GetChildren()
Dim MyPart As Part = child.Prototype.OwningPart
If LoadComponent(child) Then
Dim displayModification1 As DisplayModification
displayModification1 = theSession.DisplayManager.NewDisplayModification()
For Each temp As Features.Feature In myPart.Features
If temp.Layer <> layernumber Then
Exit For
End If
temp.Layer = newLayer
next
myPart.Layers.MoveDisplayableObjects(temp)
With displayModification1
.NewLayer = 1
.Apply(allobjects)
.Dispose
End With
Else
'component could not be loaded
End If
reportComponentChildren(child, indent+1)
Next
End Sub
Private Function LoadComponent(ByVal theComponent As Component) As Boolean
Dim thePart As Part = theComponent.Prototype.OwningPart
Dim partName As String = ""
Dim refsetName As String = ""
Dim instanceName As String = ""
Dim origin(2) As Double
Dim csysMatrix(8) As Double
Dim transform(3, 3) As Double
Try
If thePart.IsFullyLoaded Then
'component is fully loaded
Else
'component is partially loaded
End If
Return True
Catch ex As NullReferenceException
'component is not loaded
Try
ufs.Assem.AskComponentData(theComponent.Tag, partName, refsetName, instanceName, origin,
csysMatrix, transform)
Dim theLoadStatus As PartLoadStatus
theSession.Parts.Open(partName, theLoadStatus)
If theLoadStatus.NumberUnloadedParts > 0 Then
Dim allReadOnly As Boolean = True
For i As Integer = 0 To theLoadStatus.NumberUnloadedParts - 1
If theLoadStatus.GetStatus(i) = 641058 Then
'read-only warning, file loaded ok
Else
'641044: file not found
lw.WriteLine("file not found")
allReadOnly = False
End If
Next
If allReadOnly Then
Return True
Else
'warnings other than read-only...
Return False
End If
Else
Return True
End If
Catch ex2 As NXException
lw.WriteLine("error: " & ex2.Message)
Return False
End Try
Catch ex As NXException
'unexpected error
lw.WriteLine("error: " & ex.Message)
Return False
End Try
End Function
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 But I get some errors. Any one can help me to solve my problem?
With best regards
Michael





RE: Move each objects on specified layer to another layer
I expect there is something wrong with your program.
If you can be more specific about what error occurred, and when/where, then maybe some more specific help could be offered.
RE: Move each objects on specified layer to another layer
I posted whole code, so anyone can try it by yourself.
The main problem is here:
CODE
and also here
CODE
With best regards
Michael
RE: Move each objects on specified layer to another layer
Hope this helps.
CODE -->
Option Strict Off Imports System Imports System.Collections Imports NXOpen Imports NXOpen.Utilities Imports NXOpen.Assemblies Imports NXOpen.UF Imports NXOpenUI Module MoveComponentBodies Dim theSession As Session = Session.GetSession() Dim ufs As UFSession = UFSession.GetUFSession() Dim lw As ListingWindow = theSession.ListingWindow Dim blnCancel As Boolean = False Dim answer As String Dim layerNumber As Integer = 10 Dim newlayer As Integer = 1 Dim allObjects As NXObject() Sub Main() Dim workPart As Part = theSession.Parts.Work Dim dispPart As Part = theSession.Parts.Display Try Dim c As ComponentAssembly = dispPart.ComponentAssembly If Not IsNothing(c.RootComponent) Then reportComponentChildren(c.RootComponent, 0) End If Catch e As Exception theSession.ListingWindow.WriteLine("Failed: " & e.ToString) End Try theSession.Parts.SetWork(dispPart) End Sub '********************************************************** Sub reportComponentChildren(ByVal comp As Component, ByVal indent As Integer) For Each child As Component In comp.GetChildren() Dim MyPart As Part = child.Prototype.OwningPart ' set each part as work part in turn theSession.Parts.SetWork(MyPart) ' get all the solid bodies in this part and move them to layer 1 Dim bodies As BodyCollection = MyPart.Bodies For Each partBody As Body In bodies If partBody.IsSolidBody Then ' you can do other definition such as sheet body partBody.Layer = newlayer End If Next reportComponentChildren(child, indent + 1) Next 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 ModuleRegards
Frank Swinkels
RE: Move each objects on specified layer to another layer
Sometimes when You import stp files, nx can't saw it automatically and You get sheets bodies. So I tried features, because I want to select all objects (solid body, sheet body, curves etc.) on specific layer.
It looks like I have to list all object I want.
With best regards
Michael
RE: Move each objects on specified layer to another layer
Or, you can use the .GetAllObjectsOnLayer() method to get an array of DisplayableObjects on the specified layer. The entire array can then be passed to the DisplayModification object in one shot, no need to loop through the array and change each one.
www.nxjournaling.com
RE: Move each objects on specified layer to another layer
This is an issue with the variable scope. The variable temp was declared in a For loop, therefore it is only valid within the loop. When the loop ends, the variable goes out of scope and can no longer be referenced.
www.nxjournaling.com
RE: Move each objects on specified layer to another layer
Thanks for Your replay.
I made some research on what have write. Below is my code, but I get errors.
CODE
CODE
Sub reportComponentChildren2( ByVal comp As Component, ByVal indent As Integer) Dim workPart As Part = theSession.Parts.Work Dim dispPart As Part = theSession.Parts.Display For Each child As Component In comp.GetChildren() Dim MyPart As Part = child.Prototype.OwningPart If LoadComponent(child) Then allObjects = myPart.Layers.GetAllObjectsOnLayer(layerNumber) for each someObject as NXObject in allObjects redim preserve myObjects(i) myobjects(i) = someObject i += 1 next myPart.Layers.MoveDisplayableObjects(1, myobjects) Dim displayModification1 As DisplayModification displayModification1 = theSession.DisplayManager.NewDisplayModification() displayModification1.NewLayer = newlayer displayModification1.Apply(myobjects) displayModification1.Dispose Else 'component could not be loaded End If reportComponentChildren(child, indent+1) Next End SubAny suggestion, what I done wrong? when I change mypart to workpart I get error:
CODE
With best regards
Michael
RE: Move each objects on specified layer to another layer
Faces and edges cannot be moved to a layer independent of the body that owns them. Remove the faces and edges from your selection before attempting to move the objects to a new layer.
www.nxjournaling.com