VBA Macro - Change Part Colour Using RGB Values

VBA Macro - Change Part Colour Using RGB Values

isocam
Collaborator Collaborator
790 Views
6 Replies
Message 1 of 7

VBA Macro - Change Part Colour Using RGB Values

isocam
Collaborator
Collaborator

Can anybody help?

 

Does anybody know how to change the colour of a part, using RGB values, using a VBA macro?

 

I have the following VBA Code.....


Public Function ChangeColour()
Dim oDoc As PartDocument

Set oDoc = ThisApplication.ActiveDocument

Dim oAppearence As Asset

oAppearence = oDoc.Assets.Add(kAssetTypeAppearance, "Generic", "Appearances")

Dim oColor As ColorAssetValue

oColor = oAppearence.Item("generic_diffuse")

Red = 0

Green = 145

Blue = 255

oColor.Value = ThisApplication.TransientObjects.CreateColor(Red, Green, Blue)

oExtrude1.Appearance = oAppearence
End Function

 

Can anybody update the code so that it runs?

Many thanks in advance!

 

Darren

0 Likes
Accepted solutions (1)
791 Views
6 Replies
Replies (6)
Message 2 of 7

A.Acheson
Mentor
Mentor

Hi @isocam 

I assume you want to change the part document appearance? If so this is the help page syntax for appearance.

I haven't checked your code for further functionality.

Syntax

PartDocument.ActiveAppearance() As Asset

 

Here is also an old post where you asked the same question. In the last post curtis has a part. Is that part doing what you expect? 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 3 of 7

isocam
Collaborator
Collaborator

Hi,

 

Nothing that I do to the code works.

 

Please can you help resolve this problem?

 

Many thanks in advance!

 

Kindest Regards

 

Darren

0 Likes
Message 4 of 7

A.Acheson
Mentor
Mentor

I'm not near a computer so help is limited can you post the error messages? How far are you getting? Is the appearance being created? 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 5 of 7

Michael.Navara
Advisor
Advisor
Message 6 of 7

isocam
Collaborator
Collaborator

Hi,

 

Even this does not work.

 

Please try it to see if you get the same error.

 

Kind Regards

 

Darren

0 Likes
Message 7 of 7

WCrihfield
Mentor
Mentor
Accepted solution

Hi @isocam.  Within the code sample you originally posted here, the 'oExtrude1' variable has not been 'declared', and no value has been assigned to it.  So, it does not know what 'oExtrude1' is, and therefore does not recognize it as having a property named 'Appearance'.  I can only assume from its name that it is supposed to be representing an Inventor.ExtrudeFeature type object.  If that is the case, then your VBA macro will need to include more code that finds that specific ExtrudeFeature first, then declares that variable and sets the Type of that variable to ExtrudeFeature, then sets the value of that variable to that ExtrudeFeature that it finds...all before that line of code that is attempting to set its appearance.  If I had to guess, I might assume that you are working on a PartDocument that only has one ExtrudeFeature present in it, then maybe you only want this VBA macro to effect that one ExtrudeFeature, and not any of the other objects in the whole part.  Does that sound correct to you.  If so, then that will give us a better idea of how to fix the code for you.

 

The code below is according to my unconfirmed assumptions.

 

 

Function ChangeColour()
    Dim oDoc As PartDocument
    Set oDoc = ThisApplication.ActiveDocument
    
    Dim oAppearence As Asset
    Set oAppearence = oDoc.Assets.Add(kAssetTypeAppearance, "Generic")
    
    Dim oColor As ColorAssetValue
    Set oColor = oAppearence.Item("generic_diffuse")
    
    Red = 0
    Green = 145
    Blue = 255
    oColor.Value = ThisApplication.TransientObjects.CreateColor(Red, Green, Blue)
    
    Dim oExtFeats As ExtrudeFeatures
    Set oExtFeats = oDoc.ComponentDefinition.Features.ExtrudeFeatures
    If oExtFeats.Count = 0 Then Return
    
    Dim oExtrude1 As ExtrudeFeature
    Set oExtrude1 = oExtFeats.Item(1)
    oExtrude1.Appearance = oAppearence
End Function

 

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

 

Edit:  I also corrected another think I saw later.  The Assets.Add method's online documentation says that its third input is 'reserved for future use', so that one should not be specified (and it was in the original code here).  However, we can specify the forth input, which would be the DisplayName of the new Asset.  But I left that out for now, because if we do specify that, and that one already exists, that would likely just cause another error that we would need to work around somehow.

 

Edit 2:  Also removed 'Public' from the definition line, and added the 'Set' keyword in there a few times were it was needed.  Need to test these things before posting them. 😅

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes