Finding the minimum thickness of a part, using ray tracing

Finding the minimum thickness of a part, using ray tracing

evan.shimek
Participant Participant
888 Views
9 Replies
Message 1 of 10

Finding the minimum thickness of a part, using ray tracing

evan.shimek
Participant
Participant

I'm using a modified VBA code from Mod the Machine to find the thickness of all of my platework. This is the code:

 

Function GetThickness(sb As SurfaceBody) As Double
' Find biggest face
Dim f As Face
Dim bf As Face
Dim area As Double
For Each f In sb.Faces
' Only care about planar faces
If TypeOf f.Geometry Is Plane And f.Evaluator.area > area Then
Set bf = f
area = f.Evaluator.area
End If
Next

' Find the opposite face
Dim p As Plane
Set p = bf.Geometry

Dim pt1 As Point
Set pt1 = bf.PointOnFace

Dim tr As TransientGeometry
Set tr = ThisApplication.TransientGeometry

Dim objs As ObjectsEnumerator
Dim pts As ObjectsEnumerator
Dim n As UnitVector
' We have to search in the opposite direction
' of the face's normal vector
If bf.IsParamReversed Then
Set n = p.Normal
Else
Set n = tr.CreateUnitVector( _
-p.Normal.X, -p.Normal.Y, -p.Normal.Z)
End If
' objs(2) should be the opposite face
' but we do not need it, the intersection point
' is enough, i.e. pts(2)
Call sb.FindUsingRay(pt1, n, 0, objs, pts)

' The first point found will be on the same face
' The second one will be on the face opposite
Dim pt2 As Point
Set pt2 = pts(2)

GetThickness = pt1.DistanceTo(pt2)
End Function

Sub ConvertToSheetMetal()

Dim oDoc As PartDocument
Set oDoc = ThisApplication.ActiveDocument

' Turn it into a sheet metal part
oDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}"

Dim cd As SheetMetalComponentDefinition
Set cd = oDoc.ComponentDefinition

cd.UseSheetMetalStyleThickness = False
cd.Thickness.Value = GetThickness(cd.SurfaceBodies(1))

Call cd.Unfold
End Sub

 

It works for about 70% of my plates. I'm guessing the issue might be related to the location of my origins - all of our part files are derived from a single "skeleton" part file where we do all of our modeling. So, the origin is rarely on or near the part file.

 

I've attached two seemingly identical part files. their only real difference is their relationship to the origin. The code works correctly for one part and not the other. Can anyone help me understand why that is or provide a solution to this problem?

0 Likes
Accepted solutions (1)
889 Views
9 Replies
Replies (9)
Message 2 of 10

WCrihfield
Mentor
Mentor

Hi @evan.shimek.  Interesting case.  I looked into both parts, and put the code above into their DocumentProject Module1, and ran them.  Just as you said, the one works without any problems, but the other one stops at the line of code where it is getting the second point object from the ObjectCollection.  Upon further investication, I found that the largest area Face that is being used on the one part, has its IsParamReversed property = False, while the other part's largest area face's same property = True.  I am not sure why, but I assume this is part of the issue.  Since the two parts seem to be a mirror image of the each other, it may be finding the opposite face for each part, since two faces should be equal area, but they are just maybe being looped through in different order.  Maybe the bit of code dealing with this property's value is not 100% effective.  In simple cases like this one, simply finding two faces that have either the same area, or closest area might be a much more efficient way to find opposing flat faces of material, then measure between them.  Just some thoughts.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 10

WCrihfield
Mentor
Mentor
Accepted solution

Hi @evan.shimek.  Here is a similar VBA macro you may be able to use for that task.  It uses the size of the OrientedMinimumRangeBox of the FlatPattern's Body to determine Thickness, then writes that value back to the SheetMetalComponentDefinition.Thickness.Value.  I first thought of measuring between the TopFace and BottomFace of the FlatPattern, but it throws an error when accessing the BottomFace property when going that route.  This is just as simple though and seemed to work OK in my testing.  You can give this a try if you want.

Sub FlattenThenGetThickness()
    If ThisApplication.ActiveDocumentType <> kPartDocumentObject Then Exit Sub
    Dim oPDoc As PartDocument
    Set oPDoc = ThisApplication.ActiveDocument
    If oPDoc.SubType <> "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then
        oPDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}"
    End If
    Dim oSMDef As SheetMetalComponentDefinition
    Set oSMDef = oPDoc.ComponentDefinition
    Dim oFlatPattern As FlatPattern
    If oSMDef.HasFlatPattern Then
        Set oFlatPattern = oSMDef.FlatPattern
    Else
        oSMDef.Unfold
        Set oFlatPattern = oSMDef.FlatPattern
        oFlatPattern.ExitEdit
    End If
    Dim oBox As Inventor.OrientedBox
    Set oBox = oFlatPattern.Body.OrientedMinimumRangeBox
    Dim oLengths(1 To 3) As Double
    oLengths(1) = oBox.DirectionOne.Length
    oLengths(2) = oBox.DirectionTwo.Length
    oLengths(3) = oBox.DirectionThree.Length
    Dim oThickness As Double
    For Each oItem In oLengths
        If oThickness = 0 Then
            oThickness = oItem
        End If
        If oItem < oThickness Then
            oThickness = oItem
        End If
    Next
    oThickness = Math.Round(oThickness, 3)
    oSMDef.Thickness.Value = oThickness
End Sub

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 10

evan.shimek
Participant
Participant

This works brilliantly. Thank you!

0 Likes
Message 5 of 10

sbalasubramanianJJDFU
Enthusiast
Enthusiast

Does this code work for checking minimum thickness in a cylindrical part?

0 Likes
Message 6 of 10

WCrihfield
Mentor
Mentor

Hi @sbalasubramanianJJDFU.  If you are talking about the wall thickness of a hollow cylindrical body, like a tube or pipe then no, this would not work good for that.  The VBA code I posted above was specifically designed for use on a sheet metal type part document, which can be flattened.  Since the oriented minimum range box (link1, link2) itself is shaped like a rectangular prism, it works good for measuring the thickness of flat bodies.  If the cylinder is not hollow, and you are not looking for wall thickness but overall bounding box size of the cylinder, then we may be able to use the oriented minimum range box to get that type of data from it.  But at least two of the returned measurements will be the same, due to the shape, and if one measurement is different, that will be the 'length' of the cylinder.  But the surface area and volume of that oriented box will not be accurate to the cylinder's surface area or volume, due to their difference in shape.  If the body within the part is a true, simple cylinder shape, then we could likely get its surface area and volume another way, if that data was needed.  The original poster here was working with another idea where the code tries to find the largest face on the supplied body, then tries to case a perpendicular line from that face to another face on the same body, then get the distance from the first face to that other face.  But which direction that line needs to point from the starting face can be difficult to predict at times, and is best suited for situations where there will be only two flat parallel faces.  If you need help developing some code to accurately measure cylindrical shaped bodies within parts, or cylindrical shaped components within assemblies, please include as much detail as possible, and include screen captured images and/or attach a sample part for us to test the code on.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 7 of 10

sbalasubramanianJJDFU
Enthusiast
Enthusiast

Thanks!.. Im looking for iLogic code to measure minimum distance of any two faces in the entire part which is less than 1mm wall thickness. It will be of great help if it shows all the areas where the minimum distances falls below certain value(for example 1mm) and highlights them same like interference command does. 

0 Likes
Message 8 of 10

WCrihfield
Mentor
Mentor

That would be pretty difficult to achieve.  From a code standpoint, there is no good way to determine which side of a Face is pointing towards the inside of the volume of a body, and which side is pointing towards the outside of the body, so in some situations, it would be trying to measure across gaps or cutouts, instead of through the body.  And in some situations, the two faces will intersect each other, such as at a common edge, which would return a value of zero (which is obviously less than 1 mm).  Here is a link to one of the main tools for measuring minimum distance between things in Inventor's API.

MeasureTools.GetMinimumDistance Method

And we can get to that MeasureTools object directly from the 'ThisApplication' variable (represents the Inventor.Application object).  It is a pretty useful tool, but not smart enough to know if the two input objects are the opposite sides of a solid material's wall thickness.  Technically, it may be possible to attempt to collect every Face type object from every body in a multi-body (or single body) part into a single collection, then try looping through that collection testing each one against every other one in the collection, but that could potentially take a lot of processing, and may not result in any meaningful results.  The highlighting step is another matter, but also potentially possible.  The Document object has a method  called Document.CreateHighlightSet, which could be used to initially create the HighlightSet object.  Then, if the test results in a value that is greater than zero, but less that 1 mm, you could add the two input entities into the HighlightSet.  This may be a subject for another new forum topic though.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 9 of 10

sbalasubramanianJJDFU
Enthusiast
Enthusiast

Is there a possibility of atleast to check if two holes are very close to each other? Like feature proximity..

0 Likes
Message 10 of 10

WCrihfield
Mentor
Mentor

Sure.  Here is a simple example iLogic rule which, when you run it, it will prompt you to pick a first face, then a second face (the inside cylinder faces of the two holes).  Then it will attempt to measure the minimum distance between them, then it converts those units from database units (Inventor's internal length units - centimeters) to the document's length units.  Then it shows the result in a simple message.

 

Dim oFace1 As Face = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceFilter, "Pick first face.")
If oFace1 Is Nothing Then Exit Sub
Dim oFace2 As Face = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceFilter, "Pick second face.")
If oFace2 Is Nothing Then Exit Sub
Dim oMT As MeasureTools = ThisApplication.MeasureTools
Dim dMinDist As Double = oMT.GetMinimumDistance(oFace1, oFace2)
Dim oDoc As Document = oFace1.Parent.ComponentDefinition.Document
Dim UOM As UnitsOfMeasure = oDoc.UnitsOfMeasure
dMinDist = UOM.ConvertUnits(dMinDist, "cm", UOM.LengthUnits)
MsgBox("Minimum Distance = " & dMinDist.ToString, vbInformation, "iLogic")

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes