/// <summary> /// add family parameter to the family /// </summary> /// <returns> /// if succeeded, return true; otherwise false /// </returns> private bool AddFamilyParameter() { bool allParamValid = true; if (File.Exists(m_familyFilePath) && 0 == m_familyParams.Count) { MessageManager.MessageBuff.AppendLine("No family parameter available for adding."); return(false); } foreach (FamilyParameter param in m_manager.Parameters) { string name = param.Definition.Name; if (m_familyParams.ContainsKey(name)) { allParamValid = false; FamilyParam famParam = m_familyParams[name]; MessageManager.MessageBuff.Append("Line " + famParam.Line + ": paramName \"" + famParam.Name + "\"already exists in the family document.\n"); } } // there're errors in the family parameter text file if (!allParamValid) { return(false); } foreach (FamilyParam param in m_familyParams.Values) { try { m_manager.AddParameter(param.Name, param.Group, param.Type, param.IsInstance); } catch (Exception e) { MessageManager.MessageBuff.AppendLine(e.Message); return(false); } } return(true); }
/// <summary> /// load family parameters from the text file /// </summary> /// <returns> /// return true if succeeded; otherwise false /// </returns> private bool LoadFamilyParameterFromFile(out bool exist) { exist = true; // step 1: find the file "FamilyParameter.txt" and open it string fileName = m_assemblyPath + "\\FamilyParameter.txt"; if (!File.Exists(fileName)) { exist = false; return(true); } FileStream file = null; StreamReader reader = null; try { file = new FileStream(fileName, FileMode.Open, FileAccess.Read); reader = new StreamReader(file); // step 2: read each line, if the line records the family parameter data, store it // record the content of the current line string line; // record the row number of the current line int lineNumber = 0; while (null != (line = reader.ReadLine())) { ++lineNumber; // step 2.1: verify the line // check whether the line is blank line (contains only whitespaces) Match match = Regex.Match(line, @"^\s*$"); if (true == match.Success) { continue; } // check whether the line starts from "#" or "*" (comment line) match = Regex.Match(line, @"\s*['#''*'].*"); if (true == match.Success) { continue; } // step 2.2: get the parameter data // it's a valid line (has the format of "paramName paramGroup paramType isInstance", separate by tab or by spaces) // split the line to an array containing parameter items (format of string[] {"paramName", "paramGroup", "paramType", "isInstance"}) string[] lineData = Regex.Split(line, @"\s+"); // check whether the array has blank items (containing only spaces) List <string> values = new List <string>(); foreach (string data in lineData) { match = Regex.Match(data, @"^\s*$"); if (true == match.Success) { continue; } values.Add(data); } // verify the parameter items (should have 4 items exactly: paramName, paramGroup, paramType and isInstance) if (4 != values.Count) { MessageManager.MessageBuff.Append("Loading family parameter data from \"FamilyParam.txt\"."); MessageManager.MessageBuff.Append("Line [\"" + line + "]\"" + "doesn't follow the valid format.\n"); return(false); } // get the paramName string paramName = values[0]; // get the paramGroup string groupString = values[1]; // in case the groupString is format of "BuiltInParameterGroup.PG_Text", need to remove the "BuiltInParameterGroup.", // keep the "PG_Text" only int index = -1; if (0 <= (index = groupString.ToLower().IndexOf("builtinparametergroup"))) { // why +1? need to remove the "." after "builtinparametergroup" groupString = groupString.Substring(index + 1 + "builtinparametergroup".Length); } BuiltInParameterGroup paramGroup = (BuiltInParameterGroup)Enum.Parse(typeof(BuiltInParameterGroup), groupString); // get the paramType string typeString = values[2]; if (0 <= (index = typeString.ToLower().IndexOf("parametertype"))) { // why +1? need to remove the "." after "builtinparametergroup" typeString = typeString.Substring(index + 1 + "parametertype".Length); } ParameterType paramType = (ParameterType)Enum.Parse(typeof(ParameterType), typeString); // get data "isInstance" string isInstanceString = values[3]; bool isInstance = Convert.ToBoolean(isInstanceString); // step 2.3: store the parameter fetched, check for exclusiveness (as the names of parameters should keep unique) FamilyParam param = new FamilyParam(paramName, paramGroup, paramType, isInstance, lineNumber); // the family parameter with the same name has already been stored to the dictionary, raise an error if (m_familyParams.ContainsKey(paramName)) { FamilyParam duplicatedParam = m_familyParams[paramName]; string warning = "Line " + param.Line + "has a duplicate parameter name with Line " + duplicatedParam.Line + "\n"; MessageManager.MessageBuff.Append(warning); continue; } m_familyParams.Add(paramName, param); } } catch (System.Exception e) { MessageManager.MessageBuff.AppendLine(e.Message); return(false); } finally { if (null != reader) { reader.Close(); } if (null != file) { file.Close(); } } return(true); }
/// <summary> /// load family parameters from the text file /// </summary> /// <returns> /// return true if succeeded; otherwise false /// </returns> private bool LoadFamilyParameterFromFile(out bool exist) { exist = true; // step 1: find the file "FamilyParameter.txt" and open it string fileName = m_assemblyPath + "\\FamilyParameter.txt"; if (!File.Exists(fileName)) { exist = false; return true; } FileStream file = null; StreamReader reader = null; try { file = new FileStream(fileName, FileMode.Open, FileAccess.Read); reader = new StreamReader(file); // step 2: read each line, if the line records the family parameter data, store it // record the content of the current line string line; // record the row number of the current line int lineNumber = 0; while (null != (line = reader.ReadLine())) { ++lineNumber; // step 2.1: verify the line // check whether the line is blank line (contains only whitespaces) Match match = Regex.Match(line, @"^\s*$"); if (true == match.Success) { continue; } // check whether the line starts from "#" or "*" (comment line) match = Regex.Match(line, @"\s*['#''*'].*"); if (true == match.Success) { continue; } // step 2.2: get the parameter data // it's a valid line (has the format of "paramName paramGroup paramType isInstance", separate by tab or by spaces) // split the line to an array containing parameter items (format of string[] {"paramName", "paramGroup", "paramType", "isInstance"}) string[] lineData = Regex.Split(line, @"\s+"); // check whether the array has blank items (containing only spaces) List<string> values = new List<string>(); foreach (string data in lineData) { match = Regex.Match(data, @"^\s*$"); if (true == match.Success) { continue; } values.Add(data); } // verify the parameter items (should have 4 items exactly: paramName, paramGroup, paramType and isInstance) if (4 != values.Count) { MessageManager.MessageBuff.Append("Loading family parameter data from \"FamilyParam.txt\"."); MessageManager.MessageBuff.Append("Line [\"" + line + "]\"" + "doesn't follow the valid format.\n"); return false; } // get the paramName string paramName = values[0]; // get the paramGroup string groupString = values[1]; // in case the groupString is format of "BuiltInParameterGroup.PG_Text", need to remove the "BuiltInParameterGroup.", // keep the "PG_Text" only int index = -1; if (0 <= (index = groupString.ToLower().IndexOf("builtinparametergroup"))) { // why +1? need to remove the "." after "builtinparametergroup" groupString = groupString.Substring(index + 1 + "builtinparametergroup".Length); } BuiltInParameterGroup paramGroup = (BuiltInParameterGroup)Enum.Parse(typeof(BuiltInParameterGroup), groupString); // get the paramType string typeString = values[2]; if (0 <= (index = typeString.ToLower().IndexOf("parametertype"))) { // why +1? need to remove the "." after "builtinparametergroup" typeString = typeString.Substring(index + 1 + "parametertype".Length); } ParameterType paramType = (ParameterType)Enum.Parse(typeof(ParameterType), typeString); // get data "isInstance" string isInstanceString = values[3]; bool isInstance = Convert.ToBoolean(isInstanceString); // step 2.3: store the parameter fetched, check for exclusiveness (as the names of parameters should keep unique) FamilyParam param = new FamilyParam(paramName, paramGroup, paramType, isInstance, lineNumber); // the family parameter with the same name has already been stored to the dictionary, raise an error if (m_familyParams.ContainsKey(paramName)) { FamilyParam duplicatedParam = m_familyParams[paramName]; string warning = "Line " + param.Line + "has a duplicate parameter name with Line " + duplicatedParam.Line + "\n"; MessageManager.MessageBuff.Append(warning); continue; } m_familyParams.Add(paramName, param); } } catch (System.Exception e) { MessageManager.MessageBuff.AppendLine(e.Message); return false; } finally { if (null != reader) { reader.Close(); } if (null != file) { file.Close(); } } return true; }