<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Same part - same colour in Inventor Programming - iLogic, Macros, AddIns &amp; Apprentice</title>
    <link>https://forums.autodesk.com/t5/inventor-programming-ilogic/same-part-same-colour/m-p/8962368#M100383</link>
    <description>&lt;P&gt;Hello from Japan,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Try this code!&lt;/P&gt;&lt;P&gt;&lt;SPAN class="tlid-translation translation"&gt;&lt;SPAN class=""&gt;Please use freely.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;Sub IColor()
    Dim doc As AssemblyDocument
    Dim designView As DesignViewRepresentation
    Dim viewFound As Boolean
    Dim repManager As RepresentationsManager
    
    Set doc = ThisApplication.ActiveDocument
    Set repManager = doc.ComponentDefinition.RepresentationsManager
    
    viewFound = False
    For Each designView In repManager.DesignViewRepresentations
        If designView.Name = "IColor" Then
            viewFound = True
            Exit For
        End If
    Next
    If Not viewFound Then
        Set designView = repManager.DesignViewRepresentations.Add("IColor")
    End If
    designView.Activate
    
    Dim occ As ComponentOccurrence
    Dim index As Integer
    Dim localAsset As Asset
    
    index = 0
    
    For Each occ In doc.ComponentDefinition.Occurrences
        Set localAsset = GetAsset(doc, index)
        occ.Appearance = localAsset
        index = index + 1
        If index = 16 Then
            index = 0
        End If
    Next
End Sub

Private Function GetAsset(doc As AssemblyDocument, colorNumber As Integer) As Asset
    Dim localAsset As Asset
    Dim colorName As String
    
    colorName = "IColor" &amp;amp; colorNumber

    On Error Resume Next
    Set localAsset = doc.Assets.Item(colorName)
    If Err Then
        Set localAsset = doc.Assets.Add(kAssetTypeAppearance, "Generic", colorName, colorName)
        
        Dim tobjs As TransientObjects
        Set tobjs = ThisApplication.TransientObjects
        
        Dim color As ColorAssetValue
        Dim red As Integer
        Dim green As Integer
        Dim blue As Integer
        Dim high As Integer
        Dim low As Integer
        
        If colorNumber &amp;lt; 8 Then
            high = 255
            low = 30
        Else
            high = 127
            low = 0
        End If
        
        If colorNumber And 1 Then
            red = high
        Else
            red = low
        End If
        If colorNumber And 2 Then
            green = high
        Else
            green = low
        End If
        If colorNumber And 4 Then
            blue = high
        Else
            blue = low
        End If
        localAsset.Item("generic_diffuse").Value = tobjs.CreateColor(red, green, blue)
        
        Dim floatValue As FloatAssetValue
        Set floatValue = localAsset.Item("generic_reflectivity_at_0deg")
        floatValue.Value = 0.1
        
        Set floatValue = localAsset.Item("generic_reflectivity_at_90deg")
        floatValue.Value = 0.1
    End If
    On Error GoTo 0

    Set GetAsset = localAsset
End Function&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="tlid-translation translation"&gt;&lt;SPAN class=""&gt;Best Regards,&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="tlid-translation translation"&gt;&lt;SPAN class=""&gt;=====&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="tlid-translation translation"&gt;&lt;SPAN class=""&gt;Freeradical&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="tlid-translation translation"&gt;&lt;SPAN class=""&gt;&amp;nbsp;Hideo Yamada&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 13 Aug 2019 02:10:31 GMT</pubDate>
    <dc:creator>HideoYamada</dc:creator>
    <dc:date>2019-08-13T02:10:31Z</dc:date>
    <item>
      <title>Same part - same colour</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-ilogic/same-part-same-colour/m-p/8956985#M100340</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;i am looking for a code that will go through the assembly and will change the colour of each part. But same parts should have the same colour. So it is easy&amp;nbsp;to understand the assembly that is created by an colleague.&lt;/P&gt;&lt;P&gt;thanks in advance&lt;/P&gt;&lt;P&gt;Regards from germany&lt;/P&gt;&lt;P&gt;Martin&lt;/P&gt;</description>
      <pubDate>Fri, 09 Aug 2019 06:41:10 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-ilogic/same-part-same-colour/m-p/8956985#M100340</guid>
      <dc:creator>martinhoos</dc:creator>
      <dc:date>2019-08-09T06:41:10Z</dc:date>
    </item>
    <item>
      <title>Re: Same part - same colour</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-ilogic/same-part-same-colour/m-p/8962368#M100383</link>
      <description>&lt;P&gt;Hello from Japan,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Try this code!&lt;/P&gt;&lt;P&gt;&lt;SPAN class="tlid-translation translation"&gt;&lt;SPAN class=""&gt;Please use freely.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;Sub IColor()
    Dim doc As AssemblyDocument
    Dim designView As DesignViewRepresentation
    Dim viewFound As Boolean
    Dim repManager As RepresentationsManager
    
    Set doc = ThisApplication.ActiveDocument
    Set repManager = doc.ComponentDefinition.RepresentationsManager
    
    viewFound = False
    For Each designView In repManager.DesignViewRepresentations
        If designView.Name = "IColor" Then
            viewFound = True
            Exit For
        End If
    Next
    If Not viewFound Then
        Set designView = repManager.DesignViewRepresentations.Add("IColor")
    End If
    designView.Activate
    
    Dim occ As ComponentOccurrence
    Dim index As Integer
    Dim localAsset As Asset
    
    index = 0
    
    For Each occ In doc.ComponentDefinition.Occurrences
        Set localAsset = GetAsset(doc, index)
        occ.Appearance = localAsset
        index = index + 1
        If index = 16 Then
            index = 0
        End If
    Next
End Sub

Private Function GetAsset(doc As AssemblyDocument, colorNumber As Integer) As Asset
    Dim localAsset As Asset
    Dim colorName As String
    
    colorName = "IColor" &amp;amp; colorNumber

    On Error Resume Next
    Set localAsset = doc.Assets.Item(colorName)
    If Err Then
        Set localAsset = doc.Assets.Add(kAssetTypeAppearance, "Generic", colorName, colorName)
        
        Dim tobjs As TransientObjects
        Set tobjs = ThisApplication.TransientObjects
        
        Dim color As ColorAssetValue
        Dim red As Integer
        Dim green As Integer
        Dim blue As Integer
        Dim high As Integer
        Dim low As Integer
        
        If colorNumber &amp;lt; 8 Then
            high = 255
            low = 30
        Else
            high = 127
            low = 0
        End If
        
        If colorNumber And 1 Then
            red = high
        Else
            red = low
        End If
        If colorNumber And 2 Then
            green = high
        Else
            green = low
        End If
        If colorNumber And 4 Then
            blue = high
        Else
            blue = low
        End If
        localAsset.Item("generic_diffuse").Value = tobjs.CreateColor(red, green, blue)
        
        Dim floatValue As FloatAssetValue
        Set floatValue = localAsset.Item("generic_reflectivity_at_0deg")
        floatValue.Value = 0.1
        
        Set floatValue = localAsset.Item("generic_reflectivity_at_90deg")
        floatValue.Value = 0.1
    End If
    On Error GoTo 0

    Set GetAsset = localAsset
