10 Replies Latest reply on Nov 27, 2013 2:29 PM by Matt Martens

    SW Macro - How to reference $PRPSHEET info in VB??

    Jim Mitchell

      Hello All,

       

       

      I am writing this macro to help handle our drawing releases throughout the company I work for. We have issues with Solidworks assemblies (and the occasional drawing) not being the most stable things in the world so I have "pieced" this together to save clean copies of our work at revision releases in .pdf format (I'm no programmer, last time I used VB was 5 years ago in intro to engineering).

       

       

      Feel free to review and suggest ways to clean up the code but the main reason I am here is I wish to automate the process of pulling the revision letter from the part. Currently I have the code set up to prompt the user to enter the current Revision level ([COLOR="red"]see line in Red[/COLOR]). I would like to rewrite this line to pull from our property sheet for the part/assembly that is referenced in the open drawing. Each part we make has a Property sheet we pull info from to fill out the title block on our drawings, this typically requires a $PRPSHEET call in the drawing template to pull the info in automatically. I would really like to know how I can reference this information from my VB code and dimension it to a string variable to be used later in the code.

       

       

      Does anyone know how to execute this?

       

       

       

       

      [CODE]Imports SolidWorks.Interop.sldworks

      Imports SolidWorks.Interop.swconst

      Imports System.Runtime.InteropServices

      Imports System

      Imports System.IO

       

       

      Partial Class SolidWorksMacro

       

       

      Public Sub main()

       

       

       

       

       

       

      Dim swDoc As ModelDoc2 = Nothing

      Dim longstatus As Integer = 0

      Dim CurrentFilePath As String = ""

      Dim CurrentFileName As String = ""

      Dim ParentFilePath As String = ""

      Dim NewFileName As String = ""

      Dim ReleaseFilePath As String = ""

      Dim RevFilePath As String = ""

      Dim RevFileName As String = ""

      Dim RevLevelAdd As String = ""

      Dim RevLevel As String = ""

       

       

       

       

      swDoc = CType(swApp.ActiveDoc, ModelDoc2)

       

       

      [COLOR="red"]RevLevel = InputBox("Please enter the new revision level", "Revision Entry", "A", , )[/COLOR]

       

       

       

       

       

       

      CurrentFilePath = swDoc.GetPathName

      CurrentFileName = IO.Path.GetFileNameWithoutExtension(CurrentFilePath)

      ParentFilePath = IO.Path.GetDirectoryName(CurrentFilePath)

      RevLevelAdd = CurrentFileName & "_Rev " & RevLevel

      My.Computer.FileSystem.CreateDirectory(ParentFilePath & "\Revision Archive\")

      RevFileName = IO.Path.ChangeExtension(RevLevelAdd, ".pdf")

      RevFilePath = ParentFilePath & "\Revision Archive\"

      If File.Exists(RevFilePath & RevFileName) Then

      MsgBox("Revision Level Already Exists - Cannot Overwrite File", MsgBoxStyle.Information)

      Exit Sub

      Else

      longstatus = swDoc.SaveAs3(RevFilePath & RevFileName, 0, 0)

      MsgBox("Saved Drawing as: " & RevFilePath & RevFileName, MsgBoxStyle.Information)

       

       

       

       

       

       

      CurrentFilePath = swDoc.GetPathName

      CurrentFileName = IO.Path.GetFileName(CurrentFilePath)

      ParentFilePath = IO.Path.GetDirectoryName(CurrentFilePath)

      My.Computer.FileSystem.CreateDirectory(ParentFilePath & "\Release\")

      NewFileName = IO.Path.ChangeExtension(CurrentFileName, ".pdf")

      ReleaseFilePath = ParentFilePath & "\Release\"

      longstatus = swDoc.SaveAs3(ReleaseFilePath & NewFileName, 0, 0)

      MsgBox("Saved Drawing as: " & ReleaseFilePath & NewFileName, MsgBoxStyle.Information)

      MsgBox("All Done, Have A Nice Day!!", MsgBoxStyle.Information)

       

       

      End If

       

       

       

       

       

       

      End Sub

       

       

       

       

      Public swApp As SldWorks

       

       

       

       

      End Class[/CODE]

        • Re: SW Macro - How to reference $PRPSHEET info in VB??
          Jim Mitchell

          Okay so I have kind of figured out how to draw information from the property tables of a part with the CustomPropertyManager and swCustProp.Get4 now I have to figure out how to reference the part in the drawing and not the drawing itself. Right now all I get is the property table available from within the print, there has to be a way to redirect where the macro is looking for the information.


          Or alternately I think I can write a sub with a public dim to open the part/assembly file, reference the "Revision" property, store it in my variable, close the part and exit the sub to continue on with the main.

            • Re: SW Macro - How to reference $PRPSHEET info in VB??
              Josh Brady

              You don't need to "open" the part/assembly file.  It's already open in memory when you open the drawing. 

               

              You just need to get a handle to the proper document.  Any drawing can reference any number of part/assembly documents.  The information about which view drives $prpsheet is the CustomPropertyView of the Sheet.  Then you can get the ModelDoc2 from View::ReferencedDocument.  Then you can get custom properties from that ModelDoc2

                • Re: SW Macro - How to reference $PRPSHEET info in VB??
                  Jim Mitchell

                  Odd request but could you show me an example of this without blending it into my existing code. I am trying to learn and I'd like to figure this out but I need just a little insight into waht you are referringt to.

                    • Re: SW Macro - How to reference $PRPSHEET info in VB??
                      Josh Brady

                      Here is some code.

                       

                      This code can be run from a drawing, part, or assembly document.  After the code runs, the object variable "part" will have a pointer to the ModelDoc2 object corresponding to:

                       

                      1) Part document: the current document

                      2) Drawing document: either

                           a) The model referenced by the view specified in sheet properties or

                           b) The Part or Assembly document that is currently selected.

                      3) Assembly document: either

                           a) The current document or

                           b) The Part or Assembly document that is currently selected.

                       

                      Set swApp = Application.SldWorks

                      Set part = swApp.ActiveDoc

                      Set SelMgr = part.SelectionManager

                      swDocType = part.GetType

                       

                       

                      If swDocType = swDocDRAWING Then

                          'first we will go ahead and set "part" to be the model referenced by the first view.

                          Set swDrawing = part  'get the drawing object of the active model

                          Set swSheet = swDrawing.GetCurrentSheet

                          sViewName = swSheet.CustomPropertyView

                          Set swView = swDrawing.GetFirstView    'get view

                          While swView.GetReferencedModelName <> sViewName

                              Set swView = swView.GetNextView

                              If swView Is Nothing Then Exit Sub

                          Wend

                          sRefModelName = swView.GetReferencedModelName       'get name of referenced part

                          Set part = swView.ReferencedDocument

                          'Now we will check and see if stuff was selected.  If so, change part to that.

                          If SelMgr.GetSelectedObjectCount2(-1) > 0 Then

                              If SelMgr.GetSelectedObjectType2(1) = swSelDRAWINGVIEWS Then

                                  Set myView = SelMgr.GetSelectedObject5(1)

                                  Set ModRefByView = myView.ReferencedDocument

                                  If ModRefByView.GetType = swDocPART Then

                                      Set part = ModRefByView

                                  ElseIf ModRefByView.GetType = swDocASSEMBLY Then

                                      If SelMgr.GetSelectedObjectCount = 2 Then

                                          Set MyDwgComp = SelMgr.GetSelectedObjectsComponent2(2)

                                          Set myComp = MyDwgComp.Component

                                          Set part = myComp.GetModelDoc2

                                      End If

                                  End If

                              ElseIf SelMgr.GetSelectedObjectType3(1, -1) = swSelCOMPONENTS Then

                                  Set MyDwgComp = SelMgr.GetSelectedObject6(1, -1)

                                  Set myComp = MyDwgComp.Component

                                  Set part = myComp.GetModelDoc2

                              End If

                       

                          End If

                          'Exit Sub

                      ElseIf swDocType = swDocASSEMBLY Then

                          If SelMgr.GetSelectedObjectCount2(-1) > 0 Then

                              Set myComp = SelMgr.GetSelectedObjectsComponent2(1)

                              Set part = myComp.GetModelDoc2

                          End If

                      End If

                • Re: SW Macro - How to reference $PRPSHEET info in VB??
                  Matt Martens

                  Jim,

                   

                  I took an existing Save As PDF macro I had and altered it to mirror what you have going on in the code above. It is in VBA, so take it if you want.