示例#1
0
        private static IfcStyledItem ToIfcStyledItem(this Material material, IfcRepresentationItem shape, Document doc)
        {
            var color   = material.Color.ToIfcColourRgb(doc);
            var shading = new IfcSurfaceStyleShading(color);

            var styles = new List <IfcSurfaceStyleElementSelect> {
            };

            styles.Add(new IfcSurfaceStyleElementSelect(shading));
            var surfaceStyle = new IfcSurfaceStyle(material.Name, IfcSurfaceSide.POSITIVE, styles);
            var styleSelect  = new IfcPresentationStyleSelect(surfaceStyle);
            var assign       = new IfcPresentationStyleAssignment(new List <IfcPresentationStyleSelect> {
                styleSelect
            });
            var assignments = new List <IfcPresentationStyleAssignment>();

            assignments.Add(assign);
            var styledByItem = new IfcStyledItem(shape, assignments, material.Name);

            doc.AddEntity(color);
            doc.AddEntity(shading);
            doc.AddEntity(surfaceStyle);
            doc.AddEntity(styleSelect);
            doc.AddEntity(assign);

            return(styledByItem);
        }
示例#2
0
        private static Boolean tryGetColorFromIfcRepresentationItem(IfcRepresentationItem reprItem, out Color color)
        {
            color = Color.Black;
            try
            {
                if (reprItem.mStyledByItem != null)
                {
                    IfcStyledItem ifcStyledItem = reprItem.mStyledByItem;

                    IfcPresentationStyleAssignment sas = (IfcPresentationStyleAssignment)ifcStyledItem.Styles[0];

                    IfcSurfaceStyle ss = (IfcSurfaceStyle)sas.Styles[0];

                    IfcSurfaceStyleRendering ssr = (IfcSurfaceStyleRendering)ss.Styles[0];

                    int alpha = Convert.ToInt32((1 - ssr.Transparency) * 255);

                    color = Color.FromArgb(alpha, ssr.SurfaceColour.Colour);

                    return(true);
                }
            }
            catch (Exception e)
            {
                //debug += e.Message + "\n";
            }
            return(false);
        }
        public static IfcPresentationStyleAssignment GetPresentationStyleAssignment(this IfcRepresentationItem repItem)
        {
            IfcStyledItem item = repItem.StyledByItem.FirstOrDefault();

            if (item != null && item.Styles != null)
            {
                return(item.Styles.FirstOrDefault());
            }
            else
            {
                return(null);
            }
        }
