Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Macro for changing objects on all layers to specified colors 1

Status
Not open for further replies.

Singsonite

Automotive
May 5, 2011
25
0
0
US
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.
 
Replies continue below

Recommended for you

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.
 
If you have nothing against a journal try this:

Code:
[COLOR=green]'September 13, 2012[/color]
[COLOR=green]'eng-tips thread561-329693[/color]
[COLOR=green]'[/color]
[COLOR=green]'Turn all layers on (make them selectable)[/color]
[COLOR=green]'change color of each object on a given layer to specified color[/color]
[COLOR=green]'change view to trimetric and perform "fit all"[/color]

[COLOR=blue]Option[/color] [COLOR=blue]Strict[/color] [COLOR=blue]Off[/color]  
[COLOR=blue]Imports[/color] System  
[COLOR=blue]Imports[/color] System.Collections.Generic  
[COLOR=blue]Imports[/color] NXOpen  

[COLOR=blue]Module[/color] Module1  

    [COLOR=blue]Sub[/color] Main()  

        [COLOR=blue]Dim[/color] theSession [COLOR=blue]As[/color] Session [COLOR=blue]=[/color] Session.GetSession()  
        [COLOR=blue]Dim[/color] workPart [COLOR=blue]As[/color] Part [COLOR=blue]=[/color] theSession.Parts.Work  

        [COLOR=blue]Dim[/color] markId1 [COLOR=blue]As[/color] Session.UndoMarkId  
        markId1 [COLOR=blue]=[/color] theSession.SetUndoMark(Session.MarkVisibility.Visible, "Layer Object Color Journal")  

        [COLOR=blue]Dim[/color] layerObjects() [COLOR=blue]As[/color] NXObject  
        [COLOR=blue]Dim[/color] layerObjectsDisplayable() [COLOR=blue]As[/color] DisplayableObject  
        [COLOR=blue]Dim[/color] layerColorID(256) [COLOR=blue]As[/color] [COLOR=blue]Integer[/color]  

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

        [COLOR=blue]For[/color] i [COLOR=blue]As[/color] [COLOR=blue]Integer[/color] [COLOR=blue]=[/color] 1 [COLOR=blue]To[/color] 256  

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

 [COLOR=green]'get all objects on layer[/color]
            layerObjects [COLOR=blue]=[/color] workPart.Layers.GetAllObjectsOnLayer(i)  
            [COLOR=blue]If[/color] layerObjects.Length > 0 [COLOR=blue]Then[/color]  

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

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

                layerObjectsDisplayable [COLOR=blue]=[/color] objects1.ToArray  
                displayModification1.Apply(layerObjectsDisplayable)  

                displayModification1.Dispose()  

            End [COLOR=blue]If[/color]  
        [COLOR=blue]Next[/color]  

 [COLOR=green]'fit view, trimetric[/color]
        workPart.ModelingViews.WorkView.Orient(View.Canned.Trimetric, View.ScaleAdjustment.Fit)  

    End [COLOR=blue]Sub[/color]  


    [COLOR=blue]Public[/color] [COLOR=blue]Function[/color] GetUnloadOption(ByVal dummy [COLOR=blue]As[/color] [COLOR=blue]String[/color]) [COLOR=blue]As[/color] [COLOR=blue]Integer[/color]  

 [COLOR=green]'Unloads the image when the NX session terminates[/color]
        GetUnloadOption [COLOR=blue]=[/color] NXOpen.Session.LibraryUnloadOption.AtTermination  

    End [COLOR=blue]Function[/color]  

End [COLOR=blue]Module[/color]

Or, as a downloadable file

www.nxjournaling.com
 
 http://files.engineering.com/getfile.aspx?folder=2ef8ca6f-17a0-4331-9816-02d32d2c31b6&file=Layer_objects_color.vb
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.
 
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.[s][COLOR=#EF2929]Trimetric[/color][/s][COLOR=#3465A4]Isometric[/color], View.ScaleAdjustment.Fit)

www.nxjournaling.com
 
Status
Not open for further replies.
Back
Top