1、SolidWorks2014基于宏实现快速"图号名称"分离 1. 两类代码 Ø 代码1(内容添加至“自定义下”) Dim swApp As Object Dim Part As Object Dim SelMgr As Object Dim boolstatus As Boolean Dim longstatus As Long, longwarnings As Long Dim Feature As Object Dim a As Integer Dim b As String Dim m As String Dim e As String Dim k As Str
2、ing Dim t As String Dim c As String Dim j As Integer Dim strmat As String Dim tempvalue As String Sub main() 'link solidworks Set swApp = Application.SldWorks Set Part = swApp.ActiveDoc Set SelMgr = Part.SelectionManager swApp.ActiveDoc.ActiveView.FrameState = 1 '设定变量 c = swApp.ActiveDo
3、c.GetTitle() '零件名 strmat = Chr(34) + Trim("SW-Material" + "@") + c + Chr(34) blnretval = Part.DeleteCustomInfo2("", "图样代号") blnretval = Part.DeleteCustomInfo2("", "图样名称") blnretval = Part.DeleteCustomInfo2("", "材料") a = InStr(c, " ") - 1 '重点:分隔标识符,这里是一个空格,也可用其他符号区分 If a > 0 Then k =
4、Left(c, a) t = Left(LTrim(e), 3) If t = "GBT" Then e = "GB/T" + Mid(k, 4) Else e = k End If b = Mid(c, a + 2) t = Right(c, 7) If t = ".SLDPRT" Or t = ".SLDASM" Or t = ".sldprt" Or t = ".sldasm"Then j = Len(b) - 7 '消除后缀(区分大小写,即含4种) El
5、se j = Len(b) End If m = Left(b, j) End If blnretval = Part.AddCustomInfo3("", "图样代号", swCustomInfoText, e) '代号 blnretval = Part.AddCustomInfo3("", "图样名称", swCustomInfoText, m) '名称 blnretval = Part.AddCustomInfo3("", "表面处理", swCustomInfoText, " ") End Sub Ø 代码2(内容添加至“配置特定”
6、下) '定义sw Dim a As Integer Dim b As String Dim m As String Dim e As String Dim k As String Dim t As String Dim c As String Dim j As Integer Dim strmat
7、 As String Dim tempvalue As String Dim Part As Object Dim swApp As SldWorks.SldWorks Dim swModelDoc As SldWorks.ModelDoc2 Dim swConfig As SldWorks.Configuration Dim CustPropMgr As SldWorks.CustomPropertyManager Dim swModel
8、 As SldWorks.ModelDoc2 Sub main() Set swApp = Application.SldWorks Set swModelDoc = swApp.ActiveDoc Set swConfig = swModelDoc.ConfigurationManager.ActiveConfiguration Set swModel = swApp.ActiveDoc Set CustPropMgr = swModel.Extension.CustomPropertyManager(swModel.ConfigurationManage
9、r.ActiveConfiguration.Name) '配置特定延伸 '设定变量 c = swApp.ActiveDoc.GetTitle() '零件名 strmat = Chr(34) + Trim("SW-Material" + "@") + c + Chr(34) a = InStr(c, " ") - 1 '重点:分隔标识符,这里是一个空格,也可换成其他符号 If a > 0 Then k = Left(c, a) t = Left(LTrim(e), 3) If t = "GBT" Then e = "GB/T
10、" + Mid(k, 4) Else e = k End If b = Mid(c, a + 2) t = Right(c, 7) If t = ".SLDPRT" Or t = ".SLDASM" Or t = ".sldprt" Or t = ".sldasm"Then j = Len(b) - 7 '消除后缀(区分大小写,即含4种) Else j = Len(b) End If m = Left(b, j) End If '删除栏 CustPropM
11、gr.Delete ("图样代号") CustPropMgr.Delete ("图样名称") CustPropMgr.Delete ("材料") '新增 CustPropMgr.Add2 "图样代号", swCustomInfoText, e CustPropMgr.Add2 "图样名称", swCustomInfoText, m CustPropMgr.Add2 "数量", swCustomInfoText, "" CustPropMgr.Add2 "材料", swCustomInfoText, strmat CustPropMgr.Add2 "单重", swCustom
12、InfoText, "" CustPropMgr.Add2 "总重", swCustomInfoText, "" CustPropMgr.Add2 "备注", swCustomInfoText, "" End Sub 特别说明:在虚拟件中添加属性时,名称会自动加上从属装配体,非虚拟件则不会(此问题急需高人指点) 2. 操作步骤(以上述代码1为例) 一、宏代码1 新建宏代码:工具——宏——新建 在文件名中输入:图号分离.swp ;点击保存,进入宏编辑界面; 1. 2 认识宏编辑界面: 1、工程信息 2、编程区域 3、做一个写代码前的准备工作,把2区域的内
13、容Ctrl+A 全选删除。 2. 3 写代码:复制如下代码,写到上图的2区域; '从这里开始复制: '定义solidwork Dim swApp As Object Dim Part As Object Dim SelMgr As Object Dim boolstatus As Boolean Dim longstatus As Long, longwarnings As Long Dim Feature As Object Dim a As Integer Dim b As String Dim m As String Dim e As String Di
14、m k As String Dim t As String Dim c As String Dim j As Integer Dim strmat As String Dim tempvalue As String Sub main() 'link solidworks Set swApp = Application.SldWorks Set Part = swApp.ActiveDoc Set SelMgr = Part.SelectionManager swApp.ActiveDoc.ActiveView.FrameState = 1 '设定变量 c = swAp
15、p.ActiveDoc.GetTitle() '零件名 strmat = Chr(34) + Trim("SW-Material" + "@") + c + Chr(34) blnretval = Part.DeleteCustomInfo2("", "代号") blnretval = Part.DeleteCustomInfo2("", "名称") blnretval = Part.DeleteCustomInfo2("", "材料") a = InStr(c, " ") - 1 '重点:分隔标识符,这里是一个空格 If a > 0 Then k = Left
16、c, a) t = Left(LTrim(e), 3) If t = "GBT" Then e = "GB/T" + Mid(k, 4) Else e = k End If b = Mid(c, a + 2) t = Right(c, 7) If t = ".SLDPRT" Or t = ".SLDASM" Then j = Len(b) - 7 Else j = Len(b) End If m = Left(b, j)
17、 End If blnretval = Part.AddCustomInfo3("", "代号", swCustomInfoText, e) '代号 blnretval = Part.AddCustomInfo3("", "名称", swCustomInfoText, m) '名称 blnretval = Part.AddCustomInfo3("", "表面处理", swCustomInfoText, " ") End Sub 3. 4 保存退出。 第一部分做到这就算完成了。下面是创建宏按钮; END 二、创建宏按钮 1. 返回Solidworks工作界面,进入自定义工具栏:工具——自定义; 选择命令选项卡,找到宏 ,把新建宏按钮拖到工具栏的空白处; 2. 弹出自定义宏按钮对话框: 1、选择上面保存的宏文件; 2、指定一个图标,以便在工具栏中显示; 3、指定一个指示的名称,以便在工具栏中显示; 3. 点击确定,退出自定义宏按钮,完成制作。 4. 4 新建零件命名规则: 代号+空格+名称 注意:代号中不能出现空格,宏以第一个空格为分隔符。






