IfcDoc.FormatCSV.Load C# (CSharp) Method

Load() public method

public Load ( ) : void
return void
        public void Load()
        {
            // prepare map
            Dictionary<string, DocObject> map = new Dictionary<string, DocObject>();
            foreach (DocSection docSection in this.m_project.Sections)
            {
                foreach (DocSchema docSchema in docSection.Schemas)
                {
                    foreach (DocEntity docEntity in docSchema.Entities)
                    {
                        map.Add(docEntity.Name, docEntity);
                    }
                    foreach (DocType docType in docSchema.Types)
                    {
                        map.Add(docType.Name, docType);
                    }
                    foreach (DocPropertySet docPropertySet in docSchema.PropertySets)
                    {
                        map.Add(docPropertySet.Name, docPropertySet);
                    }
                    foreach (DocQuantitySet docQuantitySet in docSchema.QuantitySets)
                    {
                        map.Add(docQuantitySet.Name, docQuantitySet);
                    }
                    foreach(DocPropertyEnumeration docPropertyEnumeration in docSchema.PropertyEnums)
                    {
                        map.Add(docPropertyEnumeration.Name, docPropertyEnumeration);
                    }
                }
            }

            // use tabs for simplicity
            using (System.IO.StreamReader reader = new System.IO.StreamReader(this.m_filename))
            {
                string headerline = reader.ReadLine();
                string[] headercols = headerline.Split(new char[]{'\t'}, StringSplitOptions.RemoveEmptyEntries);

                string blankline = reader.ReadLine(); // expect blank line

                // first column is name that identifies definition

                string[] locales = new string[headercols.Length];
                string[] fields = new string[headercols.Length];
                for(int icol = 0; icol < headercols.Length; icol++)
                {
                    string col = headercols[icol];

                    int popen = col.IndexOf('(');
                    int pclos = col.IndexOf(')');
                    if(popen > 0 && pclos > popen)
                    {
                        locales[icol] = col.Substring(popen + 1, pclos - popen - 1);
                        fields[icol] = col.Substring(0, popen);
                    }
                }

                // now rows
                while (!reader.EndOfStream)
                {
                    string rowdata = reader.ReadLine();

                    string[] rowcells = rowdata.Split(new char[] { '\t' }, StringSplitOptions.RemoveEmptyEntries);

                    if (rowcells.Length > 1)
                    {
                        DocObject docObj = null;
                        string[] fullID = rowcells[0].Split('.');
                        string identifier = fullID[0];

                        if (map.TryGetValue(identifier, out docObj))
                        {
                            if(fullID.Length == 2)
                            {
                                string subType = fullID[1];

                                if(docObj is DocEntity)
                                {
                                    DocEntity entity = (DocEntity)docObj;

                                    foreach (DocAttribute attr in entity.Attributes)
                                    {
                                        if(attr.Name == subType)
                                        {
                                            docObj = attr;
                                            break;
                                        }
                                    }
                                }
                                if(docObj is DocEnumeration)
                                {
                                    DocEnumeration type = (DocEnumeration)docObj;

                                    foreach(DocConstant constnt in type.Constants)
                                    {
                                        docObj = type;
                                        break;
                                    }
                                }
                                if(docObj is DocPropertyEnumeration)
                                {
                                    DocPropertyEnumeration propEnum = (DocPropertyEnumeration)docObj;

                                    foreach(DocPropertyConstant propConst in propEnum.Constants)
                                    {
                                        docObj = propEnum;
                                        break;
                                    }
                                }
                                if(docObj is DocPropertySet)
                                {
                                    DocPropertySet propSet = (DocPropertySet)docObj;

                                    foreach(DocProperty docProp in propSet.Properties)
                                    {
                                        docObj = propSet;
                                        break;
                                    }
                                }
                                if(docObj is DocQuantitySet)
                                {
                                    DocQuantitySet quantSet = (DocQuantitySet)docObj;

                                    foreach(DocQuantity docQuant in quantSet.Quantities)
                                    {
                                        docObj = quantSet;
                                        break;
                                    }
                                }
                            }

                            for(int i = 1; i < rowcells.Length && i < headercols.Length; i++)
                            {
                                if (locales[i] != null)
                                {
                                    // find existing
                                    DocLocalization docLocalization = null;
                                    foreach (DocLocalization docLocal in docObj.Localization)
                                    {
                                        if (docLocal.Locale.Equals(locales[i]))
                                        {
                                            docLocalization = docLocal;
                                            break;
                                        }
                                    }

                                    // create new
                                    if (docLocalization == null)
                                    {
                                        docLocalization = new DocLocalization();
                                        docLocalization.Locale = locales[i];
                                        docObj.Localization.Add(docLocalization);
                                    }

                                    string value = rowcells[i];
                                    if (value != null && value.StartsWith("\"") && value.EndsWith("\""))
                                    {
                                        // strip quotes
                                        value = value.Substring(1, value.Length - 2);
                                    }

                                    // update info
                                    switch (fields[i])
                                    {
                                        case "Name":
                                            docLocalization.Name = value;
                                            break;

                                        case "Description":
                                            docLocalization.Documentation = value;
                                            break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

Usage Example

Exemplo n.º 1
0
        private void toolStripMenuItemFileImport_Click(object sender, EventArgs e)
        {
            StringBuilder sbErrors = new StringBuilder();

            DialogResult res = this.openFileDialogImport.ShowDialog(this);
            if (res == DialogResult.OK)
            {
                List<DocSchema> importedschemas = new List<DocSchema>();

                bool updateDescriptions = false;
                if(this.openFileDialogImport.FileName.EndsWith(".vex"))
                {
                    DialogResult resUpdate = MessageBox.Show(this, "Do you want to update the documentation? Click Yes to update documentation and definitions, or No to update just definitions.", "Import VEX", MessageBoxButtons.YesNoCancel);
                    if (resUpdate == System.Windows.Forms.DialogResult.Cancel)
                        return;

                    if (resUpdate == System.Windows.Forms.DialogResult.Yes)
                        updateDescriptions = true;
                }

                foreach (string filename in this.openFileDialogImport.FileNames)
                {
                    string ext = System.IO.Path.GetExtension(filename).ToLower();
                    switch (ext)
                    {
                        case ".vex":
                            using (FormatSPF format = new FormatSPF(filename, SchemaVEX.Types, null))
                            {
                                format.Load();

                                // get the root schemata
                                SCHEMATA vexschema = null;
                                foreach (SEntity entity in format.Instances.Values)
                                {
                                    if (entity is SCHEMATA)
                                    {
                                        vexschema = (SCHEMATA)entity;
                                        break;
                                    }
                                }

                                if (vexschema != null)
                                {
                                    DocSchema schema = Program.ImportVex(vexschema, this.m_project, updateDescriptions);
                                    importedschemas.Add(schema); // add schemas from multiple files first, process later
                                }
                            }
                            break;

                        case ".xml":
                            if (filename.Contains("Pset_"))
                            {
                                using (FormatXML format = new FormatXML(filename, typeof(PropertySetDef), "http://buildingSMART-tech.org/xml/psd/PSD_IFC4.xsd"))
                                {
                                    format.Load();
                                    PropertySetDef psd = (PropertySetDef)format.Instance;

                                    string schema = null;
                                    if (psd.Versions != null && psd.Versions.Count > 0)
                                    {
                                        schema = psd.Versions[0].schema;
                                    }

                                    if (String.IsNullOrEmpty(schema))
                                    {
                                        // guess the schema according to applicable type value
                                        if (psd.ApplicableTypeValue != null)
                                        {
                                            string[] parts = psd.ApplicableTypeValue.Split(new char[] { '/', '[' });
                                            TreeNode tnEntity = null;
                                            if (this.m_mapTree.TryGetValue(parts[0].ToLowerInvariant(), out tnEntity))
                                            {
                                                DocSchema docschema = (DocSchema)tnEntity.Parent.Parent.Tag;
                                                schema = docschema.Name;
                                            }
                                        }
                                    }

                                    if(schema == null)
                                    {
                                        schema = "IfcProductExtension";//fallback
                                    }

                                    // find the schema
                                    TreeNode tn = null;
                                    if (schema != null && this.m_mapTree.TryGetValue(schema.ToLowerInvariant(), out tn))
                                    {
                                        DocSchema docschema = (DocSchema)tn.Tag;

                                        // find existing pset if applicable
                                        DocPropertySet pset = docschema.RegisterPset(psd.Name);

                                        // use hashed guid
                                        if (pset.Uuid == Guid.Empty)
                                        {
                                            System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
                                            byte[] hash = md5.ComputeHash(Encoding.Default.GetBytes(pset.Name));
                                            pset.Uuid = new Guid(hash);
                                        }

                                        pset.Name = psd.Name;
                                        if (psd.Definition != null)
                                        {
                                            pset.Documentation = psd.Definition.Trim();
                                        }
                                        if (psd.ApplicableTypeValue != null)
                                        {
                                            pset.ApplicableType = psd.ApplicableTypeValue.Replace("Type", "").Replace("[PerformanceHistory]", ""); // organize at occurrences; use pset type to determine type applicability
                                        }

                                        // for now, rely on naming convention (better to capture in pset schema eventually)
                                        if (psd.Name.Contains("PHistory")) // special naming convention
                                        {
                                            pset.PropertySetType = "PSET_PERFORMANCEDRIVEN";
                                        }
                                        else if (psd.Name.Contains("Occurrence"))
                                        {
                                            pset.PropertySetType = "PSET_OCCURRENCEDRIVEN";
                                        }
                                        else
                                        {
                                            pset.PropertySetType = "PSET_TYPEDRIVENOVERRIDE";
                                        }

                                        // import localized definitions
                                        if (psd.PsetDefinitionAliases != null)
                                        {
                                            foreach (PsetDefinitionAlias pl in psd.PsetDefinitionAliases)
                                            {
                                                pset.RegisterLocalization(pl.lang, null, pl.Value);
                                            }
                                        }

                                        foreach (PropertyDef subdef in psd.PropertyDefs)
                                        {
                                            DocProperty docprop = pset.RegisterProperty(subdef.Name);
                                            Program.ImportPsdPropertyTemplate(subdef, docprop);
                                        }

                                        // add to Use Definition at applicable entity
            #if false
                                        if (pset.ApplicableType != null)
                                        {
                                            string[] apptypes = pset.ApplicableType.Split('/');
                                            if (this.m_mapTree.TryGetValue(apptypes[0].ToLowerInvariant(), out tn))
                                            {
                                                DocEntity entity = (DocEntity)tn.Tag;

                                                if (this.m_project.ModelViews.Count == 0)
                                                {
                                                    // must have at least one model view for populating property set links
                                                    this.m_project.ModelViews.Add(new DocModelView());
                                                }

                                                foreach (DocModelView docView in this.m_project.ModelViews)
                                                {
                                                    DocConceptRoot docRoot = null;
                                                    foreach (DocConceptRoot eachRoot in docView.ConceptRoots)
                                                    {
                                                        if (eachRoot.ApplicableEntity == entity)
                                                        {
                                                            docRoot = eachRoot;
                                                            break;
                                                        }
                                                    }

                                                    if (docRoot == null)
                                                    {
                                                        docRoot = new DocConceptRoot();
                                                        docRoot.ApplicableEntity = entity;
                                                        docView.ConceptRoots.Add(docRoot);
                                                    }

                                                    // find the pset template
                                                    DocTemplateUsage templateuse = null;
                                                    foreach (DocTemplateUsage eachtemplateuse in docRoot.Concepts)
                                                    {
                                                        if (eachtemplateuse.Definition != null && eachtemplateuse.Definition.Name.StartsWith("Property"))
                                                        {
                                                            templateuse = eachtemplateuse;
                                                            break;
                                                        }
                                                    }

                                                    DocTemplateDefinition docdefpset = this.m_project.GetTemplate(new Guid("f74255a6-0c0e-4f31-84ad-24981db62461"));
                                                    if (docdefpset != null)
                                                    {
                                                        // if no template, add it
                                                        if (templateuse == null)
                                                        {
                                                            // get the pset template
                                                            templateuse = new DocTemplateUsage();
                                                            docRoot.Concepts.Add(templateuse);
                                                            templateuse.Definition = docdefpset;
                                                        }

                                                        DocTemplateItem templateitem = new DocTemplateItem();
                                                        templateuse.Items.Add(templateitem);
                                                        templateitem.RuleInstanceID = "IfcPropertySet";

                                                        if (apptypes.Length == 2)
                                                        {
                                                            templateitem.RuleParameters += "PredefinedType=" + apptypes[1] + ";";
                                                        }
                                                        templateitem.RuleParameters += "Name=" + pset.Name + ";";
                                                        templateitem.RuleParameters += "TemplateType=" + pset.PropertySetType + ";";
                                                        // don't include documentation -- too wordy templateitem.Documentation = pset.Documentation;
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                sbErrors.Append(System.IO.Path.GetFileNameWithoutExtension(filename) + ": unrecognized ApplicableTypeValue; ");
                                            }
                                        }
            #endif
                                    }
                                    else
                                    {
                                        sbErrors.Append(System.IO.Path.GetFileNameWithoutExtension(filename) + ": unrecognized schema; ");
                                    }

                                }
                            }
                            else if (filename.Contains("Qto_"))
                            {
                                using (FormatXML format = new FormatXML(filename, typeof(QtoSetDef)))
                                {
                                    format.Load();
                                    QtoSetDef qto = (QtoSetDef)format.Instance;

                                    string schema = qto.Versions[0].schema;
                                    TreeNode tn = null;
                                    if (schema != null && this.m_mapTree.TryGetValue(schema.ToLowerInvariant(), out tn))
                                    {
                                        DocSchema docschema = (DocSchema)tn.Tag;

                                        // find existing pset if applicable
                                        DocQuantitySet qset = docschema.RegisterQset(qto.Name);

                                        // use hashed guid
                                        if (qset.Uuid == Guid.Empty)
                                        {
                                            System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
                                            byte[] hash = md5.ComputeHash(Encoding.Default.GetBytes(qset.Name));
                                            qset.Uuid = new Guid(hash);
                                        }

                                        // everything is currently named "Base Quantities"; get name from file instead; e.g. "Qto_Beam"
                                        qset.Name = System.IO.Path.GetFileNameWithoutExtension(filename);
                                        qset.Documentation = qto.Definition;
                                        qset.ApplicableType = qto.ApplicableClasses[0].Value;

                                        // fix: remove "Type"
                                        if (qset.ApplicableType.EndsWith("Type"))
                                        {
                                            qset.ApplicableType = qset.ApplicableType.Substring(0, qset.ApplicableType.Length - 4);
                                        }

                                        // import localized definitions
                                        if (qto.QtoDefinitionAliases != null)
                                        {
                                            foreach (QtoDefinitionAlias pl in qto.QtoDefinitionAliases)
                                            {
                                                qset.RegisterLocalization(pl.lang, null, pl.Value);
                                            }
                                        }

                                        foreach (QtoDef qtodef in qto.QtoDefs)
                                        {
                                            DocQuantity q = qset.RegisterQuantity(qtodef.Name);
                                            q.Documentation = qtodef.Definition;

                                            switch (qtodef.QtoType)
                                            {
                                                case "IfcQuantityCount":
                                                    q.QuantityType = DocQuantityTemplateTypeEnum.Q_COUNT;
                                                    break;

                                                case "IfcQuantityLength":
                                                    q.QuantityType = DocQuantityTemplateTypeEnum.Q_LENGTH;
                                                    break;

                                                case "IfcQuantityArea":
                                                    q.QuantityType = DocQuantityTemplateTypeEnum.Q_AREA;
                                                    break;

                                                case "IfcQuantityVolume":
                                                    q.QuantityType = DocQuantityTemplateTypeEnum.Q_VOLUME;
                                                    break;

                                                case "IfcQuantityWeight":
                                                    q.QuantityType = DocQuantityTemplateTypeEnum.Q_WEIGHT;
                                                    break;

                                                case "IfcQuantityTime":
                                                    q.QuantityType = DocQuantityTemplateTypeEnum.Q_TIME;
                                                    break;
                                            }

                                            foreach (NameAlias namealias in qtodef.NameAliases)
                                            {
                                                string desc = null;
                                                foreach (DefinitionAlias docalias in qtodef.DefinitionAliases)
                                                {
                                                    if (docalias.lang.Equals(namealias.lang))
                                                    {
                                                        desc = docalias.Value;
                                                        break;
                                                    }
                                                }

                                                q.RegisterLocalization(namealias.lang, namealias.Value, desc);
                                            }

                                        }

                                        // map to use definition
                                        if (this.m_mapTree.TryGetValue(qset.ApplicableType.ToLowerInvariant(), out tn))
                                        {
                                            DocEntity entity = (DocEntity)tn.Tag;

                                            if (this.m_project.ModelViews.Count == 0)
                                            {
                                                // must have at least one model view for populating property set links
                                                this.m_project.ModelViews.Add(new DocModelView());
                                            }

                                            foreach (DocModelView docView in this.m_project.ModelViews)
                                            {
                                                DocConceptRoot docRoot = null;
                                                foreach (DocConceptRoot eachRoot in docView.ConceptRoots)
                                                {
                                                    if (eachRoot.ApplicableEntity == entity)
                                                    {
                                                        docRoot = eachRoot;
                                                        break;
                                                    }
                                                }

                                                if (docRoot == null)
                                                {
                                                    docRoot = new DocConceptRoot();
                                                    docRoot.ApplicableEntity = entity;
                                                    docView.ConceptRoots.Add(docRoot);
                                                }

                                                // find the qset template
                                                DocTemplateUsage templateuse = null;
                                                foreach (DocTemplateUsage eachtemplateuse in docRoot.Concepts)
                                                {
                                                    if (eachtemplateuse.Definition.Name.StartsWith("Quantity"))
                                                    {
                                                        templateuse = eachtemplateuse;
                                                        break;
                                                    }
                                                }

                                                // if no template, add it
                                                if (templateuse == null)
                                                {
                                                    // get the pset template
                                                    templateuse = new DocTemplateUsage();
                                                    docRoot.Concepts.Add(templateuse);
                                                    templateuse.Definition = this.m_project.GetTemplate(new Guid("6652398e-6579-4460-8cb4-26295acfacc7"));
                                                }

                                                if (templateuse != null)
                                                {
                                                    DocTemplateItem templateitem = new DocTemplateItem();
                                                    templateuse.Items.Add(templateitem);
                                                    templateitem.RuleInstanceID = "IfcElementQuantity";
                                                    templateitem.RuleParameters = "Name=" + qset.Name + ";TemplateType=QTO_OCCURRENCEDRIVEN;";
                                                }
                                            }
                                        }
                                    }
                                    else
                                    {
                                        sbErrors.Append(System.IO.Path.GetFileNameWithoutExtension(filename) + ": unrecognized schema; ");
                                    }
                                }
                            }
                            else if (filename.Contains("ifcXML"))
                            {
                                using (FormatXML format = new FormatXML(filename, typeof(configuration), null, SchemaCNF.Prefixes))
                                {
                                    try
                                    {
                                        this.m_loading = true; // prevent constructors from registering instances (xml serializer instantiates)
                                        format.Load();

                                        DocModelView docView = null;
                                        using(FormSelectView form = new FormSelectView(this.m_project, null))
                                        {
                                            if(form.ShowDialog(this) == System.Windows.Forms.DialogResult.OK && form.Selection != null && form.Selection.Length == 1)
                                            {
                                                docView = form.Selection[0];
                                            }
                                        }

                                        configuration cnf = (configuration)format.Instance;
                                        Program.ImportCnf(cnf, this.m_project, docView);
                                    }
                                    catch (Exception xx)
                                    {
                                        MessageBox.Show(this, xx.Message, "Import CNFXML");
                                    }
                                    finally
                                    {
                                        this.m_loading = false;
                                    }
                                }
                            }
                            break;

                        case ".mvdxml":
                            this.ImportMVD(filename);
                            break;

                        case ".txt":
                            using (FormatCSV format = new FormatCSV(filename))
                            {
                                try
                                {
                                    format.Instance = this.m_project;
                                    format.Load();
                                }
                                catch (System.Exception xx)
                                {
                                    MessageBox.Show(this, xx.Message, "Import CSV");
                                }
                            }
                            break;

                        case ".ifd":
                            using (FormatIFD format = new FormatIFD(filename))
                            {
                                try
                                {
                                    format.Instance = this.m_project;
                                    format.Load();
                                }
                                catch (System.Exception xx)
                                {
                                    MessageBox.Show(this, xx.Message, "Import IFD");
                                }
                            }
                            break;

                        case ".xsd":
                            using (FormatXML format = new FormatXML(filename, typeof(IfcDoc.Schema.XSD.schema), IfcDoc.Schema.XSD.SchemaXsd.DefaultNamespace))
                            {
                                try
                                {
                                    format.Load();
                                    DocSchema docSchema = Program.ImportXsd((IfcDoc.Schema.XSD.schema)format.Instance, this.m_project);
                                    if(docSchema.Name == null)
                                    {
                                        docSchema.Name = System.IO.Path.GetFileNameWithoutExtension(filename);
                                    }
                                }
                                catch(System.Exception xx)
                                {
                                    MessageBox.Show(this, xx.Message, "Import XSD");
                                }
                            }
                            break;
                    }

                }

                // load tree before generating use definitions
                this.LoadTree();

                // load tree again to pick up definitions
                if (importedschemas.Count > 0)
                {
                    LoadTree();
                }
            }

            if (sbErrors.Length > 0)
            {
                MessageBox.Show(this, "Import succeeded, however one or more definitions have missing or incorrect information:\r\n" + sbErrors.ToString(), "Import Errors");
            }
        }
All Usage Examples Of IfcDoc.FormatCSV::Load