示例#4
0
        /// <summary>
        /// Returns the first IfcSurfaceStyle associated with the representation item
        /// </summary>
        /// <param name="repItem"></param>
        /// <returns></returns>
        public static IfcSurfaceStyle SurfaceStyle(this IfcRepresentationItem repItem)
        {
            IfcStyledItem styledItem = repItem.ModelOf.Instances.Where <IfcStyledItem>(s => s.Item == repItem).FirstOrDefault();

            if (styledItem != null)
            {
                foreach (var presStyle in styledItem.Styles)
                {
                    if (presStyle != null)
                    {
                        IfcSurfaceStyle aSurfaceStyle = presStyle.Styles.OfType <IfcSurfaceStyle>().FirstOrDefault();
                        if (aSurfaceStyle != null)
                        {
                            return(aSurfaceStyle);
                        }
                    }
                }
            }
            return(null);
        }
 /// <summary>
 ///   IFC only allows one presentation style assignment
 /// </summary>
 /// <param name = "si"></param>
 /// <returns></returns>
 public static IfcPresentationStyleAssignment GetPresentationStyleAssignment(this IfcStyledItem si)
 {
     return(si.Styles.OfType <IfcPresentationStyleAssignment>().FirstOrDefault());
 }
        internal static List <IfcProduct> ToIfcProducts(this Element e,
                                                        IfcRepresentationContext context,
                                                        Document doc,
                                                        Dictionary <Guid, List <IfcStyleAssignmentSelect> > styleAssignments)
        {
            var products = new List <IfcProduct>();

            IfcProductDefinitionShape shape      = null;
            GeometricElement          geoElement = null;
            Transform trans = null;
            Guid      id    = default(Guid);

            if (e is ElementInstance)
            {
                // If we're using an element instance, get the transform
                // and the id and use those to uniquely position and
                // identify the element.
                var instance = (ElementInstance)e;
                geoElement = instance.BaseDefinition;
                id         = instance.Id;
                trans      = instance.Transform;
            }
            else if (e is GeometricElement)
            {
                // If we've go a geometric element, use its properties as-is.
                geoElement = (GeometricElement)e;
                id         = geoElement.Id;
                trans      = geoElement.Transform;
            }

            geoElement.UpdateRepresentations();

            var localPlacement = trans.ToIfcLocalPlacement(doc);

            doc.AddEntity(localPlacement);

            var geoms = new List <IfcRepresentationItem>();

            if (geoElement is MeshElement)
            {
                var meshEl  = (MeshElement)geoElement;
                var lengths = meshEl.Mesh.Vertices.Select(v => v.Position.ToArray().Select(vi => new IfcLengthMeasure(vi)).ToList()).ToList();
                var pts     = new IfcCartesianPointList3D(lengths);
                doc.AddEntity(pts);
                var indices = meshEl.Mesh.Triangles.Select(t => t.Vertices.Select(vx => new IfcPositiveInteger(vx.Index + 1)).ToList()).ToList();
                var idxs    = new List <List <IfcPositiveInteger> >(indices);
                var geom    = new IfcTriangulatedFaceSet(pts, indices);
                geom.Closed = false;
                doc.AddEntity(geom);
                geoms.Add(geom);
                shape = ToIfcProductDefinitionShape(geoms, "Tessellation", context, doc);
            }
            else
            {
                foreach (var op in geoElement.Representation.SolidOperations)
                {
                    if (op is Sweep)
                    {
                        var sweep = (Sweep)op;

                        // Neither of these entities, which are part of the
                        // IFC4 specification, and which would allow a sweep
                        // along a curve, are supported by many applications
                        // which are supposedly IFC4 compliant (Revit). For
                        // Those applications where these entities appear,
                        // the rotation of the profile is often wrong or
                        // inconsistent.
                        // geom = sweep.ToIfcSurfaceCurveSweptAreaSolid(doc);
                        // geom = sweep.ToIfcFixedReferenceSweptAreaSolid(geoElement.Transform, doc);

                        // Instead, we'll divide the curve and create a set of
                        // linear extrusions instead.
                        Polyline pline;
                        if (sweep.Curve is Line)
                        {
                            pline = sweep.Curve.ToPolyline(1);
                        }
                        else
                        {
                            pline = sweep.Curve.ToPolyline();
                        }
                        foreach (var segment in pline.Segments())
                        {
                            var position         = segment.TransformAt(0.0).ToIfcAxis2Placement3D(doc);
                            var extrudeDepth     = segment.Length();
                            var extrudeProfile   = sweep.Profile.Perimeter.ToIfcArbitraryClosedProfileDef(doc);
                            var extrudeDirection = Vector3.ZAxis.Negate().ToIfcDirection();
                            var geom             = new IfcExtrudedAreaSolid(extrudeProfile, position,
                                                                            extrudeDirection, new IfcPositiveLengthMeasure(extrudeDepth));

                            doc.AddEntity(extrudeProfile);
                            doc.AddEntity(extrudeDirection);
                            doc.AddEntity(position);
                            doc.AddEntity(geom);
                            geoms.Add(geom);
                        }
                    }
                    else if (op is Extrude)
                    {
                        var extrude = (Extrude)op;
                        var geom    = extrude.ToIfcExtrudedAreaSolid(doc);
                        doc.AddEntity(geom);
                        geoms.Add(geom);
                    }
                    else if (op is Lamina)
                    {
                        var lamina = (Lamina)op;
                        var geom   = lamina.ToIfcShellBasedSurfaceModel(doc);
                        doc.AddEntity(geom);
                        geoms.Add(geom);
                    }
                    else
                    {
                        throw new Exception("Only IExtrude, ISweepAlongCurve, and ILamina representations are currently supported.");
                    }
                }
                shape = ToIfcProductDefinitionShape(geoms, "SolidModel", context, doc);
            }
            doc.AddEntity(shape);


            // Can we use IfcMappedItem?
            // https://forums.buildingsmart.org/t/can-tessellation-typed-representation-hold-items-from-another-group/1621
            // var rep = new IfcShapeRepresentation(context, "Body", "Solids", geoms);
            // doc.AddEntity(rep);
            // var axisPt = Vector3.Origin.ToIfcCartesianPoint();
            // doc.AddEntity(axisPt);
            // var axis = new IfcAxis2Placement2D(axisPt);
            // doc.AddEntity(axis);
            // var repMap = new IfcRepresentationMap(new IfcAxis2Placement(axis), rep);
            // doc.AddEntity(repMap);
            // var x = trans.XAxis.ToIfcDirection();
            // var y = trans.YAxis.ToIfcDirection();
            // var z = trans.ZAxis.ToIfcDirection();
            // var origin = trans.Origin.ToIfcCartesianPoint();
            // var cart = new IfcCartesianTransformationOperator3D(x, y, origin, trans.XAxis.Length(), z);
            // doc.AddEntity(x);
            // doc.AddEntity(y);
            // doc.AddEntity(z);
            // doc.AddEntity(origin);
            // doc.AddEntity(cart);
            // var mappedItem = new IfcMappedItem(repMap, cart);
            // doc.AddEntity(mappedItem);
            // var shapeRep= new IfcShapeRepresentation(context, new List<IfcRepresentationItem>(){mappedItem});
            // doc.AddEntity(shapeRep);
            // shape = new IfcProductDefinitionShape(new List<IfcRepresentation>(){shapeRep});
            // doc.AddEntity(shape);

            var product = ConvertElementToIfcProduct(id, geoElement, localPlacement, shape);

            products.Add(product);
            doc.AddEntity(product);

            var ifcOpenings = doc.AllEntities.Where(ent => ent.GetType() == typeof(IfcOpeningElement)).Cast <IfcOpeningElement>();

            // If the element has openings, make opening relationships in
            // the IfcElement.
            if (e is IHasOpenings)
            {
                var openings = (IHasOpenings)e;
                if (openings.Openings.Count > 0)
                {
                    foreach (var o in openings.Openings)
                    {
                        var element = (IfcElement)product;
                        // TODO: Find the opening that we've already created that relates here
                        var opening = ifcOpenings.First(ifcO => ifcO.GlobalId == IfcGuid.ToIfcGuid(o.Id));
                        var voidRel = new IfcRelVoidsElement(IfcGuid.ToIfcGuid(Guid.NewGuid()), element, opening);
                        element.HasOpenings.Add(voidRel);
                        doc.AddEntity(voidRel);
                    }
                }
            }

            foreach (var geom in geoms)
            {
                var styledItem = new IfcStyledItem(geom, styleAssignments[geoElement.Material.Id], null);
                doc.AddEntity(styledItem);
            }

            return(products);
        }
