16 Replies Latest reply on Oct 23, 2015 7:17 AM by Patrick Malone

    Replace specific text (not Search/Replace)??

    Patrick Malone

      I have several pieces of text in a title block that I want to change.

      I tried doing a record macro and it doesn't do it completely.

      When I run the macro, I see it touch each piece of text in succession so

      I am part way there. (Trying a different method than the search/replace

      because I cannot assume the text value I am replacing is constant.)

      I do know what it needs to be though...

       

      I am looking into how to do a 'DoReplaceString strNote01.. for each item (boolstatus = item lines below)

      that I want changed but do not know how to incorporate that function into my code.

      (Admitting to being a total novice-wannabe-newbie programmer)

      I am as interested in learning how to do this as much as getting it to work...

       

      This is what I recorded and edited.

      Adding my variables (the "mytext1" items will be replaced with the actual text I am to use of course)

       

      Am I close or way off here?

       

      Any help is appreciated!!

       

      = = = = = == = == = == = == = == = == = == = == = == = == = =

       

      Dim swApp As Object

       

      Dim Part As Object

      Dim boolstatus As Boolean

      Dim longstatus As Long, longwarnings As Long

       

      Dim strNote01 As String = "mytext1"

      Dim strNote02 As String = "mytext2"

      Dim strNote03 As String = "mytext3"

      Dim strNote04 As String = "mytext4"

      Dim strNote05 As String = "mytext5"

       

      Sub main()

       

      Set swApp = _

      Application.SldWorks

       

      Set Part = swApp.ActiveDoc

      boolstatus = Part.Extension.SelectByID2("Sheet1", "SHEET", 0.544872734994015, 6.84439369661325E-02, 0, False, 0, Nothing, 0)

      Part.EditTemplate

      Part.EditSketch

      Part.ClearSelection2 True

      boolstatus = Part.Extension.SelectByID2("DetailItem2173@Sheet Format11", "NOTE", 0.551409013785214, 7.55404682251479E-02, 0, False, 0, Nothing, 0)

       

      'Hoping I can replace the text selected here with my "mytext" item....

       

      Part.ClearSelection2 True

      boolstatus = Part.Extension.SelectByID2("DetailItem2172@Sheet Format11", "NOTE", 0.552529518720848, 6.84439369661325E-02, 0, False, 0, Nothing, 0)

      Part.ClearSelection2 True

      boolstatus = Part.Extension.SelectByID2("DetailItem2171@Sheet Format11", "NOTE", 0.555144030237327, 6.09739040619058E-02, 0, False, 0, Nothing, 0)

      Part.ClearSelection2 True

      boolstatus = Part.Extension.SelectByID2("DetailItem2170@Sheet Format11", "NOTE", 0.542258223477536, 5.68653859645811E-02, 0, False, 0, Nothing, 0)

      Part.ClearSelection2 True

      boolstatus = Part.Extension.SelectByID2("DetailItem2164@Sheet Format11", "NOTE", 0.57269860756226, 3.33347823162669E-02, 0, False, 0, Nothing, 0)

      Part.ClearSelection2 True

      Part.EditSheet

      Part.EditSketch

      'Part.ViewZoomtofit2

      End Sub

        • Re: Replace specific text (not Search/Replace)??
          Roland Schwarz

          I sincerely want to help, but it just seems you are not quite far enough along to help.

           

          First lesson: macro recording suuuuuucks! Double-sucky when selections are involved. The sooner you get to where you can write from scratch, the better.

           

          If you want to learn code, start simple. Learn VBA macro programming in a simpler environment, like Excel. Once you are comfortable with object-oriented programming in VB and Excel, you will be in a better position to understand SW's more complex object structure.

            • Re: Replace specific text (not Search/Replace)??
              Patrick Malone

              Thanks Roland...

               

              My problem is I work exclusively in a Solidworks environment so learning something

              that doesn't help me directly in SW is not as exciting as things that help me get my job done..

               

              This one little thing has to be done on hundreds of drawings so

              it will help me exponentially if I can get it figured out...

              Looking at other examples helps me greatly... (I learned autolisp that way)

               

              I am working my way through some of the basics (so far, loops, arrays, conditionals)

              but I have to get work done 8 hours a day...

              My need exceeds my ability so I am trying to record where I can (it actually works sometimes)

               

              I am working my way through vba at night when I can...

               

              Slow going... but I am going...;-)

            • Re: Replace specific text (not Search/Replace)??
              Roland Schwarz

              Ooh, but think of the possibilities. Learn VB. Learn Excel. Write your own macros. Link Excel to SW!

               

              One nice thing about learning API is that you learn a lot about the inner workings of SW.

               

              Not exciting?

              • Re: Replace specific text (not Search/Replace)??
                Patrick Malone

                I am not trying to write complex applications.

                Just simple little routines for a specific purpose.

                At this point, 99% of what I am after is pick an object - usually text (or a set) and have something done to it.

                 

                I had asked in another thread about changing text from BOLD to REGULAR.

                It was to only apply to objects I selected. I am thinking small little things not giant application size routines run on entire drawings.

                This small snippet of code does exactly what I was after for the Bold to Regular conversion...

                '-----

                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)

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

                Part.FONTBOLD False

                Part.ClearSelection2 True

                End Sub

                '-----

                 

                Looking at that simplicity I can't help but wonder is it that hard to pick one piece of text and have it change to something else I have specified in the code.

                My world is small but I have to repeat tiny things hundreds of times..

                 

                :-\

                • Re: Replace specific text (not Search/Replace)??
                  Adam Hoffman

                  Patrick,

                  What you are wanting to do is possible, but you have to keep in mind that when replacing text using a macro you have to have a way of filtering what text you want replaced. If not, then your macro will end up just looping through all notes in your sheet template and replacing it. Which I wouldn't think would be your desired goal here. Do you have any specific identifiers for the notes you are wanting to replace?

                    • Re: Replace specific text (not Search/Replace)??
                      Patrick Malone

                      In everything that I've recorded (and in this case) I select exactly what I want the routine to work on before I run the macro.

                      I am thinking in much smaller terms than all of you with the extensive programming background.

                       

                      I have 5 pieces of text in the exact same place on every drawing.

                       

                      my "code" in the above post...

                      It picks the exact items I want changed. Nothing more.

                      I don't want it looking at or changing anything else...

                      I see the macro highlight these items in turn. That part worked.

                       

                      boolstatus = Part.Extension.SelectByID2("DetailItem2172@Sheet Format11", "NOTE", 0.552529518720848, 6.84439369661325E-02, 0, False, 0, Nothing, 0)

                      Part.ClearSelection2 True

                       

                      In simplest terms, pick a specific piece of text and have it changed to a string that is assigned to a variable.

                      or in my case go to the first item specified (in my selection set - the macro did that part) change item 1 to string 1, item 2 to string 2.

                       

                      I wish the macro recorder executed the exact steps that you record.

                       

                      I wish there were little applets to look at as examples.

                      Here is how you change the value of a selected piece of text.

                      Here is how you set the height of a selected table row.

                      Here is how you change the line type of a selected line (or selection set)

                      It may not be possible but that is how I learn best.... seeing examples in small scale

                      then I can build upon it...

                       

                      Thank you (all) for attempting to assist.

                      I realize that I am learning how to add and subtract in a class full of Geometry and Calculus students..

                       

                      I understand your frustration and appreciate your patience and help!

                      • Re: Replace specific text (not Search/Replace)??
                        Patrick Malone

                        Adam...

                        please see my last post below for a different spin on my question...

                      • Re: Replace specific text (not Search/Replace)??
                        Patrick Malone

                        Dim swApp As Object

                         

                        Dim Part As Object

                        Dim boolstatus As Boolean

                        Dim longstatus As Long, longwarnings As Long

                         

                        Dim strNote01 As String = "mytext1"

                         

                        Sub main()

                         

                        Set swApp = _

                        Application.SldWorks

                         

                        Set Part = swApp.ActiveDoc

                        boolstatus = Part.Extension.SelectByID2("Sheet1", "SHEET", 0.544872734994015, 6.84439369661325E-02, 0, False, 0, Nothing, 0)

                        Part.EditTemplate

                        Part.EditSketch

                        Part.ClearSelection2 True

                        boolstatus = Part.Extension.SelectByID2("DetailItem2173@Sheet Format11", "NOTE", 0.551409013785214, 7.55404682251479E-02, 0, False, 0, Nothing, 0)

                        Part.EditSheet

                        Part.EditSketch

                        'Part.ViewZoomtofit2

                        End Sub

                         

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

                        Let me approach this from a different angle.

                        ASSUMING that this is the exact object I want to change the text value of...

                        boolstatus = Part.Extension.SelectByID2("DetailItem2173@Sheet Format11", "NOTE", 0.551409013785214, 7.55404682251479E-02, 0, False, 0, Nothing, 0)

                        What steps are required to change ONLY that text/note to Dim strNote01 As String = "mytext1"

                         

                        No error checking, no additional conditions.

                        I know that the object I picked is the object that I want changed.

                         

                        Is that "doable" with very minimal additional code?

                         

                        I know this is duct-tape to you guys but it is million dollar-duct tape to me...

                         

                        Thanks!