使用SOLIDWORKS API从绘图视图获取实体和材料
{ width=200 }
这个VBA宏使用SOLIDWORKS API查找所选绘图视图(包括钣金展开图)的所有实体,并提取它们的材料。
IView::Bodies 属性用于查找绘图视图的实体,但是对于从钣金展开图创建的绘图视图,这个SOLIDWORKS API属性返回的是空值。
{ width=250 }
下面的宏在两种情况下(普通零件和钣金图案)提取实体并查找分配给它们的材料。结果将输出到VBA编辑器的即时窗口。
Dim swApp As SldWorks.SldWorks
Sub main()
Set swApp = Application.SldWorks
Dim swModel As SldWorks.ModelDoc2
Set swModel = swApp.ActiveDoc
If Not swModel Is Nothing Then
Dim swView As SldWorks.view
Set swView = swModel.SelectionManager.GetSelectedObject6(1, -1)
If Not swView Is Nothing Then
Dim vBodies As Variant
vBodies = GetBodies(swView)
Dim i As Integer
For i = 0 To UBound(vBodies)
Dim swBody As SldWorks.Body2
Set swBody = vBodies(i)
Dim matDb As String
Dim matName As String
matName = swBody.GetMaterialPropertyName(swView.ReferencedConfiguration, matDb)
Debug.Print swView.Name & " - " & swBody.Name & " - " & matName & " - " & matDb
Next
Else
MsgBox "请选择视图"
End If
Else
MsgBox "请打开模型"
End If
End Sub
Function GetBodies(view As SldWorks.view) As Variant
If view.IsFlatPatternView() Then
Dim vComps As Variant
vComps = view.GetVisibleComponents()
'展开图只能为单个实体创建(单实体零件或多实体零件选择实体)
Dim swComp As SldWorks.Component2
Set swComp = vComps(0)
Dim vFaces As Variant
vFaces = view.GetVisibleEntities2(swComp, swViewEntityType_e.swViewEntityType_Face)
Dim swFace As SldWorks.Face2
Set swFace = vFaces(0)
Dim swBodies(0) As SldWorks.Body2
Set swBodies(0) = swFace.GetBody()
GetBodies = swBodies
Else
GetBodies = view.Bodies
End If
End Function