ilogic rule to create a rectangular pattern in an assembly

ilogic rule to create a rectangular pattern in an assembly

Anonymous
Not applicable
2,523 Views
7 Replies
Message 1 of 8

ilogic rule to create a rectangular pattern in an assembly

Anonymous
Not applicable

Hello there,

 

I am looking for an ilogic code to create a rectangular pattern within an assembly.

There are 2 simple parts which must be patterned.

They are constrained to an UCS in the model's skeleton.

Pattern direction will always follow the same direction - let's say positive site X axis - and no Y direction. 

Pattern quantity and distance must follow two already created parameters.

 

Later on the pattern and parts must also be deleted by code.

If someone could help me I would very much appreciate it.

 

Thanks.

0 Likes
Accepted solutions (2)
2,524 Views
7 Replies
Replies (7)
Message 2 of 8

WCrihfield
Mentor
Mentor
Accepted solution

Try this.  This assumes your Parameters are called "Distance" and "Quantity".

It also assumes you wanted to select the items you wanted to pattern durring the rule running manually.

Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
Dim oOccs As ComponentOccurrences = oADef.Occurrences
Dim oOcc1 As ComponentOccurrence = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyOccurrenceFilter,"Select the first Occurrence to pattern.")
Dim oOcc2 As ComponentOccurrence = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyOccurrenceFilter,"Select the second Occurrence to pattern.")

Dim oTO As TransientObjects = ThisApplication.TransientObjects
Dim oObjects As ObjectCollection = oTO.CreateObjectCollection
oObjects.Add(oOcc1)
oObjects.Add(oOcc2)
'oObjects.Add(oOccs.Item(1))
'oObjects.Add(oOccs.Item(2))
Dim oRowEntity As Object = oADef.WorkAxes.Item(1)
Dim oColumnEntity As Object = oADef.WorkAxes.Item(2)

Dim oPatt As RectangularOccurrencePattern
oPatt = oADef.OccurrencePatterns.AddRectangularPattern(oObjects, oColumnEntity, True, 0, 1, oRowEntity, True, Parameter("Distance"), Parameter("Quantity"))
MsgBox("Check Model screen to make sure they are patterned right." & vbNewLine &
"Because, after you click OK, they will be deleted again.")
'Do whatever other stuff here

'For i=1 To oPatt.ParentComponents.Count
'	oPatt.ParentComponents.Remove(i)
'Next
oPatt.Delete
oOcc1.Delete
oOcc2.Delete

I hope this helps.
If this solves your problem, or answers your questions, please click 'Accept As Solution".
Or, if this helps you reach your goal, please click 'LIKES" 👍.

Here are a few of the ideas I like.
If you agree, please vote for them, so they might get implemented.

  • Options to format contents of MessageBox, InputBox, and InputListBox Click Here
  • Ability to fully constrain and dimension to the edjes of images within drawing title block sketches, SketchedSymbol sketches, other drawing sketches, and assembly sketches Click Here
  • Save section view status in DesignViewRepresentation, so it can be used in drawing view Click Here
  • Add SolidBodies folder to iLogic Rule Editor Model Tab Click Here
  • Convert all views to Raster before autosave stores to 'OldVersions' folder Click Here

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 8

Anonymous
Not applicable

Hi there WCrihfield!

 

You almost got it!

Thanks for the help so far.

 

A few issues:

  • the parts which will be patterned are always the same, called P1 and P2, assembled on the model tree. Is there a way to put it in the code so the user doesn't have to select it?
  • Can the pattern feature assume the actual parameters, and not their current values at the moment I run the rule? because later when the parameters change then the rule must be runned again.
  • Can the pattern feature deleting rule be in a second rule? So i run if after I am done with it?

Thank you again, I tried changing the rule myself but I coun't get what I need.

 

Cheers

0 Likes
Message 4 of 8

Anonymous
Not applicable

Oh also, how can I have it in the Z axis and not in the X axis?

0 Likes
Message 5 of 8

WCrihfield
Mentor
Mentor
Accepted solution

1st rule:

Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
Dim oOccs As ComponentOccurrences = oADef.Occurrences
Dim oTO As TransientObjects = ThisApplication.TransientObjects
Dim oObjects As ObjectCollection = oTO.CreateObjectCollection
oObjects.Add(oOccs.ItemByName("P1"))
oObjects.Add(oOccs.ItemByName("P2"))
Dim oRowEntity As Object = oADef.WorkAxes.Item(3)
Dim oColumnEntity As Object = oADef.WorkAxes.Item(2)

Dim oPatt As RectangularOccurrencePattern
oPatt = oADef.OccurrencePatterns.AddRectangularPattern(oObjects, oColumnEntity, True, 0, 1, oRowEntity, True, 24, 3)
oPatt.Name = "Pattern 1"

2nd Rule:

Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
Dim oOccs As ComponentOccurrences = oADef.Occurrences
Dim oPatt1 As RectangularOccurrencePattern = oADef.OccurrencePatterns.Item("Pattern 1")
oPatt1.Delete
Dim oOcc1 As ComponentOccurrence = oOccs.ItemByName("P1")
Dim oOcc2 As ComponentOccurrence = oOccs.ItemByName("P2")
oOcc1.Delete
oOcc2.Delete

I hope this helps.
If this solves your problem, or answers your questions, please click 'Accept As Solution".
Or, if this helps you reach your goal, please click 'LIKES" 👍.

Here are a few of the ideas I like.
If you agree, please vote for them, so they might get implemented.

  • Options to format contents of MessageBox, InputBox, and InputListBox Click Here
  • Ability to fully constrain and dimension to the edjes of images within drawing title block sketches, SketchedSymbol sketches, other drawing sketches, and assembly sketches Click Here
  • Save section view status in DesignViewRepresentation, so it can be used in drawing view Click Here
  • Add SolidBodies folder to iLogic Rule Editor Model Tab Click Here
  • Convert all views to Raster before autosave stores to 'OldVersions' folder Click Here

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 6 of 8

WCrihfield
Mentor
Mentor

The first 3 Work Planes & Work Axis are the Origin ones, in order of X,Y, & Z.  So you can always count on them being there. This is how you can switch which axis you want to pattern along.

If the first rule was to be a local rule, you could put something like:

oDV = Distance

oDV = Quantity

at the top of it to force it to run automatically, any time either one of those Parameters changes.

Or if it is an external rule, you can place a simple local rule in the document that simply has contents like this in it to accomplish the same thing.

oDV = Distance

oDV = Quantity

iLogiVb.RunExternalRule("FirstRuleName")

to get it to fire automatically.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 7 of 8

Anonymous
Not applicable

 

WCriehfield thank you so much for your help.

 

A few comments:

 

This is how my first rule is. I've replaced proper values of Distance, Quantity, P1, P2, and pattern name:

 

Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument
	Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
	Dim oOccs As ComponentOccurrences = oADef.Occurrences
	Dim oTO As TransientObjects = ThisApplication.TransientObjects
	Dim oObjects As ObjectCollection = oTO.CreateObjectCollection
	oObjects.Add(oOccs.ItemByName("4349:1"))
	oObjects.Add(oOccs.ItemByName("0157:1"))
	Dim oRowEntity As Object = oADef.WorkAxes.Item(3)
	Dim oColumnEntity As Object = oADef.WorkAxes.Item(2)

	Dim oPatt As RectangularOccurrencePattern
	oPatt = oADef.OccurrencePatterns.AddRectangularPattern(oObjects, oColumnEntity, True, 0, 1, oRowEntity, True, Parameter("DIST_SUP"), Parameter("QTD_SUP"))
	oPatt.Name = "PADR_PARAFUSOS_INOX"

 

  • It still shows when I run it an Invalid Parameter Error ( (Excepction de HRESULT: 0x80070057 (E_INVALIDARG))), so I've had to add at the end a:
    On Error Resume Next
  • I couldn't manage to have the pattern update to the parameters with your suggestion

    oDV = Distance

    oDV = Quantity

    So I am just running the second and the first rule again - recreating the pattern feature.
  • For some reason, inside the pattern feature it was multiplying by 10 my distance parameter, so i had to create a new one divided by 10.
  • I discovered I had to go to the negative side of the Z axis and I couldn't manage to flip the pattern within the code, so this newly created distance "DIST_SUP" parameter was also a negative

So it all works, just not so elegantly as I desired.

 

WCriehfield if you could also suggest on how could I improve my programming skills with vba and inventor, that'd be great, I want to be able to solve these on my own as well.

 

Thanks

0 Likes
Message 8 of 8

WCrihfield
Mentor
Mentor

 

Hi @Anonymous .  I updated my code to include your Item names, Parameter names, & Pattern name.

I also added a Try...Catch...End Try statement in there to try and reduce the possible errors.

If you run that code a second time on the same file, it was probably trying to re-create the same named pattern a second time, which would throw an error, because it already exists.  The Try Catch statement should take care of that.

 

As far as the automatic updating not working.  Within the local rule, make sure the text after "oDV = " exactly matches the spelling and case of the name of your local Parameter.  When its done right, the text should turn blue (by default, this means it recognizes that word as representing a Parameter).  Don't put any quote marks around the names of the parameters.

 

Now about the multiplying by 10 issue.  Depending on what you're doing, I believe Inventor,  uses centimeters for its distance units, by default, unless you specify different Units where the numbers are being used.

When dealing with Parameters, it already know to look at the Units column of the Parameters chart to know what Units it is supposed to be, but raw numbers in iLogic, aren't seen the same way.

 

About flipping the direction of an existing pattern.  I have included a section in the code, where it checks to see if the Pattern already exists.  If it does, it asks you a couple of questions about what you want to do with it.

First you can choose to delete it.  This opens it up for the opportunity to either run your other external rule automatically with that line of code I have commented out, or simply place that deletion code right there.

Next it asks you if you want to Edit the Pattern.  If Yes, it asks you a series of questions so you can change its values.

Amongst those questions it asks you if you want to flip the direction of the pattern.

I didn't complete all possible questions, because I wasn't sure if you wanted to use them or not.

But there there as an example of the possibility, for your convenience.

You can delete this whole Edit section if you want.  Or just comment it out.

 

You'll notice too that I changed from calling out the Axis by item number, to calling them by name.

That's an odd behavior in iLogic & VBA.  The tool tip for all the Item's says its looking for an Integer, but you can most often put the String type name of the item in that space instead, even though the instructions don't mention it.

 

If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
	MessageBox.Show("THIS RULE '" & iLogicVb.RuleName & "' ONLY WORKS FOR ASSEMBLY DOCUMENTS.", "WRONG DOCUMENT TYPE", MessageBoxButtons.OK, MessageBoxIcon.Stop)
	Return
End If

Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
Dim oOccs As ComponentOccurrences = oADef.Occurrences
Dim oUParams As UserParameters = oADef.Parameters.UserParameters

Try
	Dim oPatt As RectangularOccurrencePattern = oADef.OccurrencePatterns.Item("PADR_PARAFUSOS_INOX")
	Dim oDelete As MsgBoxResult = MsgBox("An OccurrencePattern named '" & oPatt.Name & "' already exists." & vbNewLine &
	"Do you want to Delete it and its Components?", vbYesNo + vbQuestion, "PATTERN EXISTS - DELETE?")
	If oDelete = vbYes Then
		'iLogicVb.RunExternalRule("Name Of Rule To Delete Them")
		'Or
		oPatt.Delete
		'If the following names don't match actual items exactly, it will throw an error.
'		Dim oOcc1 As ComponentOccurrence = oOccs.ItemByName("4349:1")
'		Dim oOcc2 As ComponentOccurrence = oOccs.ItemByName("0157:1")
'		oOcc1.Delete
'		oOcc2.Delete
		'This can become difficult when more than one of the same item is in the same assembly.
		'Instead you can use a loop code which searches for a name that 'contains' some text, like this
		For Each oOcc As ComponentOccurrence In oOccs
			If oOcc.Name.Contains("4349") Or oOcc.Name.Contains("0157") Then
				oOcc.Delete
			End If
		Next				
	End If
	
	Dim oEdit As MsgBoxResult = MsgBox("Do you want to Edit its Properties or Values?", vbYesNo + vbQuestion, "EDIT IT?")
	If oEdit = vbYes Then
		'Perhaps use a series of InputBox or InputListBox lines of code here to offer the user the ability to change its values
		'Something like the following
		'ColumnCount is Read Only, so to change its value, change the value of the Parameter being used by it
		Dim oColumnCount As Parameter = oPatt.ColumnCount
		oColumnCount.Value = InputBox("Enter New Column Count.")
		Dim oAxisList As List(Of String)
		oAxisList.Add("X Axis")
		oAxisList.Add("Y Axis")
		oAxisList.Add("Z Axis")
		oPatt.ColumnEntity = InputListBox("Choose Axis For Column Entity.", oAxisList, oAxisList.Item(1), "COLUMN ENTITY")
		oPatt.ColumnEntityNaturalDirection = InputRadioBox("Use Column Entity Natural Direction?", True, False)
		'ColumnOffset is Read Only, so to change its value, change the value of the Parameter being used by it
		Dim oColumnOffset As Parameter = oPatt.ColumnOffset
		oColumnOffset.Value = InputBox("Enter New Column Offset.")
		'Etc
	ElseIf oEdit = vbNo Then
		Return
	End If
Catch
	'It doesn't exist yet, so create it
	Dim oTO As TransientObjects = ThisApplication.TransientObjects
	Dim oObjects As ObjectCollection = oTO.CreateObjectCollection
	oObjects.Add(oOccs.ItemByName("4349:1"))
	oObjects.Add(oOccs.ItemByName("0157:1"))
	Dim oRowEntity As Object = oADef.WorkAxes.Item("Z Axis")
	Dim oColumnEntity As Object = oADef.WorkAxes.Item("Y Axis")
	'You may also want to include a few lines here which check to make sure these UserParameters exist.
'	Dim oColumnOffset As UserParameter = 
'	Dim oColumnCount As UserParameter = 
	Dim oRowOffset As UserParameter = oUParams.Item("DIST_SUP")
	Dim oRowCount As UserParameter = oUParams.Item("QTD_SUP")

	Dim oPatt As RectangularOccurrencePattern
	oPatt = oADef.OccurrencePatterns.AddRectangularPattern(oObjects, oColumnEntity, True, 0, 1, oRowEntity, True, oRowOffset, oRowCount)
	oPatt.Name = "PADR_PARAFUSOS_INOX"
End Try

iLogic is fairly easy to pick up if you're really interested in it.

VBA is perhaps a little bit more involved, because it covers more ground, since it has been around for a really long time and is also used by Microsoft for their Office softwares.

But they are fairly similar in many aspects.  iLogic is just a simplified programming platform that Autodesk built into Inventor to help make it easier for users new to programming to get started into using code to manipulate stuff within its software.

Actualy Inventor's help files aren't a bad place to get started.  It seems a bit more focussed on VBA than iLogic, but since things in iLogic are so similar to corresponding things in VBA, its fairly easy to put the two togeather.

Check out these locations in the online help especially, to help you figure out what certain program words mean.

Inventor 2020 online Help 

Inventer Help capture.PNG

Wesley Crihfield

EESignature

(Not an Autodesk Employee)