示例#7
0
        private static List <IfcProduct> ToIfcProducts(this Element e, IfcRepresentationContext context, Document doc)
        {
            var products = new List <IfcProduct>();

            if (e is IAggregateElements)
            {
                // TODO: Create the IFC aggregation relationship
                foreach (var subEl in ((IAggregateElements)e).Elements)
                {
                    products.AddRange(subEl.ToIfcProducts(context, doc));
                }

                return(products);
            }

            IfcProductDefinitionShape shape = null;
            var localPlacement = e.Transform.ToIfcLocalPlacement(doc);
            IfcGeometricRepresentationItem geom = null;

            if (e is ISweepAlongCurve)
            {
                var sweep = (ISweepAlongCurve)e;
                geom = sweep.ToIfcSurfaceCurveSweptAreaSolid(e.Transform, doc);
            }
            else if (e is IExtrude)
            {
                var extrude = (IExtrude)e;
                geom = extrude.ToIfcExtrudedAreaSolid(e.Transform, doc);
            }
            else if (e is ILamina)
            {
                var lamina = (ILamina)e;
                geom = lamina.ToIfcShellBasedSurfaceModel(e.Transform, doc);
            }
            else
            {
                throw new Exception("Only IExtrude, ISweepAlongCurve, and ILamina representations are currently supported.");
            }

            shape = ToIfcProductDefinitionShape(geom, context, doc);

            doc.AddEntity(shape);
            doc.AddEntity(localPlacement);
            doc.AddEntity(geom);

            var product = ConvertElementToIfcProduct(e, localPlacement, shape);

            products.Add(product);
            doc.AddEntity(product);

            // If the element has openings,
            // Make opening relationships in
            // the IfcElement.
            if (e is IHasOpenings)
            {
                var openings = (IHasOpenings)e;
                if (openings.Openings.Count > 0)
                {
                    foreach (var o in openings.Openings)
                    {
                        var element = (IfcElement)product;
                        var opening = o.ToIfcOpeningElement(context, doc, localPlacement);
                        var voidRel = new IfcRelVoidsElement(IfcGuid.ToIfcGuid(Guid.NewGuid()), null, element, opening);
                        element.HasOpenings.Add(voidRel);
                        doc.AddEntity(opening);
                        doc.AddEntity(voidRel);
                    }
                }
            }

            IfcStyledItem style = null;

            if (e is IMaterial)
            {
                var m = (IMaterial)e;
                style = m.Material.ToIfcStyledItem(geom, doc);
            }
            if (e is IElementType <StructuralFramingType> )
            {
                var m = (IElementType <StructuralFramingType>)e;
                style = m.ElementType.Material.ToIfcStyledItem(geom, doc);
            }
            else if (e is IElementType <WallType> )
            {
                var m = (IElementType <WallType>)e;
                style = m.ElementType.MaterialLayers[0].Material.ToIfcStyledItem(geom, doc);
            }
            else if (e is IElementType <FloorType> )
            {
                var m = (IElementType <FloorType>)e;
                style = m.ElementType.MaterialLayers[0].Material.ToIfcStyledItem(geom, doc);
            }

            // Associate the style with the element
            style.Item = geom;

            geom.StyledByItem = new List <IfcStyledItem> {
                style
            };
            doc.AddEntity(style);

            return(products);
        }