12 Replies Latest reply on Oct 20, 2015 11:06 AM by Patrick Malone

    Change Drawing Text from Bold to Regular?

    Patrick Malone

      Upfront I will state this, limited programming knowledge.

      I've done some auto lisp (Autocad) programming so

      I understand some programming concepts.

      VBA/API are new waters that I am stepping into so it won't take much to exceed my knowledge base at this point.

       

      I am starting off by recorded macro...

      Here is what I have.

       

      ' ******************************************************************************

      ' macro recorded on 10/16/15

      ' ******************************************************************************

      Dim swApp As Object

       

      Dim Part As Object

      Dim boolstatus As Boolean

      Dim longstatus As Long, longwarnings As Long

       

      Sub main()

       

      Set swApp = _

      Application.SldWorks

       

      Set Part = swApp.ActiveDoc

      boolstatus = Part.Extension.SelectByID2("Sheet2", "SHEET", 0.838669660377358, 0.44465719245283, 0, False, 0, Nothing, 0)

      Part.EditTemplate

      Part.EditSketch

      Part.ClearSelection2 True

      boolstatus = Part.Extension.SelectByID2("DetailItem2219@Sheet Format12", "NOTE", 0.703758615709871, 3.44084042712842E-02, 0, False, 0, Nothing, 0)

      boolstatus = Part.Extension.SelectByID2("DetailItem2217@Sheet Format12", "NOTE", 0.728801374375636, 3.47156773837475E-02, 0, True, 0, Nothing, 0)

      boolstatus = Part.Extension.SelectByID2("DetailItem2176@Sheet Format12", "NOTE", 0.759682322178205, 3.37938580463574E-02, 0, True, 0, Nothing, 0)

      boolstatus = Part.Extension.SelectByID2("DetailItem2221@Sheet Format12", "NOTE", 0.826360587582757, 3.42547677150525E-02, 0, True, 0, Nothing, 0)

      boolstatus = Part.Extension.SelectByID2("DetailItem2216@Sheet Format12", "NOTE", 0.708367712396822, 2.35002087788344E-02, 0, False, 0, Nothing, 0)

      boolstatus = Part.Extension.SelectByID2("DetailItem2175@Sheet Format12", "NOTE", 0.797784188123663, 2.61120302347731E-02, 0, False, 0, Nothing, 0)

      boolstatus = Part.Extension.SelectByID2("DetailItem2216@Sheet Format12", "NOTE", 0.73279592483766, 2.48829377849196E-02, 0, True, 0, Nothing, 0)

      boolstatus = Part.Extension.SelectByID2("DetailItem2220@Sheet Format12", "NOTE", 0.829740591819854, 2.54974840098463E-02, 0, True, 0, Nothing, 0)

      boolstatus = Part.Extension.SelectByID2("DetailItem2229@Sheet Format12", "NOTE", 0.708674985509285, 1.55111078547867E-02, 0, False, 0, Nothing, 0)

      boolstatus = Part.Extension.SelectByID2("DetailItem2226@Sheet Format12", "NOTE", 0.750003219135609, 1.55111078547867E-02, 0, True, 0, Nothing, 0)

      boolstatus = Part.Extension.SelectByID2("DetailItem2228@Sheet Format12", "NOTE", 0.738941387086927, 1.50501981860916E-02, 0, True, 0, Nothing, 0)

      boolstatus = Part.Extension.SelectByID2("DetailItem2227@Sheet Format12", "NOTE", 0.783035078725421, 0.015357471298555, 0, True, 0, Nothing, 0)

      boolstatus = Part.Extension.SelectByID2("DetailItem2222@Sheet Format12", "NOTE", 0.795326003223956, 1.52038347423233E-02, 0, True, 0, Nothing, 0)

      boolstatus = Part.Extension.SelectByID2("DetailItem2223@Sheet Format12", "NOTE", 0.808538747059881, 1.61256540797134E-02, 0, True, 0, Nothing, 0)

      boolstatus = Part.Extension.SelectByID2("DetailItem2225@Sheet Format12", "NOTE", 0.818371486658709, 1.42820154049332E-02, 0, True, 0, Nothing, 0)

      boolstatus = Part.Extension.SelectByID2("DetailItem2224@Sheet Format12", "NOTE", 0.829433318707391, 1.50501981860916E-02, 0, True, 0, Nothing, 0)

      boolstatus = Part.Extension.SelectByID2("DetailItem84@Sheet Format12", "NOTE", 0.741287052720168, 0.551920199589513, 0, False, 0, Nothing, 0)

      boolstatus = Part.Extension.SelectByID2("DetailItem85@Sheet Format12", "NOTE", 0.721665405361847, 0.552279132163141, 0, True, 0, Nothing, 0)

      boolstatus = Part.Extension.SelectByID2("DetailItem83@Sheet Format12", "NOTE", 0.659330781741816, 0.552159487971932, 0, True, 0, Nothing, 0)

      boolstatus = Part.Extension.SelectByID2("DetailItem86@Sheet Format12", "NOTE", 0.687686455058414, 0.549527315765328, 0, False, 0, Nothing, 0)

      boolstatus = Part.Extension.SelectByID2("DetailItem88@Sheet Format12", "NOTE", 0.730279787128915, 0.548689806426863, 0, True, 0, Nothing, 0)

      boolstatus = Part.Extension.SelectByID2("DetailItem88@Sheet Format12", "NOTE", 0.745953176177329, 0.548689806426863, 0, True, 0, Nothing, 0)

      Part.EditSheet

      Part.EditSketch

      Part.ClearSelection2 True

      Part.ViewZoomtofit2

      End Sub

       

       

      It appears to touch these objects but it does not change any of them.

      Nothing in the code suggests any property change (specifically to Regular)

       

      I selected several similar text objects, went to the Note properties, picked Font,

      on the Choose Font dialog I only changed the text style from Bold to Regular.

       

      Selected several more similar text objects, repeat...

      Selected several more similar text objects, repeat....

       

      This was at least 5 different "selection sets"

       

      in each drawing sheet the text items are in the same placement.

       

      Having to correct some legacy drawings and this would save quite a bit of time...

       

       

      I am not sure where to start

       

      = = = =

      Here are the same steps with just fewer objects in case it makes it easier to assist me..

       

      Dim swApp As Object

       

      Dim Part As Object

      Dim boolstatus As Boolean

      Dim longstatus As Long, longwarnings As Long

       

      Sub main()

       

      Set swApp = _

      Application.SldWorks

       

      Set Part = swApp.ActiveDoc

      boolstatus = Part.Extension.SelectByID2("Sheet2", "SHEET", 0.751470654740781, 1.51601575968275E-02, 0, False, 0, Nothing, 0)

      Part.EditTemplate

      Part.EditSketch

      Part.ClearSelection2 True

      boolstatus = Part.Extension.SelectByID2("DetailItem2229@Sheet Format12", "NOTE", 0.707642546161755, 1.54023018431205E-02, 0, False, 0, Nothing, 0)

      boolstatus = Part.Extension.SelectByID2("DetailItem2226@Sheet Format12", "NOTE", 0.74808063529268, 1.54023018431205E-02, 0, True, 0, Nothing, 0)

      boolstatus = Part.Extension.SelectByID2("DetailItem2222@Sheet Format12", "NOTE", 0.794088042088343, 1.54023018431205E-02, 0, True, 0, Nothing, 0)

      Part.EditSheet

      Part.EditSketch

      Part.ClearSelection2 True

      Part.ViewZoomtofit2

      End Sub

        • Re: Change Drawing Text from Bold to Regular?
          Viktor Bovzdarenko

          That is true

          Macro did not actually record the change of of the text. Last recorded action is the selection of the text.

          Why don't you just save your template manually and the text will be as you wish when you create new drawings?

          If you still want to go with API then here is the example of similar core from API help:

          2016 SOLIDWORKS API Help - Change Text Format Example (VBA)

          ' Preconditions:

          ' 1. Open a part, assembly, or drawing that has at least

          ' one annotation containing text.

          ' 2. Select that annotation.

          ' 3. Open the Immediate window.

          '

          ' Postconditions:

          ' 1. Changes the text format of the selected annotation.

          ' 2. Examine the annotation in the graphics area and

          ' the Immediate window.

          '----------------------------------------------------

          Option Explicit

          Sub main()
             Dim swApp                      As SldWorks.SldWorks
             Dim swModel                    As SldWorks.ModelDoc2
             Dim swSelMgr                    As SldWorks.SelectionMgr
             Dim swAnnObj                    As Object
             Dim swAnn                      As SldWorks.Annotation
             Dim swTextFormat                As SldWorks.TextFormat
             Dim i                          As Long
             Dim bRet                        As Boolean

              Set swApp = Application.SldWorks
              Set swModel = swApp.ActiveDoc
              Set swSelMgr = swModel.SelectionManager
              Set swAnnObj = swSelMgr.GetSelectedObject6(1, -1)
              Set swAnn = swAnnObj.GetAnnotation    'Get text format information
              Debug.Print "File = " & swModel.GetPathName
             
              For i = 0 To swAnn.GetTextFormatCount - 1
                  Set swTextFormat = swAnn.GetTextFormat 'Change text to be 10mm, bold, italic, and Comic Sans MS font
                  swTextFormat.CharHeight = 0.01
                  swTextFormat.Bold = True
                  swTextFormat.Italic = True
                  swTextFormat.TypeFaceName = "Comic Sans MS"
                  bRet = swAnn.SetTextFormat(i, False, swTextFormat)
              Next
          End Sub
            • Re: Change Drawing Text from Bold to Regular?
              Patrick Malone

              Why don't you just save your template manually and the text will be as you wish when you create new drawings?

               

              That is what I have now and am starting with.

              I am taking a legacy drawing and dragging all the views into the new title block format.

              The new title block is correct. I have had more than once instance in which when all the views

              were moved over, certain text in the title block changed to bold. It's one of those WTF! things....

              ie: Drawing number, Sheet Number, etc...

               

              This is an API attempt to fix it when that happens.

              • Re: Change Drawing Text from Bold to Regular?
                Patrick Malone

                Oops!

                Replied to the wrong post!

                • Re: Change Drawing Text from Bold to Regular?
                  Patrick Malone

                  https://forum.solidworks.com/external-link.jspa?url=http%3A%2F%2Fhelp.solidworks.com%2F2016%2FEnglish%2Fapi%2Fsldworksap…

                   

                  How can I make this work on ALL selected text?

                  (It only works on one text object at a time...)

                  If I use the Solidworks Text Format button (in the left panel) any changes are applied to all selected text....

                   

                  Option Explicit

                  Sub main()
                     Dim swApp                      As SldWorks.SldWorks
                     Dim swModel                    As SldWorks.ModelDoc2
                     Dim swSelMgr                    As SldWorks.SelectionMgr
                     Dim swAnnObj                    As Object
                     Dim swAnn                      As SldWorks.Annotation
                     Dim swTextFormat                As SldWorks.TextFormat
                     Dim i                          As Long
                     Dim bRet                        As Boolean

                      Set swApp = Application.SldWorks
                      Set swModel = swApp.ActiveDoc
                      Set swSelMgr = swModel.SelectionManager
                      Set swAnnObj = swSelMgr.GetSelectedObject6(1, -1)
                      Set swAnn = swAnnObj.GetAnnotation    'Get text format information
                      Debug.Print "File = " & swModel.GetPathName
                     
                      For i = 0 To swAnn.GetTextFormatCount - 1
                          Set swTextFormat = swAnn.GetTextFormat 'Change text to be 10mm, bold, italic, and Comic Sans MS font
                          swTextFormat.CharHeight = 0.01
                          swTextFormat.Bold = True
                          swTextFormat.Italic = True
                          swTextFormat.TypeFaceName = "Comic Sans MS"
                          bRet = swAnn.SetTextFormat(i, False, swTextFormat)
                      Next
                  End Sub
                • Re: Change Drawing Text from Bold to Regular?
                  Alice Caspari

                  I'm not 100% what your looking for but below is a macro that goes to annotations in each view and changes them to use the document font. You could modify it to do what you need by modifying                 bool = notes.SetTextFormat(i, True, swTextFormat)

                  Or if you could be a bit more specific I can add the changes for you.

                   

                  Option Explicit

                  ' Select all annotations and set to use Document Font - 08/12/15 by Casparia

                     
                      Dim swApp As SldWorks.SldWorks

                      Dim swModel As SldWorks.ModelDoc2   
                      Dim swDraw As SldWorks.DrawingDoc      
                      Dim swView As SldWorks.View

                      Dim swTextFormat As SldWorks.textFormat

                      Dim i As Long  
                      Dim j As Long   
                      Dim annotations As Variant

                      Dim notes As SldWorks.annotation
                      Dim count As Long
                      Dim bool As Boolean

                    Sub main()

                      Set swApp = Application.SldWorks

                      Set swModel = swApp.ActiveDoc   
                      Set swDraw = swModel

                     
                      ' Iterate through all the views on the drawing to find annotations (first view is the sheet so starts at next view)

                      Set swView = swDraw.GetFirstView
                      Set swView = swView.GetNextView

                      Do While Not swView Is Nothing
                          count = swView.GetAnnotationCount
                                
                      ' Iterate through all the annotations in each drawing view that has them and set to use document font

                      If count > 0 Then

                              annotations = swView.GetAnnotations

                              For j = 0 To UBound(annotations)

                                  Set notes = annotations(j)           
                                  bool = notes.SetTextFormat(i, True, swTextFormat)

                              Next j

                          End If

                          Set swView = swView.GetNextView

                      Loop

                  End Sub

                    • Re: Change Drawing Text from Bold to Regular?
                      Patrick Malone

                      Alice...

                       

                      To elaborate a bit more, it's not in any of the drawing views.

                      It's in the Sheet Format.

                      Certain text (which I hope to create a set selection set of) is changing from Regular to Bold.

                      I am trying to create a macro to change it back when that happens...

                       

                       

                       

                      Just noticed this...

                       

                      Set swTextFormat = swAnn.GetTextFormat 'Change text to be 10mm, bold, italic, and Comic Sans MS font

                              swTextFormat.CharHeight = 0.01

                              swTextFormat.Bold = True

                              swTextFormat.Italic = True

                              swTextFormat.TypeFaceName = "Comic Sans MS"

                              bRet = swAnn.SetTextFormat(i, False, swTextFormat)

                       

                      What I am trying to do is select all the text that has the same properties (specifically Height) and change it to Regular.

                      I have to create a few selection sets because not all of the text is the same Height. If all the text selected has the same properties

                      I only need to change the one thing (Bold to Regular)

                       

                      I guess I could modify the code for each instance of differing Heights...

                        • Re: Change Drawing Text from Bold to Regular?
                          Alice Caspari

                          Patrick,

                           

                          Is the legacy drawing in Solidworks?

                          If it is why are you dragging views from the old drawing? Just open up the old  right Click on Sheet in the Feature Tree and select Properties then select the sheet format you want to use.

                          It sounds like your Bold issue is that in your title block text in the sheet format it is set to use Document Font which is possibly set to bold?

                          Attach your template if you want more help.

                            • Re: Change Drawing Text from Bold to Regular?
                              Patrick Malone

                              Just open up the old right Click on Sheet in the Feature Tree and select Properties then select the sheet format you want to use..

                               

                              That was the original Idea. Problem is, we have tables and blocks (in the drawing - not the sheet format) and the client wants them in exact locations.

                              Can't have a table in a sheet format. We have several in our drawing formatted specifically per client standards..

                              Blocks (in the drawing) don't come in with the Sheet Format change.

                              Dragging the views is the quicker way vs having to insert, insert, insert, insert.

                              We have discussed and tested ad nauseum.... :-\

                               

                              Plus, I really want to learn how to do this.

                              It's a bit exciting even though I am a novice, if even that with VBA/API stuff...

                              I have other questions too but one thing at a time...

                               

                               

                              It sounds like your Bold issue is that in your title block text in the sheet format it is set to use Document Font which is possibly set to bold?

                               

                              I will look into this next drawing...

                               

                              = = = =

                               

                              How would I incorporate these two items together?

                               

                              Set swTextFormat = swAnn.GetTextFormat 'Change text to be 10mm, bold, italic, and Comic Sans MS font

                                      swTextFormat.Bold = True

                                      swTextFormat.TypeFaceName = "SWRomns"

                                      bRet = swAnn.SetTextFormat(i, False, swTextFormat)

                               

                               

                              = = = = = = my original attempt ========

                               

                              Dim swApp As Object

                               

                              Dim Part As Object

                              Dim boolstatus As Boolean

                              Dim longstatus As Long, longwarnings As Long

                               

                              Sub main()

                               

                              Set swApp = _

                              Application.SldWorks

                               

                              Set Part = swApp.ActiveDoc

                              boolstatus = Part.Extension.SelectByID2("Sheet2", "SHEET", 0.751470654740781, 1.51601575968275E-02, 0, False, 0, Nothing, 0)

                              Part.EditTemplate

                              Part.EditSketch

                              Part.ClearSelection2 True

                              boolstatus = Part.Extension.SelectByID2("DetailItem2229@Sheet Format12", "NOTE", 0.707642546161755, 1.54023018431205E-02, 0, False, 0, Nothing, 0)

                              boolstatus = Part.Extension.SelectByID2("DetailItem2226@Sheet Format12", "NOTE", 0.74808063529268, 1.54023018431205E-02, 0, True, 0, Nothing, 0)

                              boolstatus = Part.Extension.SelectByID2("DetailItem2222@Sheet Format12", "NOTE", 0.794088042088343, 1.54023018431205E-02, 0, True, 0, Nothing, 0)

                              Part.EditSheet

                              Part.EditSketch

                              Part.ClearSelection2 True

                              Part.ViewZoomtofit2

                              End Sub

                              • Re: Change Drawing Text from Bold to Regular?
                                Patrick Malone

                                >>It sounds like your Bold issue is that in your title block text in the sheet format it is set to use Document Font which is possibly set to bold?

                                 

                                I just verified that it is not using the Document Font In my *Target* Title Block.

                                I wonder if in my Legacy drawings this is the case...?

                          • Re: Change Drawing Text from Bold to Regular?
                            Patrick Malone

                            Easy enough... Figured it out...

                             

                            ==

                             

                            Dim swApp As Object

                             

                            Dim Part As Object

                            Dim boolstatus As Boolean

                            Dim longstatus As Long, longwarnings As Long

                             

                            Sub main()

                             

                            Set swApp = _

                            Application.SldWorks

                             

                            Set Part = swApp.ActiveDoc

                            boolstatus = Part.ActivateView("Drawing View1")

                            boolstatus = Part.Extension.SelectByID2("DetailItem1484@Drawing View1", "NOTE", 0.270250169969187, 0.152884902406487, 0, False, 0, Nothing, 0)

                            Part.FONTBOLD False

                            Part.ClearSelection2 True

                            End Sub

                             

                            ========= and to change to BOLD =====

                             

                            Dim swApp As Object

                             

                            Dim Part As Object

                            Dim boolstatus As Boolean

                            Dim longstatus As Long, longwarnings As Long

                             

                            Sub main()

                             

                            Set swApp = _

                            Application.SldWorks

                             

                            Set Part = swApp.ActiveDoc

                            boolstatus = Part.ActivateView("Drawing View1")

                            boolstatus = Part.Extension.SelectByID2("DetailItem1484@Drawing View1", "NOTE", 0.274271642695254, 0.153503590518189, 0, False, 0, Nothing, 0)

                            Part.FONTBOLD True

                            Part.ClearSelection2 True

                            End Sub

                             

                            = = = =

                             

                            I am just learning. To me this is an adventure.

                            I have tons of questions with hopefully just as simple answers...

                             

                            Please be patient with a novice...

                              • Re: Change Drawing Text from Bold to Regular?
                                Viktor Bovzdarenko

                                Try this. The same as your last time but for all annotations, whereas your code was changing parameters only for views  and notes with specified ID

                                 

                                Dim swApp As Object

                                Dim swModel As SldWorks.ModelDoc2

                                Dim swSelMgr As SldWorks.SelectionMgr

                                Dim boolstatus As Boolean

                                 

                                 

                                Sub main()

                                Dim swDraw As SldWorks.DrawingDoc

                                Dim swSheet As SldWorks.Sheet

                                Dim swView As View

                                 

                                 

                                Set swApp = Application.SldWorks

                                Set swModel = swApp.ActiveDoc

                                Set swModelDocExt = swModel.Extension

                                 

                                 

                                Set swDraw = swModel

                                Set swSheet = swDraw.GetCurrentSheet

                                Dim swTitleBlock As TitleBlock

                                Set swTitleBlock = swSheet.TitleBlock

                                Dim vNotes As Variant

                                Dim swViews As Variant

                                swViews = swSheet.GetViews

                                For j = 0 To UBound(swViews)

                                    Set swView = swViews(j)

                                    vNotes = swView.GetNotes

                                    For i = 0 To UBound(vNotes)

                                        Dim swNote As Note

                                        Set swNote = vNotes(i)

                                        Dim swAnn As IAnnotation

                                        Set swAnn = swNote.GetAnnotation()

                                        Dim swTextFormat As SldWorks.TextFormat

                                        Set swTextFormat = swAnn.GetTextFormat(i) 'Change text to be 10mm, bold, italic, and Comic Sans MS font

                                        swTextFormat.CharHeight = 0.02

                                        swTextFormat.BOLD = True

                                        swTextFormat.Italic = True

                                        swTextFormat.TypeFaceName = "Comic Sans MS"

                                        bRet = swAnn.SetTextFormat(i, False, swTextFormat) 

                                    Next

                                Next

                                End Sub