Change Imported File Associated with an Imported Feature Example (VBA)
This example shows how to change the imported file associated with an
imported feature.
'--------------------------------
'
' Preconditions:
' (1)
Model document is open and contains an imported feature.
' (2)
Imported feature is selected.
' (3)
The file C:\samples\case2.x_t exists.
'
' Postconditions: Selected imported feature is replaced
by the specified
' imported
file, case2.x_t.
'
'---------------------------------
Option Explicit
Public Enum swBodyType_e
swSolidBody
= 0
swSheetBody
= 1
swWireBody
= 2
swMinimumBody
= 3
swGeneralBody
= 4
swEmptyBody
= 5
End Enum
Sub main()
Const
sImportFileName As
String = "C:\samples\case2.x_t"
Dim
swApp As
SldWorks.SldWorks
Dim
swModel As
SldWorks.ModelDoc2
Dim
swSelMgr As
SldWorks.SelectionMgr
Dim
swFeat As
SldWorks.feature
Dim
swNewModel As
SldWorks.ModelDoc2
Dim
swNewPart As
SldWorks.PartDoc
Dim
vBodyArr As
Variant
Dim
swBody As
SldWorks.body2
Dim
swTempBody As
SldWorks.body2
Dim
bRet As
Boolean
Set
swApp = Application.SldWorks
Set
swModel = swApp.ActiveDoc
Set
swSelMgr = swModel.SelectionManager
Set
swFeat = swSelMgr.GetSelectedObject5(1)
Debug.Print
"File = " & swModel.GetPathName
Debug.Print
" Feat
= " & swFeat.Name
Debug.Print
" FeatTypeName
= " & swFeat.GetTypeName
'
Open import file into a new part file
bRet
= swApp.LoadFile2(sImportFileName,
""): Debug.Assert bRet
Set
swNewModel = swApp.ActiveDoc
Set
swNewPart = swNewModel
'
Only consider solid bodies
vBodyArr
= swNewPart.GetBodies2(swSolidBody,
True)
Debug.Assert
Not IsEmpty(vBodyArr)
Debug.Assert
0 = UBound(vBodyArr)
'
Only consider first solid body
Set
swBody = vBodyArr(0)
Set
swTempBody = swBody.Copy
bRet
= swFeat.SetBody2(swTempBody,
False): Debug.Assert bRet
'
Close SolidWorks file associated with opening the import file
swApp.QuitDoc
swNewModel.GetTitle
bRet
= swModel.ForceRebuild3(False):
Debug.Assert bRet
End Sub
'--------------------------------