Example #1
0
        //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;
            }
        }
Example #2
0
        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;
        }
Example #3
0
        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
        }
Example #4
0
        public void ClearSize()
        {
            Sizes.Clear();
            EquationMgr emgr = (EquationMgr)swDoc.GetEquationMgr();

            while (emgr.GetCount() > 0)
            {
                emgr.Delete(0);
            }
        }
Example #5
0
        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);
        }
        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");
        }
 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");
     }
 }
Example #8
0
        /// <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);
        }
Example #9
0
        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");
            }
        }
Example #10
0
        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);
            }
        }
Example #11
0
        /// <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);
        }
Example #12
0
        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);
        }
Example #13
0
 public override Dictionary <string, string> initPartParamNameUnit()
 {
     for (int i = 0; i < m_partParams.GetCount(); i++)
     {
         if (m_partParams.get_GlobalVariable(i))
         {
             string equation = m_partParams.get_Equation(i);
             string name     = equation.Substring(1, equation.IndexOf('=') - 2);
             string value    = equation.Substring(equation.IndexOf('=') + 2);
             //Console.WriteLine(name);
             //Console.WriteLine(value);
             nameIndexPair.Add(name, i);
             string unit = getUnit(value);
             //Console.WriteLine(unit);
             nameUnitPair.Add(name, unit);
         }
     }
     return(nameUnitPair);
 }
Example #14
0
        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();
        }
Example #15
0
        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);
        }
Example #16
0
            /// <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
            }
Example #17
0
        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);
        }
Example #18
0
 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
 }