/// <summary> /// 函数不在=后面加上引号,用于方程式 /// </summary> /// <param name="swApp"></param> /// <param name="variableName"></param> /// <param name="value"></param> public static void SetFunction(this SldWorks swApp, string variableName, string value) { ModelDoc2 swDoc = (ModelDoc2)swApp.ActiveDoc; IEquationMgr equationMgr = swDoc.GetEquationMgr(); int count = equationMgr.GetCount(); string equation = ""; for (int i = count - 1; i > -1; i--) { equation = equationMgr.Equation[i]; if (equation == null || !equation.Contains("\"") || !equation.Contains("=")) { continue; } int s = equation.IndexOf("\"") + 1; int e = equation.IndexOf("="); string name = equation.Substring(s, e - s).Trim(); e = name.LastIndexOf("\""); name = name.Substring(0, e).Trim(); if (name == variableName) { equationMgr.Delete(i); } } equation = "\"" + variableName + "\"" + " = " + value.ToString() + ""; equationMgr.Add2(-1, equation, false); }
//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; } }
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); } }
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); }
/// <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 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(); }
/// <summary> /// 获取全局变量的方程式内容,若无则创建 /// </summary> /// <param name="swApp"></param> /// <param name="name"></param> /// <param name="defaultValue"></param> /// <returns></returns> public static string GetFunction(this SldWorks swApp, string variableName, string defaultFunction) { ModelDoc2 swDoc = (ModelDoc2)swApp.ActiveDoc; IEquationMgr equationMgr = swDoc.GetEquationMgr(); int count = equationMgr.GetCount(); for (int i = count - 1; i > -1; i--) { string equation = equationMgr.Equation[i]; if (equation == null || !equation.Contains("\"") || !equation.Contains("=")) { continue; } int s = equation.IndexOf("\"") + 1; int e = equation.IndexOf("="); string name = equation.Substring(s, e - s).Trim(); e = name.LastIndexOf("\""); name = name.Substring(0, e).Trim(); if (name == variableName) { string function = equation.Substring(equation.IndexOf("=") + 1); return(function); } } //若无,或者转换失败则添加全局变量 string newEquation = "\"" + variableName + "\"" + " = " + defaultFunction; equationMgr.Add2(-1, newEquation, false); return(defaultFunction); }
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 }
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); }
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 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 }
public bool SetObjectValue(ModelDoc2 inModel, string inName, int inType, double inVal, bool noArtIfSuppressed = false) { object swObj; Dimension swDim; Feature swFeature; Component2 swComp; bool ret = false; if (GetObjectByName(inModel, inName, inType, out swObj)) { double dTestVal; switch (inType) { case 14: swDim = (Dimension)swObj; dTestVal = swDim.GetSystemValue2("") * 1000; if (dTestVal != inVal) { ret = (swDim.SetSystemValue2(inVal / 1000, (int) swSetValueInConfiguration_e.swSetValue_InAllConfigurations) == (int)swSetValueReturnStatus_e.swSetValue_Successful); } else ret = true; break; case 22: swFeature = (Feature)swObj; dTestVal = (swFeature.IsSuppressed() ? 0 : 1); if (dTestVal != inVal) { //ret = swFeature.SetSuppression(inVal == 0 ? (int) swFeatureSuppressionAction_e.swSuppressFeature : (int)swFeatureSuppressionAction_e.swUnSuppressFeature); ret = swFeature.SetSuppression2( inVal == 0 ? (int)swFeatureSuppressionAction_e.swSuppressFeature : (int)swFeatureSuppressionAction_e.swUnSuppressFeature, (int)swInConfigurationOpts_e.swAllConfiguration, inModel.GetConfigurationNames()); #region ������ �������� Component2 newComp = null; var config = inModel.IGetActiveConfiguration(); if (config != null) { swComp = config.IGetRootComponent2(); if (swComp != null) { var outComps = new LinkedList<Component2>(); if (GetComponents(swComp, outComps, false, false)) { foreach (var component2 in outComps) { var mod = component2.IGetModelDoc(); if (mod != null) { var texture = component2.GetTexture(""); if (mod.GetType() == (int)swDocumentTypes_e.swDocPART && texture != null) { newComp = component2; } } } } } } if (newComp != null) { inModel.Save(); if (newComp.Select(false)) ((AssemblyDoc)inModel).ComponentReload(); } #endregion } else ret = true; break; case 20: swComp = (Component2)swObj; try { if ((swComp.IsSuppressed() ? 0 : 1) == inVal) { ret = true; } else { int k = inVal == 0 ? (int)swComponentSuppressionState_e.swComponentSuppressed : (int)swComponentSuppressionState_e.swComponentFullyResolved; bool isCavity = false; if (inVal == 0) { var outC = new LinkedList<Component2>(); if (GetComponents(swComp, outC, true, false)) { isCavity = outC.Select(component2 => component2.IGetModelDoc()).Any( m => m != null && m.get_CustomInfo2("", "swrfIsCut") == "Yes"); } } int tmp = k; if (k == (int)swComponentSuppressionState_e.swComponentFullyResolved) tmp = swComp.SetSuppression2(k); if (noArtIfSuppressed) { //���� ���� ������, �� ������ �����.. //���� ���� ��������- �� �������� ��.. ModelDoc2 outModel; if (GetModelByName(inModel, inName, false, out outModel, true)) { if (k == (int)swComponentSuppressionState_e.swComponentSuppressed) { //������� ������� RenameCustomProperty(outModel, string.Empty, "Articul", "Noarticul"); } else if (k == (int)swComponentSuppressionState_e.swComponentFullyResolved) { //����� ������� RenameCustomProperty(outModel, string.Empty, "Noarticul", "Articul"); } } } if (k == (int)swComponentSuppressionState_e.swComponentSuppressed) tmp = swComp.SetSuppression2(k); k = tmp; #region ��������� ��������� var equMrg = inModel.GetEquationMgr(); if (equMrg != null) { var outList = new LinkedList<Component2>(); if (GetComponents(inModel.IGetActiveConfiguration().IGetRootComponent2(), outList, true, false)) { bool notSuppComp = outList.All( component2 => !swComp.IsSuppressed() || swComp.GetPathName() != component2.GetPathName() || component2.IsSuppressed()); for (int i = 0; i < inModel.GetEquationMgr().GetCount(); i++) { if (equMrg.Equation[i].Contains( Path.GetFileNameWithoutExtension(swComp.GetPathName()))) { if ((equMrg.get_Suppression(i) ? 0 : 1) != inVal) { equMrg.set_Suppression(i, inVal == 0 && notSuppComp); } } } } } #endregion #region ��������� ��������� if (isCavity) { LinkedList<Component2> swComps; List<Feature> list; if (_features.Count == 0 || _comps.Count == 0) list = GetAllCavitiesFeatures(out swComps); else { list = _features; swComps = _comps; } string nameMod = Path.GetFileNameWithoutExtension(inModel.GetPathName()); var delList = (from component2 in swComps where component2.Name.Contains(nameMod) && component2.Name.Contains(swComp.Name) from feature in list where component2.Name == GetSpecialNameFromFeature(feature.IGetFirstSubFeature().Name). Split('/').First() + "/" + GetSpecialNameFromFeature(feature.IGetFirstSubFeature().Name). Split('/').ToArray()[1] select feature).ToList(); SwModel.ClearSelection(); foreach (var feature in delList) { feature.Select(true); } SwModel.DeleteSelection(false); } #endregion ret = (k == (int)swSuppressionError_e.swSuppressionChangeOk); } } catch { } break; } } return ret; }