Deleting Materials

Deleting Materials

Anonymous
Not applicable
984 Views
5 Replies
Message 1 of 6

Deleting Materials

Anonymous
Not applicable

I have the following code which will eventually be used to replace the materials in an old template with those from a new template.

 

When I try and run it, I get an error at the line 'OldDocMatls.Item(1).Delete' and an error saying 

'Delete method of object material failed'

 

Can someone please explain why?  I have tried several methods to iterate over the materials and delete them but they all come back with this error.  There are 23 materials so it is not as if I am trying to delete something that is not actually there!!

 

Const OldDocLoc As String = "C:\Standard (mm).ipt"


Sub TransferMaterials()
    Dim OldDocObj As Inventor.Document   

Dim OldDocMatls As Inventor.Materials       

'open old document   

Set OldDocObj = ThisApplication.Documents.Open(OldDocLoc, False)
    'get list of materials in old document   

Set OldDocMatls = OldDocObj.Materials       

'clean out old materials   

Do Until (OldDocMatls.Count = 0)       

OldDocMatls.Item(1).Delete   

Loop
End Sub

 

Also whilst I am here, is there a way to copy a collection (ie the material collection) to another without having to iterate through both and replacing each element one by one?

 

Thanks

 

0 Likes
985 Views
5 Replies
Replies (5)
Message 2 of 6

YuhanZhang
Autodesk
Autodesk

Do you want to delete all the materials in a document? Or you just want to purge the un-used materials in a document?

 

A material can be stored in both library and local(document), while the Document.Materials collection will access both the library and local materials, and what you can delete is the local materials, so you need to check if the material is local. Use (Material.StyleLocation =kLocalStyleLocation or Material.StyleLocation = kBothStyleLocation) to determine that the material is local. And even for the local materials, what you can delete are the un-used materials, so you also have to check the Material.InUse before you can delete them. If you want to delete the in-use materials, you can stop using them onto any objects in the document and then delete them.

 

If you are going to delete any material in the library, you can do this with a tool(Start->Programs->Autodesk->Autodesk Inventor 20XX->Tools->Style Library Manager).

 

You can also use the tool above to copy the materials collection from one library to another(or new one).

 

Hope it helps.



If this solves the problem please click ACCEPT SOLUTION so other people can find it easily.



Rocky Zhang
Inventor API PD
Manufacturing Solutions
Autodesk, Inc.

0 Likes
Message 3 of 6

freesbee
Collaborator
Collaborator

Rocky, I'm exactly on this topic: I want to purge all unused materials and appearances from one document.

 

I wrote a small VBA purging all unused local materials and appearances.

It works quite fine, but in some sporadic cases the method .AppearanceAssets.Item(i).Delete fails, even if the AppearanceAssets.Item(i) is NOT used.

I tried to debug it at my best capacity, but I couldn't figure out what is going wrong Smiley Mad

Do you have an idea of what I am missing here?

 

Here is the code, and also one ipt file that makes the routine fail (Inv2015 SP2u6)

 

Public Sub PurgeMaterials()
'this macro assumes that a Part document is opened in Inventor
Dim oDoc As Inventor.PartDocument Set oDoc = ThisApplication.ActiveDocument Dim i As Integer i = 1 While i <= oDoc.MaterialAssets.Count If oDoc.MaterialAssets.Item(i).IsUsed = False Then oDoc.MaterialAssets.Item(i).Delete Else i = i + 1 End If Wend i = 1 While i <= oDoc.AppearanceAssets.Count If oDoc.AppearanceAssets.Item(i).IsUsed = False Then oDoc.AppearanceAssets.Item(i).Delete Else i = i + 1 End If Wend End Sub

 

Massimo Frison
CAD R&D // PDM Admin · Hekuma GmbH
0 Likes
Message 4 of 6

YuhanZhang
Autodesk
Autodesk

Hi Massimo,

 

I can reproduce the API problem with the specific data, a workaround for this issue is to use the legacy RenderStyle to delete the unused appearance asset. Below is the modified VBA code:

 

Public Sub PurgeMaterials()
'this macro assumes that a Part document is opened in Inventor

Dim oDoc As Inventor.PartDocument
Set oDoc = ThisApplication.ActiveDocument


Dim i As Integer

i = 1
While i <= oDoc.MaterialAssets.Count
  If oDoc.MaterialAssets.Item(i).IsUsed = False Then
    oDoc.MaterialAssets.Item(i).Delete
  Else
    i = i + 1
  End If
Wend

Dim oRenderStyle As RenderStyle, sName As String
On Error Resume Next
i = 1
While i <= oDoc.AppearanceAssets.Count
  If oDoc.AppearanceAssets.Item(i).IsUsed = False Then
    sName = oDoc.AppearanceAssets.Item(i).DisplayName
    oDoc.AppearanceAssets.Item(i).Delete
    If Err <> 0 Then
        Set oRenderStyle = oDoc.RenderStyles.Item(sName)
        oRenderStyle.Delete
        Err.Clear
    End If
  Else
    i = i + 1
  End If
Wend

End Sub

 

 

 

Please let me if it helps.



If this solves the problem please click ACCEPT SOLUTION so other people can find it easily.



Rocky Zhang
Inventor API PD
Manufacturing Solutions
Autodesk, Inc.

Message 5 of 6

freesbee
Collaborator
Collaborator

yes, this is working perfectly.

Also the ported version in VB.net with the Try/Catch error handling works fine.

Thanks a lot!!

Massimo Frison
CAD R&D // PDM Admin · Hekuma GmbH
0 Likes
Message 6 of 6

adam.nagy
Autodesk Support
Autodesk Support

In the meantime I logged the issue with the Asset API in our database: #186918



Adam Nagy
Autodesk Platform Services
0 Likes