BOM materials changing

BOM materials changing

Anonymous
Not applicable
1,233 Views
16 Replies
Message 1 of 17

BOM materials changing

Anonymous
Not applicable

Is there any way to change the materials in BOM either preferably in iLogic or in API? it should change material of all the applicable parts in an assembly. eg., Requirement is to replace the all parts with "Steel" to "Aliminium".

0 Likes
1,234 Views
16 Replies
Replies (16)
Message 2 of 17

Anonymous
Not applicable

Here in VBA. This will change material for all parts also in sub-assemblies. Material has to exist in material library.

 

Dim oApp As Inventor.Application
Set oApp = ThisApplication

 

Dim oDoc As AssemblyDocument
Set oDoc = ThisApplication.ActiveDocument

 

Dim strMaterialName As String
strMaterialName = "Aluminium"

 

Dim oOcc As ComponentOccurrence
For Each oOcc In oDoc.ComponentDefinition.Occurrences.AllLeafOccurrences
       oOcc.Definition.Material = oDoc.Materials.Item(strMaterialName)
Next

0 Likes
Message 3 of 17

Anonymous
Not applicable

Hi,

Some Error comes in the Red line  Method 'Material' of object 'PartComponentDefinition' failed

 

Private Sub CommandButton2_Click()
Dim oApp As Inventor.Application
Set oApp = ThisApplication

Dim oDoc As AssemblyDocument
Set oDoc = ThisApplication.ActiveDocument

Dim strMaterialName As String
strMaterialName = "1.4307"

Dim oOcc As ComponentOccurrence
For Each oOcc In oDoc.ComponentDefinition.Occurrences.AllLeafOccurrences
       oOcc.Definition.Material = oDoc.Materials.Item(strMaterialName)
Next

End Sub

 

 

0 Likes
Message 4 of 17

Anonymous
Not applicable

Hi,

 

Is there any  alternate preferably in iLogic for changing the materials in an iassembly. It shouldn't replace the materials for all the parts except the applicable parts. For eg, It should replace "Aluminium" in place of "Steel". It shouldn't change the other parts with different materials like "Rubber" etc.,

0 Likes
Message 5 of 17

Anonymous
Not applicable
Hi,

You can put there condition if material name is "steel" then change material.

To error you got: Do you have material named 1.4307 on your material library?

You can translate vba to ilogic quite easy, just take "set" off from lines. Also "on error resume next is not supported in ilogic, but you can use try-catch-method instead.

It would be also good to put condition that checks that part is not content center component. Through api material can be chaged to library components also...even though it should not be possible.

I haven't used iassemblies very much so I can't answer to this issue.
Message 6 of 17

Anonymous
Not applicable

Hi,

Thanks. As I'm new to this iLogic I don't know how write the condition. this code simply puts the material in all occurrences without caring about its previous material. My requirement is to have a conditional replacement like "Steel" in place of "Aluminium".

0 Likes
Message 7 of 17

Anonymous
Not applicable

Hi,

 

Here in VBA. This changes material for each component if material is "Aluminium" to "Steel". You can modify your code from this. This also skips standard content center components.Material will be changed for custom content center components.


Dim oApp As Inventor.Application
Set oApp = ThisApplication

Dim oDoc As AssemblyDocument
Set oDoc = ThisApplication.ActiveDocument

 

Dim strMaterialNameToChange As String
strMaterialNameToChange = "Aluminium"

 

Dim strNewMaterialName As String
strNewMaterialName = "Steel"

 

Dim oOcc As ComponentOccurrence
For Each oOcc In oDoc.ComponentDefinition.Occurrences.AllLeafOccurrences

       If oOcc.Definition.Material.name = strMaterialNameToChange And oOcc.Definition.IsContentMember = False Then
             oOcc.Definition.Material = oDoc.Materials.Item(strNewMaterialName)
       End If

Next

 

Message 8 of 17

Anonymous
Not applicable

Hi,

 

There is a problem in this red line.. It simply closes the Inventor without changing any material.

 

Dim oApp As Inventor.Application
Set oApp = ThisApplication

Dim oDoc As AssemblyDocument
Set oDoc = ThisApplication.ActiveDocument

 

Dim strMaterialNameToChange As String
strMaterialNameToChange = "Aluminium"

 

Dim strNewMaterialName As String
strNewMaterialName = "Steel"

 

Dim oOcc As ComponentOccurrence
For Each oOcc In oDoc.ComponentDefinition.Occurrences.AllLeafOccurr​ences

       If oOcc.Definition.Material.name = strMaterialNameToChange And oOcc.Definition.IsContentMember = False Then
             oOcc.Definition.Material = oDoc.Materials.Item(strNewMaterialName)
       End If

Next

 

I don't know the exact issue. but, Do I need to include any object library?

 

Sukpal

0 Likes
Message 9 of 17

Anonymous
Not applicable

Hi,

 

No need for any extra object library. 

I have used this kind of loop in many cases and never had problem like that. Is your assembly weldment assembly? Or did you try with other assemblies just in case that there's something wrong with iam you tried??

 

Message 10 of 17

Anonymous
Not applicable

Hi,

 

Excellent code. It's working in other assembly. Many thanks..

 

One more help needed.

 

Can it be converted in to iLogic?

 

I tried the same code(Removing 'Set' Property). but here it gives some error like "End of Statement expected". It would be much better if it works in iLogic as well.

 

Sukpal

 

 

0 Likes
Message 11 of 17

Anonymous
Not applicable

The code below seems to work fine with an assembly file.

 

Sub Main()

Dim oApp As Inventor.Application
oApp = ThisApplication

If ThisApplication.ActiveDocumentType <> kAssemblyDocumentObject Then
	MessageBox.Show("ERROR - Active Document is not an assembly document!","iLogic Error")
	Exit Sub
End If

Dim oDoc As AssemblyDocument
oDoc = ThisApplication.ActiveDocument
MessageBox.Show("ASSEMBLY DOCUMENT!","iLogic Debug")

Dim strMaterialNameToChange As String
strMaterialNameToChange = "Aluminium"

Dim strNewMaterialName As String
strNewMaterialName = "Steel"

Dim oOcc As ComponentOccurrence
For Each oOcc In oDoc.ComponentDefinition.Occurrences.AllLeafOccurrences
   If oOcc.Definition.Material.Name = strMaterialNameToChange And oOcc.Definition.IsContentMember = False Then
         oOcc.Definition.Material = oDoc.Materials.Item(strNewMaterialName)
   End If
Next

End Sub

 

 

0 Likes
Message 12 of 17

Anonymous
Not applicable

Sukpal,

 

From iLogic Rule editor: Options tab-> put tab in Straight VB Code

Then it works.

0 Likes
Message 13 of 17

Anonymous
Not applicable

Hi,

 

Unfortunately it's not working for me. gives the same error in iLogic. "End of statement expected" in the below line. 


For Each oOcc In oDoc.ComponentDefinition.Occurrences.AllLeafOccurr​ences

 

BR

Sukpal

0 Likes
Message 14 of 17

Anonymous
Not applicable

Hi Asikainen,

 

For me it's not working unfortunately. It gives error in all lines now. Smiley Embarassed previously I had only one error in the below line

 

"For Each oOcc In oDoc.ComponentDefinition.Occurrences.AllLeafOccurrences" that End of statement expected.

 

BR

Sukpal

0 Likes
Message 15 of 17

Anonymous
Not applicable
What version of Inventor? Inventor 2015 or ?
0 Likes
Message 16 of 17

Anonymous
Not applicable

Hi,

 

Yes dear Yosso. Inventor 2015 only. But I think I may need to include some object library. Please give some solution.

 

BR

Sukpal

0 Likes
Message 17 of 17

Anonymous
Not applicable

The iLogic code seems to work w/o issues.

 

http://adndevblog.typepad.com/manufacturing/2013/07/inventor-2014-api-set-part-material.html

 

Sub Main()

Dim i As Integer
Dim oApp As Inventor.Application
oApp = ThisApplication

If ThisApplication.ActiveDocumentType <> kAssemblyDocumentObject Then
    MessageBox.Show("Active document is NOT an assembly document!","DEBUG ME")
    Exit Sub
End If

Dim oDoc As AssemblyDocument
oDoc = ThisApplication.ActiveDocument

Dim strMaterialNameToChange As String
strMaterialNameToChange = "Aluminum 6061"

Dim strNewMaterialName As String
strNewMaterialName =  "Steel, Galvanized" '"Aluminum 6061"

Dim oMat As Asset

' MessageBox.Show("ASSEMBLY DOCUMENT!","iLogic Debug")

Dim oOcc As ComponentOccurrence
Dim oPartDoc As PartDocument
Dim newMat As Asset

For Each oOcc In oDoc.ComponentDefinition.Occurrences.AllLeafOccurrences
    oPartDoc = oOcc.Definition.Document
    
   For i = 1 To oPartDoc.Assets.Count
       If oPartDoc.Assets.Item(i).DisplayName = strNewMaterialName Then
           oMat = oPartDoc.Assets.Item(i)
           bMatFound = True
           ' MessageBox.Show("MATERIAL FOUND!","iLogic Debug")
           Exit For
       End If
   Next i
    
    If oPartDoc.ActiveMaterial.DisplayName = strMaterialNameToChange And oOcc.Definition.IsContentMember = False Then
        ' MessageBox.Show("IN if/then","iLogic Debug")
        oPartDoc.ActiveMaterial = oMat
    End If
    
Next

Call oDoc.BrowserPanes.ActivePane.TopNode.DoSelect
 
End Sub
0 Likes