Find Touching Area of Two Coplanar Faces

By Xiaodong Liang

Two planar faces which are coplanar are not always overlapped. To know if they are overlapped and the touching area, one workaround could be extrude the sketches based on the faces with a tiny distance, and use TransientBRep.DoBoolean to check if the bodies of the two extrude features are intersected.

The following is demo code. It assumes two planar faces are selected. One material named “Magenta” is available.

Note: in UI, if the two planar faces are within one surface body and are overlapped, they cannot be selected independently. So to make a demo, it assumes the two planar faces are from different bodies. Of course, you can distinguish two faces from one surface body in the workflow of code.

 Private Sub Test()

 

        Try

            ‘get application

            oApp =

                System.Runtime.InteropServices.

                Marshal.GetActiveObject("Inventor.Application")

        Catch

        End Try

 

        Dim oPartDoc As PartDocument = oApp.ActiveDocument

        Dim oPartDef = oPartDoc.ComponentDefinition

 

        ‘get face1

        Dim oFace1 As Face

        oFace1 = oPartDoc.SelectSet(1)

        ‘get face2

        Dim oFace2 As Face

        oFace2 = oPartDoc.SelectSet(2)

 

        ‘if two faces are planar and coplanar

        If oFace1.SurfaceType =

                SurfaceTypeEnum.kPlaneSurface _

                    And oFace2.SurfaceType =

                    SurfaceTypeEnum.kPlaneSurface Then

 

            Dim oPlane1 As Plane

            oPlane1 = oFace1.Geometry

            Dim oPlane2 As Plane

            oPlane2 = oFace2.Geometry

 

            ‘ if normal of two faces are same

            If oPlane1.Normal.IsEqualTo(oPlane2.Normal) Then

 

                Dim extrudeDis As Double = 0.001

 

                ‘create the extrude feature from face1

                Dim oSketch1 As PlanarSketch =

                    oPartDef.Sketches.Add(oFace1)

                Dim oEdgeInFace1 As Edge

                For Each oEdgeInFace1 In oFace1.Edges

                    oSketch1.AddByProjectingEntity(oEdgeInFace1)

                Next

                Dim oProfile1 As Profile

                oProfile1 = oSketch1.Profiles.AddForSolid()

 

                Dim oExtrudeF1Def As ExtrudeDefinition

                oExtrudeF1Def =

                    oPartDef.Features.ExtrudeFeatures.

                    CreateExtrudeDefinition(oProfile1,

                        PartFeatureOperationEnum.kNewBodyOperation)

                oExtrudeF1Def.SetDistanceExtent(extrudeDis,

                        PartFeatureExtentDirectionEnum.kPositiveExtentDirection)

 

                Dim oExtrudeF1 As ExtrudeFeature

                oExtrudeF1 =

                    oPartDef.Features.ExtrudeFeatures.Add(oExtrudeF1Def)

 

                Dim oSB1 As SurfaceBody

                oSB1 = oExtrudeF1.SurfaceBodies(1)

 

 

                ‘create the extrude feature from face2

                Dim oSketch2 As PlanarSketch =

                    oPartDef.Sketches.Add(oFace2)

                Dim oEdgeInFace2 As Edge

                For Each oEdgeInFace2 In oFace2.Edges

                    oSketch2.AddByProjectingEntity(oEdgeInFace2)

                Next

 

                Dim oProfile2 As Profile

                oProfile2 =

                    oSketch2.Profiles.AddForSolid()

 

                Dim oExtrudeF2Def As ExtrudeDefinition

                oExtrudeF2Def =

                    oPartDef.Features.ExtrudeFeatures.

                    CreateExtrudeDefinition(oProfile2,

                                 PartFeatureOperationEnum.kNewBodyOperation)

                oExtrudeF2Def.SetDistanceExtent(extrudeDis,

                                 PartFeatureExtentDirectionEnum.

                                  kPositiveExtentDirection)

 

                Dim oExtrudeF2 As ExtrudeFeature

                oExtrudeF2 =

                    oPartDef.Features.ExtrudeFeatures.Add(oExtrudeF2Def)

 

                Dim oSB2 As SurfaceBody

                oSB2 = oExtrudeF2.SurfaceBodies(1)

 

                Dim oTransientB1 As SurfaceBody =

                    oApp.TransientBRep.Copy(oSB1)

                Dim oTransientB2 As SurfaceBody =

               &#160
;   
oApp.TransientBRep.Copy(oSB2)

 

                ‘check if bodies of the two extrude features are intersected

                oApp.TransientBRep.DoBoolean(oTransientB1,

                                   oTransientB2,

                                   BooleanTypeEnum.kBooleanTypeIntersect)

 

                If oTransientB1.Volume(0.01) > 0 Then

                    MsgBox("The two faces are overlapped!")

 

                    ‘create a client graphics to show the intersect area

                    Dim oClientGraphics As ClientGraphics

                    Try

                        oClientGraphics =

                            oPartDef.ClientGraphicsCollection.Item("overlap")

                        oClientGraphics.Delete()

                    Catch ex As Exception

 

                    End Try

 

                    oApp.ActiveView.Update()

 

                    oClientGraphics =

                        oPartDef.ClientGraphicsCollection.Add("overlap")

 

                    ‘add graphics node

                    Dim oSurfacesNode As GraphicsNode

                    oSurfacesNode =

                        oClientGraphics.AddNode(1)

 

                    ‘add the face of WeldBead.BeadFaces

                    Dim oSurfaceGraphics As SurfaceGraphics

                    oSurfaceGraphics =

                        oSurfacesNode.AddSurfaceGraphics(oTransientB1)

 

 

                    ‘set graphics’ color. assume “Magenta” exists in the document

                    oSurfacesNode.Appearance =

                        oPartDoc.Assets("Magenta")

 

                    ‘transform the client graphics to a location

                    Dim oTransGeom As TransientGeometry

                    oTransGeom =

                        oApp.TransientGeometry

 

                    Dim oV As Vector

                    oV = oTransGeom.CreateVector(1, 1, 1)

 

                    Dim oM As Matrix

                    oM = oTransGeom.CreateMatrix()

                    Call oM.SetTranslation(oV)

 

                    oSurfacesNode.Transformation = oM

 

                    ‘update the view

                    oApp.ActiveView.Update()

 

 

                Else

                    MsgBox("The two faces are NOT overlapped!")

                End If

 

                oExtrudeF1.Delete()

                oExtrudeF2.Delete()

 

            Else

 

                MsgBox("the faces are NOT coplanar!")

            End If

 

        Else

 

            MsgBox("one of the faces are NOT planar!")

        End If

 

 

    End Sub

<

p style=”line-height: normal;margin: 0cm 0cm 0pt” class=”MsoNormal” align=”left”>image


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading