Cancel
Showing results for 
Search instead for 
Did you mean: 

move objects to a layer

Solution Partner Pioneer Solution Partner Pioneer
Solution Partner Pioneer

Hi All,

 

I´m trying to write a journal that moves all selected objects to a specific layer.

First a user should select the objects and then click on a button in the menu.

The button on the menu starts the journal.

 

Below you can find how far i´ve gotten but i´m unable to get it working.
All advise is welcome!

 

Module Move_To_Layer_66
    Sub Main()

        Dim theSession As Session = Session.GetSession()
        Dim theUI As UI = UI.GetUI
        Dim workPart As Part = theSession.Parts.Work
        Dim displayPart As Part = theSession.Parts.Display

        Dim index As Integer = theUI.SelectionManager.GetNumSelectedObjects()

        Dim SelectedObject As TaggedObject

        Dim LayerObjects() As DisplayableObject = Nothing

        For i As Integer = 0 To index - 1
            SelectedObject = theUI.SelectionManager.GetSelectedTaggedObject(i)
            LayerObjects(i) = CType(SelectedObject, DisplayableObject)
        Next

        workPart.Layers.MoveDisplayableObjects(66, LayerObjects)

    End Sub
End Module
Sven Voeten
Pre-sales NX CAM / Manufacturing Consultant
cards PLM Solutions
2 REPLIES 2

Re: move objects to a layer

Gears Esteemed Contributor Gears Esteemed Contributor
Gears Esteemed Contributor

Here's a quick edit to get something (somewhat) working:

Option Strict Off
Imports System

Imports NXOpen
Imports NXOpen.UF

Module Move_To_Layer_66

    Sub Main()

        Dim theSession As Session = Session.GetSession()
        Dim theUI As UI = ui.GetUI
        Dim workPart As Part = theSession.Parts.Work
        Dim displayPart As Part = theSession.Parts.Display
        Dim lw As ListingWindow = theSession.ListingWindow
        lw.Open()

        Dim index As Integer = theUI.SelectionManager.GetNumSelectedObjects()

        Dim SelectedObject As TaggedObject

        For i As Integer = 0 To index - 1
            SelectedObject = theUI.SelectionManager.GetSelectedTaggedObject(i)
            'lw.WriteLine("selected object type: " & SelectedObject.GetType.ToString)
            If TypeOf (SelectedObject) Is DisplayableObject Then
                Try
                    workPart.Layers.MoveDisplayableObjects(66, {SelectedObject})
                Catch ex As NXException
                    'object type could not be moved to a new layer (e.g. features, faces, edges, etc) 'lw.WriteLine("error: " & ex.Message) End Try End If Next End Sub End Module

The main issue in your code is that the LayerObjects array is set to nothing and it wasn't ReDim'd before attempting to add objects to it. An easier alternative would have been to use a List object. You can add and remove objects from a list and the list object will resize itself as necessary. The list object's .ToArray method can be used to effectively convert the list to an array for those NXOpen API commands that expect array input.

 

The code above attempts to move each object individually. This eliminates the need for array management or a list object; but more importantly, it ignores objects that cannot be moved to a new layer. Objects such as features can be selected in the graphics window, but they cannot be moved to a new layer (at least not with the current strategy). Even some displayable objects have limitations; for instance, a face or edge can be selected in the graphics window but it cannot be moved to a layer independently of the body that owns it.

Re: move objects to a layer

Legend
Legend

Hi Sven,

 

You don't need to make a list of displayableobjects. Just change each object directly in the loop

Dim obj1 As DisplayableObject=CType(SelectedObject, DisplayableObject)
obj1.Layer = 5

 

Br

Fred

  

Evolve Consulting bvba

Freelance NX & Teamcenter consultant

 

 

 

Frederik Verscheure
Evolve Consulting bvba
Freelance NX & Teamcenter consultant
frederik@evolve-consulting.be