SketchText.GetCoordinates - Inconsistent Behavior

I am having some inconsistency in the behavior of the GetCoordinates method for a SketchText object. When the text is not left-justified, the coordinate returned by the method is not correct. I was hoping at the very least to get either the left corner or the right corner of the bounding box that is touching the construction line. Has anyone else run into this? Am I missing something?

I’ve made a small macro to find the point and insert a green SketchPoint right back into the sketch at the location given. I’m not sure how to upload a sample file here, however if you create a sketch and add text and exit the sketch then the macro should run fine.

Left Justified - Not flipped / Not mirrored

MiddleJustified - Not flipped / Not mirrored

Right Justified - Not flipped / Not mirrored

Left Justified - flipped / mirrored

Middle Justified - flipped / mirrored

Right Justified - flipped / mirrored

Macro Code for Testing:

Option Explicit

Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As ModelDoc2
    Dim swSketchSegment As SketchSegment
    Dim swSketch As Sketch
    Dim swSketchText As SketchText
    Dim swSketchMgr As SketchManager
    Dim swSketchPoint As SketchPoint
    Dim swExtension As ModelDocExtension
    Dim curFeature As feature
    Dim featureTypeName As String
    Dim bRet As Boolean
    Dim vSketchSegments As Variant
    Dim vSketchSegment As Variant
    Dim vCoords As Variant
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swSketchMgr = swModel.SketchManager
    Set swExtension = swModel.Extension
    Set curFeature = swModel.FirstFeature
    Do While Not curFeature Is Nothing
        featureTypeName = curFeature.GetTypeName2()
        If featureTypeName = "ProfileFeature" Then
                Set swSketch = curFeature.GetSpecificFeature2
                vSketchSegments = swSketch.GetSketchSegments
                If (Not IsEmpty(vSketchSegments)) Then
                    For Each vSketchSegment In vSketchSegments
                        Set swSketchSegment = vSketchSegment
                        If swSketchSegment.GetType = swSketchSegments_e.swSketchText Then
                            Set swSketchText = swSketchSegment
                            Debug.Print "[F] FeatureTypeName: " & curFeature.Name & " " & featureTypeName
                            Debug.Print "    Sketch text = " & swSketchText.Text
                            vCoords = swSketchText.GetCoordinates
                            Debug.Print "    X:" & toInches(vCoords(0)) & "   Y:" & toInches(vCoords(1)) & "   Z:" & toInches(vCoords(2))
                            curFeature.Select2 False, -1
                            'swSketchMgr.AddToDB = True
                            Set swSketchPoint = swSketchMgr.CreatePoint(vCoords(0), vCoords(1), vCoords(2))
                            swSketchPoint.Color = 65280
                            'swSketchMgr.AddToDB = False
                            swModel.InsertSketch2 False
                        End If
                    Next vSketchSegment
                End If
        End If
        Set curFeature = curFeature.GetNextFeature()
    bRet = swExtension.FinishRecordingUndoObject2("Add Insert Coords for SketchText", False)
    swModel.ForceRebuild3 True
    Debug.Print "MACRO COMPLETE"
End Sub

Function toInches(number As Variant) As String

    toInches = Format(number / 0.0254, "0.0000") & " in"

End Function
1 Like

@artem this is one is a hard one if you are feeling good today!

@BigCrazyAl : Can you do me a favor and upload that sldprt sketch so people can download and test it. If there is no upload function here, I can get you access to our cloud storage.

@AmenJlili : I can’t see a way to upload support files right now. I’m not sure if it’s a server setting that needs to be changed to enable it.

I turned the settings on now. It is might take a couple of minutes to propagate.

What if you try to get the bounding box from the sketch edges via ISketchText::GetEdges2?

Hi @artem - My current workaround is using the start/end points of the edges to determine the bounding box. It gets an insertion point that is closer to the correct position than the API GetCoordinates.

This is likely the best option at the moment, but it makes the Macro code for my current project a bit messy since I am not easily able to determine if the text is mirrored/flipped in order to use the lower left bounding box position or the lower right one. I’ve had to make assumptions, which I am not a fan of doing, regarding how the part file and drawing file are set up in order to determine which position is likely the correct one.