SolidWorks 2013 Macro Macro?

SolidWorks uses VBA for their macros, but it's very different from Excel VBA (which I'm used to). They made it very difficult (and possibly impossible) to manipulate strings in SW. I have tried using the Left () function and the Mid () function, but I cannot figure out how to get it to work. Basically what I need is a Save As.DXF file and name it a title, but WITHOUT a sheet name. The sheet title is causing the problem and I am trying to cut it out. I can use

Part.GetTitle

      

to get a title bar that looks like for example

PA0000 - Sheet1

      

and I just want him to be

PA0000

      

Sometimes the length is different, so I tried using

Left(Part.GetTitle,Instr(Part.GetTitle, " ")-1)

      

but it gives a type mismatch error. What am I doing wrong? All that's left for this macro is to cut "- Sheet1".

+3


source to share


1 answer


I've made some code changes at help.solidworks.com; this should work for you. Just change folders as needed.



Dim swApp As Object

Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
'http://help.solidworks.com/2012/English/api/sldworksapi/Save_File_As_PDF_Example_VB.htm
Sub main()
    Dim swApp                       As SldWorks.SldWorks
    Dim swModel                     As SldWorks.DrawingDoc
    Dim swPart                      As SldWorks.PartDoc
    Dim bRet                        As Boolean
    Dim MyPath                      As String
    Dim MyFolder                    As String
    Dim longstatus                  As Long
    Dim filename                    As String
    Set swApp = Application.SldWorks

    Set swModel = swApp.ActiveDoc

    MyFolder = "C:\temp" ' Change to temp drive since curdir is often write protected.
    myFile = Left(swModel.GetPathName, InStrRev(swModel.GetPathName, ".") - 1)
    myFile = Right(myFile, Len(myFile) - InStrRev(myFile, "\"))
    MyPath = Left(swModel.GetPathName, InStrRev(swModel.GetPathName, "\") - 1)
    MyPath = Right(MyPath, Len(MyPath) - InStrRev(MyPath, "\"))

    Debug.Print "FileName = " & myFile
    Debug.Print "File = " & swModel.GetPathName
    Debug.Print "Folder = " & MyPath
    Debug.Print "Current Folder = " & CurDir$
    Debug.Print (MyFolder & "\" & myFile & ".DXF")
    filename = MyFolder & "\" & myFile & ".DXF"
    longstatus = swModel.SaveAs3(filename, 0, 0)
    ' For PDF Generation
    ' longstatus = swModel.SaveAs3(MyFolder & "\" & myFile & ".PDF", 0, 0) 
End Sub

      

0


source







All Articles