18 Replies Latest reply on Mar 22, 2016 6:28 AM by Cad Admin

    Macro file type to run check

    Cad Admin

      I have a macro i run to insert a block into drawings.  It works fine, but i would like to add a file-type check loop in it.

      I want to run the macro if it is a solidworks drawing, but close if it is anything else...no message or prompt needed

       

      I know a solution is not to run it on anything other than a slddrw, but this macro is being included in a transitional step (script) in EPDM, and will be tasked to run on any file being run through the workflow.

        • Re: Macro file type to run check
          Matt Gjertson

          I do something similar in almost all my macros at this point.  This little snippet should take care of what you're looking for.

           

          Set swApp = Application.SldWorks

          Set Part = swApp.ActiveDoc

          If Part Is Nothing Then

              MsgBox "Please open a file first."

              End

          End If

           

          If Part.GetType <> 3 Then '1 = part, 2 = assembly, 3 = drawing

              End

          End If

            • Re: Macro file type to run check
              Cad Admin

              Matt,

              I'm getting a type mismatch error on:

              Set swDraw = swApp.ActiveDoc

              basically the response if i'm in a model

                • Re: Macro file type to run check
                  Matt Gjertson

                  Ahh, I bet you declared as a drawing document from the get go.  The problem is you don't know at that point what type of document it's going to be yet.  Use IModelDoc2 as a general document, and then get the type.  From there, depending on what type it is, set the document to whatever you need.  Try this:

                   

                  Dim swApp as ISldWorks

                  Dim swPart as IModelDoc2

                  Dim swDraw as IDrawingDoc

                   

                  Set swApp = Application.SldWorks

                  Set swPart = swApp.ActiveDoc

                  If swPart Is Nothing Then

                      MsgBox "Please open a file first."

                      End

                  End If

                   

                  If swPart.GetType = 3 Then '1 = part, 2 = assembly, 3 = drawing

                      Set swDraw = swPart

                  Else

                      End

                  End If

                    • Re: Macro file type to run check
                      Cad Admin

                      Matt,

                      I'm altering some existing code below, it works fine on drawings, but faults on models.  I would like to integrate your methodology for file verification.

                       

                      Dim swApp As Object

                          Dim swDraw As SldWorks.DrawingDoc

                          Dim swSheet As SldWorks.Sheet

                          Dim swRevTable As SldWorks.RevisionTableAnnotation

                          Dim swTableAnn As SldWorks.TableAnnotation

                          Dim UserName As String

                          Dim UserName1 As String

                       

                          Dim REVISION_DATA_TO_ADD As Variant 'values for columns in new revision

                       

                          Sub main()

                       

                              UserName = Environ("USERNAME")

                              UserName1 = UCase(UserName)

                           'use empty string to skip the column

                              REVISION_DATA_TO_ADD = Array("00", "RELEASED FOR PRODUCTION", Date, "-", UserName1)

                          

                              Set swApp = Application.SldWorks

                              Set swDraw = swApp.ActiveDoc

                              Set swSheet = swDraw.GetCurrentSheet

                              Set swRevTable = swSheet.RevisionTable

                       

                              If Not swRevTable Is Nothing Then

                                  Set swTableAnn = swRevTable

                                  Dim i As Integer

                                  For i = swTableAnn.RowCount - 1 To 0 Step -1

                                  

                                      Dim revId As Long

                                      revId = swRevTable.GetIdForRowNumber(i)

                                   

                                      If revId <> 0 Then

                                          swRevTable.DeleteRevision revId, True

                                      End If

                       

                                 Next

                               

                                  swRevTable.AddRevision "NewA"

                               

                                  For i = 0 To UBound(REVISION_DATA_TO_ADD)

                             

                                      If REVISION_DATA_TO_ADD(i) <> "" Then

                                          swTableAnn.Text(swTableAnn.RowCount - 1, i) = REVISION_DATA_TO_ADD(i)

                                      End If

                       

                                  Next

                       

                              End If

                       

                          End Sub

                       

                      What would the best solution to embed into this

                        • Re: Macro file type to run check
                          Deepak Gupta

                          Try this

                           

                               Dim swApp As Object

                              Dim swDraw As SldWorks.DrawingDoc

                               Dim swModel As SldWorks.ModelDoc2

                              Dim swSheet As SldWorks.Sheet

                              Dim swRevTable As SldWorks.RevisionTableAnnotation

                              Dim swTableAnn As SldWorks.TableAnnotation

                              Dim UserName As String

                              Dim UserName1 As String

                           

                              Dim REVISION_DATA_TO_ADD As Variant 'values for columns in new revision

                           

                              Sub main()

                           

                                  UserName = Environ("USERNAME")

                                  UserName1 = UCase(UserName)

                               'use empty string to skip the column

                                  REVISION_DATA_TO_ADD = Array("00", "RELEASED FOR PRODUCTION", Date, "-", UserName1)

                             

                                  Set swApp = Application.SldWorks

                                  Set swModel= swApp.ActiveDoc

                          If swModel Is Nothing Then

                              MsgBox "Please open a file first."

                              End

                          End If

                           

                          If swModel.GetType = 3 Then '1 = part, 2 = assembly, 3 = drawing

                              Set swDraw = swModel

                          Else

                              End

                          End If