public static void GetEquationValueByName(ModelDoc2 Part, string VariableName, ref object Value) { /// <summary> /// 获取方程式中全局变量的值,Golbal Variables /// </summary> /// <param name="VariableName">全局变量的名称</param> /// <param name="Part">模型</param> /// <returns>如果没有找到这个全局变量,返回-1</returns> EquationMgr emgr = (EquationMgr)Part.GetEquationMgr(); int ic = emgr.GetCount(); for (int i = 0; i < ic; i++) { string s = emgr.get_Equation(i); s = s.Replace("\"", ""); s = s.Remove(s.IndexOf('=')); s = s.Trim(); if (s.ToLower() == VariableName.ToLower()) { Value = emgr.get_Value(i); return; } } Value = -1; }
public override void initPart(string path) { try { string newPath = Utility.Instance.CopyFile(path); m_solidworksInstance = Activator.CreateInstance(Type.GetTypeFromProgID("SldWorks.Application")) as SldWorks; m_solidworksInstance.Visible = false; int error = 0; m_partInstance = m_solidworksInstance.OpenDocSilent(newPath, (int)swDocumentTypes_e.swDocPART, ref error) as ModelDoc2; if (error != 0) { throw new Exception(); } //m_partParams = null; m_partParams = m_partInstance.GetEquationMgr(); //if (m_partParams == null) //{ // throw new Exception(); //} else //{ // Console.WriteLine("yessss"); //} } catch (Exception e) { MessageBox.Show(e.Message); } }
//by hying 2012/1/14 判断是否有失效的方程式,并将其禁用掉 public void disableEqu(ModelDoc2 swModel) { // WriteLog("整理方程式", 1); try { EquationMgr swEquationMgr = null; swEquationMgr = ((EquationMgr)(swModel.GetEquationMgr())); int EQUnum = swEquationMgr.GetCount(); for (int i = 0; i < EQUnum; i++) { double dval = swEquationMgr.get_Value(i); if (swEquationMgr.Status == -1) { swEquationMgr.set_Suppression(i, true); //WriteLog("整理方程式成功", 1); } } } catch (Exception ex) { //swEquationMgr.set_Suppression(i, true); //Exception ex = new Exception("整理方程式失败!"); throw ex; } }
/// <summary> /// 添加方程式 /// </summary> /// <param name="equMgr"></param> /// <param name="equation">方程式表达式</param> /// <param name="solve">是否立刻求解</param> /// <param name="index">序号,-1代表在后面添加</param> /// <param name="configOption">配置选项,默认为所有配置</param> /// <param name="configName">如果不是所有配置,指定配置名</param> /// <returns>-1 代表添加失败,成功则返回Index</returns> public static int AddEqu(this EquationMgr equMgr, string equation, bool solve = true, int index = -1, swInConfigurationOpts_e configOption = swInConfigurationOpts_e.swAllConfiguration, string configName = "") { return(equMgr.Add3(index, equation, solve, (int)configOption, configName)); }
public void Load() { this.swDoc = OpenSWDoc(this.FilePath); #region 00. 获取方程式的值 Sizes.Clear(); EquationMgr emgr = (EquationMgr)swDoc.GetEquationMgr(); int ic = emgr.GetCount(); for (int i = 0; i < ic; i++) { //在这里想办法只获取全局变量 string s = emgr.get_Equation(i); if (s.Contains("@")) { continue; } s = s.Replace("\"", ""); s = s.Remove(s.IndexOf('=')); s = s.Trim(); s = s.ToUpper(); object v = emgr.get_Value(i); if (Sizes.ContainsKey(s)) { Sizes[s] = v; } else { Sizes.Add(s, v); } } _Sizes = new Dictionary <string, object>(Sizes); #endregion #region 10. 获取零部件属性的值 Attrs.Clear(); object[] PropNames; string cfgname2 = swDoc.ConfigurationManager.ActiveConfiguration.Name; CustomPropertyManager cpmMdl = swDoc.Extension.get_CustomPropertyManager(cfgname2); if (cpmMdl.Count > 0) { PropNames = (object[])cpmMdl.GetNames(); string outval = ""; string sovVal = ""; foreach (object AtrName in PropNames) { string s = AtrName.ToString(); cpmMdl.Get2(AtrName.ToString(), out outval, out sovVal); if (Attrs.ContainsKey(s)) { Attrs[s] = sovVal; } else { Attrs.Add(s, sovVal); } } } _Attrs = new Dictionary <string, string>(Attrs); #endregion }
public EquationMgr getEquationMgr() { EquationMgr swEqMgr = default(EquationMgr); swEqMgr = activeModel.GetEquationMgr(); return(swEqMgr); }
public void ClearSize() { Sizes.Clear(); EquationMgr emgr = (EquationMgr)swDoc.GetEquationMgr(); while (emgr.GetCount() > 0) { emgr.Delete(0); } }
public static void DeleteEquation(EquationMgr equationManager, int index) { equationManager.Delete(index); if (equationManager.GetCount() != 0) { logger.Debug("\n ERROR: Could not delete equations"); } logger.Debug("\n Equation Deleted"); }
/// <summary> /// /// </summary> /// <param name="equMgr"></param> /// <param name="equationValue">字典的Value应该为表达式</param> /// <remarks> /// 全局变量表达式: "B" = 2 /// 组件表达式: "N_SPOKES@CirPattern1" = "BARLENGTH@Sketch2" /10 /// 尺寸表达式 "D1@Extrude2" = (("D1@Extrude3">20, 15, 6))+5 /// </remarks> public static void SetEqu(this EquationMgr equMgr, Dictionary <int, string> equationValue) { foreach (var item in equationValue) { var result = equMgr.SetEqu(item.Key, item.Value); if (!result) { throw new Exception("方程式值设置失败," + item.Key + "," + item.Value); } } }
/// <summary> /// 获取单条方程式 /// </summary> /// <param name="equMgr"></param> /// <param name="i"></param> /// <returns></returns> public static swEqu GetEqu(this EquationMgr equMgr, int i) { return(new swEqu() { Index = i, Equation = equMgr.Equation[i], Value = equMgr.Value[i], Status = equMgr.Status, Suppression = equMgr.Suppression[i], GlobalVariable = equMgr.GlobalVariable[i] }); }
/// <summary> /// 获取所有的方程式 /// </summary> /// <param name="equMgr"></param> /// <returns></returns> public static List <swEqu> GetAllEqu(this EquationMgr equMgr) { int count = equMgr.GetCount(); List <swEqu> swEquList = new List <swEqu>(); for (int i = 0; i < count; i++) { swEqu equ = equMgr.GetEqu(i); swEquList.Add(equ); } return(swEquList); }
public static void AddEquation(EquationMgr equationMgr, string equation) { if ((equationMgr.Add(equationMgr.GetCount(), equation)) == 1) { logger.Debug("\n ERROR: Equation not added to Equation Manager"); } else { logger.Debug("\n Equation added successfully"); } }
/// <summary> /// delete equation for model, by index /// </summary> /// <param name="index"></param> /// <param name="swModel"></param> private void DeleteEquation(int index, IModelDoc2 swModel) { try { EquationMgr myEqu = swModel.GetEquationMgr(); myEqu.Delete(index); swModel.EditRebuild3(); } catch (Exception exception) { MessageObserver.Instance.SetMessage("Exeption at delete equations " + exception.ToString()); } }
public static void DeleteEquation(EquationMgr equationManager, int index) { equationManager.Delete(index); if (equationManager.GetCount() == 0) { Console.WriteLine(" - Delete Equation - Success"); } else { Console.WriteLine(" - WARNING - Delete Equation - Failed"); } }
/// <summary> /// 设置全局变量的值 /// </summary> /// <param name="equMgr"></param> /// <param name="equList"></param> /// <remarks> /// 全局变量表达式: "B" = 2 /// </remarks> public static void SetGlobalEqu(this EquationMgr equMgr, List <swEqu> equList) { foreach (var item in equList) { if (item.GlobalVariable) { var result = equMgr.SetEqu(item.Index, item.Equation); if (!result) { throw new Exception("方程式值设置失败," + item.Index + "," + item.Value); } } } }
/// <summary> /// 设置方程式 /// </summary> /// <param name="equMgr"></param> /// <param name="Index">序号</param> /// <param name="Value">表达式</param> /// <returns></returns> public static bool SetEqu(this EquationMgr equMgr, int Index, string equation) { equMgr.Equation[Index] = equation; string value = equMgr.Equation[Index]; Debug.Print(value); if (value != equation) { return(false); } else { return(true); } }
/// <summary> /// 设置表达式值 /// </summary> /// <param name="equMgr"></param> /// <param name="name"></param> /// <param name="equation">表达式</param> /// <returns></returns> /// <remarks> /// 全局变量表达式: "B" = 2 /// 组件表达式: "N_SPOKES@CirPattern1" = "BARLENGTH@Sketch2" /10 /// 尺寸表达式 "D1@Extrude2" = (("D1@Extrude3">20, 15, 6))+5 /// </remarks> public static bool SetEqu(this EquationMgr equMgr, string name, string equation) { int Index = equMgr.GetAllEqu(). Where(p => p.Equation.Split('=')?.FirstOrDefault()?.Replace("\"", "").Trim() == name) .Select(p => p.Index).FirstOrDefault(); equMgr.Equation[Index] = equation; if (equMgr.Equation[Index] != equation) { return(false); } else { return(true); } }
/// <summary> /// 获取方程式中全局变量的值,Golbal Variables /// </summary> /// <param name="VariableName">全局变量的名称</param> /// <param name="Part">模型</param> /// <returns>如果没有找到这个全局变量,返回-1</returns> public double getGolbalVariablesValue(string VariableName, ModelDoc2 Part) { EquationMgr emgr = (EquationMgr)Part.GetEquationMgr(); int ic = emgr.GetCount(); for (int i = 0; i < ic; i++) { string s = emgr.get_Equation(i); s = s.Replace("\"", ""); s = s.Remove(s.IndexOf('=')); s = s.Trim(); if (s.ToLower() == VariableName.ToLower()) { return(emgr.get_Value(i)); } } return(-1); }
public static void AddEquation(EquationMgr equationMgr, string equation) { Console.WriteLine(" - Equation Manager - Adding Equation - " + equation); equationMgr.Add(0, equation); if (equationMgr.GetCount() == 1) { Console.WriteLine(" - Equation Manager - Equation - " + equation + " - Successfully Added"); } else { Console.WriteLine(" - WARNING - Equation Manager - Failed to Add Equation - " + equation); } }
public static bool setGolbalVariablesValue(string VariableName, ModelDoc2 Part, string Value) { EquationMgr emgr = (EquationMgr)Part.GetEquationMgr(); int ic = emgr.GetCount(); for (int i = 0; i < ic; i++) { string s = emgr.get_Equation(i); s = s.Replace("\"", ""); s = s.Remove(s.IndexOf('=')); s = s.Trim(); if (s.ToLower() == VariableName.ToLower()) { emgr.set_Equation(i, Value); return(true); } } return(false); }
public string[] getSolidWorksGlobalVariables() { ModelDoc2 swModelDoc = swApp.ActiveDoc; EquationMgr swEqnMgr = swModelDoc.GetEquationMgr(); string[] temp = new string[swEqnMgr.GetCount()]; int count = 0; for (int i = 0; i < swEqnMgr.GetCount(); i++) { if (swEqnMgr.GlobalVariable[i]) { temp[count] = swEqnMgr.Equation[i].Split('\"')[1]; count++; } } temp[count] = null; return(temp); }
private void OpenDoc(string _FilePath) { this.FilePath = _FilePath; swDoc = OpenSWDoc(_FilePath); #region 00. 获取方程式的值 Sizes.Clear(); EquationMgr emgr = (EquationMgr)swDoc.GetEquationMgr(); int ic = emgr.GetCount(); for (int i = 0; i < ic; i++) { //在这里想办法只获取全局变量 string s = emgr.get_Equation(i); if (s.Contains("@")) { continue; } s = s.Replace("\"", ""); s = s.Remove(s.IndexOf('=')); s = s.Trim(); s = s.ToUpper(); object v = emgr.get_Value(i); if (Sizes.ContainsKey(s)) { Sizes[s] = v; } else { Sizes.Add(s, v); } } _Sizes = new Dictionary <string, object>(Sizes); #endregion #region 10. 获取零部件属性的值 Attrs.Clear(); _Attrs = new Dictionary <string, string>(Attrs); #endregion //CloseDoc(); }
public void setObject(string[] values) { ModelDoc2 swModelDoc = swApp.ActiveDoc; EquationMgr swEqnMgr = swModelDoc.GetEquationMgr(); for (int i = 0; i < swEqnMgr.GetCount(); i++) { if (swEqnMgr.GlobalVariable[i]) { string newEquation; // Get the variable name from the swEqn string[] temp = swEqnMgr.Equation[i].Split(new string[] { "=" }, StringSplitOptions.None); string name = temp[0]; // Get the variable unit from the swEqn Regex re = new Regex(@"([._0-9]+)([a-zA-Z]+)"); Match result = re.Match(temp[1]); string unit = result.Groups[2].Value; // Get the index of swEqn variable at variables. temp = temp[0].Split('"'); int index = variables.indexVariable(temp[1]); if (index >= 0) { newEquation = name + "= " + values[index] + unit; swEqnMgr.Equation[i] = newEquation; } } } swModelDoc.ForceRebuild3(true); }
/// <summary> /// Function Name: getValues /// Description: returns the variable values /// </summary> /// <param name=""></param> /// <returns>N/A</returns> public string[] getValues() //gets values provided by user { // Hooked into solidworks ModelDoc2 swModelDoc = swApp.ActiveDoc; EquationMgr swEqnMgr = swModelDoc.GetEquationMgr(); // Create the variables list string[] result = new string[numVariables]; // Loop through the solidworks variables for (int i = 0; i < swEqnMgr.GetCount(); i++) { // if there is a global variable i => return the variable if (swEqnMgr.GlobalVariable[i]) { int index = indexVariable(swEqnMgr.Equation[i].ToString().Split('"')[1]); if (index >= 0) { result[index] = swEqnMgr.Value[i].ToString(); } } } return(result); // Return result }
static void Main(string[] args) { // LiveUpdateTest(); var swType = SWType.GetFromProgID(PROG_ID); if (swType == null) { logger.Error("\n ERROR: GetFromProgID returned null\n" + " - Exiting Program"); promptToExitProgram(); return; } ISldWorks swApp = CreateSWInstance.Create(swType); if (swApp == null) { logger.Error("\n ERROR: Could not get reference to " + "SolidWorks App\n - Exiting Program"); promptToExitProgram(); return; } var path = "C:\\Users\\bolinger\\Desktop\\test install\\toppAppDBdaemon\\blob\\C-HSSX.blob.SLDPRT"; DocumentSpecification documentSpecification = SWDocSpecification.GetDocumentSpecification(swApp, path); if (documentSpecification == null) { logger.Error("\n ERROR: Could not Get Document Specification for file: " + path + "\n - Exiting Program"); promptToExitProgram(); return; } logger.Debug("\n Getting Model from Document Specification"); ModelDoc2 model = (ModelDoc2)swApp.OpenDoc7( documentSpecification); if (model == null) { logger.Error("\n ERROR: Could not get Model from " + "Document Specification\n - Exiting Program"); promptToExitProgram(); return; } Config.model = model; logger.Debug("\n Getting Equation Manager from Model"); EquationMgr equationManager = model.GetEquationMgr(); if (equationManager == null) { logger.Error("\n ERROR: Could not get Equation Manager from Model\n" + " - Exiting Program"); promptToExitProgram(); return; } Config.equationManager = equationManager; logger.Debug("\n SolidWorks App Instance Initialized - Starting Microservice Daemon"); Daemon.Start(); promptToExitProgram(); logger.Debug("\n Closing Open SolidWorks Documents" + "\n - Exiting Microservice"); swApp.CloseAllDocuments(true); }
public static void Start(string installDirectory, string programStatePath, string GUIconfigPath, string DDTOpath) { /* * This program listens for a 'call to action bool' * - the second character in a string from the * - run state config file for this MS * On TRUE it reads the DDTO, which the GUI MS * will have sent the current set of expressions to * evaluate, then set the GUI's call to action state * to false - meaning it cannot write to the DDTO, * though it will store the command and do it asap, * then this daemon will read the DDTO and implement * the expressions in it, after it resets its own * and the GUI's call to action, turning itself off * and the GUI's write ability on */ RunState programState; Console.WriteLine(" -- Daemon - Start --"); try { do { programState = RunState.GetProgramState(programStatePath); if (programState.GetActionState()) { CloseGUIsemaphore(GUIconfigPath); var PROG_ID = "SldWorks.Application.24"; var swType = Type.GetTypeFromProgID(PROG_ID); ValidateSWtype(swType); swApp = (ISldWorks)Activator.CreateInstance(swType); CreateSWAppInstance(swApp); var blobName = programState.GetBlobName(); var blobPath = installDirectory + "blob\\" + blobName; DocumentSpecification documentSpecification = SWDocSpecification.GetDocumentSpecification(swApp, blobPath); if (documentSpecification == null) { Console.WriteLine("Could not Get Document Specification for - " + blobPath); return; } Console.WriteLine(" - Obtained Document Specification for - " + blobPath); var model = swApp.OpenDoc7( documentSpecification); if (model == null) { Console.WriteLine(" - ERROR - Could not Open Document - " + blobPath); return; } Console.WriteLine(" - Opened SolidWorks Document - " + blobPath); EquationMgr equationManager = model.GetEquationMgr(); if (equationManager == null) { Console.WriteLine("Could Not Get Equation Manager Instance"); Console.WriteLine(" - Closing All Open SolidWorks Documents"); swApp.CloseAllDocuments(true); return; } Console.WriteLine(" - Created Equation Manager Instance"); var rawBlempString = Blemp.LoadDDTO(DDTOpath); var writeSuccess = false; var timeOut = 5; if (rawBlempString != null) { var equationSegments = Blemp.GetDDTOequationSegments(rawBlempString); if (equationSegments != null) { Console.WriteLine(" - Valid Equations Found - Processing"); try { for (var i = 0; i < equationSegments.Length; ++i) { SWEquation.AddEquation( equationManager, equationSegments[i] ); SWEquation.Build( model ); SWEquation.DeleteEquation( equationManager, 0); } } catch (ArgumentOutOfRangeException exception) { Console.WriteLine(exception); } } else { Console.WriteLine(" - WARNING - No Valid Equations for Processing Found"); } } else { Console.WriteLine("No Equations Found in DDTO"); } do { Console.WriteLine(" - Closing Call to Action Semaphore"); writeSuccess = FileWrite.WriteStringToFileFalseOnFail( programStatePath, "011!" ); FileWrite.WriteStringToFileFalseOnFail(DDTOpath, ""); Thread.Sleep(300); } while (!writeSuccess && timeOut-- > 0); if (timeOut > 0) { Console.WriteLine(" - Call to Action Semaphore - Successfuly Closed"); } else { Console.WriteLine(" - ERROR - Could Not Write Call to Action Close Command"); Console.WriteLine(" - Exiting Daemon -"); Console.WriteLine(" -- Daemon - Exit --"); return; } timeOut = 5; do { Console.WriteLine(" - Opening GUI Call to Action Semaphore"); writeSuccess = FileWrite.WriteStringToFileFalseOnFail( GUIconfigPath, "00" ); Thread.Sleep(300); } while (!writeSuccess && timeOut-- > 0); } if (programState.GetCloseBlob()) { CloseGUIsemaphore(GUIconfigPath); if (swApp != null) { swApp.CloseAllDocuments(true); } FileWrite.WriteStringToFileFalseOnFail(programStatePath, "011!"); Console.WriteLine(" - Opening GUI Call to Action Semaphore"); FileWrite.WriteStringToFileFalseOnFail( GUIconfigPath, "00"); } Thread.Sleep(300); } while (programState.GetRunState()); } catch (Exception exception) { Console.WriteLine(exception); } if (swApp != null) { swApp.CloseAllDocuments(true); } Console.WriteLine(" -- Daemon - Exit --"); }
private int Refresh() { EquationMgr mgr = model.GetEquationMgr(); return(mgr.EvaluateAll()); }
public void generateRandomObject() //this function makes the "generate Random Object" button in the GUI work { string[] arguments = new string[variables.numVariables + constraints.numConstraints + 1]; arguments[0] = variables.numVariables.ToString() + "," + constraints.numConstraints.ToString(); for (int i = 0; i < variables.numVariables; i++) { arguments[i + 1] = variables.variablesName[i] + "," + variables.variablesMin[i] + "," + variables.variablesMax[i] + "," + variables.variablesType[i].ToString(); } for (int i = variables.numVariables; i < variables.numVariables + constraints.numConstraints; i++) { arguments[i + 1] = constraints.constraintsEquation[i - variables.numVariables]; } writeArgument(arguments); string[] pythonResult = runPython("randomSample.py"); if (pythonResult == null) { return; } else if (pythonResult[1] == "false") //If couldn't create object { sendMessageToUser("Failed to generate random object"); return; } ModelDoc2 swModelDoc = swApp.ActiveDoc; EquationMgr swEqnMgr = swModelDoc.GetEquationMgr(); string[] csStdout = pythonResult[0].Split(','); for (int i = 0; i < swEqnMgr.GetCount(); i++) { if (swEqnMgr.GlobalVariable[i]) { string newEquation; // Get the variable name from the swEqn string[] temp = swEqnMgr.Equation[i].Split(new string[] { "=" }, StringSplitOptions.None); string name = temp[0]; // Get the variable unit from the swEqn Regex re = new Regex(@"([._0-9]+)([a-zA-Z]+)"); Match result = re.Match(temp[1]); string unit = result.Groups[2].Value; // Get the index of swEqn variable at variables. temp = temp[0].Split('"'); int index = variables.indexVariable(temp[1]); //string[] randomVariables; if (index >= 0) { // No constraints //string random = randomNumber(variables.variablesMax[index], variables.variablesMin[index], variables.variablesType[index]); //newEquation = name + "= " + random + unit; // // HAVE TO COMPARE WITH CONSTRAINTS!!!!!! newEquation = name + "= " + float.Parse(csStdout[index]).ToString() + unit; swEqnMgr.Equation[i] = newEquation; } } } swModelDoc.ForceRebuild3(true); }
public void Commit() { #region 00. 提交尺寸 EquationMgr emgr = (EquationMgr)swDoc.GetEquationMgr(); int ic = emgr.GetCount(); for (int i = 0; i < ic; i++) { //在这里想办法只获取全局变量 string s = emgr.get_Equation(i); if (s.Contains("@")) { continue; } s = s.Replace("\"", ""); s = s.Remove(s.IndexOf('=')); s = s.Trim(); s = s.ToUpper(); if (Sizes.ContainsKey(s)) { if (Sizes[s] != _Sizes[s]) { string Equation = JswFunc.FormatEquation(s, Sizes[s].ToString()); emgr.set_Equation(i, Equation); } } else { if (_Sizes.ContainsKey(s)) { emgr.Delete(i); i--; ic--; } } } //foreach (string key in Sizes.Keys) //{ // if (Sizes[key] != _Sizes[key]) // { // JswFunc.SetEquationValueByName(swDoc, key, Sizes[key]); // } //} _Sizes = new Dictionary <string, object>(Sizes); #endregion #region 10. 提交属性 string cfgname2 = swDoc.ConfigurationManager.ActiveConfiguration.Name; CustomPropertyManager cpmMdl = swDoc.Extension.get_CustomPropertyManager(cfgname2); //修改属性 foreach (string key in Attrs.Keys) { if (_Attrs.ContainsKey(key)) { if (Attrs[key] != _Attrs[key]) { cpmMdl.Set(key, Attrs[key]); } } else { //如果该Key在_Attrs中找不到,表示为新增的属性 int outval = 30; string sovVal = ""; // cpmMdl.Set(AtrName, Value); cpmMdl.Add2(key, outval, Attrs[key]); } } //清理已经删除的属性 foreach (string key in _Attrs.Keys) { if (!Attrs.ContainsKey(key)) { //旧属性不在新列表中,表示该属性已被删除。 cpmMdl.Delete(key); } } _Attrs = new Dictionary <string, string>(Attrs); #endregion }
/// <summary> /// Méthode interne /// Initialise l'objet GestEquations /// </summary> /// <param name="SwGestionnaire"></param> /// <param name="Modele"></param> /// <returns></returns> internal Boolean Init(EquationMgr SwGestionnaire, eModele Modele) { Log.Methode(cNOMCLASSE); if ((SwGestionnaire != null) && (Modele != null) && Modele.EstInitialise) { _SwGestEquations = SwGestionnaire; _Modele = Modele; _EstInitialise = true; } else { Log.Message("!!!!! Erreur d'initialisation"); } return _EstInitialise; }