6 Replies Latest reply on Aug 24, 2014 1:16 PM by Robert O'Neill

    configuration specific custom properties in macro

    Robert O'Neill

      trying to write a macro to read the current configuration custom properties, and save-as a combination of these properties.  I have it working fine for the "default" config. but can't get it to work with the current config.

      I think it is a simple as setting the "cfg" variable to the current. But I am not getting what is wrong with the ways I have tried.

       

      Dim swApp As Object
      Sub main()

           Dim swApp       As SldWorks.SldWorks
           Dim SWmoddoc    As SldWorks.ModelDoc2
           Dim config      As SldWorks.Configuration
           Dim cfg         As String
           Dim cusPropMgr  As SldWorks.CustomPropertyManager

           'used to combine together custom prop.
           Dim partnumber  As String
           Dim rev         As String
           Dim description As String
           Dim newname     As String
           Dim swname      As String

       

      Set swApp = Application.SldWorks
      Set SWmoddoc = swApp.ActiveDoc
      Set config = SWmoddoc.GetActiveConfiguration

      Set cusPropMgr = config.CustomPropertyManager

       

      cfg = "default"
      partnumber = SWmoddoc.GetCustomInfoValue(cfg, "partno")
      rev = SWmoddoc.GetCustomInfoValue(cfg, "Rev")
      description = SWmoddoc.GetCustomInfoValue(cfg, "description")
      newname = partnumber + "-r" + rev + "-" + description
      swname = partnumber + "-" + description

      PathName = SWmoddoc.GetPathName     'this gets the full path of the current open document (including filename)
      FilePath = Left(PathName, InStrRev(PathName, "\"))  'uses to full pathname to get the folder path, to be added to the save as file name

       

      If (SWmoddoc.GetType = swDocASSEMBLY) Then
          SWmoddoc.SaveAs (FilePath + partnumber + ".sldasm")
         
      ElseIf (SWmoddoc.GetType = swDocPART) Then
           SWmoddoc.SaveAs (FilePath + swname   + ".sldprt")
           SWmoddoc.SaveAs (FilePath + newname + ".step")
      End If

      End Sub

       

      thank you very much for any suggestions

      Robert

        • Re: configuration specific custom properties in macro
          Deepak Gupta

          Use this line to get the current active configuration name:

          cfg = SWmoddoc.GetActiveConfiguration.Name

            • Re: configuration specific custom properties in macro
              Robert O'Neill

              Thank you very much Deepak.  That answered the question.

               

              A couple of follow-on questions.

              How do I grab the current file name?

               

              How do I access the configuration specific properties of the part view, from a drawing file using VB.

               

              thanks again for the help.

              Robert

                • Re: configuration specific custom properties in macro
                  Deepak Gupta

                  How do I grab the current file name?

                  If you need Name then use : SWmoddoc.GetTitle

                   

                  If you need Full Path then use : SWmoddoc.GetPathName

                   

                  How do I access the configuration specific properties of the part view, from a drawing file using VB.

                  Check the macro codes here: https://forum.solidworks.com/message/447412#447412

                    • Re: configuration specific custom properties in macro
                      Robert O'Neill

                      Thank you again Deepak,

                      Now I am trying to just get the file name without the extension.

                      GetTitle returns "test.SLDPRT" But I want to open the drawing by the same name "test.SLDDRW"

                      How do I strip the file type from it?  so I can open the drawing.

                        • Re: configuration specific custom properties in macro
                          Deepak Gupta

                          Use

                           

                          Dim sFileName as String

                           

                          If InStr(UCase(SWmoddoc.GetTitle), ".SLD") > 0 Then

                                  sFileName = Left(SWmoddoc.GetTitle, Len(SWmoddoc.GetTitle) - 6) 'Change 6 to 7 in case you want to remove the . too.

                          Else

                                  sFileName = SWmoddoc.GetTitle

                          End If

                            • Re: configuration specific custom properties in macro
                              Robert O'Neill

                              Thank you once again for your great pointers Deepak.

                              I am not a programmer but some might find this useful.

                              wrote this for a particular client that wants different names for the part and drawing. Wrote this to make sure the conversion is methodical, and consistent.

                               

                              Dim swApp As Object
                              'Original intent is to rename the files based on configuration specific custom properties.
                              '
                              'STARTING ASSUMPTIONS:
                              'Part or assy is open with all the upstream assy open.
                              'Drawing by the same name will be loaded.
                              'Current configuration is correct.
                              '
                              'END RESULT:
                              'Parts are saved with new name combining part number and description.
                              'Parts are also saved in step format as part number, rev, and description.
                              'Drawings by the same name is loaded and saved with part number, and description.
                              'Drawings are also saved as PDF in the form Part number, rev, and description
                              'Assy files are saved as the new name, along with drawing.
                              '
                              'UNFINISHED
                              'I am not a programmer, I only pretend to be one on TV.
                              'No error checking has been added. This is a work in progress.
                              'This is was written for a specific client to follow their needs. As a tool to keep consistent.
                              'Can easily be modified for other properties and naming conventions.

                              Sub main()
                              'basic set up
                              Dim swApp       As SldWorks.SldWorks
                              Dim SWmoddoc    As SldWorks.ModelDoc2
                              Dim config      As SldWorks.Configuration
                              Dim cfg         As String

                              'used to combine together.
                              Dim partnumber  As String
                              Dim rev         As String
                              Dim description As String
                              Dim newname     As String
                              Dim swname      As String
                              Dim startname   As String
                              Dim dwgno       As String
                              Dim dwgname     As String
                              Dim pdfname     As String

                              Dim fileerror   As Long
                              Dim filewarning As Long

                              Set swApp = Application.SldWorks
                              Set SWmoddoc = swApp.ActiveDoc
                              Set config = SWmoddoc.GetActiveConfiguration
                              cfg = SWmoddoc.GetActiveConfiguration.Name

                              'gets the configuration specific custom prop
                              partnumber = SWmoddoc.GetCustomInfoValue(cfg, "partno")
                              rev = SWmoddoc.GetCustomInfoValue(cfg, "Rev")
                              description = SWmoddoc.GetCustomInfoValue(cfg, "description")
                              dwgno = SWmoddoc.GetCustomInfoValue(cfg, "dwgno")

                              'saves the combined file names.
                              newname = partnumber + " Rev" + rev + " " + description
                              swname = partnumber + " " + description
                              dwgname = dwgname + " " + description
                              pdfname = dwgname + " Rev" + rev + " " + description

                              PathName = SWmoddoc.GetPathName     'this gets the full path of the current open document (including filename)
                              FilePath = Left(PathName, InStrRev(PathName, "\"))  'uses to full pathname to get the folder path, to be added to the save as file name

                              'opens drawing
                              'gets file name
                              If InStr(UCase(SWmoddoc.GetTitle), ".SLD") > 0 Then
                                      startname = Left(SWmoddoc.GetTitle, Len(SWmoddoc.GetTitle) - 6)
                              Else
                                      startname = SWmoddoc.GetTitle
                              End If
                              Set SWdwgdoc = swApp.OpenDoc6(FilePath + startname + "SLDDRW", swDocDRAWING, swOpenDocOptions_Silent, "", fileerror, filewarning)

                              'saves files
                              If (SWmoddoc.GetType = swDocASSEMBLY) Then
                                  SWmoddoc.SaveAs (FilePath + partnumber + ".sldasm")
                                 
                              ElseIf (SWmoddoc.GetType = swDocPART) Then
                                   SWmoddoc.SaveAs (FilePath + swname + ".sldprt")
                                   SWmoddoc.SaveAs (FilePath + newname + ".step")
                              End If
                              If (SWdwgdoc.GetType = swDocDRAWING) Then
                                   SWdwgdoc.SaveAs (FilePath + swname + ".slddrw")
                                   SWdwgdoc.SaveAs (FilePath + newname + ".pdf")
                              End If

                              End Sub