What is the Best Way to Compute a Normal of a Face in Inventor API?

By Barbara Han

There can be many ways to compute a face’s normal using the geometric information provided by the Inventor objects. However, the best way to compute a normal using the API is to use the Face.Evaluator object.

Here is a sample that iteratesthroughl faces of a SurfaceBody and output the normal vectors for the faces that are planar.

VBA sample:

Public Sub ComputePlanarFacesNormals()

 

    Dim oPartDoc As PartDocument

    Set oPartDoc = ThisApplication.ActiveDocument

   

    Dim oFaces As Faces

    Set oFaces = oPartDoc.ComponentDefinition.SurfaceBodies(1).Faces

   

    Dim Params(1 To 2) As Double

    Dim Normals(1 To 3) As Double

   

    ‘If Faces is planar, then the Normal wil be the same all over the face

    Params(1) = 0

    Params(2) = 0

   

    Dim index As Long

    index = 1

   

    Dim oFace As Face

    For Each oFace In oFaces

   

        ‘Ensures Face is planar

        If (TypeOf oFace.Geometry Is Plane) Then

   

            Call oFace.Evaluator.GetNormal(Params, Normals)

           

            Dim oUnitNormal As UnitVector

            Set oUnitNormal = ThisApplication.TransientGeometry.CreateUnitVector(Normals(1), Normals(2), Normals(3))

           

            Debug.Print "Planar Face[" & index & "] Normal: [" & oUnitNormal.X & ", " & oUnitNormal.Y & ", " & oUnitNormal.Z & "]"

           

            index = index + 1

           

        End If

   

    Next

 

End Sub

VB.NET sample:

Private SubComputePlanarFacesNormals()

    Try

 

        Dim oPartDoc As PartDocument = m_inventorApp.ActiveDocument

 

        Dim oFaces As Faces

        oFaces = oPartDoc.ComponentDefinition.SurfaceBodies(1).Faces

 

        Dim Params(0 To 3) As Double

        Dim Normals(0 To 3) As Double

 

        ‘If Faces is planar, then the Normal wil be the same all over the face

        Params(1) = 0

        Params(2) = 0

 

        Dim index As Long = 1

 

        Dim oFace As Face

        For Each oFace In oFaces

 

            ‘Ensures Face is planar

            If (TypeOf oFace.Geometry Is Plane) Then

 

                Call oFace.Evaluator.GetNormal(Params, Normals)

 

                Dim oUnitNormal As UnitVector

                oUnitNormal = m_inventorApp.TransientGeometry.CreateUnitVector(Normals(1), Normals(2), Normals(3))

 

                System.Diagnostics.Debug.Write("Planar Face[" & index & "] Normal: [" & oUnitNormal.X & ", " & oUnitNormal.Y & ", " & oUnitNormal.Z & "]" & vbCrLf)

 

                index = index + 1

 

            End If 

        Next

    Catch ex As Exception

        MsgBox(ex.ToString())

    End Try

End Sub


Comments

2 responses to “What is the Best Way to Compute a Normal of a Face in Inventor API?”

  1. For anyone else doing this, a much simpler way to get the normal of a planar face is just to get it from the face’s “Plane” geometry, like so (in VB.NET):
    If TypeOf oFace.Geometry Is Plane Then
    Dim oFacePlane As Plane = oFace.Geometry
    Dim oUnitNormal As UnitVector = oFacePlane.Normal
    End If
    And that’s it. No need to set up Params() or Normals() and call the face’s Evaluator or CreateUnitVector method.

  2. @DRoam,
    I used to do it like that aswell. Sometimes the direction of the normal is inverted to that of the actual face when using the geometry property though, so the way presented here is the most reliable :)

Leave a Reply to JhoelCancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading