I had a couple of requests lately asking about the possibility of using the API to change the colors of the curves in a drawing view to match the color of the parts in the assembly. Below is a program that does this. It starts with a selected drawing view and gets the associated assembly. It then iterates over every occurrence in the assembly and for each part occurrence gets the color of that part as it exists in the assembly. If the color has been overridden in the assembly then it will use the override color. It then checks to see if there is a layer in the drawing that has the same name as the color. If there isn’t it creates a new layer with the same color. It then gets all of the curves in the drawing that are associated with that occurrence and assigns the layer to those curves. The picture below shows the result after running it on the engine sample that’s delivered with Inventor.
Here’s the VBA code that does the work. A key API functions that the program uses is the DrawingView.DrawingCurves property, which given an occurrence returns all of the curves in the view that are associated with that occurrence. It also uses the Sheet.ChangeLayer method which is a new method in Inventor 2011 that changes the layer of a group of objects. The program could be written without this, but would be much slower since it will need to change the layer of each curve one at a time.
Public Sub ChangeLayerOfOccurrenceCurves()
' Get the active drawing document.
Dim drawDoc As DrawingDocument
Set drawDoc = ThisApplication.ActiveDocument
' Have the user select a drawing view.
Dim drawView As DrawingView
Set drawView = ThisApplication.CommandManager.Pick( _
kDrawingViewFilter, "Select a drawing view.")
Dim docDesc As DocumentDescriptor
Set docDesc = drawView.ReferencedDocumentDescriptor
' Verify that the selected drawing view is of an assembly.
If docDesc.ReferencedDocumentType <> kAssemblyDocumentObject Then
MsgBox "The selected view must be of an assembly."
Exit Sub
End If
' Get the component definition for the assembly.
Dim asmDef As AssemblyComponentDefinition
Set asmDef = docDesc.ReferencedDocument.ComponentDefinition
' Process the occurrences, wrapping it in a transaction so the
' entire process can be undone with a single undo operation.
Dim trans As Transaction
Set trans = ThisApplication.TransactionManager.StartTransaction( _
drawDoc, "Change drawing view color")
' Call the recursive function that does all the work.
Call ProcessAssemblyColor(drawView, asmDef.Occurrences)
trans.End
End Sub
Private Sub ProcessAssemblyColor(drawView As DrawingView, _
Occurrences As ComponentOccurrences)
' Iterate through the current collection of occurrences.
Dim occ As ComponentOccurrence
For Each occ In Occurrences
' Check to see if this occurrence is a part or assembly.
If occ.DefinitionDocumentType = kPartDocumentObject Then
' ** It's a part so process the color.
' Get the render style of the occurrence.
Dim color As RenderStyle
Dim sourceType As StyleSourceTypeEnum
Set color = occ.GetRenderStyle(sourceType)
' Get the TransientsObjects object to use later.
Dim transObjs As TransientObjects
Set transObjs = ThisApplication.TransientObjects
' Verify that a layer exists for this color.
Dim layers As LayersEnumerator
Set layers = drawView.Parent.Parent.StylesManager.layers
Dim drawDoc As DrawingDocument
Set drawDoc = drawView.Parent.Parent
On Error Resume Next
Dim colorLayer As Layer
Set colorLayer = layers.Item(color.Name)
If Err.Number <> 0 Then
On Error GoTo 0
' Get the diffuse color for the render style.
Dim red As Byte
Dim green As Byte
Dim blue As Byte
' Create a color object that is the diffuse color.
Call color.GetDiffuseColor(red, green, blue)
Dim newColor As color
Set newColor = transObjs.CreateColor(red, green, blue)
' Copy an arbitrary layer giving it the name
' of the render style.
Set colorLayer = layers.Item(1).Copy(color.Name)
' Set the attributes of the layer to use the color,
' have a solid line type, and a specific width.
colorLayer.color = newColor
colorLayer.LineType = kContinuousLineType
colorLayer.LineWeight = 0.02
End If
On Error GoTo 0
' Get all of the curves associated with this occurrence.
On Error Resume Next
Dim drawcurves As DrawingCurvesEnumerator
Set drawcurves = drawView.DrawingCurves(occ)
If Err.Number = 0 Then
On Error GoTo 0
' Create an empty collection.
Dim objColl As ObjectCollection
Set objColl = transObjs.CreateObjectCollection()
' Add the curve segments to the collection.
Dim drawCurve As DrawingCurve
For Each drawCurve In drawcurves
Dim segment As DrawingCurveSegment
For Each segment In drawCurve.Segments
objColl.Add segment
Next
Next
' Change the layer of all of the segments.
Call drawView.Parent.ChangeLayer(objColl, colorLayer)
End If
On Error GoTo 0
Else
' It's an assembly so process its contents.
Call ProcessAssemblyColor(drawView, occ.SubOccurrences)
End If
Next
End Sub
This sample program sets the color of each occurrence based on its color in the assembly. You could choose to set the color based on any other criteria instead. It can be anything associated with the part. you just have to choose what attribute of the part to use and what the associated set of colors are that cam be assigned to each part. For example, you might have a custom iProperty that controls what color is assigned to each part in the drawing.