Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Problem with component replace

3 REPLIES 3
Reply
Message 1 of 4
SharkDesign
245 Views, 3 Replies

Problem with component replace

 

The code works fine on it's own. I basically change a multi-value parameter between right and drive and left hand drive. 

The problem I have is when I then try to change a different parameter. 

 

I begin by defining the parts in the model I want to swap and then what I want to bring in from another folder.

 

'Define current parts and what to switch with
If Drive_Side = "Right Hand Drive" Then
RHSCurrentPart = "SSC - RH Idle Sub:1" 
LHSCurrentPart = "SSC - LH Drive Sub:1"
RHSSwitchPart = "SSC - RH Drive Sub.iam"
LHSSwitchPart = "SSC - LH Idle Sub.iam"
Else If Drive_Side = "Left Hand Drive" Then RHSCurrentPart = "SSC - RH Drive Sub:1" LHSCurrentPart = "SSC - LH Idle Sub:1" RHSSwitchPart = "SSC - RH Idle Sub.iam" LHSSwitchPart = "SSC - LH Drive Sub.iam" End If 'Replace subs Component.Replace(RHSCurrentPart, SubsLocation & RHSSwitchPart, True) Component.Replace(LHSCurrentPart, SubsLocation & LHSSwitchPart, True)

Everything works fine, but then I change another parameter somewhere else and it reruns the above code.

 

Now it can't find the RHSCurrentPart, obviously because it's not there anymore because I just switched it out, and I get an error. How do I get it to work properly?

  Expert Elite
  Inventor Certified Professional
3 REPLIES 3
Message 2 of 4
SharkDesign
in reply to: SharkDesign

I put a try catch in which worked at first and then randomly stopped working. 

Basically if the part is in the browser, run the code after the try, if it doesn't exist, the catch, it skips the block of code.

 

If Drive_Side = "Right Hand Drive" Then
Try
Component.IsActive("SSC 02 03 00:1") = True
Catch
Goto skip
End Try
RHSCurrentPart = "SSC 02 03 00:1"
LHSCurrentPart = "SSC 02 02 00:1"
CoverCurrentPart = "SSC 02 00 05:1"
RHSSwitchPart = "SSC 02 04 00.iam"
LHSSwitchPart = "SSC 02 01 00.iam"
CoverSwitchPart = "SSC 02 00 03.ipt"

'Replace side subs
Component.Replace(RHSCurrentPart, SubsLocation & RHSSwitchPart, True)
Component.Replace(LHSCurrentPart, SubsLocation & LHSSwitchPart, True)
Component.Replace(CoverCurrentPart, StretchLocation & CoverSwitchPart, True)

skip:
  Expert Elite
  Inventor Certified Professional
Message 3 of 4
SharkDesign
in reply to: SharkDesign

I ended up putting all my code inside the try, is that the best way to do it?

 

'Define current parts and what to switch with
If Drive_Side = "Right Hand Drive" Then
Try
RHSCurrentPart = "SSC 02 03 00:1"
LHSCurrentPart = "SSC 02 02 00:1"
CoverCurrentPart = "SSC 02 00 05:1"
RHSSwitchPart = "SSC 02 04 00.iam"
LHSSwitchPart = "SSC 02 01 00.iam"
CoverSwitchPart = "SSC 02 00 03.ipt"

'Replace side subs
Component.Replace(RHSCurrentPart, SubsLocation & RHSSwitchPart, True)
Component.Replace(LHSCurrentPart, SubsLocation & LHSSwitchPart, True)
Component.Replace(CoverCurrentPart, StretchLocation & CoverSwitchPart, True)
InventorVb.DocumentUpdate()
Catch
Goto skip
End Try
  Expert Elite
  Inventor Certified Professional
Message 4 of 4

@SharkDesign,

 

I feel that best way would be checking occurrence before replacing components. Try below iLogic code.

Dim oDoc As AssemblyDocument
oDoc = ThisApplication.ActiveDocument 

Dim oDef As AssemblyComponentDefinition 
oDef = oDoc.ComponentDefinition 

Dim occ As ComponentOccurrence 

'Define current parts and what to switch with
If Drive_Side = "Right Hand Drive" Then
 
	RHSCurrentPart = "SSC 02 03 00:1"
	LHSCurrentPart = "SSC 02 02 00:1"
	CoverCurrentPart = "SSC 02 00 05:1"
	RHSSwitchPart = "SSC 02 04 00.iam"
	LHSSwitchPart = "SSC 02 01 00.iam"
	CoverSwitchPart = "SSC 02 00 03.ipt"
	
	For Each occ In oDef.Occurrences 		
		If occ.Name = RHSCurrentPart Then
			'Replace side subs
			Component.Replace(RHSCurrentPart, SubsLocation & RHSSwitchPart, True)		
		End If	
	Next

	For Each occ In oDef.Occurrences 
		If occ.Name = LHSCurrentPart Then
			Component.Replace(LHSCurrentPart, SubsLocation & LHSSwitchPart, True)
		End If 
	Next
	
	For Each occ In oDef.Occurrences 
		If occ.Name = CoverCurrentPart Then
			Component.Replace(CoverCurrentPart, StretchLocation & CoverSwitchPart, True)
		End If 		
	Next 
	
	InventorVb.DocumentUpdate()

End If 

Please feel free to contact if there are any queries.

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Technology Administrators


Autodesk Design & Make Report