7 Replies Latest reply on Mar 6, 2017 2:59 AM by Deepak Gupta

    Save as Configuration Macro

    Daniel Fielder

      Hi,

       

      I am trying to save my part using a Tool Number in "Summary Information, Configuration Specific" but I cant get this Macro to work, it only saves as "TN".

       

      Sub main()

      Dim swApp As SldWorks.SldWorks

      Dim SWmoddoc As SldWorks.ModelDoc2

      Dim partnumber As String

      Set swApp = Application.SldWorks

      Set SWmoddoc = swApp.ActiveDoc

      partnumber = SWmoddoc.CustomInfo("ToolNumber")

      If (SWmoddoc.GetType = swDocPART) Then

          SWmoddoc.SaveAs ("TN" + partnumber + ".sldprt")
        

      ElseIf (SWmoddoc.GetType = swDocASSEMBLY) Then

           SWmoddoc.SaveAs ("TN" + partnumber + ".sldasm")
          
      ElseIf (SWmoddoc.GetType = swDocDRAWING) Then

           SWmoddoc.SaveAs ("TN" + partnumber + ".slddrw")

      End If

      End Sub

       

      Please Help!!!!

        • Re: Save as Configuration Macro
          Deepak Gupta

          Where you're getting the ToolNumber property for drawing?

           

          What is the path you're saving the files to?

           

           

          • Re: Save as Configuration Macro
            Peter Brinkhuis

            I always use the ampersand (&) to combine strings in VBA, so I checked to make sure the plus doesn't work. According to this link, the plus sign doesn't concatenate strings if the variable type isn't correct. The ampersand would be more robust.

             

            To make sure your code always works, I would also build in a lot more checks to see if the custom property is read, if the output name is what you expect, if there aren't two dots before the extension. Those kind of things.

             

            It would by the way be better / more modern to ditch the CustomInfo method and use a Custom Property Manager. The ModelDoc2 SaveAs function is also deprecated and replaced by the IModelDocExtension SaveAs

            • Re: Save as Configuration Macro
              Nilesh Patel

              Hi Daniel,

               

              I believe 'CustomInfo' only works to get the document level custom property. You are trying to get configuration specific property. In this case, I would suggest you use 'ICustomPropertyManger', I would also use 'IModelDocExtension.SaveAs' method to save the documents as Peter Brinkhuis mentioned.

               

              If you plan to save the document for each configurations (for part and assembly documents), then run the loop for each configuration.

               

              Hope this helps.

               

              Regards,

              • Re: Save as Configuration Macro
                Deepak Gupta

                Daniel, I've not tested these but you can try these.

                 

                Option Explicit
                    Dim swApp           As SldWorks.SldWorks
                    Dim swModel         As SldWorks.ModelDoc2
                    Dim swCustPropMgr   As SldWorks.CustomPropertyManager
                    Dim Path            As String
                    Dim ValOut          As String
                    Dim ResolvedValOut  As String
                    Dim wasResolved     As Boolean
                    Dim resolved        As Variant
                    Dim nErrors         As Long
                    Dim nWarnings       As Long
                Sub Main()
                    Set swApp = Application.SldWorks
                    Set swModel = swApp.ActiveDoc
                    Path = Left(swModel.GetPathName, InStrRev(swModel.GetPathName, "\"))
                
                If swModel.GetType = 3 Then
                    Set swCustPropMgr = swModel.Extension.CustomPropertyManager("")
                    swCustPropMgr.Get5 "ToolNumber", False, ValOut, ResolvedValOut, wasResolved
                    swModel.Extension.SaveAs Path + "TN" + ResolvedValOut + ".slddrw", swSaveAsCurrentVersion, swSaveAsOptions_Silent, Nothing, nErrors, nWarnings
                Else
                    
                    Set swCustPropMgr = swModel.Extension.CustomPropertyManager(swModel.ConfigurationManager.ActiveConfiguration.Name)
                    swCustPropMgr.Get5 "ToolNumber", False, ValOut, ResolvedValOut, wasResolved
                    
                    If swModel.GetType = 1 Then
                    swModel.Extension.SaveAs Path + "TN" + ResolvedValOut + ".sldprt", swSaveAsCurrentVersion, swSaveAsOptions_Silent, Nothing, nErrors, nWarnings
                    Else
                    swModel.Extension.SaveAs Path + "TN" + ResolvedValOut + ".sldasm", swSaveAsCurrentVersion, swSaveAsOptions_Silent, Nothing, nErrors, nWarnings
                    End If
                End If        
                End Sub
                
                • Re: Save as Configuration Macro
                  Daniel Fielder

                  Thanks Guys!

                   

                  I have tried Deepaks code and it worked, all that needed changing was after the Set swCustPropMgr = swModel.Extension.CustomPropertyManager("")  I needed to add in ToolNumber in between the speech marks.

                   

                  I' change all the +'s to &'s as said in Peters reply, but I cant get my head around what the CustomInfo method and the  IModelDocExtension SaveAs  control. I think I'm going to have to get myself booked in for an API training session with Solidworks.

                   

                  Also it is all on one configuration so I would guess that I wouldn't need to loop it.

                   

                  Much appreciated

                  • Re: Save as Configuration Macro
                    Daniel Fielder

                    Just one question with that program Deepak, will it work with an assembly and a drawing?

                      • Re: Save as Configuration Macro
                        Deepak Gupta

                        Daniel, I was not sure if you have the property value in model or drawing, so made the codes such that if active file is drawing, it would pull the value from drawing and in case of model it would pull from the model.

                         

                        But yes codes would work on all files.

                         

                        If you need to pull model information while in drawing, then you need to get the referenced model configuration and pull information from there.