How to select face first and set a new base before continue measure with code give

How to select face first and set a new base before continue measure with code give

syafiqfarhan
Enthusiast Enthusiast
580 Views
5 Replies
Message 1 of 6

How to select face first and set a new base before continue measure with code give

syafiqfarhan
Enthusiast
Enthusiast

I have one simple question, 

Currently, I try to create new iproperties using ilogic (code below). but I face issues if the part is not on the proper USC.

 

Please help me how can do selection first (red arrow) and set a new base before creating a new iproperties

 

syafiqfarhan_0-1639375781457.png

 

 

 

iProperties.Value("Custom", "t") = Round(MinOfMany(Measure.ExtentsHeight, Measure.ExtentsLength, Measure.ExtentsWidth))
iProperties.Value("Custom", "l") = Round(MaxOfMany(Measure.ExtentsHeight, Measure.ExtentsLength, Measure.ExtentsWidth))
If iProperties.Value("Custom", "t") = Round(Measure.ExtentsWidth) And iProperties.Value("Custom", "l") = Round(Measure.ExtentsHeight) Then iProperties.Value("Custom", "w") = Round(Measure.ExtentsLength) Else
If iProperties.Value("Custom", "t") = Round(Measure.ExtentsWidth) And iProperties.Value("Custom", "l") = Round(Measure.ExtentsLength) Then iProperties.Value("Custom", "w") = Round(Measure.ExtentsHeight) Else
If iProperties.Value("Custom", "t") = Round(Measure.ExtentsHeight) And iProperties.Value("Custom", "l") = Round(Measure.ExtentsLength) Then iProperties.Value("Custom", "w") = Round(Measure.ExtentsWidth) Else
If iProperties.Value("Custom", "t") = Round(Measure.ExtentsHeight) And iProperties.Value("Custom", "l") = Round(Measure.ExtentsWidth) Then iProperties.Value("Custom", "w") = Round(Measure.ExtentsLength) Else
If iProperties.Value("Custom", "t") = Round(Measure.ExtentsLength) And iProperties.Value("Custom", "l") = Round(Measure.ExtentsWidth) Then iProperties.Value("Custom", "w") = Round(Measure.ExtentsHeight) Else
If iProperties.Value("Custom", "t") = Round(Measure.ExtentsLength) And iProperties.Value("Custom", "l") = Round(Measure.ExtentsHeight) Then iProperties.Value("Custom", "w") = Round(Measure.ExtentsWidth) 

 

0 Likes
Accepted solutions (1)
581 Views
5 Replies
Replies (5)
Message 2 of 6

theo.bot
Collaborator
Collaborator

the measurement is based on the XYZ axis. so it's difficult to get the accurate measurement for an object that is not aligned with the XYZ axis. You could consider aligning the body before using measure. also take a look at this blog: Mod the Machine: Getting the Overall Size of Parts (typepad.com)

0 Likes
Message 3 of 6

syafiqfarhan
Enthusiast
Enthusiast

Ok, Understood. That means I need to change the USC to make sure the object aligns with the XYZ axis. 

 

Public Sub TestTightBoundingBox()
    Dim invApp As Inventor.Application = GetObject(, "Inventor.Application")
    ' Have a body selected.
    Dim body As SurfaceBody
    body = invApp.CommandManager.Pick(SelectionFilterEnum.kPartBodyFilter, "Select the body.")

    ' Call the function to get the tight bounding box.
    Dim bndBox As Box = calculateTightBoundingBox(body)

    ' Draw the bounding box using a 3D sketch.
    Dim partDoc As PartDocument = invApp.ActiveDocument
    Dim sk As Sketch3D = partDoc.ComponentDefinition.Sketches3D.Add()
    Dim lines As SketchLines3D = sk.SketchLines3D

    Dim tg As TransientGeometry = invApp.TransientGeometry

    Dim minXYZ As Point = bndBox.MinPoint
    Dim minXYmaxZ As Point = tg.CreatePoint(bndBox.MinPoint.X, bndBox.MinPoint.Y, bndBox.MaxPoint.Z)
    Dim minXmaxYZ As Point = tg.CreatePoint(bndBox.MinPoint.X, bndBox.MaxPoint.Y, bndBox.MaxPoint.Z)
    Dim minXZmaxY As Point = tg.CreatePoint(bndBox.MinPoint.X, bndBox.MaxPoint.Y, bndBox.MinPoint.Z)

    Dim maxXYZ As Point = bndBox.MaxPoint
    Dim maxXYminZ As Point = tg.CreatePoint(bndBox.MaxPoint.X, bndBox.MaxPoint.Y, bndBox.MinPoint.Z)
    Dim maxXZminY As Point = tg.CreatePoint(bndBox.MaxPoint.X, bndBox.MinPoint.Y, bndBox.MaxPoint.Z)
    Dim maxXminYZ As Point = tg.CreatePoint(bndBox.MaxPoint.X, bndBox.MinPoint.Y, bndBox.MinPoint.Z)

    lines.AddByTwoPoints(minXYZ, minXYmaxZ)
    lines.AddByTwoPoints(minXYZ, minXZmaxY)
    lines.AddByTwoPoints(minXZmaxY, minXmaxYZ)
    lines.AddByTwoPoints(minXYmaxZ, minXmaxYZ)

    lines.AddByTwoPoints(maxXYZ, maxXYminZ)
    lines.AddByTwoPoints(maxXYZ, maxXZminY)
    lines.AddByTwoPoints(maxXYminZ, maxXminYZ)
    lines.AddByTwoPoints(maxXZminY, maxXminYZ)

    lines.AddByTwoPoints(minXYZ, maxXminYZ)
    lines.AddByTwoPoints(minXYmaxZ, maxXZminY)
    lines.AddByTwoPoints(minXmaxYZ, maxXYZ)
    lines.AddByTwoPoints(minXZmaxY, maxXYminZ)
End Sub


' Calculates a tight bounding box around the input body.  An optional
' tolerance argument is available.  This specificies the tolerance in
' centimeters.  If not provided the best existing display mesh is used.
Public Function calculateTightBoundingBox(body As SurfaceBody, Optional Tolerance As Double = 0) As Box
    Try
        Dim vertCount As Integer
        Dim facetCount As Integer
        Dim vertCoords() As Double = {}
        Dim normVectors() As Double = {}
        Dim vertInds() As Integer = {}

        ' If the tolerance is zero, use the best display mesh available.
        If Tolerance <= 0 Then
            ' Get the best display mesh available.
            Dim tolCount As Long
            Dim tols() As Double = {}
            Call body.GetExistingFacetTolerances(tolCount, tols)
            Dim bestTol As Double
            bestTol = tols(0)
            For i As Integer = 1 To tolCount - 1
                If tols(i) < bestTol Then
                    bestTol = tols(i)
                End If
            Next

            body.GetExistingFacets(bestTol, vertCount, facetCount, vertCoords, normVectors, vertInds)
        Else
            ' Calculate a new mesh based on the input tolerance.
            body.CalculateFacets(Tolerance, vertCount, facetCount, vertCoords, normVectors, vertInds)
        End If

        Dim tg As TransientGeometry = body.Application.TransientGeometry

        ' Calculate the range of the mesh.
        Dim smallPnt As Point = tg.CreatePoint(vertCoords(0), vertCoords(1), vertCoords(2))
        Dim largePnt As Point = tg.CreatePoint(vertCoords(0), vertCoords(1), vertCoords(2))
        For i As Integer = 1 To vertCount - 1
            Dim vertX As Double = vertCoords(i * 3)
            Dim vertY As Double = vertCoords(i * 3 + 1)
            Dim vertZ As Double = vertCoords(i * 3 + 2)

            If vertX < smallPnt.X Then
                smallPnt.X = vertX
            End If

            If vertY < smallPnt.Y Then
                smallPnt.Y = vertY
            End If

            If vertZ < smallPnt.Z Then
                smallPnt.Z = vertZ
            End If

            If vertX > largePnt.X Then
                largePnt.X = vertX
            End If

            If vertY > largePnt.Y Then
                largePnt.Y = vertY
            End If

            If vertZ > largePnt.Z Then
                largePnt.Z = vertZ
            End If
        Next

        ' Create and return a Box as the result.
        Dim newBox As Box = tg.CreateBox()
        newBox.MinPoint = smallPnt
        newBox.MaxPoint = largePnt
        Return newBox
    Catch ex As Exception
        Return Nothing
    End Try
End Function

 

I try to copy-paste into my Inventor rules but it shows 

syafiqfarhan_0-1639379180018.png

 

0 Likes
Message 4 of 6

theo.bot
Collaborator
Collaborator
Accepted solution

replace your first line with "sub main" and the code will work.

 

But if you have read the entire blog, brian mentioned he was not going into detail about repositioning of an object.

"The ability to automatically calculate a bounding box that is oriented to get the tightest fitting box possible is a very difficult problem that I haven’t seen a solution to. It’s something you could probably write a PhD paper on. The easiest approach to the orientation problem is to ask the user for help by lettin them define the coordinate system the bounding box should be calculated in, essentially defining the directions for the length, width and height. That’s possible but a little more than I wanted to go into in this post so I’ll limit this discussion to calculating a tight fitting bounding box that’s oriented parallel to the world XYZ planes"

0 Likes
Message 5 of 6

_dscholtes_
Advocate
Advocate

If you're using Inventor v2020 and up, a new feature is introduced which takes care of the orientation problem. It returns the smallest box in which the part will fit. See this post by DRoam in another thread.

Message 6 of 6

theo.bot
Collaborator
Collaborator

@_dscholtes_ 

 

Thx! I wasn't aware of this. 

0 Likes