Use VBA to set color option to "By Layer"

Use VBA to set color option to "By Layer"

jaconoorland45
Contributor Contributor
1,560 Views
8 Replies
Message 1 of 9

Use VBA to set color option to "By Layer"

jaconoorland45
Contributor
Contributor

Hi all,

 

Before we make exports to PDF we need to change the line color of objects using the following code and this works perfect:

 

{

Dim oDrawDoc As DrawingDocument
Set oDrawDoc = ThisApplication.ActiveDocument

Dim oLayer As Layer

Set oLayer = oDrawDoc.StylesManager.Layers.Item("HIS_0")
oLayer.Color = ThisApplication.TransientObjects.CreateColor(0, 0, 0)

}

 

(This is used for about 20 different layers)

 

The only problem is that in some inventor drawings, the color of some specific objects isn't referenced to a layer with the "By Layer" option. So if we change the layer color the color of the object stays the same.

 

jaconoorland45_0-1655980171556.png

 

Is there any code in VBA available to change the color of the object (e.g. Dimensions or Hatch or anything) to "By Layer"? So it looks like this:

 

jaconoorland45_2-1655980381371.png

(We have thousands of drawings so therefore it is no option to do it by hand.)

 

Thanks in advance!

Accepted solutions (2)
1,561 Views
8 Replies
Replies (8)
Message 2 of 9

JMGunnar
Collaborator
Collaborator
Accepted solution

You need too get DimensionStyles

 

and then you can set oDimension.color too Nothing

 

Best Regards Johan 

 

 

Dim oDrawDoc As DrawingDocument
        oDrawDoc = ThisApplication.ActiveDocument

Dim oDimension As DimensionStyle
   Set     oDimension = oDrawDoc.StylesManager.DimensionStyles.Item("Dimension")

oDimension.Color = Nothing

Message 3 of 9

jaconoorland45
Contributor
Contributor

I am going to try it immidiately. Thanks for your help!

0 Likes
Message 4 of 9

jaconoorland45
Contributor
Contributor

It Worked!! I am very happy, thanks a lot.😅

 

There was a little mistake in your code. You forgot "Set" before oDrawDoc.

 

So here is the code which works for me:

 

Public Sub editedit()

Dim oDrawDoc As DrawingDocument
Set oDrawDoc = ThisApplication.ActiveDocument

Dim oDimension As DimensionStyle
Set oDimension = oDrawDoc.StylesManager.DimensionStyles.Item("HIS_DIMENSION")

oDimension.Color = Nothing

End Sub

 

 

0 Likes
Message 5 of 9

jaconoorland45
Contributor
Contributor

@JMGunnar 

 

So after printing we need to reverse it too its original color. Is this possible? So we need to uncheck the "By Layer" option so it reverts to its previously color.   

0 Likes
Message 6 of 9

jaconoorland45
Contributor
Contributor
Accepted solution

@JMGunnar 

 

I already found a solution for this by storing the RGB Value and then recalling it later.

 

Public Sub editedit2()

Dim oldcolor As Color
Dim oDrawDoc As DrawingDocument

Set oDrawDoc = ThisApplication.ActiveDocument

Dim oDimension As DimensionStyle

Set oDimension = oDrawDoc.StylesManager.DimensionStyles.Item("HIS_DIMENSION")

Set oldcolor = oDimension.Color

MsgBox ("It Works! COLOR VALUE:" & test)

oDimension.Color = Nothing

MsgBox ("It Works! COLOR VALUE:" & test)

oDimension.Color = oldcolor

End Sub

0 Likes
Message 7 of 9

JMGunnar
Collaborator
Collaborator

Congratulations @jaconoorland45 

 

I create the code in ilogic and not VBA 

that's why i forgot SET in VBA

 

Autodesk ends with VBA in Inventor in future versions

 

Best Regards Johan 

 

Message 8 of 9

jaconoorland45
Contributor
Contributor

Will my VBA modules become obselete? 😥

0 Likes
Message 9 of 9

JMGunnar
Collaborator
Collaborator

I recommend starting to use ilogic

VBA will be retired.

 

 

 

As an alternative to VBA, consider using Inventor’s iLogic capability. It provides access to the Inventor API using VB.NET. You can learn more about iLogic 

 

https://adndevblog.typepad.com/manufacturing/2015/11/convert-vba-to-net-ilogic.html

 

https://knowledge.autodesk.com/support/inventor/downloads/caas/downloads/content/download-the-micros...

 

Johan

 

0 Likes