Save Rolled Backed Part as Parasolid File Example (VBA)
This example shows how to save a part in a rolled backed state as a
Parasolid file.
NOTE: When a part is in a rolled
back state, you cannot export the part to any other format until the model
has been rolled forward. This example shows how to use various geometry-related
and topology-related APIs to create a temporary part that you can then
export to any other format, in this example, Parasolid.
'------------------------------------------------
'
' Preconditions:
' (1)
Part is open.
' (2)
Part is rolled back.
'
' Postconditions:
' (1)
Parasolid file is saved to the same directory,
' overwriting
any existing file of the same name.
' (2)
Parasolid file has same geometry as part in rolled back
' state.
'
'-----------------------------------------------
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 '
Filename
must not be empty
swFileNameContainsAtSign
= &H8 '
Filename
cannot contain an at-sign (@)
'
character
swFileLockError
= &H10
swFileSaveFormatNotAvailable
= &H20 ' Save
As file type is not valid
swFileSaveAsDoNotOverwrite
= &H80 '
Do not
overwrite an existing file
swFileSaveAsInvalidFileExtension
= &H100 '
File extension
differs from
'
SOLIDWORKS
document type
End Enum
Public Enum swFileSaveWarning_e
swFileSaveWarning_RebuildError
= &H1 '
File was
saved with a rebuild error
End Enum
Public Enum swUserPreferenceIntegerValue_e
swParasolidOutputVersion
= 89
End Enum
Public Enum swUserPreferenceStringValue_e
swFileSaveAsCoordinateSystem
= 16
End Enum
Public Enum swParasolidOutputVersion_e
swParasolidOutputVersion_latest
= 0
swParasolidOutputVersion_80
= 1
swParasolidOutputVersion_90
= 2
swParasolidOutputVersion_91
= 3
swParasolidOutputVersion_100
= 4
swParasolidOutputVersion_110
= 5
swParasolidOutputVersion_111
= 6
swParasolidOutputVersion_120
= 7
swParasolidOutputVersion_121
= 8
End Enum
Public Enum swBodyType_e
swSolidBody
= 0
swSheetBody
= 1
swWireBody
= 2
swMinimumBody
= 3
swGeneralBody
= 4
swEmptyBody
= 5
End Enum
Public Enum swCreateFeatureBodyOpts_e
swCreateFeatureBodyCheck
= &H1
swCreateFeatureBodySimplify
= &H2
End Enum
Public Enum swMessageBoxIcon_e
swMbWarning
= 1
swMbInformation
= 2
swMbQuestion
= 3
swMbStop
= 4
End Enum
Public Enum swMessageBoxBtn_e
swMbAbortRetryIgnore
= 1
swMbOk
= 2
swMbOkCancel
= 3
swMbRetryCancel
= 4
swMbYesNo
= 5
swMbYesNoCancel
= 6
End Enum
Public Enum swMessageBoxResult_e
swMbHitAbort
= 1
swMbHitIgnore
= 2
swMbHitNo
= 3
swMbHitOk
= 4
swMbHitRetry
= 5
swMbHitYes
= 6
swMbHitCancel
= 7
End Enum
Sub main()
Dim
swApp As
SldWorks.SldWorks
Dim
swModel As
SldWorks.ModelDoc2
Dim
swPart As
SldWorks.PartDoc
Dim
swNewPart As
SldWorks.PartDoc
Dim
swNewModel As
SldWorks.ModelDoc2
Dim
sNewModelName As
String
Dim
vBodyArr As
Variant
Dim
vBody As
Variant
Dim
swBody As
SldWorks.body2
Dim
swCopyBody As
SldWorks.body2
Dim
swFeat As
SldWorks.feature
Dim
sPathName As
String
Dim
nOldVal As
Long
Dim
nErrors As
Long
Dim
nWarnings As
Long
Dim
nRetval As
Long
Dim
i As
Long
Dim
bRet As
Boolean
Set
swApp = Application.SldWorks
Set
swModel = swApp.ActiveDoc
Set
swPart = swModel
Set
swNewPart = swApp.NewPart
Set
swNewModel = swNewPart
sNewModelName
= swNewModel.GetTitle
'
Current Parasolid settings
Debug.Print
"ParasolidOutputVersion =
" + Str(swApp.GetUserPreferenceIntegerValue(swParasolidOutputVersion))
Debug.Print
"SaveAsCoordinateSystem =
" + swModel.GetUserPreferenceStringValue(swFileSaveAsCoordinateSystem)
'
Strip off SOLIDWORKS file extension (sldxxx)
'
and add Parasolid extension (x_b)
sPathName
= swModel.GetPathName
sPathName
= Left(sPathName, Len(sPathName) - 6)
sPathName
= sPathName + "x_t"
'
Store existing Parasolid export version and change to v10.0
nOldVal
= swApp.GetUserPreferenceIntegerValue(swParasolidOutputVersion)
bRet
= swApp.SetUserPreferenceIntegerValue(swParasolidOutputVersion,
swParasolidOutputVersion_100)
'
Create temporary part with bodies from existing part
For
i = 0 To 5
vBodyArr
= swPart.GetBodies2(i, False)
If
Not IsEmpty(vBodyArr) Then
For
Each vBody In vBodyArr
Set
swBody = vBody
Set
swCopyBody = swBody.Copy
Set
swFeat = swNewPart.CreateFeatureFromBody3(swCopyBody,
False, _
swCreateFeatureBodyCheck
+ swCreateFeatureBodySimplify)
Next
vBody
End
If
Next
i
'
Export temporary part to Parasolid
bRet
= swNewModel.SaveAs4(sPathName,
_
swSaveAsCurrentVersion,
_
swSaveAsOptions_Silent,
_
nErrors,
_
nWarnings)
If
bRet = False Then
nRetval
= swApp.SendMsgToUser2("Problems saving file.", swMbWarning,
swMbOk)
End
If
'
Get rid of temporary file
swApp.QuitDoc sNewModelName
'
Restore old Parasolid export version
bRet
= swApp.SetUserPreferenceIntegerValue(swParasolidOutputVersion,
nOldVal)
End Sub
'----------------------------------------------