SolidWorks机械工程师网——最大的SolidWorks学习平台

标题: 求一个删除颜色宏 [打印本页]

作者: qq385629761    时间: 2025-3-2 20:46
标题: 求一个删除颜色宏
对选中的零件或装配体的面的颜色进行清除  移除外观操作   保留材质原设定颜色

作者: xxf8983    时间: 2025-3-3 10:52
顶一下,坐等高手!
作者: jack11111    时间: 2025-3-3 11:02
  1. Option Explicit

  2. Sub Main()
  3.     Dim swApp As SldWorks.SldWorks
  4.     Dim swModel As SldWorks.ModelDoc2
  5.     Dim swPart As SldWorks.PartDoc
  6.     Dim swComp As SldWorks.Component2
  7.     Dim vBodies As Variant
  8.     Dim vComps As Variant
  9.     Dim i As Integer
  10.    
  11.     Set swApp = Application.SldWorks
  12.     Set swModel = swApp.ActiveDoc
  13.    
  14.     If Not swModel Is Nothing Then
  15.         Select Case swModel.GetType
  16.             Case swDocumentTypes_e.swDocPART
  17.                 ' 处理零件文件
  18.                 Set swPart = swModel
  19.                 vBodies = swPart.GetBodies2(swBodyType_e.swAllBodies, True)
  20.                 If Not IsEmpty(vBodies) Then
  21.                     For i = 0 To UBound(vBodies)
  22.                         Dim swBody As SldWorks.Body2
  23.                         Set swBody = vBodies(i)
  24.                         ' 清除实体颜色覆盖
  25.                         swBody.SetMaterialPropertyName ""
  26.                         swBody.ClearBodyColor
  27.                     Next
  28.                 End If
  29.                 ' 清除所有面颜色覆盖
  30.                 ClearFaceColors swModel
  31.                
  32.             Case swDocumentTypes_e.swDocASSEMBLY
  33.                 ' 处理装配体文件
  34.                 vComps = swModel.GetComponents(True)
  35.                 If Not IsEmpty(vComps) Then
  36.                     For i = 0 To UBound(vComps)
  37.                         Set swComp = vComps(i)
  38.                         Set swModel = swComp.GetModelDoc2
  39.                         If Not swModel Is Nothing Then
  40.                             ' 递归处理子组件
  41.                             ClearFaceColors swModel
  42.                             swModel.ForceRebuild3 True
  43.                         End If
  44.                     Next
  45.                 End If
  46.         End Select
  47.         
  48.         ' 刷新视图
  49.         swModel.GraphicsRedraw2
  50.         MsgBox "颜色覆盖已清除!", vbInformation
  51.     Else
  52.         MsgBox "请打开一个零件或装配体文件!", vbExclamation
  53.     End If
  54. End Sub

  55. ' 清除所有面的颜色覆盖
  56. Sub ClearFaceColors(model As SldWorks.ModelDoc2)
  57.     Dim swFace As SldWorks.Face2
  58.     Dim swEntity As SldWorks.Entity
  59.     Dim swModelExt As SldWorks.ModelDocExtension
  60.    
  61.     Set swModelExt = model.Extension
  62.    
  63.     ' 遍历所有面并清除颜色
  64.     Set swEntity = model.FirstFace
  65.     While Not swEntity Is Nothing
  66.         Set swFace = swEntity
  67.         swFace.SetFaceColor Nothing  ' 清除面颜色覆盖
  68.         Set swEntity = swEntity.GetNextFace
  69.     Wend
  70.    
  71.     ' 清除全局实体颜色覆盖
  72.     swModelExt.ClearEntityColor swColorEntityType_e.swColorFaces
  73.     swModelExt.ClearEntityColor swColorEntityType_e.swColorBodies
  74. End Sub
复制代码



作者: qq385629761    时间: 2025-3-3 22:46
jack11111 发表于 2025-3-3 11:02

对选中面进行操作  不所有的面
作者: xiaozhe0581    时间: 前天 10:31
很不错,顶一下!




欢迎光临 SolidWorks机械工程师网——最大的SolidWorks学习平台 (https://www.swbbsc.com/) Powered by Discuz! X3.2