Material CHANGE in iLogic and VBA

Material CHANGE in iLogic and VBA

utm007
Participant Participant
1,257 Views
8 Replies
Message 1 of 9

Material CHANGE in iLogic and VBA

utm007
Participant
Participant

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.

 

 

0 Likes
Accepted solutions (1)
1,258 Views
8 Replies
Replies (8)
Message 2 of 9

dalton98
Collaborator
Collaborator
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
Message 3 of 9

WCrihfield
Mentor
Mentor

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

EESignature

(Not an Autodesk Employee)

Message 4 of 9

utm007
Participant
Participant

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

 

0 Likes
Message 5 of 9

WCrihfield
Mentor
Mentor

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

EESignature

(Not an Autodesk Employee)

Message 6 of 9

utm007
Participant
Participant

Thanks,

it give me this Error.

I don't know how to fill in the value.

 

utm007_0-1653576691674.png

Thank you

0 Likes
Message 7 of 9

WCrihfield
Mentor
Mentor

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

EESignature

(Not an Autodesk Employee)

Message 8 of 9

WCrihfield
Mentor
Mentor
Accepted solution

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

EESignature

(Not an Autodesk Employee)

Message 9 of 9

Luke.NashWU9MY
Participant
Participant

Had some issues with the code you guys had wrote, my brother does a lot of coding and helped me write this:

Works great!

 

Dim assetLib As AssetLibrary  = ThisApplication.AssetLibraries.Item("Inventor Material Library")
ThisApplication.ActiveMaterialLibrary = assetLib
mat = InputBox("Material", "Change Part Material")
userChoice = InputRadioBox("Defined the scope", "This Document", "All Open Documents", True, Title := "Defined the scope")
 If userChoice Then
 
iProperties.Material = mat
Else
 
For Each pDoc As PartDocument In ThisApplication.Documents.VisibleDocuments
 
If ThisApplication.ActiveDocumentType <> kPartDocumentObject Then
Call MsgBox("This VBA macro only works on Parts.", vbCritical, "Wrong Document Type")
Continue For
 
End If
 
Dim MaterialName As String
MaterialName = mat
 
Try
 
    Dim localMaterial As MaterialAsset = pDoc.MaterialAssets.Item(MaterialName)
 
Catch
 
    Dim libMaterial As MaterialAsset = assetLib.MaterialAssets.Item(MaterialName)
 
    libMaterial.CopyTo(pDoc)
 
End Try
 
Try
 
    pDoc.ActiveMaterial = pDoc.MaterialAssets.Item(MaterialName)
 
Catch ex As Exception
 
    Call MsgBox("Failed to set material, Error:" & ex.message, vbCritical, "Error")
 
End Try
 
Next
 
End If

 

0 Likes