how to set color of single part in assembly

how to set color of single part in assembly

yvandelafontaine
Advocate Advocate
333 Views
1 Reply
Message 1 of 2

how to set color of single part in assembly

yvandelafontaine
Advocate
Advocate

Usually, in an assembly, when i want to change a part colour at the root (not the occurrence) i send my Colour multilist value to the part, then from within the part i create a rule with the following to set the appearance. ( I use custom appearance libraries), it then show in the assembly (in default representation) and shows in the part list, per item, for the buyer, but it is long and painful to do...

iProperties.PartColor = INTERIOR_COLOR_PARAM
'one of my many list for native colour of part options

Now is there an easier way of doing this? this implies that i have a code in every part... way too tedious IMO...

 

Maybe something like this, but that would change not the occurrence but the active appearance of the part itself :

Component.InventorComponent("PANNEAU_ARRIERE_SIMPLE").Appearance.Asset.Name = INTERIOR_COLOR_LIST
Component.InventorComponent("PANNEAU_AVANT_SIMPLE").Appearance.Asset.Name = EXTERIOR_COLOR_LIST
'and so on...

 

I tried this but for some reason it does not always work and it is a lot of work for a small thing like changing part color... (all required colour are in library and locally in document but again it implies that I have to save the colour locally and becomes a problem if a colour is added to the assets and multilist)

Dim oDoc As PartDocument
oDoc = Component.InventorComponent("PANNEAU_ARRIERE_SIMPLE").ReferencedDocumentDescriptor.ReferencedDocument 

oDoc.ActiveAppearance = ThisApplication.AssetLibraries.Item(2).AppearanceAssets.Item(COULEUR_INTERIEUR)

 

I have found multiple rule out there to change all parts, but my need is part specific so i need to list then individually.

0 Likes
334 Views
1 Reply
Reply (1)
Message 2 of 2

yan.gauthier
Advocate
Advocate

You cannot simply assume that the Item(2) of you assetLibraries is the materialAsset library you are looking for.

 

I always loop through looking for my library name.

 

Sub MaterialAppearanceUpdate(bMaterial As Boolean)

Dim invApp As Inventor.Application
Dim oPartDoc As PartDocument
Dim oDoc As Document
Dim oAssetLibraries As AssetLibraries
Dim oAssetLibrary As AssetLibrary
Dim MyMatLibrary As AssetLibrary
Dim oMatAsset As MaterialAsset
Dim oAppearanceAsset As Asset
Dim ComparisonString As String

On Error GoTo MaterialAppearanceUpdateErr

Set invApp = ThisApplication
Set oDoc = invApp.ActiveEditDocument
Set oAssetLibraries = invApp.AssetLibraries

For Each oAssetLibrary In oAssetLibraries
    If oAssetLibrary.DisplayName = "Inventor Material Library" Then
        Set MyMatLibrary = oAssetLibrary
    End If
Next oAssetLibrary

If Not MyMatLibrary Is Nothing Then

    If TypeOf oDoc Is PartDocument Then
        Set oPartDoc = oDoc
    Else
        '*************************************************************************************************
        Exit Sub ' Ignore if launched from anything else than Part for now
    End If
    
    ComparisonString = "-" & updateString
    
    If bMaterial Then 'Update Material
        For Each oMatAsset In MyMatLibrary .MaterialAssets
            If StrComp(oMatAsset.DisplayName, ComparisonString, vbTextCompare) = 0 Then 'String are identical
                oPartDoc.ActiveMaterial = oMatAsset
                Exit For
            End If
        Next oMatAsset
    Else 'Update Appearance
        For Each oAppearanceAsset In MyMatLibrary .AppearanceAssets
            If StrComp(oAppearanceAsset.DisplayName, ComparisonString, vbTextCompare) = 0 Then
                oPartDoc.ActiveAppearance = oAppearanceAsset
                Exit For
            End If
        Next oAppearanceAsset
    End If

Else
    MsgBox "Inventor Material Library not found", vbMsgBoxSetForeground, "MaterialAppearanceUpdate"
End If

MaterialAppearanceUpdateErr:
If Err Then
    MsgBox "unexpected error: " & Err.Description, vbMsgBoxSetForeground, "MaterialAppearanceUpdate"
    Err.Clear
End If

 Here is my code. you could use it with a partdocument as parameter instead of the ActiveEditDocument 

0 Likes