You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SolidWorks装配体零件属性提取VBA宏问题求助

问题原因与解决方法

可能的问题原因

  • 组件名称不匹配:GetComponentByName使用的是SolidWorks装配体中组件的实例名称(不是零件文件名),如果装配体中零件有多个实例,名称可能是Part_A-1、Part_A-2这类带后缀的形式,直接用Part_A会找不到。
  • 装配体对象未正确初始化:swAssembly变量可能未关联到有效的SolidWorks装配体文档,导致调用方法时返回空值。
  • 目标零件在嵌套子装配体中:GetComponentByName仅能获取当前装配体的直接子组件,无法直接获取嵌套在子装配体里的零件。

正确实现方法

要获取装配体中所有零件(包括嵌套子装配体里的)的属性,建议通过递归遍历所有组件的方式实现,步骤如下:

  1. 确保获取有效的SolidWorks装配体对象
  2. 编写递归函数遍历所有组件,判断组件是否为零件(而非子装配体)
  3. 提取零件的自定义属性和配置属性
  4. 将属性数据写入XML文件

完整示例代码

Option Explicit

Sub ExportAssemblyPartsPropertiesToXML()
    Dim swApp As SldWorks.SldWorks
    Dim swAssembly As SldWorks.AssemblyDoc
    Dim swModel As SldWorks.ModelDoc2
    Dim xmlFilePath As String
    Dim xmlDoc As Object ' MSXML2.DOMDocument60
    Dim rootNode As Object
    Dim assemblyNode As Object
    Dim partNode As Object
    Dim propNode As Object
    
    ' 初始化SolidWorks应用对象
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    
    ' 检查当前文档是否为装配体
    If swModel.GetType <> swDocASSEMBLY Then
        MsgBox "请打开一个装配体文档后运行此宏!"
        Exit Sub
    End If
    Set swAssembly = swModel
    
    ' 设置XML保存路径(可自行修改)
    xmlFilePath = swAssembly.GetPathName & "_Properties.xml"
    
    ' 创建XML文档
    Set xmlDoc = CreateObject("MSXML2.DOMDocument.6.0")
    xmlDoc.async = False
    xmlDoc.preserveWhiteSpace = True
    xmlDoc.loadXML "<AssemblyProperties></AssemblyProperties>"
    
    ' 创建装配体节点
    Set rootNode = xmlDoc.DocumentElement
    Set assemblyNode = xmlDoc.createElement("Assembly")
    assemblyNode.setAttribute "Name", swAssembly.GetTitle
    assemblyNode.setAttribute "Path", swAssembly.GetPathName
    rootNode.appendChild assemblyNode
    
    ' 递归遍历所有组件并导出属性
    TraverseComponents swAssembly, assemblyNode, xmlDoc
    
    ' 保存XML文件
    xmlDoc.Save xmlFilePath
    MsgBox "属性已成功导出至:" & vbCrLf & xmlFilePath
End Sub

' 递归遍历装配体组件的函数
Sub TraverseComponents(swAssembly As SldWorks.AssemblyDoc, parentNode As Object, xmlDoc As Object)
    Dim swComp As SldWorks.Component2
    Dim swModel As SldWorks.ModelDoc2
    Dim vComps As Variant
    Dim i As Integer
    Dim partNode As Object
    Dim propNames As Variant
    Dim propValues As Variant
    Dim j As Integer
    
    ' 获取当前装配体的所有组件(包括嵌套层级)
    vComps = swAssembly.GetComponents(True)
    
    If Not IsEmpty(vComps) Then
        For i = 0 To UBound(vComps)
            Set swComp = vComps(i)
            Set swModel = swComp.GetModelDoc2
            
            ' 仅处理零件文档
            If swModel.GetType = swDocPART Then
                ' 创建零件节点
                Set partNode = xmlDoc.createElement("Part")
                partNode.setAttribute "ComponentName", swComp.Name2
                partNode.setAttribute "FileName", swModel.GetTitle
                partNode.setAttribute "Path", swModel.GetPathName
                parentNode.appendChild partNode
                
                ' 获取零件的自定义属性
                propNames = swModel.GetCustomPropertyNames(swModel.GetActiveConfigurationName)
                propValues = swModel.GetCustomPropertyValues(swModel.GetActiveConfigurationName)
                
                If Not IsEmpty(propNames) Then
                    For j = 0 To UBound(propNames)
                        ' 创建属性节点
                        Set propNode = xmlDoc.createElement("Property")
                        propNode.setAttribute "Name", propNames(j)
                        propNode.setAttribute "Value", propValues(j)
                        partNode.appendChild propNode
                    Next j
                End If
            ElseIf swModel.GetType = swDocASSEMBLY Then
                ' 如果是子装配体,递归遍历其内部组件
                Dim subAssemblyNode As Object
                Set subAssemblyNode = xmlDoc.createElement("SubAssembly")
                subAssemblyNode.setAttribute "ComponentName", swComp.Name2
                subAssemblyNode.setAttribute "Name", swModel.GetTitle
                parentNode.appendChild subAssemblyNode
                TraverseComponents swModel, subAssemblyNode, xmlDoc
            End If
        Next i
    End If
End Sub

代码说明

  • GetComponents(True):参数True表示获取装配体中所有层级的组件(包括嵌套子装配体里的零件),若仅需直接子组件可改为False。
  • GetCustomPropertyNames/GetCustomPropertyValues:用于获取零件的自定义属性,需传入目标配置名称(示例中使用当前激活配置)。
  • 递归逻辑:遇到子装配体时自动深入遍历内部组件,确保所有层级的零件属性都被导出。

内容的提问来源于stack exchange,提问作者Benjamin Franklin951012

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 21:12:33