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

SolidWorks宏需求:为装配体选中零件设置Display Hidden Lines

SolidWorks 宏:设置选中零件为显示隐藏线样式

以下是可实现需求的宏代码,直接复制到SolidWorks宏编辑器中运行即可:

Option Explicit

Sub SetSelectedComponentToDisplayHiddenLines()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swSelMgr As SldWorks.SelectionMgr
    Dim swComp As SldWorks.Component2
    Dim displayStateName As String
    
    ' 获取SolidWorks应用实例
    Set swApp = Application.SldWorks
    
    ' 获取当前激活的文档
    Set swModel = swApp.ActiveDoc
    If swModel Is Nothing Then
        MsgBox "请先打开一个装配体文档", vbExclamation
        Exit Sub
    End If
    
    ' 确认当前文档是装配体
    If swModel.GetType <> swDocASSEMBLY Then
        MsgBox "当前文档不是装配体,请打开装配体后重试", vbExclamation
        Exit Sub
    End If
    
    ' 获取选择管理器
    Set swSelMgr = swModel.SelectionManager
    If swSelMgr.GetSelectedObjectCount2(-1) <> 1 Then
        MsgBox "请在绘图区域选中单个零件", vbExclamation
        Exit Sub
    End If
    
    ' 获取选中的组件
    Set swComp = swSelMgr.GetSelectedObject6(1, -1)
    If swComp Is Nothing Then
        MsgBox "选中的对象不是零件组件", vbExclamation
        Exit Sub
    End If
    
    ' 获取当前显示状态名称
    displayStateName = swModel.GetDisplayStateName2("")
    
    ' 设置组件在当前显示状态下显示隐藏线
    swComp.SetDisplayStateProperties displayStateName, swComponentDisplayStateProperty_e.swComponentDisplayStateProperty_HiddenLinesVisible, True
    
    ' 刷新视图
    swModel.GraphicsRedraw2
    
    MsgBox "已成功设置选中零件为显示隐藏线样式", vbInformation
End Sub

关键代码说明

  • swSelMgr.GetSelectedObjectCount2(-1):检查是否仅选中单个对象,确保操作目标为单个零件
  • swSelMgr.GetSelectedObject6(1, -1):获取选中的零件组件对象
  • swComp.SetDisplayStateProperties:实现显示样式设置的核心方法,第三个参数True用于开启显示隐藏线
  • swModel.GraphicsRedraw2:刷新视图,让设置立即生效

注意事项

  1. 运行宏前需满足:
    • 已打开装配体文档
    • 绘图区域仅选中单个零件组件(不能是特征或其他类型对象)
  2. 若SolidWorks版本较旧,枚举值无法识别时,可将核心设置语句中的枚举名替换为对应数值13:
    swComp.SetDisplayStateProperties displayStateName, 13, True
    

内容的提问来源于stack exchange,提问作者Ömür tokman

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 13:50:05