iLogic - Bounding Box

iLogic - Bounding Box

GKPByDesign
Advocate Advocate
2,746 Views
7 Replies
Message 1 of 8

iLogic - Bounding Box

GKPByDesign
Advocate
Advocate

Hello I have had some code developed over two years ago now, for Inventor 2020, now I have Inventor 2022, it doesnt seem to be working perfectly. 

 

The code attached is supposed to run on an Assembly, create a bounding box in each part file, create three custom iProperties called, With, Length, Thickness (Or BoxThickness), grab the dimensions from the bounding box, put the smallest value as the thickness. It does work, but when I save the assembly to changes the values back to default, and also the thickness property isn't showing correctly. Any suggestions? 

 

Imports system.Collections
Public Sub Main() 
    ' Get the active assembly. 
    Dim oAsmDoc As AssemblyDocument 
    oAsmDoc = ThisApplication.ActiveDocument 
 
    ' Get the assembly component definition. 
    Dim oAsmDef As AssemblyComponentDefinition 
    oAsmDef = oAsmDoc.ComponentDefinition 
 
    ' Get all of the leaf occurrences of the assembly. 
    Dim oLeafOccs As ComponentOccurrencesEnumerator 
    oLeafOccs = oAsmDef.Occurrences.AllLeafOccurrences 
 
 
    ' Iterate through all part occurrences in assembly. 
    Dim oOcc As ComponentOccurrence 
    For Each oOcc In oLeafOccs 
L = Math.Abs(oOcc.RangeBox.MaxPoint.X - oOcc.RangeBox.MinPoint.X) * 10
W = Math.Abs(oOcc.RangeBox.MaxPoint.Y - oOcc.RangeBox.MinPoint.Y) * 10
H = Math.Abs(oOcc.RangeBox.MaxPoint.Z - oOcc.RangeBox.MinPoint.Z) * 10
 
Dim oList As New ArrayList 
oList.Add(L)
oList.Add(W)
oList.Add(H)
 
Call oList.Sort()
 
iProperties.Value(oOcc.Name,"Custom", "Length") =  Round(oList.Item(2),0)& " mm"
iProperties.Value(oOcc.Name,"Custom", "Width") = Round(oList.Item(1),0)& " mm"
iProperties.Value(oOcc.Name, "Custom", "BoxThickness") = Round(oList.Item(0),1) & " mm"
 
 Next 
End Sub

 

Accepted solutions (1)
2,747 Views
7 Replies
Replies (7)
Message 2 of 8

GKPByDesign
Advocate
Advocate
Accepted solution

Solved, it worked fine, I chose the wrong template file! 

0 Likes
Message 3 of 8

ivanildo.b
Advocate
Advocate

 

Excelent!

I need the dimensions to be placed in the assembly. (Iproperties)

Can you help solve this?

 

Iva.btblan

0 Likes
Message 4 of 8

pmonteiro
Enthusiast
Enthusiast
Thanks for sharing...
If it's a cylinder, only two dimensions?

 

0 Likes
Message 5 of 8

ldelcerroBHFST
Participant
Participant

HI @GKPByDesign, great script ! Thank you for Sharing

 

I was wondering if anyone had any idea on how to only run this script on the components in the assembly, and not on all the parts in the subassemblies (as a Structured BOM would be for example)

0 Likes
Message 6 of 8

WCrihfield
Mentor
Mentor

Hi @ldelcerroBHFST.  I believe you would just have to change the following lines of code:

Change

' Get all of the leaf occurrences of the assembly.
Dim oLeafOccs As ComponentOccurrencesEnumerator
oLeafOccs = oAsmDef.Occurrences.AllLeafOccurrences

...to this

' Get all top level occurrences. 
Dim oOccs As ComponentOccurrences = oAsmDef.Occurrences

...then change this line of code

For Each oOcc In oLeafOccs

...to this

For Each oOcc In oOccs 

However, there are newer properties that we can use for this type of situation now, if you have a newer version of Inventor.  See the following for more information.

ComponentOccurrence.PreciseRangeBox (became available in Inventor 2023, and also returns an Inventor.Box type value)

ComponentOccurrence.OrientedMinimumRangeBox (became available in Inventor 2024, but returns an Inventor.OrientedBox).

To get the 'size' of an OrientedBox, we can use OrientedBox.DirectionOne.Length...using the same steps for the DirectionTwo, and DirectionThree properties.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 7 of 8

ldelcerroBHFST
Participant
Participant

Hi @WCrihfield Thank you so much, I actually had a fully functioning script and was starting to work on the orientation problem... You might have solved it before I even began !

0 Likes
Message 8 of 8

ldelcerroBHFST
Participant
Participant

For anyone interested, the iLogic Rule I wrote (using a lot of help from forums ofc) is attached to this message. (The code is commented in English don't worry, only the displayed messages are in French)

 

What it does is that it scans the assembly and asks you to select on a Combo Box which sub assemblies you also want to scan, using a recursive sub until you select nothing or there are only .ipt left.

 

It then creates a .xlsx file that contains :

  • a row for each different occurrence (two different occurrences must have different names AND different model state, to allow the use of model states changing the size of an assembly or part),
  • and 7 columns : Name, Weight, Quantity, X size, Y size, Z size and Model State. (x, y and z are obtained using oriented minimum range box)

 

It's still the user that decides "how deep" the algorithm goes by selecting which occurrences of the assemblies to scan.

As we'll be using it to make loading lists, we only select non-dismountable parts, which then help us plan how many trucks we need, but it's easily customizable for other applications.

 

P.S. : there's still an error : the first time I launch it, I get the error "0x800A01A8" which I think comes from trying to write the Occurrences properties for the first time in the excel file, but launching it a second time works fine... I'll open a different thread for that error.

 

Thanks everyone !