2 Replies Latest reply on Feb 28, 2014 12:51 PM by Mike Sveda

    Can I run a Solidworks Macro during a state change?

    Mike Sveda

      I'm trying to figure out how to run a Solidworks macro during a state change?  Any ideas?

        • Re: Can I run a Solidworks Macro during a state change?
          Michael Dekoning

          Use an EPDM task triggered on state change. Copy the Convert task and modify it with your macro.

            • Re: Can I run a Solidworks Macro during a state change?
              Mike Sveda

              I have no API training.  I am not sure how to modify this macro to run as a task.

               

              Option Explicit

               

              Sub main()

               

                  Dim swApp                   As SldWorks.SldWorks

                  Dim swModel                 As SldWorks.ModelDoc2

                  Dim vConfNameArr            As Variant

                  Dim sConfigName             As String

                  Dim nStart                  As Single

                  Dim i                       As Long

                  Dim bShowConfig             As Boolean

                  Dim bRebuild                As Boolean

                  Dim bRet                    As Boolean

                  Dim CurFeature              As SldWorks.Feature

               

                  Set swApp = CreateObject("SldWorks.Application")

                  Set swModel = swApp.ActiveDoc

               

              ' Is document active?

               

              If swModel Is Nothing Then

               

                  swApp.SendMsgToUser2 "A sheet metal part must be open.", swMbWarning, swMbOk

               

                  Exit Sub

               

              End If

               

               

              ' Is it a part document?

               

              Dim modelType As Long

               

              modelType = swModel.GetType

               

              If modelType <> SwConst.swDocPART Then

               

                  swApp.SendMsgToUser2 "A sheet metal part must be open.", swMbWarning, swMbOk

               

                  Exit Sub

               

              End If

               

               

               

                      vConfNameArr = swModel.GetConfigurationNames

               

                  For i = 0 To UBound(vConfNameArr)

               

                      sConfigName = vConfNameArr(i)

                 

                     

              bShowConfig = swModel.ShowConfiguration2(sConfigName)

               

              bRebuild = swModel.ForceRebuild3(False)

               

               

              Dim FilePath As String

              Dim PathSize As Long

              Dim PathNoExtension As String

              Dim NewFilePath As String

              Dim StartPos As Integer

              Dim FilenameNoExt As String

               

               

              FilePath = swModel.GetPathName

              PathSize = Strings.Len(FilePath)

              PathNoExtension = Strings.Left(FilePath, PathSize - 6)

              StartPos = InStrRev(FilePath, "\") + 1

               

              FilenameNoExt = Mid(FilePath, StartPos, Len(FilePath) - 5 - StartPos)

               

              'NewFilePath = PathNoExtension + sConfigName & ".DXF"

              NewFilePath = "S:\Manufacturing\Cutting Dept\Laser Waterjet Approved DXFs\Sheet Metal DXF\" + FilenameNoExt + sConfigName & ".DXF"

               

               

              'Export Flat Pattern

              bRet = swModel.ExportFlatPatternView(NewFilePath, 1)

               

              Next i

              End Sub