I don't mind sharing it, but had skipped doing so before because it's very specifically tailored to what we're using it for.
Before you dig too deeply into the code - are these models you are creating yourself, or are they Frame Generator structural shapes? This code can technically be made to work in either case, but if you're working with Frame Generator, there's an easier way that doesn't involve code at all.
Below is my code. It is intended for use in steel plate fabrication work. The comments throughout the code should explain most of what it's doing. Essentially, it does the following:
- Makes sure the part is updated before doing any of the measurements
- Turns off the Object Visibility of all work features (work planes, points, axes) so they will not be included
- Measures the extents of the plate in X, Y, and Z directions.
- Sorts these to determine which is the Thickness, Width, and Length of the plate.
- Assigns the values to an appropriate User Parameter
- Turns the Object Visibility back on
' This rule uses the Measured Extents in the X, Y, and Z directions to develop parameters which will be used in the plate's
' material list description.
oDoc = ThisDoc.Document
Dim WorkFeatureVisList As New ArrayList
' Verify that the document is fully updated prior to taking measurements.
InventorVb.DocumentUpdate()
' Work features will be included in the extents if they are visible, which interferes with accurate measurement of the plate.
' Object visibility of work features and surfaces will be set to invisible before the measurements are taken, then returned to their previous state.
'toggle construction surface visibility off
oDoc.ObjectVisibility.AllWorkFeatures = False
oDoc.ObjectVisibility.ConstructionSurfaces = False
oDoc.ObjectVisibility.Sketches = False
oDoc.ObjectVisibility.Sketches3D = False
' Determine the extents of the part in X, Y, and Z directions
X_Measure = Measure.ExtentsLength
Y_Measure = Measure.ExtentsWidth
Z_Measure = Measure.ExtentsHeight
' Create an array from the measured values
Measured_Extents = New Double(){X_Measure,Y_Measure,Z_Measure}
' Sort the array in ascending order
Measured_Extents.Sort(Measured_Extents)
' Assign the measured values to the appropriate parameter based on their sorted order
THK = Measured_Extents(0)
WIDTH_DESC = Measured_Extents(1)
LENGTH_DESC = Measured_Extents(2)
'toggle construction surface visibility on
oDoc.ObjectVisibility.AllWorkFeatures = True
oDoc.ObjectVisibility.ConstructionSurfaces = True
oDoc.ObjectVisibility.Sketches = True
oDoc.ObjectVisibility.Sketches3D = True
' Update so changes are visible to the user
iLogicVb.UpdateWhenDone = True
The User Parameters are then marked for Export and embedded in the Description iProperty, just as you were doing with your reference length dimension.
In your case, if your parts are always modeled in the same orientation, you might be able to take out the "Array" portions of the code, and only measure in that direction. If that's not the case, then you might be able to use the array to find the largest of the three measurements, and then use that as your "Length" parameter.