public string Format(string fmt, object arg, IFormatProvider formatProvider) { // Convert argument to a string if (!String.IsNullOrEmpty(fmt) && fmt.ToUpper() == "R" && arg.GetType() == typeof(double)) { double dArg = (double)arg; string result = dArg.ToString("R", CultureInfo.CreateSpecificCulture("en-US")); // if compiler flag, only then do the following 3 lines string rDoubleStr = dArg.ToString("R", CultureInfo.CreateSpecificCulture("en-US")); double fixedDbl = double.Parse(rDoubleStr, CultureInfo.CreateSpecificCulture("en-US")); result = fixedDbl.ToString("R", CultureInfo.CreateSpecificCulture("en-US")); //decimal decArg = new Decimal(dArg); // string result = decArg.ToString().ToUpper(); // string result = string.Format("{0:e22}", arg); //string result = dArg.ToString("R", CultureInfo.CreateSpecificCulture("en-US")); if (!result.Contains(".")) { if (result.Contains("E")) { result = result.Replace("E", ".E"); } else { result += "."; } } return(result); } else if (!String.IsNullOrEmpty(fmt) && fmt.ToUpper() == "T") //TimeStamp { string result = arg.ToString().ToUpper(); DateTime?dt = arg as DateTime?; if (dt.HasValue == false) { throw new ArgumentException("Only valid DateTime objects can be converted to Part21 Timestamp"); } return(IfcTimeStamp.ToTimeStamp(dt.Value).ToPart21); } else if (!String.IsNullOrEmpty(fmt) && fmt.ToUpper() == "G") //Guid { string result = arg.ToString().ToUpper(); Guid guid = (Guid)arg; return(string.Format(@"'{0}'", IfcGloballyUniqueId.AsPart21(guid))); } // Return string representation of argument for any other formatting code else { return(string.Format(@"'{0}'", IfcText.Escape(arg.ToString()))); } }
private string HeaderAsString(IIfcFileHeader header) { StringBuilder headerStr = new StringBuilder(); headerStr.AppendLine("ISO-10303-21;"); headerStr.AppendLine("HEADER;"); //FILE_DESCRIPTION headerStr.Append("FILE_DESCRIPTION (("); int i = 0; if (header.FileDescription.Description.Count == 0) { headerStr.Append(@"''"); } else { foreach (string item in header.FileDescription.Description) { headerStr.AppendFormat(@"{0}'{1}'", i == 0 ? "" : ",", IfcText.Escape(item)); i++; } } headerStr.AppendFormat(@"), '{0}');", header.FileDescription.ImplementationLevel); headerStr.AppendLine(); //FileName headerStr.Append("FILE_NAME ("); headerStr.AppendFormat(@"'{0}'", (header.FileName != null && header.FileName.Name != null)? IfcText.Escape(header.FileName.Name):""); headerStr.AppendFormat(@", '{0}'", header.FileName != null? header.FileName.TimeStamp:""); headerStr.Append(", ("); i = 0; if (header.FileName.AuthorName.Count == 0) { headerStr.Append(@"''"); } else { foreach (string item in header.FileName.AuthorName) { headerStr.AppendFormat(@"{0}'{1}'", i == 0 ? "" : ",", IfcText.Escape(item)); i++; } } headerStr.Append("), ("); i = 0; if (header.FileName.Organization.Count == 0) { headerStr.Append(@"''"); } else { foreach (string item in header.FileName.Organization) { headerStr.AppendFormat(@"{0}'{1}'", i == 0 ? "" : ",", IfcText.Escape(item)); i++; } } headerStr.AppendFormat(@"), '{0}', '{1}', '{2}');", IfcText.Escape(header.FileName.PreprocessorVersion), IfcText.Escape(header.FileName.OriginatingSystem), IfcText.Escape(header.FileName.AuthorizationName)); headerStr.AppendLine(); //FileSchema headerStr.AppendFormat("FILE_SCHEMA (('{0}'));", header.FileSchema.Schemas.FirstOrDefault()); headerStr.AppendLine(); headerStr.AppendLine("ENDSEC;"); headerStr.AppendLine("DATA;"); return(headerStr.ToString()); }
/// <summary> /// Writes the value of a property to the TextWriter in the Part 21 format /// </summary> /// <param name="pInfoType"></param> /// <param name="pVal"></param> /// <param name="entityWriter"></param> private static void WriteValueType(Type pInfoType, object pVal, TextWriter entityWriter) { if (pInfoType == typeof(Double)) { entityWriter.Write(string.Format(new Part21Formatter(), "{0:R}", pVal)); } else if (pInfoType == typeof(String)) //convert string { if (pVal == null) { entityWriter.Write('$'); } else { entityWriter.Write('\''); entityWriter.Write(IfcText.Escape((string)pVal)); entityWriter.Write('\''); } } else if (pInfoType == typeof(Int16) || pInfoType == typeof(Int32) || pInfoType == typeof(Int64)) { entityWriter.Write(pVal.ToString()); } else if (pInfoType.IsEnum) //convert enum { entityWriter.Write(string.Format(".{0}.", pVal.ToString().ToUpper())); } else if (pInfoType == typeof(Boolean)) { bool b = false; if (pVal != null) { b = (bool)pVal; entityWriter.Write(string.Format(".{0}.", b ? "T" : "F")); } } else if (pInfoType == typeof(DateTime)) //convert TimeStamp { entityWriter.Write(string.Format(new Part21Formatter(), "{0:T}", pVal)); } else if (pInfoType == typeof(Guid)) //convert Guid string { if (pVal == null) { entityWriter.Write('$'); } else { entityWriter.Write(string.Format(new Part21Formatter(), "{0:G}", pVal)); } } else if (pInfoType == typeof(bool?)) //convert logical { bool?b = (bool?)pVal; entityWriter.Write(!b.HasValue ? ".U." : string.Format(".{0}.", b.Value ? "T" : "F")); } else { throw new ArgumentException(string.Format("Invalid Value Type {0}", pInfoType.Name), "pInfoType"); } }