繁体   English   中英

修改Solidworks全局变量值-C#

[英]Modify a Solidworks Global Variable Value - C#

我正在尝试在C#代码中修改全局变量。

基于以下两个示例:

回复:宏更改全局变量

2015 SOLIDWORKS API帮助-添加和修改方程式示例(C#)

我想出了以下代码,但它不会更改全局变量。 另外,在代码运行后进入方程式管理器时,方程式带有红色Xs,并说“该方程式的语法不正确”。

string depth = "\"Depth\" = " + param["Part"].Substring(0, param["Part"].IndexOf("x"));
string flangeWidth = "\"FlangeWidth\" = " + param["Width"];

swEquationMgr = swModel.GetEquationMgr();
swEquationMgr.SetEquationAndConfigurationOption(0, depth, (int)swInConfigurationOpts_e.swAllConfiguration, null);
swEquationMgr.SetEquationAndConfigurationOption(0, flangeWidth, (int)swInConfigurationOpts_e.swAllConfiguration, null);

注意:深度变量正确计算为(“ Depth” = 8),而langeWidth计算为(“ FlangeWidth” = 3.5)。

谁能帮我解决我做错的事情?

SetEquationAndConfigurationOption()仅可用于使用IEquationMgr :: IAdd3添加的方程式。 方法。 是否使用该方法添加了方程式?

我用了:

string depth = "\"Depth\" = 4";
string flangeWidth = "\"FlangeWidth\" = 7";

并且仅在使用Add3方法添加后(需要多个配置),才能使全局变量发生更改。 单一配置的Add2对我不起作用。

另外,SetEquationAndConfigurationOption的第一个参数是索引,两者都为0,因此需要对其进行修改以匹配它们在全局变量中的位置(从0开始)。 如:

swEquationMgr.SetEquationAndConfigurationOption(0, depth, (int)swInConfigurationOpts_e.swAllConfiguration, null);
swEquationMgr.SetEquationAndConfigurationOption(1, flangeWidth, (int)swInConfigurationOpts_e.swAllConfiguration, null);

您可以使用它根据其名称修改任何变量 这是唯一的,因为大多数其他代码示例向您展示了如何通过其索引设置变量!

Sub SetVar(NAME As String, VALUE As Variant)
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swEqnMgr = swModel.GetEquationMgr

    For i = 0 To swEqnMgr.GetCount - 1
        vSplit = Split(swEqnMgr.Equation(i), " = ")
        vSplit(0) = Replace(vSplit(0), Chr(34), Empty)
        If vSplit(0) = NAME Then _
            swEqnMgr.Equation(i) = Replace(swEqnMgr.Equation(i), vSplit(1), VALUE)
    Next i
End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM