Replace obsolete component

Replace obsolete component

mcgyvr
Consultant Consultant
807 Views
6 Replies
Message 1 of 7

Replace obsolete component

mcgyvr
Consultant
Consultant

Looking for some help on creating some ilogic code to replace obsolete components in assemblies when the assembly is opened and it contains that part.

Currently I've just add this simple code to each component that is obsolete so when I open an assembly it "reminds" me to manually replace it

MessageBox.Show ("0200058003 is Obsolete" & vbLf & "Please replace with 0230061111", "OBSOLETE ALERT", MessageBoxButtons.OKCancel, MessageBoxIcon.Error)

 

BUT.. Now I'd like to see if there is a way so that when an assembly is opened with an obsolete part that it will automatically replace it.

Tried this but it doesn't work (note this code is located in the obsolete part file not in the assembly.. 

MessageBox.Show("0200058003.ipt is obsolete", "Obsolete")

'Run this only if this is an assembly.
doc = ThisDoc.ModelDocument
If doc.DocumentType = kAssemblyDocumentObject Then
Component.Replace("0200058003", "0230061111.ipt", True)
MessageBox.Show("0200058003 has been replaced", "Obsolete Replaced")
End If

 

 

 



-------------------------------------------------------------------------------------------
Inventor 2023 - Dell Precision 5570

Did you find this reply helpful ? If so please use the Accept Solution button below.
Maybe buy me a beer through Venmo @mcgyvr1269
0 Likes
Accepted solutions (1)
808 Views
6 Replies
Replies (6)
Message 2 of 7

Vladimir.Ananyev
Alumni
Alumni

If your rule is located in the obsolete part file then ThisDoc.ModelDocument returns the reference to the PartDocument, not AssemblyDocument.  This explains why your Component.Replace method is never called.  You should run this rule in the context of an assembly document. 

 

BTW ThisDoc.ModelDocument property normally assumes that ThisDoc is a drawing document.  Then it returns the model document (part or assembly) that is shown in a drawing document.  In your case it is better to use ThisDoc.Document property.


Vladimir Ananyev
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 3 of 7

mcgyvr
Consultant
Consultant

(ilogic newbie alert Smiley Very Happy )

