Get External References Example (VBA)
This example shows how to retrieve external references for an active
document and for a selected feature within that document.
'-------------------------------------------
'
' Preconditions:
' (1)
File is open.
' (2)
Feature is optionally selected in the FeatureManager design tree.
'
' Postconditions: None
'
'-------------------------------------------
Option Explicit
Sub main()
Dim
swApp As
SldWorks.SldWorks
Dim
swModel As
SldWorks.ModelDoc2
Dim
swModDocExt As
SldWorks.ModelDocExtension
Dim
swSelMgr As
SldWorks.SelectionMgr
Dim
swFeat As
SldWorks.feature
Dim
swComp As
SldWorks.Component2
Dim
vModelPathName As
Variant
Dim
vComponentPathName As
Variant
Dim
vFeature As
Variant
Dim
vDataType As
Variant
Dim
vStatus As
Variant
Dim
vRefEntity As
Variant
Dim
vFeatComp As
Variant
Dim
nConfigOpt As
Long
Dim
sConfigName As
String
Dim
nRefCount As
Long
Dim
nSelType As
Long
Dim
i As
Long
Dim
bRet As
Boolean
Set
swApp = CreateObject("SldWorks.Application")
Set
swModel = swApp.ActiveDoc
Set
swSelMgr = swModel.SelectionManager
Set
swModDocExt = swModel.Extension
nSelType
= swSelMgr.GetSelectedObjectType2(1)
Select
Case nSelType
'
Component in an assembly
Case
swSelCOMPONENTS
Set
swComp = swSelMgr.GetSelectedObjectsComponent2(1)
nRefCount
= swComp.ListExternalFileReferencesCount
swComp.ListExternalFileReferences2 _
vModelPathName,
_
vComponentPathName,
_
vFeature,
_
vDataType,
_
vStatus,
_
vRefEntity,
_
vFeatComp,
_
nConfigOpt,
_
sConfigName
Set
swModel = swComp.GetModelDoc
'
Feature in a part or assembly
Case
swSelBODYFEATURES, swSelSKETCHES
Set
swFeat = swSelMgr.GetSelectedObject5(1)
nRefCount
= swFeat.ListExternalFileReferencesCount
swFeat.ListExternalFileReferences2 _
vModelPathName,
_
vComponentPathName,
_
vFeature,
_
vDataType,
_
vStatus,
_
vRefEntity,
_
vFeatComp,
_
nConfigOpt,
_
sConfigName
'
Top-level part or assembly
Case
Else
nRefCount
= swModDocExt.ListExternalFileReferencesCount
swModDocExt.ListExternalFileReferences _
vModelPathName,
_
vComponentPathName,
_
vFeature,
_
vDataType,
_
vStatus,
_
vRefEntity,
_
vFeatComp,
_
nConfigOpt,
_
sConfigName
End
Select
Debug.Print
"ModelName = " + swModel.GetPathName
Debug.Print
" RefCount
=
" + Str(nRefCount)
Debug.Print
""
For
i = 0 To nRefCount - 1
Debug.Print
" ModelPathName
=
" + vModelPathName(i)
Debug.Print
" ComponentPathName
= "
+ vComponentPathName(i)
Debug.Print
" Feature
=
" + vFeature(i)
Debug.Print
" DataType
=
" + vDataType(i)
Debug.Print
" Status
=
" + Str(vStatus(i))
Debug.Print
" RefEntity
=
" + vRefEntity(i)
Debug.Print
" FeatComp
=
" + vFeatComp(i)
Debug.Print
""
Debug.Print
""
Debug.Print
" Config
Option =
" & nConfigOpt
Debug.Print
" Config
Name =
" & sConfigName
Next
i
End Sub
'------------------------------------------