2 Replies Latest reply on Dec 5, 2012 5:26 AM by Yong Ning

    How to change text in Model with API.

    Yong Ning

      a0.jpg

       

      macro recorded code follow bottom.

       

      Sub main()

        Set swApp = Application.SldWorks
        Set Part = swApp.ActiveDoc
        Set SelMgr = Part.SelectionManager
        boolstatus = Part.Extension.SelectByID2("Sketch2", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)
        Part.EditSketch
        boolstatus = Part.Extension.SelectByID2("SketchText1", "SKETCHTEXT", 0.09499532772996, 0.01014076720589, 0, False, 0, Nothing, 0)
      End Sub

       

      Aim: Hope change note in SkethcText1.

      Thanks.

          • Re: How to change text in Model with API.
            Yong Ning

            Thanks tips

             

            'Get All Elements of Sketch Example (VB)
            'This example shows how to get all of the elements of a sketch.

             

            '---------------------------------------------
            '
            ' Preconditions: Model document is open and a sketch is selected.
            '
            ' Postconditions: None
            '
            '---------------------------------------------

             

            Option Explicit
            Public Enum swSkSegments_e
                swSketchLINE = 0
                swSketchARC = 1
                swSketchELLIPSE = 2
                swSketchSPLINE = 3
                swSketchTEXT = 4
                swSketchPARABOLA = 5
            End Enum
            Sub ProcessTextFormat _
            ( _
                swApp As SldWorks.SldWorks, _
                swModel As SldWorks.ModelDoc2, _
                swTextFormat As SldWorks.TextFormat _
            )
                Debug.Print "        BackWards                    = " & swTextFormat.BackWards
                Debug.Print "        Bold                         = " & swTextFormat.Bold
                Debug.Print "        CharHeight                   = " & swTextFormat.CharHeight
                Debug.Print "        CharHeightInPts              = " & swTextFormat.CharHeightInPts
                Debug.Print "        CharSpacingFactor            = " & swTextFormat.CharSpacingFactor
                Debug.Print "        Escapement                   = " & swTextFormat.Escapement
                Debug.Print "        IsHeightSpecifiedInPts       = " & swTextFormat.IsHeightSpecifiedInPts
                Debug.Print "        Italic                       = " & swTextFormat.Italic
                Debug.Print "        LineLength                   = " & swTextFormat.LineLength
                Debug.Print "        LineSpacing                  = " & swTextFormat.LineSpacing
                Debug.Print "        ObliqueAngle                 = " & swTextFormat.ObliqueAngle
                Debug.Print "        Strikeout                    = " & swTextFormat.Strikeout
                Debug.Print "        TypeFaceName                 = " & swTextFormat.TypeFaceName
                Debug.Print "        Underline                    = " & swTextFormat.Underline
                Debug.Print "        UpsideDown                   = " & swTextFormat.UpsideDown
                Debug.Print "        Vertical                     = " & swTextFormat.Vertical
                Debug.Print "        WidthFactor                  = " & swTextFormat.WidthFactor
                Debug.Print ""
            End Sub
            Function TransformSketchPointToModelSpace _
            ( _
                swApp As SldWorks.SldWorks, _
                swModel As SldWorks.ModelDoc2, _
                swSketch As SldWorks.Sketch, _
                swSkPt As SldWorks.SketchPoint _
            ) As SldWorks.MathPoint
                Dim swMathUtil              As SldWorks.MathUtility
                Dim swXform                 As SldWorks.MathTransform
                Dim nPt(2)                  As Double
                Dim vPt                     As Variant
                Dim swMathPt                As SldWorks.MathPoint
               
                nPt(0) = swSkPt.X:      nPt(1) = swSkPt.Y:      nPt(2) = swSkPt.Z
                vPt = nPt
               
                Set swMathUtil = swApp.GetMathUtility
                Set swXform = swSketch.ModelToSketchTransform
                Set swXform = swXform.Inverse
                Set swMathPt = swMathUtil.CreatePoint((vPt))
                Set swMathPt = swMathPt.MultiplyTransform(swXform)
                Set TransformSketchPointToModelSpace = swMathPt
            End Function
            Sub ProcessSketchLine _
            ( _
                swApp As SldWorks.SldWorks, _
                swModel As SldWorks.ModelDoc2, _
                swSketch As SldWorks.Sketch, _
                swSkLine As SldWorks.SketchLine _
            )
                Dim swStartPt               As SldWorks.SketchPoint
                Dim swEndPt                 As SldWorks.SketchPoint
                Dim swStartModPt            As SldWorks.MathPoint
                Dim swEndModPt              As SldWorks.MathPoint
                Set swStartPt = swSkLine.GetStartPoint2
                Set swEndPt = swSkLine.GetEndPoint2
                Set swStartModPt = TransformSketchPointToModelSpace(swApp, swModel, swSketch, swStartPt)
                Set swEndModPt = TransformSketchPointToModelSpace(swApp, swModel, swSketch, swEndPt)
                Debug.Print "      Start (sketch)   = (" & swStartPt.X * 1000# & ", " & swStartPt.Y * 1000# & ", " & swStartPt.Z * 1000# & ") mm"
                Debug.Print "      Start (model )   = (" & swStartModPt.ArrayData(0) * 1000# & ", " & swStartModPt.ArrayData(1) * 1000# & ", " & swStartModPt.ArrayData(2) * 1000# & ") mm"
                Debug.Print "      End   (sketch)   = (" & swEndPt.X * 1000# & ", " & swEndPt.Y * 1000# & ", " & swEndPt.Z * 1000# & ") mm"
                Debug.Print "      End   (model )   = (" & swEndModPt.ArrayData(0) * 1000# & ", " & swEndModPt.ArrayData(1) * 1000# & ", " & swEndModPt.ArrayData(2) * 1000# & ") mm"
            End Sub
            Sub ProcessSketchArc _
            ( _
                swApp As SldWorks.SldWorks, _
                swModel As SldWorks.ModelDoc2, _
                swSketch As SldWorks.Sketch, _
                swSkArc As SldWorks.SketchArc _
            )
                Dim swStartPt               As SldWorks.SketchPoint
                Dim swEndPt                 As SldWorks.SketchPoint
                Dim swCtrPt                 As SldWorks.SketchPoint
                Dim vNormal                 As Variant
                Dim swStartModPt            As SldWorks.MathPoint
                Dim swEndModPt              As SldWorks.MathPoint
                Dim swCtrModPt              As SldWorks.MathPoint
               
                Set swStartPt = swSkArc.GetStartPoint2
                Set swEndPt = swSkArc.GetEndPoint2
                Set swCtrPt = swSkArc.GetCenterPoint2
                Set swStartModPt = TransformSketchPointToModelSpace(swApp, swModel, swSketch, swStartPt)
                Set swEndModPt = TransformSketchPointToModelSpace(swApp, swModel, swSketch, swEndPt)
                Set swCtrModPt = TransformSketchPointToModelSpace(swApp, swModel, swSketch, swCtrPt)
               
                vNormal = swSkArc.GetNormalVector
               
                Debug.Print "      Start (sketch)   = (" & swStartPt.X * 1000# & ", " & swStartPt.Y * 1000# & ", " & swStartPt.Z * 1000# & ") mm"
                Debug.Print "      Start (model )   = (" & swStartModPt.ArrayData(0) * 1000# & ", " & swStartModPt.ArrayData(1) * 1000# & ", " & swStartModPt.ArrayData(2) * 1000# & ") mm"
                Debug.Print "      End   (sketch)   = (" & swEndPt.X * 1000# & ", " & swEndPt.Y * 1000# & ", " & swEndPt.Z * 1000# & ") mm"
                Debug.Print "      End   (model )   = (" & swEndModPt.ArrayData(0) * 1000# & ", " & swEndModPt.ArrayData(1) * 1000# & ", " & swEndModPt.ArrayData(2) * 1000# & ") mm"
                Debug.Print "      Center(sketch)   = (" & swCtrPt.X * 1000# & ", " & swCtrPt.Y * 1000# & ", " & swCtrPt.Z * 1000# & ") mm"
                Debug.Print "      Center(model )   = (" & swCtrModPt.ArrayData(0) * 1000# & ", " & swCtrModPt.ArrayData(1) * 1000# & ", " & swCtrModPt.ArrayData(2) * 1000# & ") mm"
                Debug.Print "      Radius           = " & swSkArc.GetRadius * 1000# & " mm"
                Debug.Print "      IsCircle         = " & CBool(swSkArc.IsCircle)
                Debug.Print "      Rot dirn         = " & swSkArc.GetRotationDir
            End Sub
            Sub ProcessSketchEllipse _
            ( _
                swApp As SldWorks.SldWorks, _
                swModel As SldWorks.ModelDoc2, _
                swSketch As SldWorks.Sketch, _
                swSkEllipse As SldWorks.SketchEllipse _
            )
                Dim swStartPt               As SldWorks.SketchPoint
                Dim swEndPt                 As SldWorks.SketchPoint
                Dim swCtrPt                 As SldWorks.SketchPoint
                Dim swMajPt                 As SldWorks.SketchPoint
                Dim swMinPt                 As SldWorks.SketchPoint
                Dim swStartModPt            As SldWorks.MathPoint
                Dim swEndModPt              As SldWorks.MathPoint
                Dim swCtrModPt              As SldWorks.MathPoint
                Dim swMajModPt              As SldWorks.MathPoint
                Dim swMinModPt              As SldWorks.MathPoint
                Set swStartPt = swSkEllipse.GetStartPoint2
                Set swEndPt = swSkEllipse.GetEndPoint2
                Set swCtrPt = swSkEllipse.GetCenterPoint2
                Set swMajPt = swSkEllipse.GetMajorPoint2
                Set swMinPt = swSkEllipse.GetMinorPoint2
                Set swStartModPt = TransformSketchPointToModelSpace(swApp, swModel, swSketch, swStartPt)
                Set swEndModPt = TransformSketchPointToModelSpace(swApp, swModel, swSketch, swEndPt)
                Set swCtrModPt = TransformSketchPointToModelSpace(swApp, swModel, swSketch, swCtrPt)
                Set swMajModPt = TransformSketchPointToModelSpace(swApp, swModel, swSketch, swMajPt)
                Set swMinModPt = TransformSketchPointToModelSpace(swApp, swModel, swSketch, swMinPt)
                Debug.Print "      Start (sketch)   = (" & swStartPt.X * 1000# & ", " & swStartPt.Y * 1000# & ", " & swStartPt.Z * 1000# & ") mm"
                Debug.Print "      Start (model )   = (" & swStartModPt.ArrayData(0) * 1000# & ", " & swStartModPt.ArrayData(1) * 1000# & ", " & swStartModPt.ArrayData(2) * 1000# & ") mm"
                Debug.Print "      End   (sketch)   = (" & swEndPt.X * 1000# & ", " & swEndPt.Y * 1000# & ", " & swEndPt.Z * 1000# & ") mm"
                Debug.Print "      End   (model )   = (" & swEndModPt.ArrayData(0) * 1000# & ", " & swEndModPt.ArrayData(1) * 1000# & ", " & swEndModPt.ArrayData(2) * 1000# & ") mm"
                Debug.Print "      Center(sketch)   = (" & swCtrPt.X * 1000# & ", " & swCtrPt.Y * 1000# & ", " & swCtrPt.Z * 1000# & ") mm"
                Debug.Print "      Center(model )   = (" & swCtrModPt.ArrayData(0) * 1000# & ", " & swCtrModPt.ArrayData(1) * 1000# & ", " & swCtrModPt.ArrayData(2) * 1000# & ") mm"
                Debug.Print "      Major (sketch)   = (" & swMajPt.X * 1000# & ", " & swMajPt.Y * 1000# & ", " & swMajPt.Z * 1000# & ") mm"
                Debug.Print "      Major (model )   = (" & swMajModPt.ArrayData(0) * 1000# & ", " & swMajModPt.ArrayData(1) * 1000# & ", " & swMajModPt.ArrayData(2) * 1000# & ") mm"
                Debug.Print "      Minor (sketch)   = (" & swMinPt.X * 1000# & ", " & swMinPt.Y * 1000# & ", " & swMinPt.Z * 1000# & ") mm"
                Debug.Print "      Minor (model )   = (" & swMinModPt.ArrayData(0) * 1000# & ", " & swMinModPt.ArrayData(1) * 1000# & ", " & swMinModPt.ArrayData(2) * 1000# & ") mm"
            End Sub
            Sub ProcessSketchSpline _
            ( _
                swApp As SldWorks.SldWorks, _
                swModel As SldWorks.ModelDoc2, _
                swSketch As SldWorks.Sketch, _
                swSkSpline As SldWorks.SketchSpline _
            )
                Dim vSplinePtArr            As Variant
                Dim vSplinePt               As Variant
                Dim swSplinePt              As SldWorks.SketchPoint
                Dim swSplineModPt           As SldWorks.MathPoint
               
                vSplinePtArr = swSkSpline.GetPoints2
                For Each vSplinePt In vSplinePtArr
                    Set swSplinePt = vSplinePt
                    Set swSplineModPt = TransformSketchPointToModelSpace(swApp, swModel, swSketch, swSplinePt)
               
                    Debug.Print "      Spline (sketch)  = (" & swSplinePt.X * 1000# & ", " & swSplinePt.Y * 1000# & ", " & swSplinePt.Z * 1000# & ") mm"
                    Debug.Print "      Spline (model )  = (" & swSplineModPt.ArrayData(0) * 1000# & ", " & swSplineModPt.ArrayData(1) * 1000# & ", " & swSplineModPt.ArrayData(2) * 1000# & ") mm"
                Next vSplinePt
            End Sub
            Sub ProcessSketchText _
            ( _
                swApp As SldWorks.SldWorks, _
                swModel As SldWorks.ModelDoc2, _
                swSketch As SldWorks.Sketch, _
                swSkText As SldWorks.SketchText _
            )
                Dim vCoordPt                As Variant
                Dim swMathUtil              As SldWorks.MathUtility
                Dim swXform                 As SldWorks.MathTransform
                Dim swCoordModPt            As SldWorks.MathPoint
               
                vCoordPt = swSkText.GetCoordinates
               
                Set swMathUtil = swApp.GetMathUtility
                Set swXform = swSketch.ModelToSketchTransform
                Set swXform = swXform.Inverse
                Set swCoordModPt = swMathUtil.CreatePoint((vCoordPt))
                Set swCoordModPt = swCoordModPt.MultiplyTransform(swXform)
                Debug.Print "      Coords (sketch)  = (" & vCoordPt(0) * 1000# & ", " & vCoordPt(1) * 1000# & ", " & vCoordPt(2) * 1000# & ") mm"
                Debug.Print "      Coords (model )  = (" & swCoordModPt.ArrayData(0) * 1000# & ", " & swCoordModPt.ArrayData(1) * 1000# & ", " & swCoordModPt.ArrayData(2) * 1000# & ") mm"
                Debug.Print "      Use doc fmt      = " & swSkText.GetUseDocTextFormat
                Debug.Print "      Text             = " & swSkText.Text
               
                ProcessTextFormat swApp, swModel, swSkText.GetTextFormat
            End Sub
            Sub ProcessSketchParabola _
            ( _
                swApp As SldWorks.SldWorks, _
                swModel As SldWorks.ModelDoc2, _
                swSketch As SldWorks.Sketch, _
                swSkParabola As SldWorks.SketchParabola _
            )
                Dim swApexPt                As SldWorks.SketchPoint
                Dim swStartPt               As SldWorks.SketchPoint
                Dim swEndPt                 As SldWorks.SketchPoint
                Dim swFocalPt               As SldWorks.SketchPoint
                Dim swApexModPt             As SldWorks.MathPoint
                Dim swStartModPt            As SldWorks.MathPoint
                Dim swEndModPt              As SldWorks.MathPoint
                Dim swFocalModPt            As SldWorks.MathPoint
                Set swApexPt = swSkParabola.GetApexPoint2
                Set swStartPt = swSkParabola.GetStartPoint2
                Set swEndPt = swSkParabola.GetEndPoint2
                Set swFocalPt = swSkParabola.GetFocalPoint2
                Set swApexModPt = TransformSketchPointToModelSpace(swApp, swModel, swSketch, swApexPt)
                Set swStartModPt = TransformSketchPointToModelSpace(swApp, swModel, swSketch, swStartPt)
                Set swEndModPt = TransformSketchPointToModelSpace(swApp, swModel, swSketch, swEndPt)
                Set swFocalModPt = TransformSketchPointToModelSpace(swApp, swModel, swSketch, swFocalPt)
                Debug.Print "      Apex  (sketch)   = (" & swApexPt.X * 1000# & ", " & swApexPt.Y * 1000# & ", " & swApexPt.Z * 1000# & ") mm"
                Debug.Print "      Apex  (model )   = (" & swApexModPt.ArrayData(0) * 1000# & ", " & swApexModPt.ArrayData(1) * 1000# & ", " & swApexModPt.ArrayData(2) * 1000# & ") mm"
                Debug.Print "      Start (sketch)   = (" & swStartPt.X * 1000# & ", " & swStartPt.Y * 1000# & ", " & swStartPt.Z * 1000# & ") mm"
                Debug.Print "      Start (model )   = (" & swStartModPt.ArrayData(0) * 1000# & ", " & swStartModPt.ArrayData(1) * 1000# & ", " & swStartModPt.ArrayData(2) * 1000# & ") mm"
                Debug.Print "      End   (sketch)   = (" & swEndPt.X * 1000# & ", " & swEndPt.Y * 1000# & ", " & swEndPt.Z * 1000# & ") mm"
                Debug.Print "      End   (model )   = (" & swEndModPt.ArrayData(0) * 1000# & ", " & swEndModPt.ArrayData(1) * 1000# & ", " & swEndModPt.ArrayData(2) * 1000# & ") mm"
                Debug.Print "      Focal (sketch)   = (" & swFocalPt.X * 1000# & ", " & swFocalPt.Y * 1000# & ", " & swFocalPt.Z * 1000# & ") mm"
                Debug.Print "      Focal (model )   = (" & swFocalModPt.ArrayData(0) * 1000# & ", " & swFocalModPt.ArrayData(1) * 1000# & ", " & swFocalModPt.ArrayData(2) * 1000# & ") mm"
            End Sub
            Sub main()
                Dim sSkSegmentsName(5)      As String
                Dim swApp                   As SldWorks.SldWorks
                Dim swModel                 As SldWorks.ModelDoc2
                Dim swSelMgr                As SldWorks.SelectionMgr
                Dim swFeat                  As SldWorks.Feature
                Dim swSketch                As SldWorks.Sketch
                Dim vSkSegArr               As Variant
                Dim vSkSeg                  As Variant
                Dim swSkSeg                 As SldWorks.SketchSegment
                Dim swSkLine                As SldWorks.SketchLine
                Dim swSkArc                 As SldWorks.SketchArc
                Dim swSkEllipse             As SldWorks.SketchEllipse
                Dim swSkSpline              As SldWorks.SketchSpline
                Dim swSkText                As SldWorks.SketchText
                Dim swSkParabola            As SldWorks.SketchParabola
                Dim vID                     As Variant
                Dim i                       As Long
                Dim bRet                    As Boolean
               
                sSkSegmentsName(swSketchLINE) = "swSketchLINE"
                sSkSegmentsName(swSketchARC) = "swSketchARC"
                sSkSegmentsName(swSketchELLIPSE) = "swSketchELLIPSE"
                sSkSegmentsName(swSketchSPLINE) = "swSketchSPLINE"
                sSkSegmentsName(swSketchTEXT) = "swSketchTEXT"
                sSkSegmentsName(swSketchPARABOLA) = "swSketchPARABOLA"
               
               
                Set swApp = Application.SldWorks
                Set swModel = swApp.ActiveDoc
                Set swSelMgr = swModel.SelectionManager
                Set swFeat = swSelMgr.GetSelectedObject5(1)
                Set swSketch = swFeat.GetSpecificFeature
               
                Debug.Print "Feature = " & swFeat.Name & " [" & swSketch.Is3D & "]"
                Debug.Print "  Sketch Segments:"
               
                vSkSegArr = swSketch.GetSketchSegments
                For Each vSkSeg In vSkSegArr
                    Set swSkSeg = vSkSeg
                   
                    vID = swSkSeg.GetID
                    Debug.Print "    ID = [" & vID(0) & "," & vID(1) & "]"
                    Debug.Print "      Type             = " & sSkSegmentsName(swSkSeg.GetType)
                    Debug.Print "      ConstGeom        = " & swSkSeg.ConstructionGeometry
               
                    Select Case swSkSeg.GetType
                        Case swSketchLINE
                            Set swSkLine = swSkSeg
                           
                            ProcessSketchLine swApp, swModel, swSketch, swSkLine
                       
                        Case swSketchARC
                            Set swSkArc = swSkSeg
                       
                            ProcessSketchArc swApp, swModel, swSketch, swSkArc
                       
                        Case swSketchELLIPSE
                            Set swSkEllipse = swSkSeg
                           
                            ProcessSketchEllipse swApp, swModel, swSketch, swSkEllipse
                       
                        Case swSketchSPLINE
                            Set swSkSpline = swSkSeg
                           
                            ProcessSketchSpline swApp, swModel, swSketch, swSkSpline
                       
                        Case swSketchTEXT
                            Set swSkText = swSkSeg
                           
                            ProcessSketchText swApp, swModel, swSketch, swSkText
                       
                        Case swSketchPARABOLA
                            Set swSkParabola = swSkSeg
                           
                            ProcessSketchParabola swApp, swModel, swSketch, swSkParabola
                           
                        Case Default
                            Debug.Assert False
                    End Select
                Next vSkSeg
            End Sub
            '---------------------------------------------

             

            Code  Result

             

            Feature = Sketch2 [False]
              Sketch Segments:
                ID = [0,1]
                  Type             = swSketchLINE
                  ConstGeom        = True
                  Start (sketch)   = (0, 10, 0) mm
                  Start (model )   = (0, 10, 0) mm
                  End   (sketch)   = (100, 10, 0) mm
                  End   (model )   = (100, 10, 0) mm
                ID = [0,1]
                  Type             = swSketchTEXT
                  ConstGeom        = False
                  Coords (sketch)  = (4.65384615384616, 10, 0) mm
                  Coords (model )  = (4.65384615384616, 10, 0) mm
                  Use doc fmt      = False
                  Text             = Solidworks
                    BackWards                    = False
                    Bold                         = False
                    CharHeight                   = .015
                    CharHeightInPts              = 42
                    CharSpacingFactor            = .9
                    Escapement                   = 0
                    IsHeightSpecifiedInPts       = False
                    Italic                       = False
                    LineLength                   = 0
                    LineSpacing                  = .001
                    ObliqueAngle                 = 0
                    Strikeout                    = False
                    TypeFaceName                 = Century Gothic
                    Underline                    = False
                    UpsideDown                   = False
                    Vertical                     = False
                    WidthFactor                  = 1

             

            *******************************


            Sub lls1()
              Dim tmp, sss As SldWorks.Sketch, Arr
              Dim swSketchText As SketchText
              Set swApp = Application.SldWorks
              Set swModel = swApp.ActiveDoc
              Set swSelMgr = swModel.SelectionManager

             

            ' Get the selected feature of the sketch text


              tmp = swModel.Extension.SelectByID2("SketchText1", "SKETCHTEXT", 0, 0, 0, False, 0, Nothing, 0)
              tmp = swModel.Extension.SelectByID2("Sketch2", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)
              Set swFeat = swSelMgr.GetSelectedObject6(1, 0)
              Set swSketch = swFeat.GetSpecificFeature2

              Arr = swSketch.GetSketchTextSegments
             
              Set swSketchText = Arr(0)
              Debug.Print swSketchText.Text
              Dim SwSketchTextFormat As SldWorks.TextFormat
              With swSketchText
                Set SwSketchTextFormat = .GetTextFormat
                Debug.Print .Text
              End With
              With SwSketchTextFormat
                Debug.Print .Bold, .CharHeight, .LineLength, .CharSpacingFactor
              End With
              tmp = swModel.Extension.SelectByID2("Wrap1", "BODYFEATURE", 0, 0, 0, False, 0, Nothing, 0)
              swModel.EditUnsuppress
              ''
              Path = swModel.GetPathName
              Path = Left(Path, InStrRev(Path, "\"))
              swModel.SaveAs2 Path & "me.EPRT", 0, True, False
              swModel.SaveAs2 Path & "me.PDF", 0, True, False
             
              tmp = swModel.Extension.SelectByID2("Wrap1", "BODYFEATURE", 0, 0, 0, False, 0, Nothing, 0)
              swModel.EditSuppress
            End Sub