ClientGraphics – text on each planar face

ClientGraphics allow you to provide visual cues to the user and there are many examples in the API help that show how to create ClientGraphics. This VB.NET example is interesting because it shows you how to put text on each planar face in a part.

To create ClientGraphics text, you use TextGraphics and you can control the orientation of the TextGraphics in 3D space. The TextGraphics itself is placed on the x-y plane and along the x-axis but you can reposition it by transforming the graphics node that the TextGraphics is owned by.

image

Below is code from this example that creates the ClientGraphics. In the VB.NET project you will also find the functions named FaceNormal and FaceXDir that are called from this Sub. See the comments in the code for more detail on how this example does the work. Notice that the second time you run the code it removes the ClientGraphics.

Public Sub CreateTextOnEveryPlanarFace()             Dim partDoc As PartDocument        partDoc = _InvApplication.ActiveDocument             Dim compDef As PartComponentDefinition        compDef = partDoc.ComponentDefinition             ' Check to see if client graphics already exist.        Dim clientGraphics As ClientGraphics        Try            clientGraphics = compDef. _              ClientGraphicsCollection.Item("FaceLabel")            ' They do exist so just delete them and exit.            clientGraphics.Delete()            _InvApplication.ActiveView.Update()            Exit Sub        Catch ex As Exception            ' They don't exist so create them            clientGraphics = compDef. _             ClientGraphicsCollection.Add("FaceLabel")        End Try             ' Iterate over all of the faces in the part.        Dim count As Integer        Dim oFace As Face        For Each oFace In _            compDef.SurfaceBodies.Item(1).Faces            ' Check if the current face is a plane.            If oFace.SurfaceType = _                SurfaceTypeEnum.kPlaneSurface Then                ' Get a point on the face. This                 ' function is good because you're                 ' guaranteed to get a(Point) on the                 ' face, where-as if you got the                 ' center of the face there could be                 ' a hole there so the point would                 ' be in space.                Dim facePoint As Point                facePoint = oFace.PointOnFace                     ' Get the normal of the face.                Dim normal As UnitVector                normal = FaceNormal(oFace, facePoint)                     ' Get the X direction of the face                ' (normal along the u).                Dim xDir As UnitVector              &
#160; xDir = FaceXDir(oFace, facePoint)                     ' Calculate the Y direction by crossing                ' the normal with the x.                Dim yDir As UnitVector                yDir = normal.CrossProduct(xDir)                     ' Create a transform to position the text                ' on the face.                Dim transform As Matrix                transform = _InvApplication. _                           TransientGeometry.CreateMatrix                Call transform. _                    SetCoordinateSystem(facePoint, _                    xDir.AsVector, yDir.AsVector, _                                    normal.AsVector)                     ' Create a graphics node.                Dim oNode As GraphicsNode                count = count + 1                oNode = clientGraphics.AddNode(count)                     ' Create text at the model origin.                Dim text As TextGraphics                text = oNode.AddScalableTextGraphics                Dim origin As Point                origin = _InvApplication. _                  TransientGeometry.CreatePoint(0, 0, 0)                     Call text.SetTransformBehavior(origin, _                DisplayTransformBehaviorEnum. _                              kNoTransformBehaviors, 1)                     text.Text = "Face " & count                text.DepthPriority = 1                text.FontSize = 0.1                text.Font = "Arial"                text.HorizontalAlignment = _                HorizontalTextAlignmentEnum. _                                    kAlignTextCenter                text.VerticalAlignment = _                VerticalTextAlignmentEnum. _                                    kAlignTextMiddle                text.Anchor = origin                Call text.PutTextColor(255, 255, 0)                     ' Transform the graphics node so the text                ' will lie on the face.                oNode.Transformation = transform            End If        Next             ' Update the view to see the results.        _InvApplication.ActiveView.Update()    End Sub

Wayne Brill


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading