Hi @NachoShaw
I messed around with this a bit and came up with this example.
Note that I ran it on a simple assembly file that was created from a Multibody base model using the Make Components tool to populate the assembly.
It looks at each occurrence in the assembly and does this:
- places another occurrence of that component
- finds the longest edge
- then uses the 2 faces that share the longest edge to mate to the assembly work planes
- then it creates a new part file
- and then invisibly copies the solid body from the mated occurrence into the new file in the mated oreintation
- then it gets the range box, XYZ values
- then it determines Length, Width, and Thickness, with length being largest, thickness being the smallest
- then it creates or edits a custom iproperty in the original part file called Size
- then it cleans up the extra, mated occurrence, and dumps the temporary part file
Then it opens the BOM editor (I didn't see a way to add the custom iprop to BOM view, so that needs to be done manually (unless I missed something).
Note that I didn't test it much, so it might trip over sheet metal files, sub assemblies, Virtual Components, etc.... but it's something to start with.
I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com
' Get the active document. This assumes it is a part document.
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition
' Set a reference to the transient geometry object.
Dim oTG As TransientGeometry
oTG = ThisApplication.TransientGeometry
Dim oMatrix As Matrix
oMatrix = oTG.CreateMatrix
'Iterate through all of the occurrences
Dim oOccurrence As ComponentOccurrence
For Each oOccurrence In oAsmCompDef.Occurrences.AllReferencedOccurrences(oAsmCompDef)
'insert new occurence
Dim oOcc1 As ComponentOccurrence
oOcc1 = oAsmCompDef.Occurrences.Add( _
oOccurrence.Definition.Document.FullFileName, oMatrix)
'rename occurence incase error causes it to
'be left behind
oOcc1.Name = "iLogic_Temporary_Component_Remove_As_Needed |" & oOcc1.Name
'get the longest edge on the component
Dim oMaxEdge As EdgeProxy
oOcc1.Grounded = False
oMaxLength = 0
For Each oEdge In oOcc1.SurfaceBodies(1).Edges
oLengthValue = ThisApplication.MeasureTools.GetMinimumDistance( _
oEdge.StartVertex, oEdge.StopVertex)
If oLengthValue > oMaxLength Then
oMaxEdge = oEdge
oMaxLength = oLengthValue
End If
Next
'get the 2 faces that include the longest edge
oFaces = oMaxEdge.Faces
'create a face proxy for the first face
Dim oAsmFace1 As FaceProxy
Dim oFace1 As FaceProxy
oFace1 = oFaces.Item(1)
Call oOcc1.CreateGeometryProxy(oFace1, oAsmFace1)
'create a face proxy for the second face
Dim oAsmFace2 As FaceProxy
Dim oFace2 As FaceProxy
oFace2 = oFaces.Item(2)
Call oOcc1.CreateGeometryProxy(oFace2, oAsmFace2)
'create work plane proxies
Dim oAsmPlane1 As WorkPlane
Dim oAsmPlane2 As WorkPlane
oAsmPlane1 = oAsmCompDef.WorkPlanes.Item(1) 'Assembly YZ Plane
oAsmPlane2 = oAsmCompDef.WorkPlanes.Item(2) 'Assembly XZ Plane
'constrain the occurence to the assembly origin workplanes
oConstraint1 = oAsmCompDef.Constraints.AddMateConstraint(oAsmFace1, oAsmPlane1, 0)
oConstraint2 = oAsmCompDef.Constraints.AddMateConstraint(oAsmFace2, oAsmPlane2, 0)
' Create a new part, invisibly.
Dim oTempDoc As PartDocument
oTempDoc = ThisApplication.Documents.Add(DocumentTypeEnum.kPartDocumentObject, _
ThisApplication.FileManager.GetTemplateFile(kPartDocumentObject), False)
'get the first solid body in the occurence document
Dim oBody As SurfaceBody
oBody = oOcc1.Definition.Document.ComponentDefinition.SurfaceBodies.Item(1)
oMatrix = oOcc1.Transformation
'copy the solid from the assembly occurence to the temporary file
'in the constrained orientation
Call oTempDoc.ComponentDefinition.Features.NonParametricBaseFeatures.Add(oBody, oMatrix)
'get the range box
oBox = oTempDoc.ComponentDefinition.SurfaceBodies.Item(1).RangeBox
'Get active document unit of measure
Dim oUOM As UnitsOfMeasure
oUOM = oOccurrence.Definition.Document.UnitsOfMeasure
'Converts length unit to a string
Dim oUnits As String = oUOM.GetStringFromType(oUOM.LengthUnits)
'converts the units string
If oUnits = "centimeter" Then
oUnits = " cm"
Else If oUnits = "millimeter" Then
oUnits = " mm"
Else If oUnits = "inch" Then
oUnits = Chr(34)
Else If oUnits = "foot" Then
oUnits = " ft"
Else If oUnits = "meter" Then
oUnits = " m"
End If
'get the range box's max point
Dim oMax As point
oMax = oBox.MaxPoint
'get the range box's min point
Dim oMin As point
oMin = oBox.MinPoint
'convert the value from Inventor's internal cm
'To the Umits Of Measure Of the orginal component file
X = oUOM.ConvertUnits ((oMax.X - oMin.X), "cm", oUOM.LengthUnits)
Y = oUOM.ConvertUnits ((oMax.Y - oMin.Y), "cm", oUOM.LengthUnits)
Z = oUOM.ConvertUnits ((oMax.Z - oMin.Z), "cm", oUOM.LengthUnits)
'figure out which is length, width and thickness
Dim oLength As Double = MaxOfMany (X,Y,Z) & " "
Dim oWidth As Double = X + Y + Z - MaxOfMany (X,Y,Z) - MinOfMany(X,Y,Z) & " "
Dim oThick As Double = MinOfMany (X,Y,Z) & " "
'round values
Dim oDecimals As Integer = 3
oLength = Round(oLength,oDecimals)
oWidth = Round(oWidth,oDecimals)
oThick = Round(oThick,oDecimals)
'create string value
oSize = oThick & oUnits & " x " & oWidth & oUnits & " x " & oLength & oUnits
'set custom iproperty in the component file
iProperties.Value(oOccurrence.Name,"Custom", "Size") = oSize
'delete the temporariily constrained occurence
oOcc1.Delete
'close the temporary new part file
oTempDoc.Close
Next 'occurence
'Open BOM editor dialog box
Dim oCtrlDef As ControlDefinition
oCtrlDef = ThisApplication.CommandManager.ControlDefinitions.Item("AssemblyBillOfMaterialsCmd")
oCtrlDef.Execute