So I got this code to work but would just like comments,improvement suggestions, etc.. on if there is a better/cleaner way (as again I'm new to ilogic)

But I set it up so that when I have an obsolete part I will add a new custom iprop to it and the value will be the replacement part.

Then this rule will be an external rule that I can run from any assembly. 

It looks for any parts that have that custom iprop and then grabs the occurrence name and performs the componet replace function..

 

Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

'Iterate through all of the occurrences
Dim oOccurrence As ComponentOccurrence
For Each oOccurrence In oAsmCompDef.Occurrences
Try
   Dim oName As String 'create new string variable of oName
    oName = oOccurrence.Name 'get name of each occurrence
    Replacement = iProperties.Value(oOccurrence.Name, "Custom", "Obsolete_Replacement") 'grab the replacement part
    Component.Replace(oName,Replacement, True) 'replace the obsolete part with the replacement part
	
Catch 
End Try
Next

I feel like there should be a better (cleaner) way instead of running into the catch for any parts that don't have that custom iprop.

I'd like to incorporate something that "if" the custom iprop is found.. "then" do the component replace.. And maybe thats what I have already but am not sure if thats the best way to handle that statement.

Thanks for any help/comments/improvements.



-------------------------------------------------------------------------------------------
Inventor 2023 - Dell Precision 5570

Did you find this reply helpful ? If so please use the Accept Solution button below.
Maybe buy me a beer through Venmo @mcgyvr1269
0 Likes
Message 4 of 7

Vladimir.Ananyev
Alumni
Alumni
Accepted solution

You may consider the following code sample as a start point: 
 

'external rule must always check the type of the active document
If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then
	MsgBox("This rule work with assemblies only")
	Exit Sub
End If

'top-level assembly document
Dim oAsmDoc as AssemblyDocument = ThisApplication.ActiveDocument 
Dim oAsmDef As AssemblyComponentDefinition = ThisApplication.ActiveDocument.ComponentDefinition

Dim n As Integer = 0  'counter

'Iterate through all of the occurrences at the top level
For Each oOcc As ComponentOccurrence In oAsmDef.Occurrences

	'get name of this occurrence
	Dim Name As String  = oOcc.Name 	
	Try
		'read custom iProperty value
    	        Dim Replacement As String  = iProperties.Value(oOcc.Name, "Custom", "Obsolete_Replacement") 
		Try
			'replace the obsolete part with the replacement part
			Component.Replace(oName, Replacement, True) 
			n += 1 'increment counter
		Catch
			'replacement failed
			MsgBox(oOcc.Name & vbNewLine & "replacement failed for some reason")
		End Try
	
	Catch 
		'custom property "Obsolete_Replacement" was not found
		'replacement not required
	End Try
Next	

MsgBox("DONE:  " & vbNewLine & n & " occurrences replaced")

This code will do a lot of unnecessary extra work if you have many copied occurrences. So it obviously could be enhanced, but this requires more programming using the Inventor API.

Cheers,


Vladimir Ananyev
Developer Technical Services
Autodesk Developer Network

Message 5 of 7

mcgyvr
Consultant
Consultant

Thanks!!!

Thats exactly the kind of "enhancement suggestions" I was looking for.. 

 



-------------------------------------------------------------------------------------------
Inventor 2023 - Dell Precision 5570

Did you find this reply helpful ? If so please use the Accept Solution button below.
Maybe buy me a beer through Venmo @mcgyvr1269
0 Likes
Message 6 of 7

mcgyvr
Consultant
Consultant

@Vladimir.Ananyev wrote:

You may consider the following code sample as a start point: 
 

'external rule must always check the type of the active document
If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then
	MsgBox("This rule work with assemblies only")
	Exit Sub
End If

'top-level assembly document
Dim oAsmDoc as AssemblyDocument = ThisApplication.ActiveDocument 
Dim oAsmDef As AssemblyComponentDefinition = ThisApplication.ActiveDocument.ComponentDefinition

Dim n As Integer = 0  'counter

'Iterate through all of the occurrences at the top level
For Each oOcc As ComponentOccurrence In oAsmDef.Occurrences

	'get name of this occurrence
	Dim Name As String  = oOcc.Name 	
	Try
		'read custom iProperty value
    	        Dim Replacement As String  = iProperties.Value(oOcc.Name, "Custom", "Obsolete_Replacement") 
		Try
			'replace the obsolete part with the replacement part
			Component.Replace(oName, Replacement, True) 
			n += 1 'increment counter
		Catch
			'replacement failed
			MsgBox(oOcc.Name & vbNewLine & "replacement failed for some reason")
		End Try
	
	Catch 
		'custom property "Obsolete_Replacement" was not found
		'replacement not required
	End Try
Next	

MsgBox("DONE:  " & vbNewLine & n & " occurrences replaced")

This code will do a lot of unnecessary extra work if you have many copied occurrences. So it obviously could be enhanced, but this requires more programming using the Inventor API.

Cheers,


One small typo

Dim Name As String  = oOcc.Name

Should be

Dim oName As String  = oOcc.Name

But it works great after thats fixed



-------------------------------------------------------------------------------------------
Inventor 2023 - Dell Precision 5570

Did you find this reply helpful ? If so please use the Accept Solution button below.
Maybe buy me a beer through Venmo @mcgyvr1269
0 Likes
Message 7 of 7

Vladimir.Ananyev
Alumni
Alumni

Thank you for this correction!  Smiley Happy 

cheers,


Vladimir Ananyev
Developer Technical Services
Autodesk Developer Network

0 Likes