×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Macro for changing objects on all layers to specified colors

Macro for changing objects on all layers to specified colors

Macro for changing objects on all layers to specified colors

(OP)
NX 6.0.3.6
Windows 7 Pro

Hello all,

I am trying to make a macro that will perform the following function:

<Start from a file that objects on some or all layers with all layers turned off>
1. Turn all layers on.
2. Set TFR-ISO view (fitting all to screen.
3. Edit Object Display.
4. Select all objects on one layer (ex. 21).
5. Change Color to specific layer number.
6. Apply change.
7. Select New Objects.
8. Loop back to #4 until objects on all 256 layers have been assigned a predetermined color.
9. Close dialog and end macro.

Additional criteria:
- Must not fault if no objects exist on specified layer.

I uploaded a shortened attempt at this concept that goes for layers 21 through 25 changing all objects to color #36 for each layer. One issue with the macro is that it doesn't change any layer color accept the last on the list (not sure why). Another issue being that it faults when no objects are present.

I would appreciate any assistance you can lend. If I'm going about this all the wrong way, feel free to let me know.

Thank you,

John B. Conger
Tool Design
Automotive Interiors
Advanced Engineering Solutions Inc.
http://www.advancedinternational.com/

RE: Macro for changing objects on all layers to specified colors

I think you discovered that the "no objects" thing is an issue - I am going to dwell on that for a while to see if I can come up with an acceptable solution to that.

RE: Macro for changing objects on all layers to specified colors

Does it have to be a macro? Would a journal be acceptable?

www.nxjournaling.com

RE: Macro for changing objects on all layers to specified colors

If you have nothing against a journal try this:

CODE

'September 13, 2012
'eng-tips thread561-329693: Macro for changing objects on all layers to specified colors: Macro for changing objects on all layers to specified colors
'
'Turn all layers on (make them selectable)
'change color of each object on a given layer to specified color
'change view to trimetric and perform "fit all"

Option Strict Off  
Imports System  
Imports System.Collections.Generic  
Imports NXOpen  

Module Module1  

    Sub Main()  

        Dim theSession As Session = Session.GetSession()  
        Dim workPart As Part = theSession.Parts.Work  

        Dim markId1 As Session.UndoMarkId  
        markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Layer Object Color Journal")  

        Dim layerObjects() As NXObject  
        Dim layerObjectsDisplayable() As DisplayableObject  
        Dim layerColorID(256) As Integer  

 'store color ID's for layers in array
        layerColorID(1) = 181  
        layerColorID(2) = 186  
        layerColorID(3) = 78  
        layerColorID(4) = 6  
        layerColorID(5) = 11  
        layerColorID(6) = 36  
        layerColorID(7) = 108  
        layerColorID(8) = 31  
        layerColorID(9) = 103  
        layerColorID(10) = 211  
        layerColorID(11) = 164  
        layerColorID(12) = 125  
 '...
 'expand this out as above and add other layer colors as necessary
        For i As Integer = 13 To 255  
            layerColorID(i) = i  
        Next  
 '...
        layerColorID(256) = 94  

        For i As Integer = 1 To 256  

 'make layer selectable
            Dim stateArray1(0) As Layer.StateInfo  
            If workPart.Layers.WorkLayer = i Then  
                stateArray1(0) = New Layer.StateInfo(i, Layer.State.WorkLayer)  
            Else  
                stateArray1(0) = New Layer.StateInfo(i, Layer.State.Selectable)  
            End If  
            workPart.Layers.ChangeStates(stateArray1, False)  

 'get all objects on layer
            layerObjects = workPart.Layers.GetAllObjectsOnLayer(i)  
            If layerObjects.Length > 0 Then  

 'change color of all objects on layer
                Dim displayModification1 As DisplayModification  
                displayModification1 = theSession.DisplayManager.NewDisplayModification()  
                displayModification1.ApplyToAllFaces = True  
                displayModification1.ApplyToOwningParts = False  
                displayModification1.NewColor = layerColorID(i)  

                Dim objects1 As New List(Of DisplayableObject)  
                For Each obj As NXObject In layerObjects  
                    obj = TryCast(obj, DisplayableObject)  
                    If obj IsNot Nothing Then  
                        objects1.Add(obj)  
                    End If  
                Next  

                layerObjectsDisplayable = objects1.ToArray  
                displayModification1.Apply(layerObjectsDisplayable)  

                displayModification1.Dispose()  

            End If  
        Next  

 'fit view, trimetric
        workPart.ModelingViews.WorkView.Orient(View.Canned.Trimetric, View.ScaleAdjustment.Fit)  

    End Sub  


    Public Function GetUnloadOption(ByVal dummy As String) As Integer  

 'Unloads the image when the NX session terminates
        GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination  

    End Function  

End Module 

Or, as a downloadable file

www.nxjournaling.com

RE: Macro for changing objects on all layers to specified colors

(OP)
Excellent! I have no problems going with a Journal file. I'll try this out and get back with you all. Thank you again for looking into it.

Best Regards,

John B. Conger
Tool Design
Automotive Interiors
Advanced Engineering Solutions Inc.
http://www.advancedinternational.com/

RE: Macro for changing objects on all layers to specified colors

(OP)
cowski

The Journal Worked! I applied our color standards to each layer and ran the journal file. The first attempt didn't work. I was getting an error on the line shown below:

displayModification1.ApplyToOwningParts = False

ApplyToOwningParts was not a valid function of the library. I commented the line and ran it again. All worked well. I do have a question though. What was the purpose of that line?

I linked the finished file for you to look over and see what the result was. Please review and let me know what you think.

Thanks again,

John B. Conger
Tool Design
Automotive Interiors
Advanced Engineering Solutions Inc.
http://www.advancedinternational.com/

RE: Macro for changing objects on all layers to specified colors

Glad to hear you got it working.

For the displayModification part, I recorded a journal and did a copy/paste job into my code. The "ApplyToOwningParts" bit was added in NX 7.5, since you are running NX 6 that is why you get the error. Commenting it out should have no affect on what you are doing, I believe it is more useful when working in the context of an assembly.

I also just noticed that I specified a trimetric view when you asked for an isometric view. You can change this line of code to get what you want.

CODE

workPart.ModelingViews.WorkView.Orient(View.Canned.TrimetricIsometric, View.ScaleAdjustment.Fit) 

www.nxjournaling.com

RE: Macro for changing objects on all layers to specified colors

(OP)
Perfect! Thank you for all your assistance.

Best Regards,

John B. Conger
Tool Design
Automotive Interiors
Advanced Engineering Solutions Inc.
http://www.advancedinternational.com/

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources