Replace content center parts with other material

Replace content center parts with other material

Anonymous
Not applicable
443 Views
3 Replies
Message 1 of 4

Replace content center parts with other material

Anonymous
Not applicable

I would like to extend the functionality of a rule I'm currently using to change the material of all parts within an assembly.

The multivalue list of materials consists of Stainless Steel 304, Stainless Steel 316 & Steel, Mild 

 

 This is my rule:

oDoc = ThisApplication.ActiveDocument

If oDoc.DocumentType <> Inventor.DocumentTypeEnum.kAssemblyDocumentObject Then
   MessageBox.Show("This rule can only be run in an Assembly file - exiting rule...", "Rule Error")
   Return
End If

Dim oAsmDef As AssemblyComponentDefinition
oAsmDef = oDoc.ComponentDefinition

oBOM = oAsmDef.BOM

' Make sure that the PartOnly view is enabled.
oBOM.PartsOnlyViewEnabled = True

oBOMRows = oBOM.BOMViews.Item("Parts Only").BOMRows
Dim oMaterial As Material Try oMaterial = oDoc.Materials.Item(MATERIAL) Catch MsgBox("Unable to find material " & MATERIAL) Return End Try values = MultiValue.List("MATERIAL") For Each oRow As BOMRow In oBOMRows oRowCompDef = oRow.ComponentDefinitions.Item(1) If oRowCompDef.isContentMember Then Continue For If values.Contains(oRowCompDef.Material.Name) Then Dim oPartCompDef As PartComponentDefinition = oRowCompDef oPartCompDef.Material = oMaterial End IF Next iLogicVb.UpdateWhenDone = True

 

I'd like to modify it to replace any content center fasteners with their counterpart of different material.

 

For example, if I change from Stainless Steel 304 to Stainless Steel 316, I would like to replace all FSL HEX BOLT SS304 M8 x 25 with FSL HEX BOLT SS316 M8 x 25

 

Can anyone tell me if this is possible?

0 Likes
444 Views
3 Replies
Replies (3)
Message 2 of 4

MechMachineMan
Advisor
Advisor

Sure. The easier way, from my experience, is to just create a list of the 2 relative file names and have it replace the old with the new when it comes across one. Trying to work with the tables of the content center directly isn't much fun and translating data between the 2 tables can get interesting.


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
Message 3 of 4

Anonymous
Not applicable

@MechMachineMan thanks for sharing your experience.

 

this is what I came up with so far:

 

Sub Main ReplaceCCparts() 
    ' Get the active assembly. 
    Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument
	
	If MATERIAL = "Stainless Steel 304" Then
		matcode = "SS304"
	ElseIf MATERIAL = "Stainless Steel 316" Then
		matcode = "SS316"
	ElseIf MATERIAL = "Steel, Mild" Then
		matcode = "ZP"
	End If
	
	'msgbox(matcode)

    ' Call the function that does the recursion. 
    Call TraverseAssembly(oAsmDoc.ComponentDefinition.Occurrences, 1, matcode) 
End Sub 

Private Sub TraverseAssembly(Occurrences As ComponentOccurrences,Level As Integer,matcode As String) 
	
    ' Iterate through all of the occurrence in this collection.  This 
    ' represents the occurrences at the top level of an assembly. 
    Dim oOcc As ComponentOccurrence 
    For Each oOcc In Occurrences 
		
		Dim filename As String
		
		Try
		
		If oOcc.Definition.Document.DocumentType = kPartDocumentObject Then
			'MsgBox("checked document type")
			If oOcc.Definition.Document.ComponentDefinition.IsContentMember Then
				'MsgBox("checked if content center")
			
				Dim oDef As PartComponentDefinition = oOcc.Definition.Document.ComponentDefinition
		
				filename = oDef.Document.FullFileName
		
				'msgbox(filename)
		
				If filename.Contains("SS304") Or filename.Contains("SS316") Or filename.Contains("ZP") Then
					replacement_file = filename.Replace("SS304", matcode).Replace("SS316", matcode).Replace("ZP", matcode)
					Try
						oOcc.Replace(replacement_file, False)
					Catch
						MsgBox("Unable to replace " & filename & vbLf &"With " & replacement_file)
					End Try
					'MsgBox(filename)
				End If
			End If
		End If

        ' Check to see if this occurrence represents a subassembly 
        ' and recursively call this function to traverse through it. 
        If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then 
            Call TraverseAssembly(oOcc.SubOccurrences, Level + 1, matcode) 
        End If 
		
		Catch ex As Exception
			MsgBox(ex.Message & vbLf & filename)
		End Try
    Next 
End Sub

 

It works for the most part, but every now and then I'll catch an error when using oOcc.Replace, saying:

xml document must have a top level element

 

Ever came across that before?

0 Likes
Message 4 of 4

Anonymous
Not applicable

+ it doesn't replace files that are part of a Bolted Connection Smiley Sad

0 Likes