在Solidworks中如何获取装配体文件中选中零部件的所有尺寸?

How can I get all the dimensions of the selected component in the assembly file in Solidworks?

我为 select 组件中的组件编写了一个宏,然后根据需要更改其尺寸以自动执行该过程。

由于我使用了维度名称,因此该脚本目前不能用于其他类似程序集。

我的问题是,如何在装配文件中获取 selected 组件的所有尺寸。

这里包括我的部分代码:


Dim vComponents As Variant
Dim vComp As Variant

Dim Part As Object



Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc

vComponents = Part.GetComponents(True)



For Each vComp In vComponents
Set swComp = vComp
If InStr(swComp.Name2, "_ST_") > 0 Then swComp.Select4 True, Nothing, False
Next


Set swSelMgr = Part.SelectionManager
Set swSelComp = swSelMgr.GetSelectedObjectsComponent4(1, -1)
Set swReferenceModel = swSelComp.GetModelDoc2

originalStello = swReferenceModel.Parameter("D7@Schizzo1").Value  // this is where I am hard coding the name

在最后一行,您可以看到我通过硬编码名称获取维度值。我宁愿获取 SELECTED 组件的所有维度,然后编写逻辑以从该列表中获取特定维度。

我 2 天前开始使用 Solidworks API。请帮忙!

您可以从所选组件的引用模型中读取所有 "Parameters"。 为此,您首先必须获取引用模型的 ActiveConfiguration,然后使用 GetParameters 填充变体变量。

变量 vParamNames 包含参数名称,变量 vParamValues 包含所属值。您可能需要过滤掉所需的维度。

Dim swSelMgr As SldWorks.SelectionMgr
Set swSelMgr = Part.SelectionManager

Dim swSelComp As SldWorks.Component2
Set swSelComp = swSelMgr.GetSelectedObjectsComponent4(1, -1)

Dim swReferenceModel As SldWorks.ModelDoc2
Set swReferenceModel = swSelComp.GetModelDoc2

Dim swConfig As SldWorks.Configuration
Set swConfig = swReferenceModel.ConfigurationManager.ActiveConfiguration

Dim vParamNames As Variant
Dim vParamValues As Variant

swConfig.GetParameters vParamNames, vParamValues

另一种选择是遍历所有特征的所有显示维度

Function GetAllDimensions(vFeats As Variant) As Variant

    Dim swDimsColl As Collection
    Set swDimsColl = New Collection

    Dim i As Integer

    For i = 0 To UBound(vFeats)

        Dim swFeat As SldWorks.Feature
        Set swFeat = vFeats(i)

        Dim swDispDim As SldWorks.DisplayDimension
        Set swDispDim = swFeat.GetFirstDisplayDimension

        While Not swDispDim Is Nothing

            If Not Contains(swDimsColl, swDispDim) Then
                swDimsColl.Add swDispDim
            End If

            Set swDispDim = swFeat.GetNextDisplayDimension(swDispDim)
        Wend

    Next

    GetAllDimensions = CollectionToArray(swDimsColl)

End Function

找到完整的例子here