Add multiple of same part to assembly using Ilogic rule

Add multiple of same part to assembly using Ilogic rule

aaron.king9NR37
Explorer Explorer
354 Views
6 Replies
Message 1 of 7

Add multiple of same part to assembly using Ilogic rule

aaron.king9NR37
Explorer
Explorer

Hi All,

 

I'm trying to create a user form that pulls all the parts for an assembly with variable options.

 

I'm trying to use an if statement to check if a Boolean parameter is true, if that is true it checks another parameter that specifics a particular part, if that Boolean is true and the 2nd parameter part is found it checks a third parameter to decided the qty of the part to add. Then it adds the part(s) to the assembly using Components.Add command.

 

I have no problem with this loop for adding one part but am unsure/don't know the proper syntax to add multiple instances of the part.

 

 

 

0 Likes
355 Views
6 Replies
Replies (6)
Message 2 of 7

A.Acheson
Mentor
Mentor

Hi @aaron.king9NR37 

Welcome to the forum, can you post your attempt code? This will help both yourself and others understand what your code is doing and where your having trouble. Include some commented code if you want to add functionality but don't know what to write.

 

For adding multiple instances a For loop would be a typical method.

Dim totalQty as Integer = 10
For qty = 1 to totalQty
' Add components
  MessageBox.Show(qty,"iLogic")
Next

 If you want the user to supply a variable qty then use an input box, see help page here

myparam = InputBox("Prompt", "Title", "Default Entry")

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
Message 3 of 7

WCrihfield
Mentor
Mentor

Since that Components.Add method wants you to specify the components name, and you are planning on adding multiple components into the assembly that all reference the same model file, then you will most likely need to come up with a naming scheme to make sure each component name you provide is different.  One of the most common ways to do this is by adding an Integer to the end of the name each time, in a loop.  So you could use the model file's name (without path or file extension) as the base name, then add an Integer after that.  Create the Integer variable before the loop starts, then start the loop where you add the components within that loop, then within that loop, but before adding a component, assemble the component name as the value of a String type variable, with file name, then the Integer variable.  Then increment the value of that Integer each time by at least 1.  This is not a very intuitive process, and it can fail, but it is a starting point.  Inventor will not allow 2 or more components in the same assembly's context to have exactly the same name.  So if you specify a name for a new component that you are adding to the assembly, and that name already exists within the assembly, it will throw an error.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 7

aaron.king9NR37
Explorer
Explorer

I've ended up finding something that works

 

I'm very very green on trying to do this in this fashion so I'm certain there's a more elegant way to structure my For loops than you will see I am attempting but the following is the code that ended up working for me. I've commented out what I was trying originally that was failing.

 

aaronking9NR37_0-1702482168716.png

 

Message 5 of 7

aaron.king9NR37
Explorer
Explorer

* some For loops*

 

not my For loops, I didn't use any

0 Likes
Message 6 of 7

A.Acheson
Mentor
Mentor

Glad to hear you got something working. If you need more assistance then copy your code from the ilogic editor and attach to the forum by pressing the toolbar button shown below. This way anyone helping you can copy the code and test in there editor. An image is  a good start but the actually code for testing would be easier to read. 

AAcheson_0-1702485522767.png

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
Message 7 of 7

J-Camper
Advisor
Advisor

@aaron.king9NR37,

 

I have a sub routine that adds a single instance of a list of filenames.  I made a small tweak so it can do multiple instances of a single filename if you are interested:

	Private Sub AddMultiplesOfFileToAssembly(aDoc As AssemblyDocument, FileName As String, Instances As Integer)
		If Instances < 1 Then Exit Sub
		
		Dim Position As Matrix = ThisApplication.TransientGeometry.CreateMatrix()
		For i As Integer = 1 To Instances
			Dim NewComponent As ComponentOccurrence = aDoc.ComponentDefinition.Occurrences.Add(FileName, Position)
			Dim AdjustVector As Vector = NewComponent.RangeBox.MinPoint.VectorTo(NewComponent.RangeBox.MaxPoint)
			AdjustVector.AddVector(Position.Translation)
			Position.SetTranslation(AdjustVector)
		Next

	End Sub

 

It staggers them the same way Inventor does when you drag/drop a bunch of files into the model.

 

Let me know if you have any questions