9 Replies Latest reply on Jun 20, 2017 7:46 PM by Michael Chamerski

    swModel.Extension.RenameDocument not working properly

    Michael Chamerski

      Hi all -

       

      I'm in a dilemma here. I ran a piece of code that worked flawlessly and renamed components and configurations in assemblies. As soon as I added another segment to unsuppress all features, the entire thing stopped working. I removed the portion I added, and to my surprise (or the lack thereof), the script still wasn't working properly. I'm not receiving any errors, and the Replace function seems to be working just fine (swModel.GetTitle properly obtains document title, but swModel.Extension.RenameDocument fails to do anything). Would greatly appreciate some guidance!

       

      Function configRename(swModel As SldWorks.ModelDoc2) As Boolean

       

        ' Declare variables path, FileName, and Description to default state String

          Dim path As String, FileName As String, FileOld As String, Description As String

       

          Dim ConfigName As String

          Dim ConfigOld As String

       

          FileOld = swModel.GetTitle

          ' swApp.SendMsgToUser(FileOld)

       

          FileName = Replace(FileOld, "@", "(a)")

          FileName = Replace(FileName, "<", "(")

          FileName = Replace(FileName, ">", ")")

          FileName = Replace(FileName, "$", "(S)")

          FileName = Replace(FileName, "%", "_")

          FileName = Replace(FileName, "\", "_")

          FileName = Replace(FileName, "*", "_")

          FileName = Replace(FileName, ":", "_")

          FileName = Replace(FileName, Chr(34), Chr(34) + " (2x ')")

          FileName = Replace(FileName, "?", "_")

          FileName = Replace(FileName, "|", "_")

          FileName = Replace(FileName, ",", "_")

          FileName = Replace(FileName, "{", "(")

          FileName = Replace(FileName, "}", ")")

          FileName = Replace(FileName, "#", "(_)")

          FileName = Replace(FileName, "&", "_")

       

              ' swApp.SendMsgToUser (FileName)

          swModel.Extension.RenameDocument FileName

          

          If FileName = FileOld Then

              'Do nothing.

          Else

              ModCount = ModCount + 1

          End If

       

      swModel.ForceRebuild3 True

       

      You can see I commented out some debugging lines. When running these, I get the following:

      swApp.SendMsgToUser (FileOld) ---> "MT1_Test_{1}"

      swApp.SendMsgToUser (FileName) ---> "MT1_Test_(1)"

       

      As you can see, FileName is the proper result I'm looking for, but after running the entire macro I'm still left with "{1}" rather than "(1)". For whatever reason this worked  a couple hours ago. Now I can't wrap my head around it anymore. I've attached the entire script I have (yes it's a mess at the moment).

       

      Thanks in advance

       

      Michael

        • Re: swModel.Extension.RenameDocument not working properly
          Alastair Cardwell

          Try checking the return value from RenameDocument to see why it is failing.

           

          Dim errorsRename as Long

          errorsRename = swModelDocExt.RenameDocument(FileName)

          Debug.Print "Rename errors = " & errorsRename

           

          Check the return value againt swRenameDocumentError_e

          Alastair

            • Re: swModel.Extension.RenameDocument not working properly
              Peter Brinkhuis

              I get a return value of 2, which is short for " You must select a valid component; your selection is invalid for renaming". I can't figure out yet why that is.

               

              I would really advice you to make functions shorter (think 10 lines absolute max) and their names very descriptive, something I recently learned from reading the book Clean Code. You can certainly reuse the renaming section so you don't have to add it five times. Fewer comments is also a somewhat controversial tip. The obvious lines don't need comments, they add more clutter than clarity.

                • Re: swModel.Extension.RenameDocument not working properly
                  Michael Chamerski

                  Hi Peter -

                   

                  Definitely a good idea as far as functions. I have updated the original post with a cleaner version including multiple functions rather than a single one (and removed excessive nonsense).

                   

                  Function docRename(swModel As SldWorks.ModelDoc2) As Boolean

                   

                      Dim TitleOld As String

                   

                      TitleOld = swModel.GetTitle

                      replaceChar TitleOld

                   

                      If Updated = TitleOld Then

                          'Do nothing.

                   

                      Else

                          ModCount = ModCount + 1

                   

                      End If

                   

                      swModel.Extension.RenameDocument Updated

                      swModel.ForceRebuild3 True

                   

                  End Function

                   

                  ...

                   

                  Function replaceChar(Old As String) As String

                   

                      Updated = Replace(Old, "@", "(a)")

                      Updated = Replace(Updated, "<", "(")

                      Updated = Replace(Updated, ">", ")")

                      Updated = Replace(Updated, "$", "(S)")

                      Updated = Replace(Updated, "%", "_")

                      Updated = Replace(Updated, "\", "_")

                      Updated = Replace(Updated, "*", "_")

                      Updated = Replace(Updated, ":", "_")

                      Updated = Replace(Updated, Chr(34), Chr(34) + " (2x ')")

                      Updated = Replace(Updated, "?", "_")

                      Updated = Replace(Updated, "|", "_")

                      Updated = Replace(Updated, ",", "_")

                      Updated = Replace(Updated, "{", "(")

                      Updated = Replace(Updated, "}", ")")

                      Updated = Replace(Updated, "#", "(_)")

                      Updated = Replace(Updated, "&", "_")

                   

                  End Function

                   

                  Still trying to figure out why my selection is invalid thank you so much for taking a look, however!

                • Re: swModel.Extension.RenameDocument not working properly
                  Michael Chamerski

                  Alastair -

                   

                  Thank you for the awesome debugging tip! I'll throw this into my repertoire

                • Re: swModel.Extension.RenameDocument not working properly
                  Michael Chamerski

                  Just a quick update -

                   

                  Here's something interesting I noticed. Only the selected component/assembly is renamed! This means that if have these "illegal characters" in the primary assembly, it successfully alters them. If it's in a component within the assembly, it does nothing; however, if I directly select this component before running the macro, it then proceeds to rename the component (but then no other component in the primary assembly is affected).

                   

                  It seems to be a problem with the for-loop then, where swModel isn't properly selected perhaps? I've tried using swModel.Extension.SelectAll, but that doesn't seem to work either.

                    • Re: swModel.Extension.RenameDocument not working properly
                      Peter Brinkhuis

                      I see in your code that you are selecting bodies. I think you should be selecting components in an assembly.

                        • Re: swModel.Extension.RenameDocument not working properly
                          Michael Chamerski

                          I'm selecting bodies with the fixFeature function but I don't believe I'm doing so in the main section.

                           

                          ' If the active document is an assembly, traverse through each component, else, continue to next function

                          If swModel.GetType = swDocASSEMBLY Then

                           

                              ' Get component array of associated assembly

                              Set swAssy = swModel

                              vComps = swAssy.GetComponents(False)

                           

                              For i = 0 To UBound(vComps)

                           

                                  ' Get ith component in assembly

                                  Set swComp = vComps(i)

                           

                                  ' If active components are fully resolved, perform function 'updateProperty', else, notify user

                                  If swComp.GetSuppression = swComponentFullyResolved Then

                           

                                      ' Associate the active model to the active component

                                      Set swModel = swComp.GetModelDoc2

                           

                          It only goes as far as selecting the components in each assembly with swModel = swComp.GetModelDoc2 (swComp is the ith component in a particular assembly, if assembly). swModel.GetType = swDocASSEMBLY confirms that the selection is a top-level assembly, then gets components of each assembly.

                           

                          I'm setting swAssy equal to swModel and then again swModel equal to swComp.GetModelDoc2, but I don't think that should be causing any discrepancies. Perhaps I should rename the second swModel to swChildModel.

                           

                          Is there a way to actively select ith vComps? Maybe using selection manager... will do more research.

                      • Re: swModel.Extension.RenameDocument not working properly
                        Michael Chamerski

                        Thought I would give everyone interested here an update. I decided to use Pack & Go as an easier alternative to altering file names (that way I don't have to deal with messed up dependencies and other nonsense).

                         

                        Dassault has been nice enough to provide API examples of Pack & Go. You can modify the names of components within an assembly using the section commented "Create your own filenames for the model's documents".

                         

                        2014 SOLIDWORKS API Help - Pack and Go an Assembly (VBA)

                         

                        For some strange reason, this particular section of code disappears in documentation for version 2015 or greater.

                         

                        Enjoy!