Adding a linear assembly pattern via ilogic with unique name

Adding a linear assembly pattern via ilogic with unique name

blandb
Mentor Mentor
1,597 Views
12 Replies
Message 1 of 13

Adding a linear assembly pattern via ilogic with unique name

blandb
Mentor
Mentor

Good morning all,

 

I am still new to loops, but if I am using ilogic to place in a rect. pattern, one has to enter the name. How can I make sure the the name starts at 1 and auto indexes if I add more?

 

For example, I want to make one pattern and it it called Pipe Pattern 1. But if I add a new one, I need it to be Pipe Pattern 2, etc. So it needs to scan the assy and see what the last number used and make the next one in order.

example:

 

for each "pattern"

i=1

patternprefix = "pipe pattern"

Patterns.AddRectangular(patternprefix & i, rest of the stuff)

next

 

thanks in advance

Autodesk Certified Professional
0 Likes
1,598 Views
12 Replies
Replies (12)
Message 2 of 13

dalton98
Collaborator
Collaborator

Do you just want to get a count of the pipe patterns or do you want to create one aswell?

This gets code gets the number of patterns that start with "Pipe Pattern":

Dim oAss As AssemblyDocument = ThisApplication.ActiveDocument
Dim i As Integer = 1

Dim oPattern As OccurrencePattern
For Each oPattern In oAss.ComponentDefinition.OccurrencePatterns
	If Left(oPattern, 12) = "Pipe Pattern" 'note* case sensetive
		i = i + 1
	End If
Next

MessageBox.Show(i)

 

0 Likes
Message 3 of 13

blandb
Mentor
Mentor

I'd like to add as well.

Autodesk Certified Professional
0 Likes
Message 4 of 13

WCrihfield
Mentor
Mentor

Here is another bit of iLogic code you can play around with for naming the pattern.  It first looks for existing ones who's name starts with the text you specified, then it tries to extract the index number at the end of its name.  Then if some were found, it adds 1 to the last highest number found for use in the new pattern name.  Then it attempts to create a new pattern (you will need to create the variables and values needed).  Then it assigns the new name to it.

Dim oADoc As AssemblyDocument = ThisDoc.Document
oADef = oADoc.ComponentDefinition
oOccPatts = oADef.OccurrencePatterns

Dim oPatternBaseName As String = "pipe pattern"
Dim oPatternIndex As Integer = 1
For Each oOccPatt As OccurrencePattern In oOccPatts
	If oOccPatt.Name.StartsWith(oPatternBaseName) Then
		oIndex = Replace(oOccPatt.Name, oPatternBaseName, "")
		If oIndex <> "" Then 'something after the base name
			If CInt(oIndex) > oPatternIndex Then
				oPatternIndex = CInt(oIndex) + 1
			End If
		End If
	End If
Next
Dim oNewPatternName As String = oPatternBaseName & oPatternIndex
'define needed variables for creating new pattern, then create it
oNewOccPatt = oOccPatts.AddRectangularPattern(oObjectCollection, oColEntity, oColDirBool, oColOffset, oColCount)
oNewOccPatt.Name = oNewPatternName

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 13

blandb
Mentor
Mentor

@WCrihfield 

So the way you are generating the pattern, I assume it will not follow the snippet inputs?

 

for example, trying to utilize your code with the original snippet way:

oNewOccPatt = oOccPatts.AddRectangularPattern(oNewPatternName, {PIPE, INLETSEAL, INLETCLAMP}, adjusted_pattern_cnt, 1990.25 mm, PIPE, "Y Axis", columnNaturalDirection := True)
Autodesk Certified Professional
0 Likes
Message 6 of 13

WCrihfield
Mentor
Mentor

You could probably use whichever one you feel more comfortable with.  I just chose to use the API way instead of the iLogic snippet shortcut way, because we already had to access all the API objects related to it.  The API method 'OccurrencePatterns.AddRectangularPattern' and the iLogic snippet shortcut method 'Patterns.AddRectangular' are a bit different.  They require different inputs, and return different objects, but ultimately the iLogic snippet is most likely using the API method in the background within an unseen Function routine created by the iLogic add-in developers.  Using the API route just skips the 'middle man' and is more conventional.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 7 of 13

dalton98
Collaborator
Collaborator

This code could combine with @WCrihfield 

It creates a for loop for you to select all the parts you want in the pattern.

Then it selects the "Y Axis" as the pattern line.

Dim oAss As AssemblyDocument
oAss = ThisApplication.ActiveDocument
Dim oOccPatts As OccurrencePatterns
oOccPatts = oAss.ComponentDefinition.OccurrencePatterns

Dim oObjectCollection As ObjectCollection 
oObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection

