Selecting all occurrences of a part with iLogic/vba code

Selecting all occurrences of a part with iLogic/vba code

fraserscott
Contributor Contributor
709 Views
6 Replies
Message 1 of 7

Selecting all occurrences of a part with iLogic/vba code

fraserscott
Contributor
Contributor

I have some code where I have repeated all occurrences of parts line by line. I am sure that there must be a way of simplifying this so that all occurrences are selected and changed in whichever way without having to list them all.

The many lines of code make the adjustments slow. Thanks in advance.

iLogic example.PNG

0 Likes
Accepted solutions (1)
710 Views
6 Replies
Replies (6)
Message 2 of 7

basautomationservices
Advocate
Advocate

You can loop through all occurrences, using a for each loop. If you click on the iLogic folder 'iLogic assemblies/components' there should be a snippet. Check if its the same document and then set the model state. 

Contact me for custom app development info@basautomationservices.com. Follow below links to view my Inventor appstore apps.

Free apps: Smart Leader | Part Visibility Utility | Mate Origins

Paid apps: Frame Stiffener Tool | Constrain Plane Toggle | Property Editor Pro


0 Likes
Message 3 of 7

fraserscott
Contributor
Contributor

I can't find anything in the 'iLogic assemblies/components' folder but there is a loop snippet in variables. It's not 100% clear how it should be used. Would you be able to show me an example by any chance?

 

Many thanks

0 Likes
Message 4 of 7

Curtis_Waguespack
Consultant
Consultant

Hi @fraserscott 

I think you can use replace all of the your Select Case code that you have with something like this ( assuming the Select Case parameter was using a parameter named "LegsType", so change that name as needed).

 

  • this looks at all the components that contain the string "Legs Assembly"
  • then it sets the active model state for each to be the same as the that of the parameter called "LegsType"'
  • then it takes the "LegsType" value, and replaces "Marine " with nothing, so that only the number value is left
  • then and it sets the Leg_Height parameter to that value

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

oType = LegsType 'parameter to use 
oCompString = "Legs Assembly" 'string to look for
oValue = Replace(oType,"Marine ","") 'extracts number

For Each oComp In ThisAssembly.Components
	If oComp.Name.Contains(oCompString) Then
		Component.ActiveModelState(oComp.Name) = oType
		Leg_Height = oValue
	End If
Next

 

EESignature

0 Likes
Message 5 of 7

fraserscott
Contributor
Contributor

Hi Curtis. Thank you for your assistance. I will take a look at that now and let you know the outcome.

 

I have attached my full code in a txt file if that helps you to understand what I did and confirm your suggestion is correct.

 

Many thanks!

 

Fraser

Message 6 of 7

Curtis_Waguespack
Consultant
Consultant
Accepted solution

Hi @fraserscott,

 

Looking at the full code, you would need to strip out some more strings from the value. Here are 2  quick examples. The 2nd one uses the Regex import to extract the number from the string, and is probably the better way to go.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

 

oValue = Leg_Choice 'get value of parameter
oValue = Replace(oValue, "Marine", "") 'remove Marine if present
oValue = Replace(oValue, "Atlantic", "") 'remove Atlantic if present
oValue = Replace(oValue, "(SAA)", "") 'remove (SAA) if present
oValue = Trim(oValue) 'remove spaces

oCompString = "Legs Assembly" 'string to look for

'look at all components
For Each oComp In ThisAssembly.Components
	'look at only components containg the string we're looking for
	If oComp.Name.Contains(oCompString) Then
		'set model state
		Component.ActiveModelState(oComp.Name) = oType
		'set height
		Leg_Height = oValue
	End If
Next

 

 

Imports System.Text.RegularExpressions

oValue = Leg_Choice 'get value of parameter
oValue = Integer.Parse(Regex.Replace(Leg_Choice, "[^\d]", "")) 'extract number

oCompString = "Legs Assembly" 'string to look for

'look at all components
For Each oComp In ThisAssembly.Components
	'look at only components containg the string we're looking for
	If oComp.Name.Contains(oCompString) Then
		'set model state
		Component.ActiveModelState(oComp.Name) = oType
		'set height
		Leg_Height = oValue
	End If
Next		

 

EESignature

0 Likes
Message 7 of 7

fraserscott
Contributor
Contributor

Thank you @Curtis_Waguespack I really appreciate your help.

 

Best regards