Changing Line Colors for Individual Parts in Drawing Views With VBA

Changing Line Colors for Individual Parts in Drawing Views With VBA

demuff
Enthusiast Enthusiast
4,308 Views
15 Replies
Message 1 of 16

Changing Line Colors for Individual Parts in Drawing Views With VBA

demuff
Enthusiast
Enthusiast

Wondering if anyone has found a way to access the properties pane of a particular part in a drawing view, like in the following pictures:

context menu.pngproperties dialog.PNG

End goal is to be able to be able to automatically apply a color to every instance of a part inside of large assembly drawings. I can get it to work by getting a subset of the view curves using the DrawingCurves() property, but it's very slow, even when working with few parts/edges. By contrast, the properties menu for the component allows the color to be set and Inventor handles it very quickly, so I would love to tap into that instead. I could do it with a different layer for each color, but that's not what I want, and that's not how Inventor handles the color change via the dialog anyway.

 

Any thoughts?

0 Likes
Accepted solutions (1)
4,309 Views
15 Replies
Replies (15)
Message 2 of 16

JamieVJohnson2
Collaborator
Collaborator

I have accomplished something very similar, you should be able to adapt it to your needs.  We wanted to set the lines to a particular layer.  This program will perform that based on a known component occurrence name the user enters.  Effectively it does the same thing a user can do by clicking on a component in the drawing view model tree and selected 'select as edges' then applying properties to those selected edges.

 

Public Sub SetComponentsReference()
        Dim strComponent As String = InputBox("Insert Component Name")
        Dim strLayer As String = InputBox("Enter in layer to use:", "Change Component Layer", "Reference (ANSI)")
        Dim layer As Inventor.Layer = Nothing
        If Not String.IsNullOrEmpty(strComponent) Then
            GetInventorApplication()
            If invApp IsNot Nothing Then
                If invApp.ActiveDocumentType = DocumentTypeEnum.kDrawingDocumentObject Then
                    Dim invdoc As Inventor.DrawingDocument = invApp.ActiveDocument
                    Dim trans As Inventor.Transaction = invApp.TransactionManager.StartTransaction(invdoc, "Change Componet Layer")
                    For Each mlayer As Inventor.Layer In invdoc.StylesManager.Layers
                        If mlayer.Name = "Reference (ANSI)" Then
                            layer = mlayer
                            Exit For
                        End If
                    Next
                    If layer IsNot Nothing Then
                        For Each mSheet As Inventor.Sheet In invdoc.Sheets
                            MsgBox(mSheet.Name & " Start")
                            Dim ioc As Inventor.ObjectCollection = invApp.TransientObjects.CreateObjectCollection
                            For Each drView As Inventor.DrawingView In mSheet.DrawingViews
                                Dim vdoc As Inventor.Document = drView.ReferencedDocumentDescriptor.ReferencedDocument
                                If vdoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
                                    Dim aDoc As Inventor.AssemblyDocument = vdoc
                                    GetComponentSegments(strComponent, drView, aDoc.ComponentDefinition.Occurrences, ioc)
                                End If
                            Next
                            mSheet.ChangeLayer(ioc, layer)
                            mSheet.Update()
                            MsgBox(mSheet.Name & " Updated")
                        Next
                    End If
                    trans.End()
                End If
            End If
        End If
    End Sub

    Public Sub GetComponentSegments(ByRef strComponent As String, ByRef drView As Inventor.DrawingView, ByRef vComps As ComponentOccurrences, ByRef ioc As Inventor.ObjectCollection)
        For Each vComp As ComponentOccurrence In vComps
            If vComp.Name = strComponent Then
                Dim drawCurves As DrawingCurvesEnumerator = drView.DrawingCurves(vComp)
                For Each curve As Inventor.DrawingCurve In drawCurves
                    For Each segment As DrawingCurveSegment In curve.Segments
                        ioc.Add(segment)
                    Next
                Next
            Else
                If vComp.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
                    Dim subOcc As ComponentOccurrences = vComp.SubOccurrences
                    GetComponentSegments(strComponent, drView, subOcc, ioc)
                End If
            End If
        Next
    End Sub

n-Joy!

 

 

Jamie Johnson : Owner / Sisu Lissom, LLC https://sisulissom.com/
0 Likes
Message 3 of 16

demuff
Enthusiast
Enthusiast

Hi jvj,

Thanks for offering this solution, but it doesn't help much due to the layer change being executed all at once using the .ChangeLayer() method. The remainder of your code is essentially what I've already been doing with .DrawingCurves(), so my original problem still exists. Is there any way to apply color at the part level in the drawing view rather than by select edges?

0 Likes
Message 4 of 16

JamieVJohnson2
Collaborator
Collaborator
Accepted solution

Other than select as edges...  I'm afraid in the background that is what is actually taking place.  But once you have the drawing curve segments of that component, you can repeat the feature like this:

 Dim drView As Inventor.DrawingView

Dim oColor As Inventor.Color = invApp.TransientObjects.CreateColor(100, 200, 150)

'apply color while looping through the segments and curves.
            drView.DrawingCurves(1).Item(1).Color=oColor

Jamie Johnson : Owner / Sisu Lissom, LLC https://sisulissom.com/
0 Likes
Message 5 of 16

-CraigZ-
Enthusiast
Enthusiast

@JamieVJohnson2 I'm a bit new on this stuff. I think I'm looking for the exact same thing (I want to make all instances of a sub-assembly inside an IDW a different color). Am I able to make this an external rule in iLogic? Or does this have to be implemented in another way? I tried copying it into an external rule and it didn't like that.

 

Thanks,

0 Likes
Message 6 of 16

demuff
Enthusiast
Enthusiast

I guess that's the end of it then if there's no other way. I can work with what I have, though I've still got the long-standing issue of automating the hatch color in sectioned views...

 

@-CraigZ- Surely you could do this with iLogic, but all of my work is with VBA for the added flexibility.

0 Likes
Message 7 of 16

-CraigZ-
Enthusiast
Enthusiast

@demuff @JamieVJohnson2

 

Can you give me a bit more instruction on that way? From the time I open VBA editor, to the time I run it? (Treat it as if I've never used this editor before, because I haven't. I have experience in iLogic, and a bit in Visual Studio, but none in Inventor VBA editor)

 

I tried creating a new project and copying that code in there. When I try and run it, I get a syntax error on the InputBox line (line 2?)

 

Even just a screencast as instruction would be super helpful if you'd rather show me how to set it up than write it out.

 

Thanks,

 

0 Likes
Message 8 of 16

demuff
Enthusiast
Enthusiast

The syntax for iLogic and VBA are slightly different, though both are based on Visual Basic. The primary issue with using jvj's code in VBA is that you can't use dim to declare a variable and then assign a value to it in a single line. For example:

Dim strComponent As String = InputBox("Insert Component Name")

needs to be changed to this in VBA:

Dim strComponent As String
strComponent = InputBox("Insert Component Name")

In general, VBA is split into Forms, Modules, and Class Modules. For most stuff, you'll write the bulk of the code in a module and supplement it with a userform if a gui is needed. Much of the syntax is identical to iLogic, so you should be able to see how things work if you've worked with iLogic.

 

Here's a piece of what I've written to change the color of all parts contained in assemblies or presentations in the active document. It's pretty rough but it gets the job done. Paste it into a module and you should be good to go.

 

Sub ChangePartColor()
    Dim oDoc As DrawingDocument, partStr As String
    If ThisApplication.ActiveDocumentType <> kDrawingDocumentObject Then
        Call MsgBox("This macro only works on drawing documents.")
        Exit Sub
    End If
    Set oDoc = ThisApplication.ActiveDocument
    partStr = InputBox("Part to color:", "Prompt")
    Dim oTrans As Transaction, i, j, k, c
    Dim ViewCurves As DrawingCurvesEnumerator, refAssyDef As ComponentDefinition, oColor As color
    
    Set oColor = ThisApplication.TransientObjects.CreateColor(255, 0, 0) 'RGB
    
    Set oTrans = ThisApplication.TransactionManager.StartTransaction(ThisApplication.ActiveDocument, "Colorize [PART]")
        For Each i In oDoc.Sheets
        For Each j In i.DrawingViews
            If j.ReferencedDocumentDescriptor.ReferencedDocumentType = kPresentationDocumentObject Then
                Set refAssyDef = j.ReferencedDocumentDescriptor.ReferencedDocument.ReferencedDocuments(1).ComponentDefinition
            ElseIf j.ReferencedFile.DocumentType = kAssemblyDocumentObject Then
                Set refAssyDef = j.ReferencedFile.DocumentDescriptor.ReferencedDocument.ComponentDefinition
            End If

            For Each k In refAssyDef.Occurrences
                If k.name Like partStr & ":*" Then
                    Set ViewCurves = j.DrawingCurves(k)
                    For Each c In ViewCurves
                        c.color = oColor
                    Next
                End If
            Next
        Next
    Next
    oTrans.End
    
End Sub
Message 9 of 16

-CraigZ-
Enthusiast
Enthusiast

@demuff Wonderful that worked perfectly! Thank you so much. I have a basic understanding of all of these languages, but I definitely wouldn't have caught something as small as that.

 

Much appreciated!!

0 Likes
Message 10 of 16

dusan.naus.trz
Advisor
Advisor

Hi @demuff 

This code does not work for iLogic
Can you please edit it for iLogic?

0 Likes
Message 11 of 16

dusan.naus.trz
Advisor
Advisor
0 Likes
Message 12 of 16

Luisfmts
Enthusiast
Enthusiast
Simple and neat function....and the loops with i, j, k, c...great for the eyes! 🙂
0 Likes
Message 13 of 16

_dscholtes_
Advocate
Advocate

@Luisfmts wrote:
great for the eyes! 🙂

Did you see the declaration of them without a datatype? That hurts my eyes. Now they're all variants, hurgl 😬

Message 14 of 16

Luisfmts
Enthusiast
Enthusiast
Well, the declaration can be easily switched to "Dim i as Sheet...j as DrawingView..."...
i think what was nice in the code was how it was composed, ease to read, oldschool nesting...felt like filling a matrix in C 😄
0 Likes
Message 15 of 16

bert.maesBK6NX
Explorer
Explorer

Hello,

I noticed that the autopartcolor only works for sub-assemblies or parts directly under the topassembly, not for parts hidden in these sub-assemblies. Is this still possible with a small adjustment to the code or not?

 

Kind regards

0 Likes
Message 16 of 16

kfc10
Participant
Participant
0 Likes