Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Change part colour in drawing with ilogic

Anonymous

Change part colour in drawing with ilogic

Anonymous
Not applicable

Is it possible to paint (change colour) of some parts in a drawing using ilogic?

I have a drawing of an assembly, where are many elements.

I would like to automatically paint parts in specific colour (type of colour doesn't matter, can be red, green etc.).

When I paint "Element1" to red, I want all occurrences of "Element1" to be painted (since there are many) and on all views. This is important to me because number of occurrences changes.

How can I do it with ilogic?

0 Likes
Reply
Accepted solutions (1)
3,464 Views
14 Replies
Replies (14)

chandra.shekar.g
Autodesk Support
Autodesk Support

@Anonymous,

 

Hoping that below forum discussion link may be helpful.

 

https://forums.autodesk.com/t5/inventor-forum/update-drawing-line-colour-based-on-part-colour/td-p/7417136

 

Thanks and regards,

 


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes

Anonymous
Not applicable

Discussion from the link is about getting colour from the part and puting it to drawing.

In my case it is impossibile, because many different parts have the same colour (it is connected with material).

I would like to paint a specific part (all occurrences of this part) with some colour (for example red) with no connection to part colour.

0 Likes

chandra.shekar.g
Autodesk Support
Autodesk Support

@Anonymous,

 

Can you please provide sample assembly with drawing to test? Please make sure that files are non confidential.

 

Thanks and regards,

 


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes

Anonymous
Not applicable

Ok I put a sample assembly with drawing.

Drawing has 2 sheets. First is balck and white. The second is coloured manually.

0 Likes

chandra.shekar.g
Autodesk Support
Autodesk Support

@Anonymous,

 

Try below iLogic code to color parts in assembly. In this example, there are 3 parts in assembly. So, code works for 3 colors only. It extended for different colors based on situations.

Sub Main()
    Dim oDoc As DrawingDocument
    oDoc = ThisApplication.ActiveDocument
    
    Dim oSheet As Sheet
    oSheet = oDoc.ActiveSheet
    
    Dim layers As LayersEnumerator
    layers = oDoc.StylesManager.Layers
    
    On Error Resume Next
    Dim oRed As Layer
    oRed = layers.Item("Red")
     
    If Err.Number <> 0 Then
        On Error GoTo 0
        
        Dim redColor As Color
        redColor = ThisApplication.TransientObjects.CreateColor(255, 0, 0)
    
        ' Copy an arbitrary layer giving it the name
        ' of the render style.
        oRed = layers.Item(1).Copy("Red")
    
        ' the attributes of the layer to use the color,
        ' have a solid line type, and a specific width.
        oRed.Color = redColor
        oRed.LineType = kContinuousLineType
        oRed.LineWeight = 0.02
     End If
     
     On Error Resume Next
    Dim oGreen As Layer
    oGreen = layers.Item("Green")
     
    If Err.Number <> 0 Then
        On Error GoTo 0
        
        Dim greenColor As Color
        greenColor = ThisApplication.TransientObjects.CreateColor(0, 255, 0)
    
        ' Copy an arbitrary layer giving it the name
        ' of the render style.
        oGreen = layers.Item(1).Copy("Green")
    
        ' the attributes of the layer to use the color,
        ' have a solid line type, and a specific width.
        oGreen.Color = greenColor
        oGreen.LineType = kContinuousLineType
        oGreen.LineWeight = 0.02
     End If
     
     On Error Resume Next
    Dim oBlue As Layer
    oBlue = layers.Item("Blue")
     
    If Err.Number <> 0 Then
        On Error GoTo 0
        
        Dim blueColor As Color
        blueColor = ThisApplication.TransientObjects.CreateColor(0, 0, 255)
    
        ' Copy an arbitrary layer giving it the name
        ' of the render style.
        oBlue = layers.Item(1).Copy("Blue")
    
        ' the attributes of the layer to use the color,
        ' have a solid line type, and a specific width.
        oBlue.Color = blueColor
        oBlue.LineType = kContinuousLineType
        oBlue.LineWeight = 0.02
     End If
     
     Dim colorColl As ObjectCollection
     colorColl = ThisApplication.TransientObjects.CreateObjectCollection
     
     Call colorColl.Add(oRed)
     Call colorColl.Add(oBlue)
     Call colorColl.Add(oGreen)
     
    
    Dim oView As DrawingView
    For Each oView In oSheet.DrawingViews
        Dim oReferDoc As AssemblyDocument
        oReferDoc = oView.ReferencedDocumentDescriptor.ReferencedDocument
        
        Dim oAssyDef As AssemblyComponentDefinition
        oAssyDef = oReferDoc.ComponentDefinition
        
        Dim occDoc As Document
        Dim cnt As Integer
        cnt = 1
        For Each occDoc In oReferDoc.AllReferencedDocuments
            
            Dim occ As ComponentOccurrence
            For Each occ In oAssyDef.Occurrences
                If occ.ReferencedDocumentDescriptor.ReferencedDocument Is occDoc Then
                    Dim oCurves As DrawingCurvesEnumerator
                    oCurves = oView.DrawingCurves(occ)
                    
                    Dim oCurve As DrawingCurve
                    For Each oCurve In oCurves
                        Dim oColl As ObjectCollection
                        oColl = ThisApplication.TransientObjects.CreateObjectCollection
                        Dim oSegment As DrawingCurveSegment
                        For Each oSegment In oCurve.Segments
                            Call oColl.Add(oSegment)
                        Next
                        Call oSheet.ChangeLayer(oColl, colorColl.Item(cnt))
                        
                        Call oDoc.Update
                    Next
                End If
                
            Next
            cnt = cnt + 1
        Next
        
    Next
End Sub

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes

Anonymous
Not applicable

It works exactly as You said - three different parts become coloured in three different colours.

But what if I would like to call a specific part by name in order to paint it? For example:

Colour "Element 01" to red (all occurrences).

Colour "Element 02" to green (all occurrences).

Do not colour "Element 03" (not all parts need to be coloured)

Colour "Element 04" to blue (if exists)

 

This is because number of different elements will change (there may be "Element 04", "Element 05" , "Element 03" may disappear, etc.)

 

0 Likes

chandra.shekar.g
Autodesk Support
Autodesk Support
Accepted solution

@Anonymous,

 

Try this.

Sub Main()
    Dim oDoc As DrawingDocument
    oDoc = ThisApplication.ActiveDocument
    
    Dim oSheet As Sheet
    oSheet = oDoc.ActiveSheet
    
    Dim layers As LayersEnumerator
    layers = oDoc.StylesManager.Layers
    
    On Error Resume Next
    Dim oRed As Layer
    oRed = layers.Item("Red")
     
    If Err.Number <> 0 Then
        On Error GoTo 0
        
        Dim redColor As Color
        redColor = ThisApplication.TransientObjects.CreateColor(255, 0, 0)
    
        ' Copy an arbitrary layer giving it the name
        ' of the render style.
        oRed = layers.Item(1).Copy("Red")
    
        ' the attributes of the layer to use the color,
        ' have a solid line type, and a specific width.
        oRed.Color = redColor
        oRed.LineType = kContinuousLineType
        oRed.LineWeight = 0.02
     End If
     
     On Error Resume Next
    Dim oGreen As Layer
    oGreen = layers.Item("Green")
     
    If Err.Number <> 0 Then
        On Error GoTo 0
        
        Dim greenColor As Color
        greenColor = ThisApplication.TransientObjects.CreateColor(0, 255, 0)
    
        ' Copy an arbitrary layer giving it the name
        ' of the render style.
        oGreen = layers.Item(1).Copy("Green")
    
        ' the attributes of the layer to use the color,
        ' have a solid line type, and a specific width.
        oGreen.Color = greenColor
        oGreen.LineType = kContinuousLineType
        oGreen.LineWeight = 0.02
     End If
     
     On Error Resume Next
    Dim oBlue As Layer
    oBlue = layers.Item("Blue")
     
    If Err.Number <> 0 Then
        On Error GoTo 0
        
        Dim blueColor As Color
        blueColor = ThisApplication.TransientObjects.CreateColor(0, 0, 255)
    
        ' Copy an arbitrary layer giving it the name
        ' of the render style.
        oBlue = layers.Item(1).Copy("Blue")
    
        ' the attributes of the layer to use the color,
        ' have a solid line type, and a specific width.
        oBlue.Color = blueColor
        oBlue.LineType = kContinuousLineType
        oBlue.LineWeight = 0.02
     End If 
	 
    Dim oView As DrawingView
    For Each oView In oSheet.DrawingViews
        Dim oReferDoc As AssemblyDocument
        oReferDoc = oView.ReferencedDocumentDescriptor.ReferencedDocument
        
        Dim oAssyDef As AssemblyComponentDefinition
        oAssyDef = oReferDoc.ComponentDefinition 
         
    	Dim occ As ComponentOccurrence
    	For Each occ In oAssyDef.Occurrences         
	        Dim oCurves As DrawingCurvesEnumerator
	        oCurves = oView.DrawingCurves(occ)
	        
	        Dim oCurve As DrawingCurve
	        For Each oCurve In oCurves
	            Dim oColl As ObjectCollection
	            oColl = ThisApplication.TransientObjects.CreateObjectCollection
	            Dim oSegment As DrawingCurveSegment
	            For Each oSegment In oCurve.Segments
	                Call oColl.Add(oSegment)
	            Next
				
				Dim occName As String 
				occName = occ.Name 
				If occName.StartsWith("Element 01") = True Then
					Call oSheet.ChangeLayer(oColl, oRed)
				Else If occName.StartsWith("Element 02") = True Then
					Call oSheet.ChangeLayer(oColl, oGreen)
				Else If occName.StartsWith("Element 04") = True Then
					Call oSheet.ChangeLayer(oColl, oBlue)
				End If 
	            
	            Call oDoc.Update
	        Next 
        
    	Next         
    Next
End Sub

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



RoyWickrama_RWEI
Advisor
Advisor

I am interested in assigning color to a layer in the drawing:

 

Color: Dark Brown

 

I am trying to assign colot to Dark Brown without success. Please help. Thanks.

 

Sub Main()
    Dim oDrawDoc As DrawingDocument
    oDrawDoc = ThisApplication.ActiveDocument
    Dim oLayer As Layer
    ' oLayer = oDrawDoc.StylesManager.Layers.Item(68)
    oLayer = oDrawDoc.StylesManager.Layers.Item("Title (ISO)")
    Dim oNewLayer As Layer
    oNewLayer_Name = "XYZ"
    Try
    oNewLayer = oLayer.Copy(oNewLayer_Name)
    Catch
    MessageBox.Show("New layer, " & oNewLayer_Name & ": Exists", "Title")
    End Try
ThisDrawing.Document.StylesManager.Layers(oNewLayer_Name).Visible = False
ThisDrawing.Document.StylesManager.Layers(oNewLayer_Name).Plot = False
End Sub 

Need help to assign  

0 Likes

chandra.shekar.g
Autodesk Support
Autodesk Support

@RoyWickrama_RWEI,

 

Hoping that below iLogic code may be helpful.

 

Sub Main()
    Dim oDrawDoc As DrawingDocument
    oDrawDoc = ThisApplication.ActiveDocument
    Dim oLayer As Layer
    ' oLayer = oDrawDoc.StylesManager.Layers.Item(68)
    oLayer = oDrawDoc.StylesManager.Layers.Item("Title (ISO)")
    Dim oNewLayer As Layer
    oNewLayer_Name = "XYZ"
    Try
    	oNewLayer = oLayer.Copy(oNewLayer_Name)
    Catch
    	MessageBox.Show("New layer, " & oNewLayer_Name & ": Exists", "Title")
    End Try
	ThisDrawing.Document.StylesManager.Layers(oNewLayer_Name).Visible = False
	ThisDrawing.Document.StylesManager.Layers(oNewLayer_Name).Plot = False
	
	Dim brownColor As Color
	brownColor = ThisApplication.TransientObjects.CreateColor(165, 42, 42) 

	' the attributes of the layer to use the color,
	' have a solid line type, and a specific width.
	oNewLayer.Color = brownColor
	oNewLayer.LineType = kContinuousLineType
	oNewLayer.LineWeight = 0.02
End Sub

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



RoyWickrama_RWEI
Advisor
Advisor

Hi Chandra Shekar;

Thanks a lot for the great help. It is fine. Also, I tailored the rule (see the attached .txt file).

 

brownColor_1 = ThisApplication.TransientObjects.CreateColor(xxx, xxx, xxx)

brownColor_2 = ThisApplication.TransientObjects.CreateColor(xxx, xxx, xxx)

 

Could you provide me with the coding for brownColor_1  andbrownColor_2: see the image below.

 

Could I know the list of TransientObjects.CreateColor for all colors.

Thanks again.

 

 

2019-06-24 23_00_31.png

0 Likes

chandra.shekar.g
Autodesk Support
Autodesk Support

@RoyWickrama_RWEI,

 

Refer below images to get RGB values for brown color.

Brown_1.pngBrown_2.png

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



RoyWickrama_RWEI
Advisor
Advisor

Thanks a lot for the help.

That is fine.

I am taking sometime off from Inventor customization training (from today till end of Sept) for taking some strenuous self faced training in using SolidWorks 2019.

Planning to take very high skills in Solidworks as well.

 

0 Likes

anthony.ravello
Enthusiast
Enthusiast

Hello
How do you change the colour of a iPart in Drawing?
That is very importante for me :slightly_smiling_face:

0 Likes

anthony.ravello
Enthusiast
Enthusiast

very interesting
is it Posible to change the colour of the Parts in Drawing using custom Properties?
I mean. Some parts have the same custom Property called "Green". Then in Drawing change all this parts in this color (preferably to have a layer with this name "Green" too)

 

0 Likes