solidworks代号名称分离宏命令(属性”自定义“或”配置特定“中)

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

solidworks代号名称分离宏命令(属性”⾃定义“或”配置特定“中)
SolidWorks2014基于宏实现快速"图号名称"分离
1.两类代码
代码1(内容添加⾄“⾃定义下”)
Dim swApp As Object
Dim Part As Object
Dim SelMgrAs 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 String
Dim t As String
Dim c As String
Dim j As Integer
Dim strmat As String
Dim tempvalue As String
Sub main()
'linksolidworks
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
Set SelMgr = Part.SelectionManager
swApp.ActiveDoc.ActiveView.FrameState = 1
'设定变量
c = swApp.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(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种)
Else
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(内容添加⾄“配置特定”下)
'定义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 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 CustPropMgrAsSldWorks.CustomPropertyManager
Dim swModel 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.ConfigurationManager.ActiveConfiguratio /doc/fe6ca6e079563c1ec4da71c4.html ) '配置特定延伸
'设定变量
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" + 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
'删除栏
CustPropMgr.Delete ("图样代号")
CustPropMgr.Delete ("图样名称")
CustPropMgr.Delete ("材料")
'新增
CustPropMgr.Add2 "图样代号", swCustomInfoText, e
CustPropMgr.Add2 "图样名称", swCustomInfoText, m
CustPropMgr.Add2 "数量", swCustomInfoText, ""
CustPropMgr.Add2 "材料", swCustomInfoText, strmat
CustPropMgr.Add2 "单重", swCustomInfoText, ""
CustPropMgr.Add2 "总重", swCustomInfoText, ""
CustPropMgr.Add2 "备注", swCustomInfoText, ""
End Sub
特别说明:在虚拟件中添加属性时,名称会⾃动加上从属装配体,⾮虚拟件则不会(此问题急需⾼⼈指点)2.操作步骤(以上述代码1为例)
⼀、宏代码1
新建宏代码:⼯具——宏——新建
在⽂件名中输⼊:图号分离.swp;点击保存,进⼊宏编辑界⾯;
1. 2
认识宏编辑界⾯:
1、⼯程信息
2、编程区域
3、做⼀个写代码前的准备⼯作,把2区域的内容Ctrl+A全选删除。

2. 3
写代码:复制如下代码,写到上图的2区域;'从这⾥开始复制: '定义solidwork
Dim swApp As Object
Dim Part As Object
Dim SelMgrAs 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 String
Dim t As String
Dim c As String
Dim j As Integer
Dim strmat As String
Dim tempvalue As String
Sub main()
'linksolidworks
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
Set SelMgr = Part.SelectionManager
swApp.ActiveDoc.ActiveView.FrameState = 1
'设定变量
c = swApp.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(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)
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
新建零件命名规则:
代号+空格+名称
注意:代号中不能出现空格,宏以第⼀个空格为分隔符。

相关文档
最新文档