求助,如何用代码添加全局变量?
如下图片,如何用代码实现添加全局变量:A2="名称"? 其中”名称”是自定义属性。首先,方程式只能是数值,这很重要。
那么,你自定义属性也得整个数值的,就简单了。
你的目的是什么呢?自定义属性可以直接用代码Part.CustomInfo2("", "名称")赋值 方程式只支持纯数值的自定义属性调用,如果调用自定义属性的文本值直接在属性调用即可
如图:
shentu 发表于 2022-5-10 08:26
我的主要问题是如何用宏代码实现自动添加?
swapi 自带的例子,swEquationMgr.Add3 有错?
例子如下:
Option Explicit
Sub main()
Dim SwApp As SldWorks.SldWorks
Dim Part As SldWorks.ModelDoc2
Dim swEquationMgr As SldWorks.EquationMgr
Dim longEquation As Long
Set SwApp = Application.SldWorks
Set Part = SwApp.ActiveDoc
Set swEquationMgr = Part.GetEquationMgr
If swEquationMgr Is Nothing Then ErrorMsg SwApp, "Failed to get the equation manager"
'Add a global variable assignment at index, 0, to all configurations
longEquation = swEquationMgr.Add3(0, """A"" = 2in", True, swAllConfiguration, Empty)
If longEquation <> 0 Then ErrorMsg SwApp, "Failed to add a global variable assignment"
'Add a dimension equation at index, 1, to all configurations
longEquation = swEquationMgr.Add3(1, """D1@Boss-Extrude1"" = 0.05in", True, swAllConfiguration, Empty)
If longEquation <> 1 Then ErrorMsg SwApp, "Failed to add a dimension equation"
'Modify dimension equation at index, 1, in all configurations
longEquation = swEquationMgr.SetEquationAndConfigurationOption(1, """D1@Boss-Extrude1"" = 0.07in", swAllConfiguration, Empty)
If longEquation <> 1 Then ErrorMsg SwApp, "Failed to modify a dimension equation"
End Sub
Function ErrorMsg(SwApp As Object, Message As String)
SwApp.SendMsgToUser2 Message, 0, 0
SwApp.RecordLine "'*** WARNING - General"
SwApp.RecordLine "'*** " & Message
SwApp.RecordLine ""
End Function
自定义属性内嵌宏代码:图号代码: "Part.Extension.CustomPropertyManager("").Set("drawingN",Left(Part.GetTitle, InStr(Part.GetTitle, " ")-1))
名称代码: Part.Extension.CustomPropertyManager("").Set("partN",Right(Part.GetTitle, Len(Part.GetTitle)-InStr(Part.GetTitle," ")))
drawingN:
partN:
方程式: 目前只能手动
A1="图号代码"
A2="名称代码"
Dim swApp As Object
Sub main()
Set swApp = Application.SldWorks
Set doc = swApp.ActiveDoc
For Each an In doc.GetCustomInfoNames '删除所有自定义属性
doc.DeleteCustomInfo an
Next
Dim ST, SG As String
ST = ""
SG = ""
If doc.GetType = 1 Then '零件图
ST = "Part.Extension.CustomPropertyManager" + Chr(40) + Chr(34) + Chr(34) + Chr(41) + ".Set" + Chr(40) + Chr(34) + "图号" + Chr(34) + _
",Left" + Chr(40) + "Part.GetTitle, InStr" + Chr(40) + "Part.GetTitle, " + Chr(34) + " " + Chr(34) + Chr(41) + "-1" + Chr(41) + Chr(41)
SG = "Part.Extension.CustomPropertyManager" + Chr(40) + Chr(34) + Chr(34) + Chr(41) + ".Set" + Chr(40) + Chr(34) + "名称" + Chr(34) + ",Right" + _
Chr(40) + "Part.GetTitle, Len" + Chr(40) + "Part.GetTitle" + Chr(41) + "-InStr" + Chr(40) + "Part.GetTitle," + Chr(34) + " " + Chr(34) + Chr(41) + Chr(41) + Chr(41)
ElseIf doc.GetType = 2 Then '装配体
ST = "Assembly.Extension.CustomPropertyManager" + Chr(40) + Chr(34) + Chr(34) + Chr(41) + ".Set" + Chr(40) + Chr(34) + "图号" + Chr(34) + _
",Left" + Chr(40) + "Assembly.GetTitle, InStr" + Chr(40) + "Assembly.GetTitle, " + Chr(34) + " " + Chr(34) + Chr(41) + "-1" + Chr(41) + Chr(41)
SG = "Assembly.Extension.CustomPropertyManager" + Chr(40) + Chr(34) + Chr(34) + Chr(41) + ".Set" + Chr(40) + Chr(34) + "名称" + Chr(34) + ",Right" + _
Chr(40) + "Assembly.GetTitle, Len" + Chr(40) + "Assembly.GetTitle" + Chr(41) + "-InStr" + Chr(40) + "Assembly.GetTitle," + Chr(34) + " " + Chr(34) + Chr(41) + Chr(41) + Chr(41)
End If
doc.AddCustomInfo3 "", "图号", swCustomInfoText, ""
doc.AddCustomInfo3 "", "名称", swCustomInfoText, ""
doc.AddCustomInfo3 "", "图号代码", swCustomInfoText, ST
doc.AddCustomInfo3 "", "名称代码", swCustomInfoText, SG
Set swEquationMgr = doc.GetEquationMgr
swEquationMgr.Add 0, Chr(34) + "A1" + Chr(34) + "=" + Chr(34) + "名称代码" + Chr(34) '添加方程式---"A1"="名称代码"
swEquationMgr.Add 0, Chr(34) + "A2" + Chr(34) + "=" + Chr(34) + "图号代码" + Chr(34) '添加方程式---"A2"="图号代码"
End Sub
页:
[1]