Appearance Asset SetColor Method

Appearance Asset SetColor Method

Anonymous
Not applicable
2,069 Views
5 Replies
Message 1 of 6

Appearance Asset SetColor Method

Anonymous
Not applicable

Hi All,

 

I'm trying to tweek the RGB color values of an existing Appearance Asset with the .SetColor method, but I can't get the method to actually change the RGB color values of the Asset.

 

The .CreateColor method works well to create the local asset, per the 'Public Sub CreateSimpleColorAppearance()' routine in the Sample Code.

 

Does anyone have a code fragment that uses the .SetColor method they would be willing to share or have an explanation as to how this method works?

 

I'm coding in VBA to update an older color change macro to work in Inventor 2018.

 

Thanks in advance,

 

Ron W.

 

 

0 Likes
Accepted solutions (3)
2,070 Views
5 Replies
Replies (5)
Message 2 of 6

JaneFan
Autodesk
Autodesk
Accepted solution

Hello Ron,

 

To set color to a appearance, the appearance need satisfy two conditions:

1. The appearance is not read only.

2. The appearance doesn't have texture

As shown in below picture, the left one has texture which is used to control the appearance color, which means the color property can't be set individually; while the right appearance can be controlled directly by color property.

CM.jpg

Please check the code below:

Public Sub SetExistingColorAppearance()
    Dim doc As Document
    Set doc = ThisApplication.ActiveDocument
    Dim docAssets As Assets
    Set docAssets = doc.Assets
   
    ' Get the document level asset
    Dim appearance As Asset
    Set appearance = docAssets.Item(1)
   
    Dim tobjs As TransientObjects
    Set tobjs = ThisApplication.TransientObjects

    If appearance.HasTexture = False And appearance.IsReadOnly = False Then
        Dim color As ColorAssetValue
        Set color = appearance.Item("generic_diffuse")
        color.Value = tobjs.CreateColor(255, 255, 15)
    End If
End Sub




Jane Fan
Inventor/Fusion QA Engineer
Message 3 of 6

chandra.shekar.g
Autodesk Support
Autodesk Support
Accepted solution

Hi @Anonymous,

 

Try the following VBA code to update color. Using transientObjects of Inventor objects, colors can be created and assigned

 

Sub Main()

    Dim oDoc As PartDocument
    Set oDoc = ThisApplication.ActiveDocument

    Dim oTG As TransientObjects
    Set oTG = ThisApplication.TransientObjects

    Dim docAsset As Assets
    Set docAsset = oDoc.Assets

    Dim appearance As Asset
    Set appearance = docAsset.Item("Appearance Name")
    'appearance = docAsset.Add(AssetTypeEnum.kAssetTypeAppearance, "Generic", "appearances", "New Appearance")

    Dim generic_color As ColorAssetValue
    Set generic_color = appearance.Item("generic_diffuse")
    generic_color.Value = oTG.CreateColor(243, 35, 56)
    generic_color.HasConnectedTexture = True
End Sub

Please feel free to contact if there is any doubt.

 

If solves problem, click on "Accept as solution" / give a "Kudo".

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



Message 4 of 6

Anonymous
Not applicable

Hi JaneFan, Chandra,

 

Thanks for both of the Light-Speed replies.

 

I tried about a dozen different code fragments to change the base color using .SetColor.  It never occurred to me to try the .CreateColor method on an existing Appearance to modify it.  So...is the .SetColor method obsolete or is it just not fully exposed to VBA?

 

The .CreateColor works great and as only the one method is required, I think I can rewrite and simplify some of my code.  

 

Thanks again for the reply.

 

Ron W.

0 Likes
Message 5 of 6

JaneFan
Autodesk
Autodesk
Accepted solution

Hello Ron,

 

Color.SetColor is not obsoleted, which is still valid and fully supported for a single Color object. But for ColorAssetValue.Value(Color Object) in AppearanceAsset case, it is not working in this way. We can prove in such way:

        Dim color As ColorAssetValue
        Set color = appearance.Item("generic_diffuse")
        Dim ogetC As color
        Set ogetC = color.Value
        Call oGetC.SetColor(0, 0, 255)

With the upper code, we can see the object ogetC is changed by .SetColor(), while color.Value stays unchanged since it is considered as the different object from ogetC.

 

Is the workaround working for you to Set color value in this case by ColorAssetValue.Value = (xxx, xxx, xxx)?




Jane Fan
Inventor/Fusion QA Engineer
0 Likes
Message 6 of 6

Anonymous
Not applicable

Hi Jane,

 

Using the .CreateColor works fine for my use.  In desperation, I probably would have tried it at some point...it just hadn't occurred to me at the time.

 

I now see the difference in the ColorAssetValue and the Color object.  Previously the distinction was unclear to me.  If the object isn't dimensioned, .SetColor doesn't work.  It would be nice if it had returned some kind of 'debug' error or warning.  

 

For my use, I have a set of standard colors used for illustration purposes.  The colors are straight RGB colors, no textures, cutouts etc. but some have transparency and reflectivity properties.  Colors are applied to an occurrence in an assembly for use in an Iso drawing view usually.  Colors are used for clarity, there is no intent to render anything realistically and are created locally on the fly from a data value set, hard coded into my 'CangeColor' macro.  It lets me create constantly colored drawing views from any model for presentations and design reviews so a drawing printed for a design review is colored the same as the live model used in the presentation.

 

Anyway I modified my routines an all is working well.

 

Thanks again for the info.

 

Ron Wollenschlager

BWXT Nuclear Energy Canada Inc.  

0 Likes