SolidWorks 2013 Macro - String manipulation?

2.7k views Asked by At

SolidWorks uses VBA for its macros, but it is very different than Excel VBA (which is what I'm accustomed to). They made it very difficult (and quite possibly impossible) to manipulate strings in SW. I've tried using the Left() function and the Mid() function, but I can not figure out how to make it work. Basically what I need it to do, is a Save As .DXF file and name it the title, but WITHOUT the sheet name. The sheet name is causing the problem and I'm trying to cut it out. I can use

Part.GetTitle

to get the string of the title which, for example is something like

PA0000 - Sheet1

and I just want it to be

PA0000

Sometimes the length is different, so I've 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 out the " - Sheet1".

2

There are 2 answers

0
Graeme On

longname = Len(Part.GetTitle) - 9

' Save As longstatus = Part.SaveAs3(Left(Part.GetTitle, longname) + ".SLDDRW", 0, 2)

so the full code is:

Dim swApp As Object
Dim longname As Long
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long

Sub main()

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc
longname = Len(Part.GetTitle) - 9

' Save As
longstatus = Part.SaveAs3(Left(Part.GetTitle, longname) + ".SLDDRW", 0, 2)
boolstatus = Part.FeatureManager.EditRollback(swMoveRollbackBarTo_e.swMoveRollbackBarToEnd, "")
Part.ClearSelection2 True

' Save As
longstatus = Part.SaveAs3(Left(Part.GetTitle, longname) + ".PDF", 0, 0)
End Sub
0
Cody Geisler On

I have made some changes to the code at help.solidworks.com; this should work for you. Just change the 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