Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

Move each objects on specified layer to another layer

Status
Not open for further replies.

niedzviedz

Mechanical
Apr 1, 2012
307
0
0
PL
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.
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
 
Replies continue below

Recommended for you

> But I get some errors. Any one can help me to solve my problem?

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.
 
Hello,

I posted whole code, so anyone can try it by yourself.
The main problem is here:
Code:
 If temp.Layer <> layernumber Then
I get error - Element "layer" is not member of "Nxopen.features.feature"
and also here
Code:
	myPart.Layers.MoveDisplayableObjects(temp)
element temp is not declared. (...)






With best regards
Michael
 
Here is a journal that basically does what you need. Your code tried to use features to move. Now features are not displayable objects and therefore features do not have layer property. What you basically need to do is cycle through the component parts, set it as the work part and then collect what you want to move to new layers. I have just taken solid bodies.

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 Module

Regards

Frank Swinkels
 
Thanks Frank,

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
 
niedzviedz said:
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.

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
 
Hello Cowski,

Thanks for Your replay.
I made some research on what have write. Below is my code, but I get errors.
Code:
Failed: NXOpen.NXException: This operation can only be done on the display part
   in NXOpen.Layer.LayerManager.GetAllObjectsOnLayer(Int32 layer)
   in MoveComponents.reportComponentChildren2(Component comp, Int32 indent) in C:\Users\m\AppData\Local\Temp\NXJournals2676\journal.vb
:line 106
   in MoveComponents.Main() w C:\Users\m\AppData\Local\Temp\NXJournals2676\journal.vb:line 38

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 Sub

Any suggestion, what I done wrong? when I change mypart to workpart I get error:

Code:
Failed: NXOpen.NXException: Cannot move an edge or face to a layer
   in NXOpen.Layer.LayerManager.MoveDisplayableObjects(Int32 newLayer, DisplayableObject[] objectArray)
   in MoveComponents.reportComponentChildren2(Component comp, Int32 indent) in C:\Users\m\AppData\Local\Temp\NXJournals2676\journal.vb
:line 115
   in MoveComponents.Main() w C:\Users\m\AppData\Local\Temp\NXJournals2676\journal.vb:line 38

With best regards
Michael
 
The .GetAllObjectsOnLayer method only works on the displayed part. If you are processing components, you will need to either make the component part the displayed part or use a different strategy, like what FrankSwinks posted.

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
 
Status
Not open for further replies.
Back
Top