Example #1
0
        public static void SaveProject(DocProject project, string filePath)
        {
            project.SortProject();
            string ext = System.IO.Path.GetExtension(filePath).ToLower();

            switch (ext)
            {
            case ".ifcdoc":
                using (FileStream streamDoc = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite))
                {
                    StepSerializer formatDoc = new StepSerializer(typeof(DocProject), SchemaDOC.Types, "IFCDOC_12_0", "IfcDoc 12.0", "BuildingSmart IFC Documentation Generator");
                    formatDoc.WriteObject(streamDoc, project);                             // ... specify header...IFCDOC_11_8
                }
                break;

#if MDB
            case ".mdb":
                using (FormatMDB format = new FormatMDB(this.m_file, SchemaDOC.Types, this.m_instances))
                {
                    format.Save();
                }
                break;
#endif
            case ".ifcdocxml":
                using (FileStream streamDoc = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite))
                {
                    XmlSerializer formatDoc = new XmlSerializer(typeof(DocProject));

                    formatDoc.WriteObject(streamDoc, project);                             // ... specify header...IFCDOC_11_8
                }
                break;
            }
        }
Example #2
0
        public FormValidator(Concept target)
            : this()
        {
            this.m_target = target;

            // get the underlying IfcProject
            object ifcProject = target.Target;

            if (ifcProject != null)
            {
                try
                {
                    // retrieve dictionary of all objects
                    Type           typeProject = ifcProject.GetType();
                    StepSerializer format      = new StepSerializer(typeProject);
                    using (MemoryStream stream = new MemoryStream())
                    {
                        format.WriteObject(stream, ifcProject);
                        stream.Position = 0;
                        format.ReadObject(stream, out this.m_instances);
                    }
                }
                catch (Exception xx)
                {
                    MessageBox.Show(this, "There was an error serializing the data:\r\n\r\n" + xx.Message, "Validation Error");
                }
            }
        }
Example #3
0
        /// <summary>
        /// Opens the IFCDOC model for reading
        /// </summary>
        /// <param name="path"></param>
        public void Open(string path)
        {
            Dictionary <long, object> instances;

            using (var streamDoc = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                var serializer = new StepSerializer(typeof(DocProject), SchemaDOC.Types);
                Project = (DocProject)serializer.ReadObject(streamDoc, out instances);
            }

            _instances = new Dictionary <Type, List <SEntity> >();
            foreach (var instance in instances.Values.OfType <SEntity>())
            {
                var type = instance.GetType();
                if (_instances.TryGetValue(type, out List <SEntity> entities))
                {
                    entities.Add(instance);
                }
                else
                {
                    _instances.Add(type, new List <SEntity> {
                        instance
                    });
                }
            }
        }
Example #4
0
        public static void SerializeToStep(this IfcProject project,
                                           Stream outputStream,
                                           String schema,
                                           String?application)
        {
            Serializer serializer = new StepSerializer(typeof(IfcProject),
                                                       null,
                                                       schema,
                                                       null,
                                                       application ?? "ECL IfcCreator");

            serializer.WriteObject(outputStream, project);
            FixAsterisk(outputStream);
        }
        public void CreateProductIfcTest()
        {
            // === create request
            string  projectName        = "test project";
            string  projectDescription = "project for test purposes";
            Project project            =
                new Project.Builder().withName(projectName)
                .withDescription(projectDescription)
                .build();
            string personGivenName  = "Cedric";
            string personFamilyName = "Mélange";
            string personIdentifier = "cmela";
            Person person           =
                new Person.Builder().withGivenName(personGivenName)
                .withFamilyName(personFamilyName)
                .withIdentifier(personIdentifier)
                .build();
            string       organizationName        = "EC Life";
            string       organizationDescription = "EC Life - for an easy, economical and ecological life";
            string       organizationIdentifier  = "ECL";
            Organization organization            =
                new Organization.Builder().withName(organizationName)
                .withDescription(organizationDescription)
                .withIdentifier(organizationIdentifier)
                .build();
            string      applicationName       = "ECL IfcCreator";
            string      applicationVersion    = "1.0";
            string      applicationIdentifier = "IfcCreator";
            Application application           =
                new Application.Builder().withName(applicationName)
                .withVersion(applicationVersion)
                .withIdentifier(applicationIdentifier)
                .withOrganization(new Organization.Builder().build())
                .build();
            OwnerHistory owner =
                new OwnerHistory.Builder().withPerson(person)
                .withOrganization(organization)
                .withApllication(application)
                .build();

            string productName        = "test product";
            string productDescription = "product for test purposes";
            string constructionString = "SHAPE({POLYLINE2D([[0,0],[0,1],[1,1],[1,0]])}).EXTRUDE(1)";
            var    material0          = new Material.Builder().withName("material0")
                                        .withColor(new ColorRGBa(1, 0, 0, 1))
                                        .withRoughness(0)
                                        .isMetal()
                                        .build();
            var material1 = new Material.Builder().withName("material1")
                            .withColor(new ColorRGBa(0, 0, 1, 1))
                            .withRoughness(1)
                            .isDielectric()
                            .build();
            RepresentationItem representationItem0 =
                new RepresentationItem.Builder()
                .withConstructionString(constructionString)
                .withMaterial(material0.id)
                .build();
            var rotationQ = new Quaternion();

            rotationQ.SetFromEuler(new double[] { 90, 90, 90 });
            RepresentationItem representationItem1 =
                new RepresentationItem.Builder()
                .withConstructionString(constructionString)
                .withTransformation(new Transformation.Builder()
                                    .withTranslation(new double[] { 2, 0, 0 })
                                    .withRotation(rotationQ.ToArray())
                                    .build())
                .withMaterial(material1.id)
                .build();
            Representation representation =
                new Representation.Builder().AddRepresentationItem(representationItem0)
                .AddRepresentationItem(representationItem1)
                .addMaterial(material0)
                .addMaterial(material1)
                .build();
            Product product =
                new Product.Builder().withName(productName)
                .withDescription(productDescription)
                .withType(ProductType.PROXY)
                .addRepresenation(representation)
                .build();
            IfcSchema         schema  = IfcSchema.IFC2X3;
            ProductIfcRequest request =
                new ProductIfcRequest.Builder().withProject(project)
                .withOwner(owner)
                .withProduct(product)
                .withSchema(schema)
                .build();

            // === convert to IFC stream
            MemoryStream       memStream         = new MemoryStream();
            IProductIfcCreator productIfcCreator = new ProductIfcCreator();

            productIfcCreator.CreateProductIfc(request, memStream);

            // === write to IFC file
            using (FileStream fileStream = File.Create("./product_ifc_creator_test.ifc"))
            {
                memStream.WriteTo(fileStream);
            }

            // === parse IFC stream
            Serializer serializer = new StepSerializer(typeof(IfcProject),
                                                       null,
                                                       schema.ToString(),
                                                       null,
                                                       "ECL IfcCreator");
            IfcProject parsedProject = (IfcProject)serializer.ReadObject(memStream);

            // === test values
            Assert.Equal(projectName, parsedProject.Name);
            Assert.Equal(projectDescription, parsedProject.Description);
            IfcOwnerHistory parsedowner = parsedProject.OwnerHistory;

            Assert.Equal(personGivenName, parsedowner.OwningUser.ThePerson.GivenName);
            Assert.Equal(personFamilyName, parsedowner.OwningUser.ThePerson.FamilyName);
            Assert.Equal(personIdentifier, parsedowner.OwningUser.ThePerson.Identification.Value.Value);
            Assert.Equal(organizationName, parsedowner.OwningUser.TheOrganization.Name);
            Assert.Equal(organizationDescription, parsedowner.OwningUser.TheOrganization.Description);
            Assert.Equal(applicationName, parsedowner.OwningApplication.ApplicationFullName);
            Assert.Equal(applicationVersion, parsedowner.OwningApplication.Version);
            Assert.Equal(applicationIdentifier, parsedowner.OwningApplication.ApplicationIdentifier.Value);
            IfcSite parsedSite = (IfcSite)GetItem(GetItem(parsedProject.IsDecomposedBy, 0).RelatedObjects, 0);

            Assert.Equal(personGivenName, parsedSite.OwnerHistory.OwningUser.ThePerson.GivenName);
            IfcBuilding parsedBuilding = (IfcBuilding)GetItem(GetItem(parsedSite.IsDecomposedBy, 0).RelatedObjects, 0);

            Assert.Equal(personGivenName, parsedBuilding.OwnerHistory.OwningUser.ThePerson.GivenName);
            IfcBuildingStorey parsedStorey = (IfcBuildingStorey)GetItem(GetItem(parsedBuilding.IsDecomposedBy, 0).RelatedObjects, 0);

            Assert.Equal(personGivenName, parsedStorey.OwnerHistory.OwningUser.ThePerson.GivenName);
            IfcProduct parsedProduct = (IfcProduct)GetItem(GetItem(parsedStorey.ContainsElements, 0).RelatedElements, 0);

            Assert.Equal(personGivenName, parsedProduct.OwnerHistory.OwningUser.ThePerson.GivenName);
            Assert.Equal(productName, parsedProduct.Name);
            Assert.Equal(productDescription, parsedProduct.Description);
            Assert.Equal(1, parsedProduct.Representation.Representations.Count);
            IfcRepresentation parsedShapeRepresentation = GetItem(parsedProduct.Representation.Representations, 0);

            Assert.Equal("Body", parsedShapeRepresentation.RepresentationIdentifier);
            Assert.Equal("SolidModel", parsedShapeRepresentation.RepresentationType);
            Assert.Equal(2, parsedShapeRepresentation.Items.Count);
            Assert.IsType <IfcExtrudedAreaSolid>(GetItem(parsedShapeRepresentation.Items, 0));
            Assert.IsType <IfcExtrudedAreaSolid>(GetItem(parsedShapeRepresentation.Items, 1));
            var parsedItem0 = (IfcExtrudedAreaSolid)GetItem(parsedShapeRepresentation.Items, 0);

            Assert.Equal(1, parsedItem0.StyledByItem.Count);
            var StyledItem0 = GetItem(parsedItem0.StyledByItem, 0);

            Assert.Equal("material0", ((IfcSurfaceStyle)GetItem(StyledItem0.Styles, 0)).Name);
            var parsedItem1 = (IfcExtrudedAreaSolid)GetItem(parsedShapeRepresentation.Items, 1);

            Assert.Equal(2, parsedItem1.Position.Location.Coordinates[0].Value);
            Assert.Equal(0, parsedItem1.Position.Location.Coordinates[1].Value);
            Assert.Equal(0, parsedItem1.Position.Location.Coordinates[2].Value);
            Assert.Equal(1, parsedItem1.Position.Axis.DirectionRatios[0].Value, 5);
            Assert.Equal(0, parsedItem1.Position.Axis.DirectionRatios[1].Value, 5);
            Assert.Equal(0, parsedItem1.Position.Axis.DirectionRatios[2].Value, 5);
            Assert.Equal(0, parsedItem1.Position.RefDirection.DirectionRatios[0].Value, 5);
            Assert.Equal(0, parsedItem1.Position.RefDirection.DirectionRatios[1].Value, 5);
            Assert.Equal(1, parsedItem1.Position.RefDirection.DirectionRatios[2].Value, 5);
            Assert.Equal(1, parsedItem0.StyledByItem.Count);
            var StyledItem1 = GetItem(parsedItem1.StyledByItem, 0);

            Assert.Equal("material1", ((IfcSurfaceStyle)GetItem(StyledItem1.Styles, 0)).Name);
        }
Example #6
0
        public static DocProject LoadFile(string filePath)
        {
            List <object> instances = new List <object>();
            string        ext       = System.IO.Path.GetExtension(filePath).ToLower();
            string        schema    = "";
            DocProject    project   = null;

            switch (ext)
            {
            case ".ifcdoc":
                using (FileStream streamDoc = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                {
                    Dictionary <long, object> dictionaryInstances = null;
                    StepSerializer            formatDoc           = new StepSerializer(typeof(DocProject), SchemaDOC.Types);
                    project = (DocProject)formatDoc.ReadObject(streamDoc, out dictionaryInstances);
                    instances.AddRange(dictionaryInstances.Values);
                    schema = formatDoc.Schema;
                }
                break;

            case ".ifcdocxml":
                using (FileStream streamDoc = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                {
                    Dictionary <string, object> dictionaryInstances = null;
                    XmlSerializer formatDoc = new XmlSerializer(typeof(DocProject));
                    project = (DocProject)formatDoc.ReadObject(streamDoc, out dictionaryInstances);
                    instances.AddRange(dictionaryInstances.Values);
                }
                break;

            default:
                MessageBox.Show("Unsupported file type " + ext, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                break;

#if MDB
            case ".mdb":
                using (FormatMDB format = new FormatMDB(this.m_file, SchemaDOC.Types, this.m_instances))
                {
                    format.Load();
                }
                break;
#endif
            }
            if (project == null)
            {
                return(null);
            }

            double schemaVersion = 0;
            if (!string.IsNullOrEmpty(schema))
            {
                string[] fields = schema.Split("_".ToCharArray());
                int      i      = 0;
                if (fields.Length > 1)
                {
                    if (int.TryParse(fields[1], out i))
                    {
                        schemaVersion = i;
                    }
                    if (fields.Length > 2 && int.TryParse(fields[2], out i))
                    {
                        schemaVersion += i / 10.0;
                    }
                }
            }
            List <SEntity> listDelete = new List <SEntity>();
            List <DocTemplateDefinition> listTemplate = new List <DocTemplateDefinition>();

            foreach (object o in instances)
            {
                if (o is DocSchema)
                {
                    DocSchema docSchema = (DocSchema)o;

                    // renumber page references
                    foreach (DocPageTarget docTarget in docSchema.PageTargets)
                    {
                        if (docTarget.Definition != null)                         // fix it up -- NULL bug from older .ifcdoc files
                        {
                            int page = docSchema.GetDefinitionPageNumber(docTarget);
                            int item = docSchema.GetPageTargetItemNumber(docTarget);
                            docTarget.Name = page + "," + item + " " + docTarget.Definition.Name;

                            foreach (DocPageSource docSource in docTarget.Sources)
                            {
                                docSource.Name = docTarget.Name;
                            }
                        }
                    }
                }
                else if (o is DocExchangeDefinition)
                {
                    // files before V4.9 had Description field; no longer needed so use regular Documentation field again.
                    DocExchangeDefinition docexchange = (DocExchangeDefinition)o;
                    if (docexchange._Description != null)
                    {
                        docexchange.Documentation = docexchange._Description;
                        docexchange._Description  = null;
                    }
                }
                else if (o is DocTemplateDefinition)
                {
                    // files before V5.0 had Description field; no longer needed so use regular Documentation field again.
                    DocTemplateDefinition doctemplate = (DocTemplateDefinition)o;
                    if (doctemplate._Description != null)
                    {
                        doctemplate.Documentation = doctemplate._Description;
                        doctemplate._Description  = null;
                    }

                    listTemplate.Add((DocTemplateDefinition)o);
                }
                else if (o is DocConceptRoot)
                {
                    // V12.0: ensure template is defined
                    DocConceptRoot docConcRoot = (DocConceptRoot)o;
                    if (docConcRoot.ApplicableTemplate == null && docConcRoot.ApplicableEntity != null)
                    {
                        docConcRoot.ApplicableTemplate      = new DocTemplateDefinition();
                        docConcRoot.ApplicableTemplate.Type = docConcRoot.ApplicableEntity.Name;
                    }
                }
                else if (o is DocTemplateUsage)
                {
                    // V12.0: ensure template is defined
                    DocTemplateUsage docUsage = (DocTemplateUsage)o;
                    if (docUsage.Definition == null)
                    {
                        docUsage.Definition = new DocTemplateDefinition();
                    }
                }
                else if (o is DocLocalization)
                {
                    DocLocalization localization = o as DocLocalization;
                    if (!string.IsNullOrEmpty(localization.Name))
                    {
                        localization.Name = localization.Name.Trim();
                    }
                }
                // ensure all objects have valid guid
                DocObject docObject = o as DocObject;
                if (docObject != null)
                {
                    if (docObject.Uuid == Guid.Empty)
                    {
                        docObject.Uuid = Guid.NewGuid();
                    }
                    if (!string.IsNullOrEmpty(docObject.Documentation))
                    {
                        docObject.Documentation = docObject.Documentation.Trim();
                    }

                    if (schemaVersion < 12.1)
                    {
                        DocChangeSet docChangeSet = docObject as DocChangeSet;
                        if (docChangeSet != null)
                        {
                            docChangeSet.ChangesEntities.RemoveAll(x => !isUnchanged(x));
                        }
                        else
                        {
                            if (schemaVersion < 12)
                            {
                                DocEntity entity = docObject as DocEntity;
                                if (entity != null)
                                {
                                    entity.ClearDefaultMember();
                                }
                            }
                        }
                    }
                }
            }

            if (project == null)
            {
                return(null);
            }

            if (schemaVersion > 0 && schemaVersion < 12.1)
            {
                Dictionary <string, DocPropertyEnumeration> encounteredPropertyEnumerations = new Dictionary <string, DocPropertyEnumeration>();
                foreach (DocSchema docSchema in project.Sections.SelectMany(x => x.Schemas))
                {
                    extractListingsV12_1(project, docSchema, encounteredPropertyEnumerations);
                }
            }
            foreach (DocModelView docModelView in project.ModelViews)
            {
                // sort alphabetically (V11.3+)
                docModelView.SortConceptRoots();
            }

            // upgrade to Publications (V9.6)
            if (project.Annotations.Count == 4)
            {
                project.Publications.Clear();

                DocAnnotation docCover    = project.Annotations[0];
                DocAnnotation docContents = project.Annotations[1];
                DocAnnotation docForeword = project.Annotations[2];
                DocAnnotation docIntro    = project.Annotations[3];

                DocPublication docPub = new DocPublication();
                docPub.Name          = "Default";
                docPub.Documentation = docCover.Documentation;
                docPub.Owner         = docCover.Owner;
                docPub.Author        = docCover.Author;
                docPub.Code          = docCover.Code;
                docPub.Copyright     = docCover.Copyright;
                docPub.Status        = docCover.Status;
                docPub.Version       = docCover.Version;

                docPub.Annotations.Add(docForeword);
                docPub.Annotations.Add(docIntro);

                project.Publications.Add(docPub);

                docCover.Delete();
                docContents.Delete();
                project.Annotations.Clear();
            }
            project.SortProject();
            return(project);
        }
Example #7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="path">Path of parent</param>
        /// <param name="docExample"></param>
        private static void ExportExample(string path, string webpath, Type typeProject, DocExample docExample, StreamWriter writerIndex)
        {
            string pathExample    = path + @"\" + DocumentationISO.MakeLinkName(docExample);
            string pathExampleWeb = webpath + "/" + DocumentationISO.MakeLinkName(docExample);

            // load SPF from internal content
            if (docExample.File != null)
            {
                try
                {
                    using (MemoryStream streamSource = new MemoryStream(docExample.File))
                    {
                        StepSerializer serSource = new StepSerializer(typeProject);
                        object         project   = serSource.ReadObject(streamSource);

                        // write original IFC file as-is (including comments)  -- or could be normalized using StepSerializer
                        using (FileStream streamIFC = new FileStream(pathExample + ".ifc", FileMode.Create))
                        {
                            streamIFC.Write(docExample.File, 0, docExample.File.Length);
                        }

#if false
                        using (FileStream streamXML = new FileStream(pathExample + ".ifcxml", FileMode.Create))
                        {
                            BuildingSmart.Serialization.Xml.XmlSerializer streamTarget = new BuildingSmart.Serialization.Xml.XmlSerializer(typeProject);
                            streamTarget.WriteObject(streamXML, project);
                        }

                        using (FileStream streamJSN = new FileStream(pathExample + ".json", FileMode.Create))
                        {
                            BuildingSmart.Serialization.Json.JsonSerializer streamTarget = new BuildingSmart.Serialization.Json.JsonSerializer(typeProject);
                            streamTarget.WriteObject(streamJSN, project);
                        }

                        using (FileStream streamTTL = new FileStream(pathExample + ".ttl", FileMode.Create))
                        {
                            BuildingSmart.Serialization.Turtle.TurtleSerializer streamTarget = new BuildingSmart.Serialization.Turtle.TurtleSerializer(typeProject);
                            streamTarget.WriteObject(streamTTL, project);
                        }
#endif
                    }
                }
                catch (Exception xx)
                {
                    System.Diagnostics.Debug.WriteLine(xx.Message);
                    return;
                }

                writerIndex.WriteLine("<tr><td><a href=\"./" + webpath + ".htm\">" + docExample.Name + "</a></td>");
                WriteExampleIndexFormat(writerIndex, pathExampleWeb, "ifc");
                WriteExampleIndexFormat(writerIndex, pathExampleWeb, "ifcxml");
                WriteExampleIndexFormat(writerIndex, pathExampleWeb, "json");
                WriteExampleIndexFormat(writerIndex, pathExampleWeb, "ttl");

                // github link
                writerIndex.WriteLine("<td><a href=\"https://github.com/BuildingSMART/IfcDoc/blob/master/IfcKit/" + pathExampleWeb + ".ifc\"><img src=\"github.png\" width=\"32\" height=\"32\" /></a></td>");

                writerIndex.WriteLine("</tr>");
            }
            else
            {
                // write header row
                writerIndex.WriteLine("<tr><th colspan=\"6\"><a href=\"./" + pathExampleWeb + ".htm\">" + docExample.Name + "</a></th></tr>");
            }

            if (!String.IsNullOrEmpty(docExample.Documentation))
            {
                string filehtml = pathExample + ".htm";
                using (StreamWriter writerHtml = new StreamWriter(filehtml, false, Encoding.UTF8))
                {
                    writerHtml.Write(docExample.Documentation);
                }
            }

            if (docExample.Examples.Count > 0)
            {
                System.IO.Directory.CreateDirectory(pathExample);

                // recurse
                foreach (DocExample docSub in docExample.Examples)
                {
                    ExportExample(pathExample, pathExampleWeb, typeProject, docSub, writerIndex);
                }
            }
        }
Example #8
0
        public void SetUp()
        {
            BasicConfigurator.Configure();

            SUT = new StepSerializer();
        }