public Converter(string sourceFolderXml, string targetFolderYaml, string targetFolderJson, string targetFolderResx, bool checkBSDD = false) { CheckBSDD = checkBSDD; _bsdd = new Bsdd(); string propertySetVersionList = string.Empty; string propertySetTemplateList = string.Empty; string propertyTypeList = string.Empty; string propertyUnitList = string.Empty; foreach (string sourceFile in Directory.EnumerateFiles(sourceFolderXml, "PSet*.xml").OrderBy(x => x).ToList())//.Where(x=>x.Contains("Pset_ConstructionResource"))) { numberOfPsets++; PropertySetDef pSet = PropertySetDef.LoadFromFile(sourceFile); log.Info("--------------------------------------------------"); log.Info($"Checking PSet {pSet.Name}"); log.Info($"Opened PSet-File {sourceFile.Replace(sourceFolderXml + @"\", string.Empty)}"); if (!propertySetVersionList.Contains(pSet.IfcVersion.version)) { propertySetVersionList += pSet.IfcVersion.version + ","; } if (!propertySetTemplateList.Contains(pSet.templatetype.ToString())) { propertySetTemplateList += pSet.templatetype.ToString() + ","; } PropertySet propertySet = new PropertySet() { name = pSet.Name, dictionaryReference = new DictionaryReference() { ifdGuid = "", legacyGuid = "" }, ifcVersion = new IfcVersion() { version = ConvertToSematicVersion(pSet.IfcVersion.version).ToString(), schema = pSet.IfcVersion.schema }, definition = pSet.Definition }; propertySet.applicableIfcClasses = new List <ApplicableIfcClass>(); foreach (var applicableClass in pSet.ApplicableClasses) { propertySet.applicableIfcClasses.Add(new ApplicableIfcClass() { name = applicableClass, type = pSet.ApplicableTypeValue }); } //Insert missing standard localizations as dummys propertySet.localizations = new List <Localization>(); foreach (string standardLanguage in StandardLanguages.OrderBy(x => x)) { if (propertySet.localizations.Where(x => x.language == standardLanguage).FirstOrDefault() == null) { propertySet.localizations.Add(new Localization() { language = standardLanguage, name = string.Empty, definition = string.Empty }); } } if (CheckBSDD) { if (propertySet.dictionaryReference.legacyGuid.Length == 0) { Console.ForegroundColor = ConsoleColor.Red; log.Info($" ERROR: The GUID is missing in PSet!"); Console.ResetColor(); } } IfdConceptList ifdConceptList = _bsdd.SearchNests(pSet.Name); if (ifdConceptList == null) { log.Info($" Could not find the PSet in bSDD"); } else { numberOfPsetsWithbSDDGuid++; IfdConcept bsddPSet = ifdConceptList.IfdConcept.FirstOrDefault(); log.Info($" Loaded Property from bSDD (1 out of {ifdConceptList.IfdConcept.Count})"); log.Info($" Loaded PSet from bSDD"); log.Info($" Guid: {bsddPSet.Guid}"); log.Info($" Status: {bsddPSet.Status}"); log.Info($" VersionDate: {bsddPSet.VersionDate}"); log.Info($" Web: http://bsdd.buildingsmart.org/#concept/browse/{bsddPSet.Guid}"); if (ifdConceptList.IfdConcept.Count == 1) { log.Info($" The GUID of the PSet in the file was changed {propertySet.dictionaryReference.legacyGuid} => {bsddPSet.Guid}"); propertySet.dictionaryReference.ifdGuid = bsddPSet.Guid; } } propertySet.dictionaryReference.dictionaryWebUri = $"http://bsdd.buildingsmart.org/#concept/browse/{propertySet.dictionaryReference.ifdGuid}"; propertySet.dictionaryReference.dictionaryApiUri = $"http://bsdd.buildingsmart.org/api/4.0/IfdConcept/{propertySet.dictionaryReference.ifdGuid}"; log.Info($" Now checking the properties within the PSet"); propertySet.properties = LoadProperties(pSet, pSet.PropertyDefs); propertySet = Utils.PrepareTexts(propertySet); string targetFileYaml = sourceFile.Replace("xml", "YAML").Replace(sourceFolderXml, targetFolderYaml); string targetFileJson = sourceFile.Replace("xml", "json").Replace(sourceFolderXml, targetFolderJson); string targetFileResx = sourceFile.Replace("xml", "resx").Replace(sourceFolderXml, targetFolderResx); var ScalarStyleSingleQuoted = new YamlMemberAttribute() { ScalarStyle = ScalarStyle.SingleQuoted }; var yamlSerializer = new SerializerBuilder() //.WithNamingConvention(new CamelCaseNamingConvention()) .WithAttributeOverride <PropertySet>(nc => nc.name, ScalarStyleSingleQuoted) .WithAttributeOverride <PropertySet>(nc => nc.definition, ScalarStyleSingleQuoted) .WithAttributeOverride <Localization>(nc => nc.name, ScalarStyleSingleQuoted) .WithAttributeOverride <Localization>(nc => nc.definition, ScalarStyleSingleQuoted) .Build(); string yamlContent = yamlSerializer.Serialize(propertySet); File.WriteAllText(targetFileYaml, yamlContent, Encoding.UTF8); log.Info("The PSet was saved as YAML file"); JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings(); jsonSerializerSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter()); jsonSerializerSettings.NullValueHandling = NullValueHandling.Ignore; string jsonContent = JsonConvert.SerializeObject(propertySet, Formatting.Indented, jsonSerializerSettings); File.WriteAllText(targetFileJson, jsonContent, Encoding.UTF8); log.Info("The PSet was saved as JSON file"); var yamlDeserializer = new DeserializerBuilder() .Build(); try { propertySet = yamlDeserializer.Deserialize <PropertySet>(new StringReader(File.ReadAllText(targetFileYaml))); log.Info("The YAML file is valid"); } catch (Exception ex) { Console.Write(" ERROR!"); log.Info(ex.Message); } ResxWriter resx = new ResxWriter(targetFileResx); resx.Write(propertySet, StandardLanguages); log.Info("The PSet was saved as RESX file"); } log.Info($"Number of PSets: {numberOfPsets}"); log.Info($" with not resolved bSDD Guid: {numberOfPsetsWithbSDDGuid}"); log.Info($"Number of Properties: {numberOfProperties}"); log.Info($" with not resolved bSDD Guid: {numberOfPropertiesWithbSDDGuid}"); }
public Converter(string sourceFolderXml, string targetFolderYaml, string targetFolderJson, bool checkBSDD = false) { Console.OutputEncoding = System.Text.Encoding.UTF8; CheckBSDD = checkBSDD; _bsdd = new Bsdd(); string propertySetVersionList = string.Empty; string propertySetTemplateList = string.Empty; string propertyTypeList = string.Empty; string propertyUnitList = string.Empty; foreach (string sourceFile in Directory.EnumerateFiles(sourceFolderXml, "PSet*.xml").OrderBy(x => x).ToList())//.Where(x=>x.Contains("Pset_ConstructionResource"))) { PropertySetDef pSet = PropertySetDef.LoadFromFile(sourceFile); Console.WriteLine("--------------------------------------------------"); Console.WriteLine($"Checking PSet {pSet.Name}"); Console.WriteLine($"Opened PSet-File {sourceFile.Replace(sourceFolderXml + @"\", string.Empty)}"); IfdConceptList ifdConceptList = _bsdd.SearchNests(pSet.Name); if (ifdConceptList == null) { Console.WriteLine($"Could not find the PSet in bSDD"); } else { IfdConcept bsddPSet = ifdConceptList.IfdConcept.FirstOrDefault(); Console.WriteLine($"Loaded PSet from bSDD"); Console.WriteLine($"Guid: {bsddPSet.Guid}"); Console.WriteLine($"Status: {bsddPSet.Status}"); Console.WriteLine($"VersionDate: {bsddPSet.VersionDate}"); } if (!propertySetVersionList.Contains(pSet.IfcVersion.version)) { propertySetVersionList += pSet.IfcVersion.version + ","; } if (!propertySetTemplateList.Contains(pSet.templatetype.ToString())) { propertySetTemplateList += pSet.templatetype.ToString() + ","; } PropertySet propertySet = new PropertySet() { name = pSet.Name, ifdGuid = "", legacyGuid = "", ifcVersion = new IfcVersion() { version = ConvertToSematicVersion(pSet.IfcVersion.version).ToString(), schema = pSet.IfcVersion.schema }, definition = pSet.Definition }; propertySet.applicableIfcClasses = new List <ApplicableIfcClass>(); foreach (var applicableClass in pSet.ApplicableClasses) { propertySet.applicableIfcClasses.Add(new ApplicableIfcClass() { name = applicableClass, type = pSet.ApplicableTypeValue }); } //Insert missing standard localizations as dummys propertySet.localizations = new List <Localization>(); foreach (string standardLanguage in StandardLanguages.OrderBy(x => x)) { if (propertySet.localizations.Where(x => x.language == standardLanguage).FirstOrDefault() == null) { propertySet.localizations.Add(new Localization() { language = standardLanguage, name = string.Empty, definition = string.Empty }); } } Console.WriteLine($"Now checking the properties within the PSet"); propertySet.properties = LoadProperties(pSet, pSet.PropertyDefs); propertySet = Utils.PrepareTexts(propertySet); string targetFileYaml = sourceFile.Replace("xml", "YAML").Replace(sourceFolderXml, targetFolderYaml); string targetFileJson = sourceFile.Replace("xml", "json").Replace(sourceFolderXml, targetFolderJson); //Console.WriteLine($" Writing {targetFileYaml.Replace(targetFolderYaml + @"\", string.Empty)}"); //Console.WriteLine($" Writing {targetFileJson.Replace(targetFolderJson + @"\", string.Empty)}"); var ScalarStyleSingleQuoted = new YamlMemberAttribute() { ScalarStyle = ScalarStyle.SingleQuoted }; var yamlSerializer = new SerializerBuilder() //.WithNamingConvention(new CamelCaseNamingConvention()) .WithAttributeOverride <PropertySet>(nc => nc.name, ScalarStyleSingleQuoted) .WithAttributeOverride <PropertySet>(nc => nc.definition, ScalarStyleSingleQuoted) .WithAttributeOverride <Localization>(nc => nc.name, ScalarStyleSingleQuoted) .WithAttributeOverride <Localization>(nc => nc.definition, ScalarStyleSingleQuoted) .Build(); string yamlContent = yamlSerializer.Serialize(propertySet); File.WriteAllText(targetFileYaml, yamlContent, Encoding.UTF8); Console.WriteLine("The PSet was saved as YAML file"); JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings(); jsonSerializerSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter()); jsonSerializerSettings.NullValueHandling = NullValueHandling.Ignore; string jsonContent = JsonConvert.SerializeObject(propertySet, Formatting.Indented, jsonSerializerSettings); File.WriteAllText(targetFileJson, jsonContent, Encoding.UTF8); Console.WriteLine("The PSet was saved as JSON file"); var yamlDeserializer = new DeserializerBuilder() .Build(); try { propertySet = yamlDeserializer.Deserialize <PropertySet>(new StringReader(File.ReadAllText(targetFileYaml))); Console.WriteLine("The YAML file is valid"); } catch (Exception ex) { Console.Write(" ERROR!"); Console.WriteLine(ex.Message); } } Console.WriteLine($"Used Versions in PSets: {propertySetVersionList}"); Console.WriteLine($"Used Templates in PSets: {propertySetTemplateList}"); Console.WriteLine($"Used PropertyTypes: {propertyTypeList}"); Console.WriteLine($"Used Units: {propertyUnitList}"); }