- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Ciao,
I would like to change Material selection via VBA code.
I did even in iLogic and it works, code is :
[...]
iProperties.Material = "TestMaterial"
[...]
In VBA i did:
[...]
ThisApplication.ActiveDocument.PropertySets.Item("Design Tracking Properties").Item("Material").Value = "TestMaterial"
[...]
In VBA i have no material change for the part.
I did the same for Custom iProperties and it works:
ThisApplication.ActiveDocument.PropertySets.Item("Inventor User Defined Properties").Item("InternalCode").Value = "TestCode"
Can you help me? Any tips?
Thanks.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Dim oDoc As PartDocument Set oDoc = ThisApplication.ActiveDocument Dim oSM As SheetMetalComponentDefinition Set oSM = oDoc.ComponentDefinition Dim oSMStyle As SheetMetalStyle Set oSMStyle = oSM.SheetMetalStyles.Item("Default") oSMStyle.Activate
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi @utm007. Although that iLogic shortcut snippet will work to easily change material, the actual iProperty's value is ReadOnly. If you want to change the material of a part through VBA, you will most likely have to set a new value to the PartDocument.ActiveMaterial Property. That is a Read/Write property, but it is wanting you to supply an Asset, instead of just a material name. To get the Asset, you need to find the one for that material either within the document's materials (PartDocument.MaterialAssets) or one of the available material libraries (ThisApplication.ActiveMaterialLibrary). There are other ways too, but this is the main way.
Wesley Crihfield
(Not an Autodesk Employee)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I've tried something like this, but can't set the ActiveMaterial I've retieve. Thanks
Dim Doc As Document
Dim pDoc As PartDocument
Dim material As MaterialAsset
Set Doc = ThisApplication.ActiveDocument
Set pDoc = ThisApplication.ActiveDocument
For Each material In Doc.MaterialAssets
If material.IsUsed Then
pDoc.ActiveMatrial = material.DisplayName '(??? I don't know how to set)
Else
End If
Next
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi @utm007. Try this version of your code. This is in VBA, so it would be a VBA macro, and it would go into a regular 'Module'. I'm just using that material name you were trying to use in your first post, so if that material does not exist, it will let you know, then exit the sub routine before trying to set it to the part.
Sub ChangeMaterial()
If ThisApplication.ActiveDocumentType <> kPartDocumentObject Then
Call MsgBox("This VBA macro only works on Parts.", vbCritical, "Wrong Document Type")
Exit Sub
End If
Dim pDoc As PartDocument
Set pDoc = ThisApplication.ActiveDocument
Dim oMaterialDisplayName As String
oMaterialDisplayName = "TestMaterial"
Dim oMyMaterial As MaterialAsset
Dim oMaterial As MaterialAsset
For Each oMaterial In pDoc.MaterialAssets
If oMaterial.DisplayName = oMaterialDisplayName Then
oMyMaterial = oMaterial
End If
Next
If oMyMaterial Is Nothing Then
For Each oMaterial In ThisApplication.ActiveMaterialLibrary
If oMaterial.DisplayName = oMaterialDisplayName Then
oMyMaterial = oMaterial.CopyTo(pDoc)
End If
Next
End If
If oMyMaterial Is Nothing Then
Call MsgBox("The specified material was not found.", vbCritical, "")
Exit Sub
End If
pDoc.ActiveMatrial = oMaterial
End Sub
If you need the iLogic version, you can pretty much just get rid of the 'Set' keywords, and the 'Call' keywords, and it will probably work just fine there.
Wesley Crihfield
(Not an Autodesk Employee)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Thanks,
it give me this Error.
I don't know how to fill in the value.
Thank you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Oh, my mistake. I forgot to add the 'Set' keyword at the start of those two lines of code, because they are setting a value to the variable.
So change this:
oMyMaterial = oMaterial
to this:
Set oMyMaterial = oMaterial
And change this:
oMyMaterial = oMaterial.CopyTo(pDoc)
to this:
Set oMyMaterial = oMaterial.CopyTo(pDoc)
Also...
Change the following line of code:
For Each oMaterial In ThisApplication.ActiveMaterialLibrary
to add the additional step at the end like this:
For Each oMaterial In ThisApplication.ActiveMaterialLibrary.MaterialAssets
I did not test it before posting it, because I knew I did not have a material by that name.
Wesley Crihfield
(Not an Autodesk Employee)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Here is the corrected full code again, since I made so many hasty mistakes the first time. This time I created a material by that name, then tested it out and fixed the code properly so that it worked in my tests.
Sub ChangeMaterial()
If ThisApplication.ActiveDocumentType <> kPartDocumentObject Then
Call MsgBox("This VBA macro only works on Parts.", vbCritical, "Wrong Document Type")
Exit Sub
End If
Dim pDoc As PartDocument
Set pDoc = ThisApplication.ActiveDocument
Dim oMaterialDisplayName As String
oMaterialDisplayName = "TestMaterial"
Dim oMyMaterial As MaterialAsset
Dim oMaterial As MaterialAsset
For Each oMaterial In pDoc.MaterialAssets
If oMaterial.DisplayName = oMaterialDisplayName Then
Set oMyMaterial = oMaterial
End If
Next
If oMyMaterial Is Nothing Then
For Each oMaterial In ThisApplication.ActiveMaterialLibrary.MaterialAssets
If oMaterial.DisplayName = oMaterialDisplayName Then
Set oMyMaterial = oMaterial.CopyTo(pDoc)
End If
Next
End If
If oMyMaterial Is Nothing Then
Call MsgBox("The specified material was not found.", vbCritical, "")
Exit Sub
End If
pDoc.ActiveMaterial = oMyMaterial
End Sub
Wesley Crihfield
(Not an Autodesk Employee)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Had some issues with the code you guys had wrote, my brother does a lot of coding and helped me write this:
Works great!