今天来简单讲一下如何修改方程式中的一些数据,有时候一些简单的模型我们就可以利用这个全局变量来控制模型.
如下图: 我设定了零件的高度方程式与全局变量h相等.
等到我们需要更新高度时,就可以直接修改这个全局变量,做到更简单的参数化方式,这个全局变量可以是上级装配体中的信息.
下面来具体演示下如何找api:
打开api帮助文件,搜索关键字 globalvariable,就会发现右侧的实例,是一个获取信息的.
- This example shows how to get the values of equations.
-
- //-----------------------------------------
- // Preconditions:
- // 1. Open public_documents\samples\tutorial\api\partequations.sldprt.
- // 2. Open the Immediate window.
- //
- // Postconditions:
- // 1. Gets each equation's value and index and whether the
- // equation is a global variable.
- // 2. Examine the Immediate window.
- //------------------------------------------
- using SolidWorks.Interop.sldworks;
- using SolidWorks.Interop.swconst;
- using System.Runtime.InteropServices;
- using System;
- using System.Diagnostics;
-
- namespace Macro1CSharp.csproj
- {
- public partial class SolidWorksMacro
- {
- public void Main()
- {
- ModelDoc2 swModel = default(ModelDoc2);
- EquationMgr swEqnMgr = default(EquationMgr);
- int i = 0;
- int nCount = 0;
-
- swModel = (ModelDoc2)swApp.ActiveDoc;
- swEqnMgr = (EquationMgr)swModel.GetEquationMgr();
- Debug.Print("File = " + swModel.GetPathName());
- nCount = swEqnMgr.GetCount();
- for (i = 0; i < nCount; i++)
- {
- Debug.Print(" Equation(" + i + ") = " + swEqnMgr.get_Equation(i));
- Debug.Print(" Value = " + swEqnMgr.get_Value(i));
- Debug.Print(" Index = " + swEqnMgr.Status);
- Debug.Print(" Global variable? " + swEqnMgr.get_GlobalVariable(i));
- }
-
- }
-
- /// <summary>
- /// The SldWorks swApp variable is pre-assigned for you.
- /// </summary>
- public SldWorks swApp;
- }
- }
我们参考这段代码来完成方程式信息的读取: 增加按钮写代码.
参照帮助文件写完的版本大概为:
运行一下:
下面我们来做修改:
- private void butGlobalVariables_Click(object sender, EventArgs e)
- {
- //连接solidworks
- ISldWorks swApp = Utility.ConnectToSolidWorks();
-
- if (swApp != null)
- {
- //获取当前模型
- ModelDoc2 swModel = (ModelDoc2)swApp.ActiveDoc;
- //定义方程式管理器
- EquationMgr swEqnMgr = default(EquationMgr);
-
- int i = 0;
- int nCount = 0;
-
- if (swModel != null)
- {
- swEqnMgr = (EquationMgr)swModel.GetEquationMgr();
- // nCount = swEqnMgr.GetCount();
- //for (i = 0; i < nCount; i++)
- //{
- // Debug.Print(" Equation(" + i + ") = " + swEqnMgr.get_Equation(i));
- // Debug.Print(" Value = " + swEqnMgr.get_Value(i));
- // Debug.Print(" Index = " + swEqnMgr.Status);
- // Debug.Print(" Global variable? " + swEqnMgr.get_GlobalVariable(i));
- //}
-
- //修改高度为60
-
- if (SetEquationValue(swEqnMgr, "h", 60))
- {
- swModel.ForceRebuild3(true);
- }
- else
- {
- MessageBox.Show("没有找到这个值!");
- }
- }
- }
- }
-
- #region 修改全局变量所用到的方法
-
- public bool SetEquationValue(EquationMgr eqMgr, string name, double newValue)
- {
- int index = GetEquationIndexByName(eqMgr, name);
-
- if (index != -1)
- {
- eqMgr.Equation[index] = "\"" + name + "\"=" + newValue;
-
- return true;
- }
- else
- {
- return false;
- }
- }
-
- //通过名字找方程式的位置
- private int GetEquationIndexByName(EquationMgr eqMgr, string name)
- {
- int i;
- for (i = 0; i <= eqMgr.GetCount() - 1; i++)
- {
- var eqName = eqMgr.Equation[i].Split('=')[0].Replace("=", "");
-
- eqName = eqName.Substring(1, eqName.Length - 2); // removing the "" symbols from the name
-
- if (eqName.ToUpper() == name.ToUpper())
- {
- return i;
- }
- }
-
- return -1;
- }
-
- #endregion 修改全局变量所用到的方法
到此,零件高度变成了60, 这一节先讲这么多吧.
最终代码请到码云或者github上下载.