找回密码 注册 QQ登录
开思网工业级高精度在线3D打印服务

iCAx开思网

CAD/CAM/CAE/设计/模具 高清视频【积分说明】如何快速获得积分?在线3D打印服务,一键上传,自动报价 
查看: 7071|回复: 34
打印 上一主题 下一主题

[分享] 宏-按顺序特征重命名

[复制链接]
跳转到指定楼层
1
发表于 2018-6-8 09:18:35 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多同行朋友,交流,分享,学习。

您需要 登录 才可以下载或查看,没有帐号?注册

x
以前搞过一个宏是英文名改中文的,有网友问能不能按顺序特征重命名.正好下载了一个,供大家分享.其中的语名值得学习
  1. 'PYCZT2018/5/26下载于https://www.codestack.net
  2. 'This macro renames all the features in active model in the order, preserving the base names.
  3. '该宏按顺序重命名活动模型中的所有特征名,保留原基本名称。
  4. 'Only indices are renamed and the base name is preserved. For example Sketch21 will be renamed to Sketch1 for
  5. 'the first appearance of the sketch feature.
  6. '只有索引被重命名,基本名称被保留。例如,对于草图特性的第一次出现,Sketch 21将被重命名为Sketch 1.


  7. 'Notes注意事项:
  8. '1.Only features with number at the end will be renamed (e.g. Front Plane will not be renamed to Front Plane1 and My1Feature will not be renamed)
  9. '只在结尾处有编号的特征将被重命名(例如,Front Plane不会被重命名为Front Plane1,以及My1Feature也不会被重命名)
  10. '2.Case is ignored (case insensitive search)
  11. '大小写是忽略的
  12. '3.Only modelling features are renamed (the ones created after the Origin feature)
  13. '只重命名建模用的特征名(在原点特征之后的)

  14. '*****************************************************************

  15. Dim swApp As SldWorks.SldWorks
  16. Dim swModel As SldWorks.ModelDoc2

  17. Sub main()

  18.     Set swApp = Application.SldWorks
  19.    
  20.     Set swModel = swApp.ActiveDoc
  21.    
  22.     Dim passedOrigin As Boolean
  23.     passedOrigin = False
  24.    
  25.     If Not swModel Is Nothing Then
  26.    
  27.         Dim featNamesTable As Object
  28.         Dim processedFeats As Collection
  29.         
  30.         Set featNamesTable = CreateObject("Scripting.Dictionary")
  31.         Set processedFeats = New Collection
  32.         
  33.         featNamesTable.CompareMode = vbTextCompare 'case insensitive
  34.         
  35.         Dim swFeat As SldWorks.Feature
  36.         Set swFeat = swModel.FirstFeature
  37.         
  38.         While Not swFeat Is Nothing
  39.             
  40.             If passedOrigin Then
  41.             
  42.                 If Not Contains(processedFeats, swFeat) Then
  43.                     processedFeats.Add swFeat
  44.                     RenameFeature swFeat, featNamesTable
  45.                 End If
  46.                
  47.                 Dim swSubFeat As SldWorks.Feature
  48.                 Set swSubFeat = swFeat.GetFirstSubFeature
  49.                
  50.                 While Not swSubFeat Is Nothing
  51.                     
  52.                     If Not Contains(processedFeats, swSubFeat) Then
  53.                         processedFeats.Add swSubFeat
  54.                         RenameFeature swSubFeat, featNamesTable
  55.                     End If
  56.                     
  57.                     Set swSubFeat = swSubFeat.GetNextSubFeature
  58.                     
  59.                 Wend
  60.             
  61.             End If
  62.             
  63.             If swFeat.GetTypeName2() = "OriginProfileFeature" Then
  64.                 passedOrigin = True
  65.             End If
  66.             
  67.             Set swFeat = swFeat.GetNextFeature
  68.         Wend
  69.         
  70.     Else
  71.         MsgBox "Please open model"
  72.     End If

  73. End Sub

  74. Sub RenameFeature(feat As SldWorks.Feature, featNamesTable As Object)

  75.     Dim regEx As Object
  76.     Set regEx = CreateObject("VBScript.RegExp")
  77.    
  78.     regEx.Global = True
  79.     regEx.IgnoreCase = True
  80.     regEx.Pattern = "(.+?)(\d+)$"
  81.    
  82.     Dim regExMatches As Object
  83.     Set regExMatches = regEx.Execute(feat.Name)
  84.    
  85.     If regExMatches.Count = 1 Then
  86.         
  87.         If regExMatches(0).SubMatches.Count = 2 Then
  88.             
  89.             Dim baseFeatName As String
  90.             baseFeatName = regExMatches(0).SubMatches(0)
  91.             
  92.             Dim nextIndex As Integer
  93.             
  94.             If featNamesTable.Exists(baseFeatName) Then
  95.                 nextIndex = featNamesTable.item(baseFeatName) + 1
  96.                 featNamesTable.item(baseFeatName) = nextIndex
  97.             Else
  98.                 nextIndex = 1
  99.                 featNamesTable.Add baseFeatName, nextIndex
  100.             End If
  101.             feat.Name = baseFeatName & nextIndex
  102.         End If
  103.     End If

  104. End Sub

  105. Function Contains(coll As Collection, item As Object) As Boolean
  106.    
  107.     Dim i As Integer
  108.    
  109.     For i = 1 To coll.Count
  110.         If coll.item(i) Is item Then
  111.             Contains = True
  112.             Exit Function
  113.         End If
  114.     Next
  115.    
  116.     Contains = False
  117.    
  118. End Function
复制代码


评分

参与人数 1贡献 +5 收起 理由
ryouss + 5 赞一个!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏8 分享淘帖 赞一下!赞一下!
2
发表于 2018-6-8 10:39:48 | 只看该作者

马上注册,结交更多同行朋友,交流,分享,学习。

您需要 登录 才可以下载或查看,没有帐号?注册

x
3
发表于 2018-6-8 19:14:18 | 只看该作者

马上注册,结交更多同行朋友,交流,分享,学习。

您需要 登录 才可以下载或查看,没有帐号?注册

x
4
发表于 2018-6-9 08:07:08 | 只看该作者

马上注册,结交更多同行朋友,交流,分享,学习。

您需要 登录 才可以下载或查看,没有帐号?注册

x
5
发表于 2018-6-9 08:13:48 | 只看该作者

马上注册,结交更多同行朋友,交流,分享,学习。

您需要 登录 才可以下载或查看,没有帐号?注册

x
6
 楼主| 发表于 2018-6-9 09:56:33 | 只看该作者

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
7
发表于 2018-6-9 16:39:25 | 只看该作者

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
8
发表于 2018-6-10 10:14:44 | 只看该作者

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
9
发表于 2018-6-11 06:32:03 | 只看该作者

马上注册,结交更多同行朋友,交流,分享,学习。

您需要 登录 才可以下载或查看,没有帐号?注册

x
10
 楼主| 发表于 2018-6-11 15:00:15 | 只看该作者

马上注册,结交更多同行朋友,交流,分享,学习。

您需要 登录 才可以下载或查看,没有帐号?注册

x
您需要登录后才可以回帖 登录 | 注册

本版积分规则

3D打印手板模型快速制作服务,在线报价下单!

QQ 咨询|手机版|联系我们|iCAx开思网  

GMT+8, 2024-4-26 00:08 , Processed in 0.026210 second(s), 10 queries , Gzip On, Redis On.

Powered by Discuz! X3.3

© 2002-2024 www.iCAx.org

快速回复 返回顶部 返回列表