Save Drawing Sheets as DXF Example (VBA)
This example shows how to save multiple drawings sheets as DXF files.
'----------------------------------------------
'
' Preconditions:
' (1)
Drawing doument is open.
' (2)
Drawing contains at least one sheet.
'
' Postconditions:
' (1)
DXF file is generated for each sheet,
' overwriting
any existing file
' (2)
DXF file names are based on the sheet name, for example Sheet1
' saved
as Sheet1.dxf, Sheet2 is saved as Sheet2.dxf, and so on.
'
'----------------------------------------------
Option Explicit
Public Enum swSaveAsVersion_e
swSaveAsCurrentVersion
= 0 ' default
swSaveAsFormatProE
= 2 '
save SolidWorks
part as Pro/E format
'
.prt/.asm
extension (not as SolidWorks.prt/.asm)
End Enum
Public Enum swSaveAsOptions_e
swSaveAsOptions_Silent
= &H1 '
Save document
silently or not
swSaveAsOptions_Copy
= &H2 '
Save document
as a copy or not
swSaveAsOptions_SaveReferenced
= &H4 '
Save referenced
documents or not (drawings and parts only)
End Enum
Public Enum swFileSaveError_e
swGenericSaveError
= &H1
swReadOnlySaveError
= &H2
swFileNameEmpty
= &H4 '
The filename
cannot be empty
swFileNameContainsAtSign
= &H8 '
The filename
cannot contain an the at-sign character (@)
swFileLockError
= &H10
swFileSaveFormatNotAvailable
= &H20 '
The Save
As file type is not valid
swFileSaveAsDoNotOverwrite
= &H80 '
The user
chose not to overwrite an existing file
swFileSaveAsInvalidFileExtension
= &H100 '
The file
extension differs from the SolidWorks document type
End Enum
Public Enum swFileSaveWarning_e
swFileSaveWarning_RebuildError
= &H1 '
The file
was saved with a rebuild error
End Enum
Public Enum swDxfFormat_e
swDxfFormat_R12
= 0
swDxfFormat_R13
= 1
swDxfFormat_R14
= 2
swDxfFormat_R2000
= 3
End Enum
Public Enum swArrowDirection_e
swINSIDE
= 0
swOUTSIDE
= 1
swSMART
= 2
End Enum
Public Enum swUserPreferenceToggle_e
swDxfMapping
= 8
swDXFDontShowMap
= 21
End Enum
Public Enum swUserPreferenceIntegerValue_e
swDxfVersion
= 0
swDxfOutputFonts
= 1
swDxfMappingFileIndex
= 2
swDxfOutputLineStyles
= 135
swDxfOutputNoScale
= 136
End Enum
Public Enum swUserPreferenceDoubleValue_e
swDxfOutputScaleFactor
= 79
End Enum
Public Enum swUserPreferenceStringListValue_e
swDxfMappingFiles
= 0
End Enum
Sub main()
Dim
swApp As
SldWorks.SldWorks
Dim
swModel As
SldWorks.ModelDoc2
Dim
swDraw As
SldWorks.DrawingDoc
Dim
vSheetName As
Variant
Dim
nErrors As
Long
Dim
nWarnings As
Long
Dim
nRetval As
Long
Dim
bShowMap As
Boolean
Dim
nNumSheet As
Long
Dim
i As
Long
Dim
bRet As
Boolean
Set
swApp = Application.SldWorks
Set
swModel = swApp.ActiveDoc
Set
swDraw = swModel
'
Current settings
Debug.Print
"DxfMapping =
" & swApp.GetUserPreferenceToggle(swDxfMapping)
Debug.Print
"DXFDontShowMap =
" & swApp.GetUserPreferenceToggle(swDXFDontShowMap)
Debug.Print
"DxfVersion =
" & swApp.GetUserPreferenceIntegerValue(swDxfVersion)
Debug.Print
"DxfOutputFonts =
" & swApp.GetUserPreferenceIntegerValue(swDxfOutputFonts)
Debug.Print
"DxfMappingFileIndex =
" & swApp.GetUserPreferenceIntegerValue(swDxfMappingFileIndex)
Debug.Print
"DxfOutputLineStyles =
" & swApp.GetUserPreferenceIntegerValue(swDxfOutputLineStyles)
Debug.Print
"DxfOutputNoScale =
" & swApp.GetUserPreferenceIntegerValue(swDxfOutputNoScale)
Debug.Print
"DxfOutputScaleFactor =
" & swApp.GetUserPreferenceDoubleValue(swDxfOutputScaleFactor)
Debug.Print
"DxfMappingFiles =
" & swApp.GetUserPreferenceStringListValue(swDxfMappingFiles)
Debug.Print
""
'
Turn off showing of map
swApp.SetUserPreferenceToggle swDXFDontShowMap,
True
vSheetName
= swDraw.GetSheetNames
For
i = 0 To UBound(vSheetName)
bRet
= swDraw.ActivateSheet(vSheetName(i))
bRet
= swModel.SaveAs4( _
vSheetName(i)
& ".dxf", _
swSaveAsCurrentVersion,
_
swSaveAsOptions_Silent,
_
nErrors,
_
nWarnings)
Debug.Assert
bRet
Next
i
'
Switch back to first sheet
bRet
= swDraw.ActivateSheet(vSheetName(0))
'
Restore old setting
swApp.SetUserPreferenceToggle swDXFDontShowMap,
bShowMap
End Sub
'----------------------------------------------