For j = 1 To 99
Try
Dim oObj As Object
oObj = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyOccurrenceFilter, "Esc to Continue")
'oAss.SelectSet.Select(oObj)
oObjectCollection.Add(oObj)
Catch
Exit For
End Try
Next

Dim oDirection As Object
oDirection = oAss.ComponentDefinition.WorkAxes.Item("Y Axis")
Dim adjusted_pattern_cnt As Integer = 4

oNewOccPatt = oOccPatts.AddRectangularPattern(oObjectCollection, oDirection, True, 199.025, adjusted_pattern_cnt)

 

0 Likes
Message 8 of 13

blandb
Mentor
Mentor

@dalton98 

Appreciate this, but unfortunately I cannot just use the "y" axis. I had it set to use a specific component axis. I also had a set set of component in this pattern. That is what I was trying to add because this is part of a select case where there are certain components needed for this config.

 

Thanks

Autodesk Certified Professional
0 Likes
Message 9 of 13

dalton98
Collaborator
Collaborator

Ok. In order to use a component axis you have to create a geometry proxy:

Dim oAss As AssemblyDocument = ThisApplication.ActiveDocument

Dim oOcc As ComponentOccurrence = oAss.ComponentDefinition.Occurrences.Item(1)

Dim oOccCompDef As PartComponentDefinition
oOccCompDef = oOcc.Definition

Dim oDirection As Object
oDirection = oOccCompDef.WorkAxes.Item("X Axis")
oOcc.CreateGeometryProxy(oDirection, oDirection)

And to add specific components you would do this:

Dim oObjectCollection As ObjectCollection 
oObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection
oObjectCollection.Add(oAss.ComponentDefinition.Occurrences.ItemByName("test:1"))
oObjectCollection.Add(oAss.ComponentDefinition.Occurrences.ItemByName("test:2"))
oObjectCollection.Add(oAss.ComponentDefinition.Occurrences.ItemByName("test:3"))
0 Likes
Message 10 of 13

blandb
Mentor
Mentor

@dalton98 

 

I have tried mixing the two solutions from you, but I keep getting an error. I know I have the syntax wrong. That collection where I am entering the variables, will more than likely always be those items. But even if I try to tell code what they are, it will not build it?

 

Dim oADoc As AssemblyDocument = ThisDoc.Document
			oADef = oADoc.ComponentDefinition
			oOccPatts = oADef.OccurrencePatterns
			
			Dim oPatternBaseName As String = "pipe pattern"
			Dim oPatternIndex As Integer = 1
			For Each oOccPatt As OccurrencePattern In oOccPatts
				If oOccPatt.Name.StartsWith(oPatternBaseName) Then
					oIndex = Replace(oOccPatt.Name, oPatternBaseName, "")
					If oIndex <> "" Then 'something after the base name
						If CInt(oIndex) > oPatternIndex Then
							oPatternIndex = CInt(oIndex) + 1
						End If
					End If
				End If
			Next
Dim oNewPatternName As String = oPatternBaseName & oPatternIndex 'define needed variables for creating new pattern, then create it oObjectCollection = {PIPE, INLETSEAL, INLETCLAMP} oNewOccPatt = oOccPatts.AddRectangularPattern(oObjectCollection, PIPE, "Y Axis", True, 1990.25 mm, adjusted_pattern_cnt) 'oNewOccPatt = oOccPatts.Patterns.AddRectangular(oNewPatternName, {PIPE, INLETSEAL, INLETCLAMP}, adjusted_pattern_cnt, 1990.25 mm, PIPE, "Y Axis", columnNaturalDirection := True) oNewOccPatt.Name = oNewPatternName 'Patterns.AddRectangular("RectPatternName", {PIPE, INLETSEAL, INLETCLAMP}, adjusted_pattern_cnt, 1990.25 mm, PIPE, "Y Axis", columnNaturalDirection := True) messagebox.Show("end extra gap") End If

 

 

Autodesk Certified Professional
0 Likes
Message 11 of 13

blandb
Mentor
Mentor

@dalton98 

I didn't see your post prior to my last, I can attempt to combine these to see if it works.

Autodesk Certified Professional
0 Likes
Message 12 of 13

blandb
Mentor
Mentor

Just out of curiosity, why do I have to create a proxy, when using the snippet one doesnt? Or is that happening automatically?

Autodesk Certified Professional
0 Likes
Message 13 of 13

WCrihfield
Mentor
Mentor

The iLogic shortcut snippet does do some stuff in the background for you to make the task easier, so it is quite possible that it is handling the whole 'proxy' business for you.  There is a code routine within the iLogic add-in somewhere that gets ran whenever that snippet is used, then it does some stuff with the inputs supplied to it, and returns a ManagedPattern object, instead of a regular OccurrencePattern object.  But you can get the OccurrencePattern object from the ManagedPattern object through its Pattern property.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes