Find a MEP sheet in VBA in solidworks

Hello people.

 

I'm trying to make a macro to save each sheet of my drawings in DXF and PDF. So far, I can save all the sheets in the right format or activate it by fiddling with the Solidworks options.

 

Nevertheless, I would like to be able to change the name directly without renaming each file (so boring!) Brief. To do that, I'd like to be able to tell my macro to open a sheet, save it and then move on to the next one. I know I have to put a meter on it.... But how do I tell him to take the sheet1 then 2, etc.

 

And, little bonus question. Is there a summary of all the Solidworks specific commands somewhere?

 

Thanks in advance

1 Like

Hello

 

I made a similar macro, but we only use one sheet or we want to have the 2 sheets in the same PDF.

 

For your macro there are already "ready-made" ones!

See here:

https://forum.solidworks.com/thread/82929

https://forum.solidworks.com/thread/66753

 

And for the commands, are you talking about the VBA commands for SolidWorks?

 

You have to go to the help of the APIs:

http://help.solidworks.com/2014/English/api/sldworksapiprogguide/Welcome.htm

 

Or a google search often allows for a faster answer!

2 Likes

For your "counter" you have to loop on all the sheets, if you are sure that it is always sheet1 then 2 etc, all you need is an instruction like this:

 

[CODE]

 

i = 1

Do while (sheet_name exist) 'I don't know the head instruction

sheet_name = "Leaf" & i

'Here is your code for export in PDF and DWG

i = i +1

Loop

 

[/CODE]

 

But if the person deletes sheet 1 or renames it, it doesn't work anymore!

 

So you need to retrieve the name or number of each sheet, and the SolidWorks API help offers an example!

 

There he is:

Edit: update of the link which was bad 

http://help.solidworks.com/2012/English/api/sldworksapi/Get_Sheet_Numbers_and_Names_Example_VB.htm

 

See also this link:

http://help.solidworks.com/2013/english/api/sldworksapi/save_drawing_sheets_as_dxf_example_vb.htm

2 Likes

Thank you very much Lucas P for your quick and provided answers.

 

At least, now, I'm armored with examples (fortunately my English is not so far behind me!)

 

Yes, I saw your similar macro. I was inspired by it a lot for mine by adapting it to my needs.

 

On the other hand, from what I understand in what you send me, I am obliged to know the names of the sheets. If it's not a problem for my short stories, I don't really manage what my predecessors did.

So, I was hoping to be able to find something like in excel where you can put "Sheet1" to designate them rather than their names.

 

I'm going to fiddle around a bit and come back and post the whole code if it works

1 Like

Sorry, I put the same link 2 times, and I didn't post the most interesting one:

 

http://help.solidworks.com/2012/English/api/sldworksapi/Get_Sheet_Numbers_and_Names_Example_VB.htm

 

This code allows you to browse all the sheets in a drawing:

 

For i = 0 To UBound(vSheetNames)

        Debug.Print "  SheetName[" & i & "] = " & vSheetNames(i)

Next i

 

 

And just put your code in place of:

        Debug.Print "  SheetName[" & i & "] = " & vSheetNames(i)

1 Like

I have a macro that goes smoothly ... but where nothing happens. I don't really understand why and where it gets stuck. If anyone has an idea?

 

Sub Save()
Dim swapp As SldWorks.SldWorks
Dim swdoc As SldWorks.ModelDoc2
Dim Swdraw As SldWorks.ModelDoc
Dim swSheet As SldWorks.Sheet
Dim vSheetNames As Variant
Dim Nbfeuille As Variant
Set swapp = Application.SldWorks
Set swdoc = swapp. ActiveDoc
Set Swdraw = swdoc
Set swSheet = Swdraw.GetCurrentSheet
'Confirmation message
ret = MsgBox("do you want to convert this drawing to DXF and PDF?", vbOKCancel + vbExclamation + vbMsgBoxSetForeground + vbSystemModal, "Laser Conversion")
If ret = vbCancel Then End
Registration new name
Do
 newname = InputBox("Please specify the new name:", "blabla", newname)
 If StrPtr(newname) = 0 Then
 MsgBox "procedure cancelled"
 Exit Sub
 End If
'Checking Forbidden Character Windows
Do While InStr(newname, "/") > 0 Or InStr(newname, "*") > 0 Or InStr(newname, "?") > 0 Or InStr(newname, "<") > 0 Or InStr(newnam, ">") > 0 Or InStr(newnam, "!") > 0
 newname = InputBox("Please note that the name contains at least one of the forbidden characters \/:*?""<>!" & vbNewLine & vbNewLine & "Please specify the new name:", "save-under by LPR", newname)
 Loop
 Loop While newname = " "
'Registration Dossier
Do
 FilePath = InputBox("Specify the path", "record folder", FilePath)
 If StrPtr(FilePath) = 0 Then
 MsgBox "procedure cancelled"
 Exit Sub
End If
'Adding the \ to the end of the folder name
If Right(FilePath, 1) <> "\" Then FilePath = FilePath & "\"
 If Dir$(FilePath) <> "" Then
 EXISTS = 1
 Else: MsgBox "the directory doesn't exist, please create it"
 Debug.Print Dir$(FilePath)
End If
Loop While EXISTS <> 1
'Indicates the number of sheets
Nbfeuille = swdoc. GetSheetCount
For i = 0 To Nbfeuille
    swdoc. SheetPrevious
    Next i
    'move to the next sheet if < to the total number
    For i = 0 To varSheetCount - 1
    If i <> 0 Then
    swmodel. SheetNext
    End If
Recording in DXF and PDF
swdoc. SaveAs (FilePath + newname + "_" + i + ".dxf")
swdoc. SaveAs (FilePath + newname + "_" + i + ".pdf")
Next i
End Sub

1 Like