End Function&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="tlid-translation translation"&gt;&lt;SPAN class=""&gt;Best Regards,&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="tlid-translation translation"&gt;&lt;SPAN class=""&gt;=====&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="tlid-translation translation"&gt;&lt;SPAN class=""&gt;Freeradical&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="tlid-translation translation"&gt;&lt;SPAN class=""&gt;&amp;nbsp;Hideo Yamada&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 13 Aug 2019 02:10:31 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-ilogic/same-part-same-colour/m-p/8962368#M100383</guid>
      <dc:creator>HideoYamada</dc:creator>
      <dc:date>2019-08-13T02:10:31Z</dc:date>
    </item>
    <item>
      <title>Re: Same part - same colour</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-ilogic/same-part-same-colour/m-p/8962571#M100391</link>
      <description>&lt;P&gt;Hello Hideo Yamada,&lt;/P&gt;&lt;P&gt;thank you very much for the reply. If i run your code, i get colored parts, thats good.&lt;/P&gt;&lt;P&gt;But i did not get the same colour for the same parts.&lt;/P&gt;&lt;P&gt;Regards Martin&lt;/P&gt;</description>
      <pubDate>Tue, 13 Aug 2019 06:41:00 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-ilogic/same-part-same-colour/m-p/8962571#M100391</guid>
      <dc:creator>martinhoos</dc:creator>
      <dc:date>2019-08-13T06:41:00Z</dc:date>
    </item>
    <item>
      <title>Re: Same part - same colour</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-ilogic/same-part-same-colour/m-p/8962726#M100395</link>
      <description>&lt;P&gt;Hello Martin,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Replace IColor() with the following :&lt;/P&gt;&lt;PRE&gt;Sub IColor()
    Dim doc As AssemblyDocument
    Dim designView As DesignViewRepresentation
    Dim viewFound As Boolean
    Dim repManager As RepresentationsManager
    
    Set doc = ThisApplication.ActiveDocument
    Set repManager = doc.ComponentDefinition.RepresentationsManager
    
    viewFound = False
    For Each designView In repManager.DesignViewRepresentations
        If designView.Name = "IColor" Then
            viewFound = True
            Exit For
        End If
    Next
    If Not viewFound Then
        Set designView = repManager.DesignViewRepresentations.Add("IColor")
    End If
    designView.Activate
    
    Dim occ As ComponentOccurrence
    Dim occ2 As ComponentOccurrence
    Dim index As Integer
    Dim index2 As Integer
    Dim localAsset As Asset
    Dim colorIndex As Integer
    
    colorIndex = 0
    
    For index = 1 To doc.ComponentDefinition.Occurrences.Count
        Set occ = doc.ComponentDefinition.Occurrences(index)
        If Left(occ.Appearance.DisplayName, 6) &amp;lt;&amp;gt; "IColor" Then
            Set localAsset = GetAsset(doc, colorIndex)
            For index2 = index To doc.ComponentDefinition.Occurrences.Count
                Set occ2 = doc.ComponentDefinition.Occurrences(index2)
                If occ.Definition Is occ2.Definition Then
                    occ2.Appearance = localAsset
                End If
            Next index2
            colorIndex = (colorIndex + 1) And 15
        End If
    Next index
End Sub&lt;/PRE&gt;&lt;P&gt;Does this work fine?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;=====&lt;/P&gt;&lt;P&gt;Freeradical&lt;/P&gt;&lt;P&gt;&amp;nbsp;Hideo Yamada&lt;/P&gt;</description>
      <pubDate>Tue, 13 Aug 2019 08:24:57 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-ilogic/same-part-same-colour/m-p/8962726#M100395</guid>
      <dc:creator>HideoYamada</dc:creator>
      <dc:date>2019-08-13T08:24:57Z</dc:date>
    </item>
    <item>
      <title>Re: Same part - same colour</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-ilogic/same-part-same-colour/m-p/8962793#M100397</link>
      <description>&lt;P&gt;Hello Hideo Yamada,&lt;/P&gt;&lt;P&gt;thank you very much! Your code did exactly what i like to have.&lt;/P&gt;&lt;P&gt;Thanks again and regards to Japan...&lt;/P&gt;&lt;P&gt;Martin&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 13 Aug 2019 09:12:34 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-ilogic/same-part-same-colour/m-p/8962793#M100397</guid>
      <dc:creator>martinhoos</dc:creator>
      <dc:date>2019-08-13T09:12:34Z</dc:date>
    </item>
  </channel>
</rss>

