6 Replies Latest reply on Apr 6, 2018 1:31 PM by William Miller

    assign variable to custom property

    William Miller

      Hi All,

       

      I am trying to write a macro to automatically take all open drawings and save them as PDF and DXF files. Right now I am currently working on assigning the filename to one of my custom properties but am having issues.  My code is below. For some reason, it says that my property "DrawingNo" is empty. Any ideas what I am doing wrong?

       

       

      Option Explicit
          Dim swApp               As SldWorks.SldWorks
          Dim swModel             As SldWorks.ModelDoc2
          Dim swModelDocExt       As SldWorks.ModelDocExtension
          Dim swExportData        As SldWorks.ExportPdfData
          Dim filename            As String
      
          Dim swDraw              As SldWorks.DrawingDoc
          Dim swFeat              As SldWorks.Feature
          Dim swCustProp       As SldWorks.CustomPropertyManager
          Dim swView          As SldWorks.View
      
      Sub main()
          Set swApp = Application.SldWorks
          Dim Path As String
          Dim PDFpath As String
          Dim filename As String
          Dim Rev As String
          Set swApp = Application.SldWorks
      
      
          Set swDraw = swApp.ActiveDoc
               
          Set swView = swDraw.GetFirstView
          Set swView = swView.GetNextView
          Set swModel = swView.ReferencedDocument
          Set swCustProp = swModel.Extension.CustomPropertyManager(swView.ReferencedConfiguration)
         
          'Opens folder browser dialogue and prompts user for PDF Folder
          Path = SelectFolder("Select PDF Folder", "C:\Users\USER\Desktop")
           
          Path = Path + "\"
           
          'create the PDF directory if it doesn't exist
          PDFpath = Path & "PDF"
          If Dir(PDFpath, vbDirectory) = "" Then MkDir PDFpath
         
          'Saves as PDF
          Set swDraw = swModel
          filename = swCustProp.Get("DrawingNo")
          Rev = swDraw.CustomInfo("Revision")
          filename = filename & "_REV-" & Rev
          swDraw.SaveAs (PDFpath & filename & ".pdf")
         
          'Saves as DXF
         
         
         
      End Sub
      
      Function SelectFolder(Optional Title As String, Optional TopFolder _
                               As String) As String
          Dim objShell As New Shell32.Shell
          Dim objFolder As Shell32.Folder
      
      'If you use 16384 instead of 1 on the next line,
      'files are also displayed
          Set objFolder = objShell.BrowseForFolder(0, Title, 1, TopFolder)
          If Not objFolder Is Nothing Then
              SelectFolder = objFolder.Items.Item.Path
          End If
      End Function
      
        • Re: assign variable to custom property
          Michael Spens

          Hey William,

          Do you expect the DrawingNo property to be in the model referenced in the first view, specifically its configuration?  That might be the problem.  If your DrawingNo property is in the drawing itself, you need to use the drawing's CustomPropertyManager interface rather than the view's referenced model.

           

          Mike

            • Re: assign variable to custom property
              William Miller

              Hi Michael,

               

              Thanks for responding. I expect it to be in the model itself (more specifically the first view and configuration), NOT the drawing. Isn't that what I am doing with my code?

               

              Sorry, I'm not new programming, just the Solidworks API!

                • Re: assign variable to custom property
                  Michael Spens

                  Yes, that's where the code is getting the value - the configuration of the model in the first view found.  Any chance you have multiple views on the drawing of different models?  Check swView.GetName2 to see if it's the view you expect.   Sorry to ask, but have you confirmed that the property and a value are in the specific configuration of the model in question?

                   

                  Mike

                    • Re: assign variable to custom property
                      William Miller

                      The part that I am testing this code on is only on a part (rather than an assembly), and the drawing itself only has the part model on it, so I don't think that is it.

                       

                      A more pointed question I should be asking is this:
                      with my line of code filename = swCustProp.Get("DrawingNo") is this pointing to the custom properties in the Drawing itself? Or the model in the first view? The line of code above that Rev = swDraw.CustomInfo("Revision") is working correctly, so I can't help but think that it is pointing to the custom properties of the drawing itself, NOT the model.

                       

                      If this is the case, how do I change that?

                        • Re: assign variable to custom property
                          Deepak Gupta

                          William Miller wrote:

                           

                          with my line of code filename = swCustProp.Get("DrawingNo") is this pointing to the custom properties in the Drawing itself? Or the model in the first view?

                          swCustProp is set to model configuration as defined in these line, so it getting this property from model configuration. So make sure your model configuration referred by drawing has the property named "DrawingNo" and it has a value.

                           

                           

                          Set swModel = swView.ReferencedDocument 

                           

                           

                          Set swCustProp = swModel.Extension.CustomPropertyManager(swView.ReferencedConfiguration

                           

                          Also swCustProp.Get is an obsolete method, use swCustProp.Get5 instead.

                            • Re: assign variable to custom property
                              William Miller

                              Thanks for the info Deepak. Been seeing a lot of your responses on other threads with this project.

                               

                              I found out what I was doing wrong. I had parentheses around the arguments for .Get5 and I was trying to assign the line to an actual variable, when the line of code already does that.

                               

                              For anyone else who is confused, my code is pasted below:

                               

                                   'gets drawing number from model

                                  Set swCustProp = swModel.Extension.CustomPropertyManager("")

                                  swCustProp.Get5 "DrawingNo", True, ValOut, filename, False

                               

                                  'gets rev from drawing properties

                                  Rev = swDraw.CustomInfo("Revision")

                                 

                                  'sets the filename to format: M*123456_REV-**

                                  filename = filename & "_REV-" & Rev

                               

                                  'saves as PDF

                                  swDraw.SaveAs (PDFpath & filename & ".pdf")

                                  'Saves as DXF

                                  swDraw.SaveAs (DXFpath & filename & ".dxf")