Example #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");
            }
        }
Example #2
0
        private void ImportMVD(string filename)
        {
            for (int iNS = 0; iNS < mvdXML.Namespaces.Length; iNS++)
            {
                string xmlns = mvdXML.Namespaces[iNS];
                using (FormatXML format = new FormatXML(filename, typeof(mvdXML), xmlns))
                {
                    try
                    {
                        this.m_loading = true; // prevent constructors from registering instances (xml serializer instantiates)
                        format.Load();
                        this.m_loading = false;
                        mvdXML mvd = (mvdXML)format.Instance;
                        Program.ImportMvd(mvd, this.m_project, filename);
                        break;
                    }
                    catch (InvalidOperationException xx)
                    {
                        // keep going until successful

                        if (iNS == mvdXML.Namespaces.Length - 1)
                        {
                            MessageBox.Show(this, "The file is not of a supported format (mvdXML 1.0 or mvdXML 1.1).", "Import MVDXML");
                        }
                    }
                    catch (Exception xx)
                    {
                        MessageBox.Show(this, xx.Message, "Import MVDXML");
                        xmlns = null;
                    }
                    finally
                    {
                        this.m_loading = false;
                    }
                }
            }
        }