iLogic Replacing Components With Multiple Different Components In Patterns

iLogic Replacing Components With Multiple Different Components In Patterns

Nauld1
Enthusiast Enthusiast
613 Views
3 Replies
Message 1 of 4

iLogic Replacing Components With Multiple Different Components In Patterns

Nauld1
Enthusiast
Enthusiast

So I wrote the following program to replace a component in a greenhouse. It works fine when I make the greenhouse bigger. But when the greenhouse is made smaller it replaces the wrong component. Can someone please help with this?

My_Variable = ThisDoc.Document.ComponentDefinition.Occurrences.Item(12*(1+Number_of_Bays)+((Nominal_Length/12 ft)-5)*(1+Number_of_Bays)*2+1).Name

Select Case Width
Case 18 ft
If Crosstie_Depth_10 = True Then
Component.Replace(My_Variable, "Y:\Workshare\Inventor Standards\Gutter Connect\Crossties\ML3010_18ft_10(3x3)_Crosstie.iam", True)
End If
If Crosstie_Depth_10 = False Then
Component.Replace(My_Variable, "Y:\Workshare\Inventor Standards\Gutter Connect\Crossties\ML3010_18ft_14(3x3)_Crosstie.iam", True)
End If
Case 21 ft
If Crosstie_Depth_10 = True Then
Component.Replace(My_Variable, "Y:\Workshare\Inventor Standards\Gutter Connect\Crossties\ML3010_21ft_10(3x3)_Crosstie.iam", True)
End If
If Crosstie_Depth_10 = False Then
Component.Replace(My_Variable, "Y:\Workshare\Inventor Standards\Gutter Connect\Crossties\ML3010_21ft_14(3x3)_Crosstie.iam", True)
End If
Case 24 ft
If Crosstie_Depth_10 = True Then
Component.Replace(My_Variable, "Y:\Workshare\Inventor Standards\Gutter Connect\Crossties\ML3010_24ft_10(3x3)_Crosstie.iam", True)
End If
If Crosstie_Depth_10 = False Then
Component.Replace(My_Variable, "Y:\Workshare\Inventor Standards\Gutter Connect\Crossties\ML3010_24ft_14(3x3)_Crosstie.iam", True)
End If
Case 27.5 ft
If Crosstie_Depth_10 = True Then
Component.Replace(My_Variable, "Y:\Workshare\Inventor Standards\Gutter Connect\Crossties\ML3010_27ft 6in_10(3x3)_Crosstie.iam", True)
End If
If Crosstie_Depth_10 = False Then
Component.Replace(My_Variable, "Y:\Workshare\Inventor Standards\Gutter Connect\Crossties\ML3010_27ft 6in_14(3x3)_Crosstie.iam", True)
End If
End Select

InventorVb.DocumentUpdate()

 

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

Owner2229
Advisor
Advisor

Hi, which component is wrong? The replaced one or the replacing one?

E.g.: Is the right component replaced by wrong one, or is the wrong component replaced by the right one?

 

This code below doesn't provide any fixes to your current code (it's the same). It's just rewriten it more readable form.

 

Sub Main()
' If you have any code in front of what you've posted, than it belongs here Dim occNumb As String = 12*(1+Number_of_Bays)+((Nominal_Length/12 ft)-5)*(1+Number_of_Bays)*2+1 Dim My_Variable As String = ThisDoc.Document.ComponentDefinition.Occurrences.Item(occNumb).Name Select Case Width Case 18 ft If Crosstie_Depth_10 = True Then DoReplace(My_Variable, "ML3010_18ft_10(3x3)_Crosstie.iam") Else DoReplace(My_Variable, "ML3010_18ft_14(3x3)_Crosstie.iam") End If Case 21 ft If Crosstie_Depth_10 = True Then DoReplace(My_Variable, "ML3010_21ft_10(3x3)_Crosstie.iam") Else DoReplace(My_Variable, "ML3010_21ft_14(3x3)_Crosstie.iam") End If Case 24 ft If Crosstie_Depth_10 = True Then DoReplace(My_Variable, "ML3010_24ft_10(3x3)_Crosstie.iam") Else DoReplace(My_Variable, "ML3010_24ft_14(3x3)_Crosstie.iam") End If Case 27.5 ft If Crosstie_Depth_10 = True Then DoReplace(My_Variable, "ML3010_27ft 6in_10(3x3)_Crosstie.iam") Else DoReplace(My_Variable, "ML3010_27ft 6in_14(3x3)_Crosstie.iam") End If End Select InventorVb.DocumentUpdate() End Sub Sub DoReplace(oReplaced As String, oReplacing As String) Dim oPath As String = "Y:\Workshare\Inventor Standards\Gutter Connect\Crossties\" Component.Replace(oReplaced, oPath & oReplacing, True) End Sub
Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 3 of 4

Nauld1
Enthusiast
Enthusiast

So the right piece is going in but it's replacing the wrong part. My posts are being replaced with crossties instead of my crossties with crossties.

0 Likes
Message 4 of 4

Owner2229
Advisor
Advisor

So the problem is in the "search" part of your code. You're picking the occurrence by it's item number, which might be wrong in many levels.

E.g. When you remove and re-add the part/assy.

 

I'd suggest you to search for it by it's name, something like this:

 

Sub Main()
' If you have any code in front of what you've posted, than it belongs here Dim SearchedOcc As String = "Crosstie" ' Here you can specify the searched text
Dim oOcc As ComponentOccurrence
For Each oOcc In ThisDoc.Document.ComponentDefinition.Occurrences Dim My_Variable As String = oOcc.Name
If My_Variable.IndexOf(SearchedOcc) > -1 Then
WorkWithOcc(My_Variable, Crosstie_Depth_10)
End If
Next
End Sub

' I have no idea where you've got the 'Crosstie_Depth_10' from, so I'll just pass it
Sub WorkWithOcc(My_Variable As String, Crosstie_Depth_10 As Boolean) Select Case Width Case 18 ft If Crosstie_Depth_10 = True Then DoReplace(My_Variable, "ML3010_18ft_10(3x3)_Crosstie.iam") Else DoReplace(My_Variable, "ML3010_18ft_14(3x3)_Crosstie.iam") End If Case 21 ft If Crosstie_Depth_10 = True Then DoReplace(My_Variable, "ML3010_21ft_10(3x3)_Crosstie.iam") Else DoReplace(My_Variable, "ML3010_21ft_14(3x3)_Crosstie.iam") End If Case 24 ft If Crosstie_Depth_10 = True Then DoReplace(My_Variable, "ML3010_24ft_10(3x3)_Crosstie.iam") Else DoReplace(My_Variable, "ML3010_24ft_14(3x3)_Crosstie.iam") End If Case 27.5 ft If Crosstie_Depth_10 = True Then DoReplace(My_Variable, "ML3010_27ft 6in_10(3x3)_Crosstie.iam") Else DoReplace(My_Variable, "ML3010_27ft 6in_14(3x3)_Crosstie.iam") End If End Select InventorVb.DocumentUpdate() End Sub Sub DoReplace(oReplaced As String, oReplacing As String) Dim oPath As String = "Y:\Workshare\Inventor Standards\Gutter Connect\Crossties\" Component.Replace(oReplaced, oPath & oReplacing, True) End Sub

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods