Quantcast
Channel: Manufacturing DevBlog
Viewing all articles
Browse latest Browse all 516

Find Touching Area of Two Coplanar Faces

$
0
0

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.

 PrivateSub Test()

 

        Try

            'get application

            oApp =

                System.Runtime.InteropServices.

                Marshal.GetActiveObject("Inventor.Application")

        Catch

        EndTry

 

        Dim oPartDoc AsPartDocument = oApp.ActiveDocument

        Dim oPartDef = oPartDoc.ComponentDefinition

 

        'get face1

        Dim oFace1 AsFace

        oFace1 = oPartDoc.SelectSet(1)

        'get face2

        Dim oFace2 AsFace

        oFace2 = oPartDoc.SelectSet(2)

 

        'if two faces are planar and coplanar

        If oFace1.SurfaceType =

                SurfaceTypeEnum.kPlaneSurface _

                    And oFace2.SurfaceType =

                    SurfaceTypeEnum.kPlaneSurface Then

 

            Dim oPlane1 AsPlane

            oPlane1 = oFace1.Geometry

            Dim oPlane2 AsPlane

            oPlane2 = oFace2.Geometry

 

            ' if normal of two faces are same

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

 

                Dim extrudeDis AsDouble = 0.001

 

                'create the extrude feature from face1

                Dim oSketch1 AsPlanarSketch =

                    oPartDef.Sketches.Add(oFace1)

                Dim oEdgeInFace1 AsEdge

                ForEach oEdgeInFace1 In oFace1.Edges

                    oSketch1.AddByProjectingEntity(oEdgeInFace1)

                Next

                Dim oProfile1 AsProfile

                oProfile1 = oSketch1.Profiles.AddForSolid()

 

                Dim oExtrudeF1Def AsExtrudeDefinition

                oExtrudeF1Def =

                    oPartDef.Features.ExtrudeFeatures.

                    CreateExtrudeDefinition(oProfile1,

                        PartFeatureOperationEnum.kNewBodyOperation)

                oExtrudeF1Def.SetDistanceExtent(extrudeDis,

                        PartFeatureExtentDirectionEnum.kPositiveExtentDirection)

 

                Dim oExtrudeF1 AsExtrudeFeature

                oExtrudeF1 =

                    oPartDef.Features.ExtrudeFeatures.Add(oExtrudeF1Def)

 

                Dim oSB1 AsSurfaceBody

                oSB1 = oExtrudeF1.SurfaceBodies(1)

 

 

                'create the extrude feature from face2

                Dim oSketch2 AsPlanarSketch =

                    oPartDef.Sketches.Add(oFace2)

                Dim oEdgeInFace2 AsEdge

                ForEach oEdgeInFace2 In oFace2.Edges

                    oSketch2.AddByProjectingEntity(oEdgeInFace2)

                Next

 

                Dim oProfile2 AsProfile

                oProfile2 =

                    oSketch2.Profiles.AddForSolid()

 

                Dim oExtrudeF2Def AsExtrudeDefinition

                oExtrudeF2Def =

                    oPartDef.Features.ExtrudeFeatures.

                    CreateExtrudeDefinition(oProfile2,

                                 PartFeatureOperationEnum.kNewBodyOperation)

                oExtrudeF2Def.SetDistanceExtent(extrudeDis,

                                 PartFeatureExtentDirectionEnum.

                                  kPositiveExtentDirection)

 

                Dim oExtrudeF2 AsExtrudeFeature

                oExtrudeF2 =

                    oPartDef.Features.ExtrudeFeatures.Add(oExtrudeF2Def)

 

                Dim oSB2 AsSurfaceBody

                oSB2 = oExtrudeF2.SurfaceBodies(1)

 

                Dim oTransientB1 AsSurfaceBody =

                    oApp.TransientBRep.Copy(oSB1)

                Dim oTransientB2 AsSurfaceBody =

                    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 AsClientGraphics

                    Try

                        oClientGraphics =

                            oPartDef.ClientGraphicsCollection.Item("overlap")

                        oClientGraphics.Delete()

                    Catch ex AsException

 

                    EndTry

 

                    oApp.ActiveView.Update()

 

                    oClientGraphics =

                        oPartDef.ClientGraphicsCollection.Add("overlap")

 

                    'add graphics node

                    Dim oSurfacesNode AsGraphicsNode

                    oSurfacesNode =

                        oClientGraphics.AddNode(1)

 

                    'add the face of WeldBead.BeadFaces

                    Dim oSurfaceGraphics AsSurfaceGraphics

                    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 AsTransientGeometry

                    oTransGeom =

                        oApp.TransientGeometry

 

                    Dim oV AsVector

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

 

                    Dim oM AsMatrix

                    oM = oTransGeom.CreateMatrix()

                    Call oM.SetTranslation(oV)

 

                    oSurfacesNode.Transformation = oM

 

                    'update the view

                    oApp.ActiveView.Update()

 

 

                Else

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

                EndIf

 

                oExtrudeF1.Delete()

                oExtrudeF2.Delete()

 

            Else

 

                MsgBox("the faces are NOT coplanar!")

            EndIf

 

        Else

 

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

        EndIf

 

 

    EndSub

image


Viewing all articles
Browse latest Browse all 516

Trending Articles