Code:
' Save Each Sheet As DXF-PDF.SWP -------------------------------- 02/03/14
'
' Description: Macro to save each sheet in the active drawing as DXF and PDF.
'
' Preconditions: The drawing document is open.
'
' Postconditions: All drawing sheets are exported to a DXF and PDF files in the same folder as the drawing with file names as specified.' Please back up your data before use and USE AT OWN RISK
' This macro is provided as is. No claims, support, refund, safety net, or
' warranties are expressed or implied. By using this macro and/or its code in
' any way whatsoever, the user and any entities which the user represents,
' agree to hold the authors free of any and all liability. Free distribution
' and use of this code in other free works is welcome. If any portion of
' this code is used in other works, credit to the authors must be placed in
' that work within a user viewable location (e.g., macro header). All other
' forms of distribution (i.e., not free, fee for delivery, etc) are prohibited
' without the expressed written consent by the authors. Use at your own risk!
'
' ------------------------------------------------------------------------------
' Written by: Deepak Gupta (http://gupta9665.com/)
' -------------------------------------------------------------------------------
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swDraw As SldWorks.DrawingDoc
Dim sFilename As String
Dim Value As String
Dim vSheetName As Variant
Dim varSheetName As Variant
Dim nErrors As Long
Dim nWarnings As Long
Dim i, j As Long
Dim bRet As Boolean
Dim swExportPDFData As SldWorks.ExportPdfData
Sub main()
On Error GoTo error
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
If (swModel Is Nothing) Or (swModel.GetType <> swDocDRAWING) Then
swApp.SendMsgToUser ("To be used for drawings only, Open a drawing first and then TRY!")
' If no model currently loaded, then exit
Exit Sub
End If
Set swDraw = swModel
'Set the DXF export option to export active sheet only
swApp.SetUserPreferenceIntegerValue swUserPreferenceIntegerValue_e.swDxfMultiSheetOption, swDxfMultisheet_e.swDxfActiveSheetOnly
Set swExportPDFData = swApp.GetExportFileData(1)
vSheetName = swDraw.GetSheetNames
For i = 0 To UBound(vSheetName)
bRet = swDraw.ActivateSheet(vSheetName(i))
swModel.ViewZoomtofit2
varSheetName = vSheetName(i)
Value = Left(swDraw.SummaryInfo(swSumInfoSaveDate), Len(swDraw.SummaryInfo(swSumInfoSaveDate)) - 9)
'vSheetName(i)
sFilename = "D:\Zeichnungen\_Export Step\" & GetZeichnungsnummer() & " " & Value
'Debug.Print sFilename
bRet = swDraw.SaveAs4(sFilename & ".dxf", swSaveAsCurrentVersion, swSaveAsOptions_Silent, nErrors, nWarnings)
bRet = swDraw.SaveAs4(sFilename & ".jpg", swSaveAsCurrentVersion, swSaveAsOptions_Silent, nErrors, nWarnings)
bRet = swDraw.SaveAs4(sFilename & ".png", swSaveAsCurrentVersion, swSaveAsOptions_Silent, nErrors, nWarnings)
bRet = swDraw.SaveAs4(sFilename & ".tif", swSaveAsCurrentVersion, swSaveAsOptions_Silent, nErrors, nWarnings)
'ExportAsModel '????
'HIER BIN ICH EIGENTLICH SCHON AUF SEITENEBENE,... EIGENTLCH MÜSSTE HIER "NUR" NOCH DER EXPORT REIN?
If swExportPDFData Is Nothing Then MsgBox "Nothing"
bRet = swExportPDFData.SetSheets(swExportData_ExportSpecifiedSheets, varSheetName)
bRet = swDraw.Extension.SaveAs(sFilename & ".PDF", 0, 0, swExportPDFData, nErrors, nWarnings)
Next i
' Switch back to first sheet
bRet = swDraw.ActivateSheet(vSheetName(0))
Exit Sub
error:
MsgBox "An error has occured - Please close and reopen SolidWorks to try again", vbCritical, "Program Error"
Exit Sub
End Sub
Private Function GetZeichnungsnummer() As String
Dim swCurrentDoc As Object
Dim swCurrentView As Object
Dim swCurrentConfig As Object
Dim swDrawModel As Object
Dim swTempDoc As Object
Dim szModelName As String
Dim szConfigName As String
Set swCurrentDoc = swApp.ActiveDoc
' Zeichenfläche ist erste Ansicht
Set swCurrentView = swCurrentDoc.GetFirstView
' nächste Ansicht ist erste Zeichenansicht
Set swCurrentView = swCurrentView.GetNextView
' Assert-Behandlung
If swCurrentView Is Nothing Then
Debug.Print "keine Ansicht des Models vorhanden"
Call MsgBox("Keine Ansicht vorhanden", vbOKOnly, "Information")
End
End If
' Überprüfen, ob Modell im Speicher geladen ist
bRet = swCurrentView.IsModelLoaded()
If (Not bRet) Then
' falls nicht, Modell nachladen
bRet = swCurrentView.LoadModel()
End If
' Modell aus aktivem Zeichenblatt laden und auswählen
szModelName = swCurrentView.GetReferencedModelName()
Set swDrawModel = swApp.GetOpenDocumentByName(szModelName)
' Verwendete Konfiguration holen
szConfigName = swCurrentView.ReferencedConfiguration()
Set swCurrentConfig = swDrawModel.GetConfigurationByName(szConfigName)
Set swTempDoc = swDrawModel
' Zeichnungsnummer aus konfigurationsspezifischer Eingenschaft "Number" und "Description" zusammensetzen
GetZeichnungsnummer = swTempDoc.CustomInfo2(szConfigName, "Number") & " - " & swTempDoc.CustomInfo2(szConfigName, "Description")
End Function