示例#1
0
        public List <IfcElementRepresentation> GetShapes()
        {
            List <IfcElementRepresentation> elementReps = new List <IfcElementRepresentation>();

            //iterate products
            IEnumerable <IIfcElement> prod = _model.Instances.OfType <IIfcElement>();

            foreach (IIfcElement e in prod)
            {
                IIfcProductRepresentation prodrep = e.Representation;

                IfcElementRepresentation eleRep = new IfcElementRepresentation();
                eleRep.attributes             = new IfcObjectAttributes();
                eleRep.attributes.GlobalId    = e.GlobalId;
                eleRep.attributes.Description = e.Description;
                eleRep.attributes.HasGeometry = true;
                eleRep.attributes.IfcType     = e.GetType().Name;
                eleRep.attributes.Namespace   = e.GetType().Namespace;
                eleRep.attributes.Name        = e.Name;

                eleRep.shapes = new List <IfcObjectShapeRepresentation>();

                //https://github.com/xBimTeam/XbimGeometry/issues/131
                using (var modgeom = _model.GeometryStore.BeginRead())
                {
                    var instances = modgeom.ShapeInstances.Where(
                        si => si.RepresentationType == XbimGeometryRepresentationType.OpeningsAndAdditionsIncluded &&
                        si.IfcProductLabel == e.EntityLabel
                        );

                    var         geometries = instances.Select(i => modgeom.ShapeGeometryOfInstance(i) as IXbimShapeGeometryData);
                    List <bool> geometryHasTransformation = new List <bool>();
                    foreach (var instance in instances)
                    {
                        if (instance.Transformation.Str() == "")
                        {
                            geometryHasTransformation.Add(false);
                        }
                        else
                        {
                            geometryHasTransformation.Add(true);
                        }
                    }

                    int geometrycount = 0;
                    foreach (var geometry in geometries)
                    {
                        List <List <double> > vertices = new List <List <double> >();
                        using (var ms = new MemoryStream(geometry.ShapeData))
                            using (var br = new BinaryReader(ms))
                            {
                                var mesh = br.ReadShapeTriangulation();
                                if (mesh == null)
                                {
                                    continue;
                                }

                                if (geometryHasTransformation.ElementAt(geometrycount))
                                {
                                    var transform = instances.ElementAt(0).Transformation;

                                    mesh = mesh.Transform(transform);
                                }

                                foreach (var Meshvertex in mesh.Vertices)
                                {
                                    List <double> vertex = new List <double>
                                    {
                                        Math.Round(Meshvertex.X, 4),
                                        Math.Round(Meshvertex.Y, 4),
                                        Math.Round(Meshvertex.Z, 4)
                                    };
                                    vertices.Add(vertex);
                                }

                                var faces   = mesh.Faces;
                                var indices = new List <int>();
                                var normals = new List <XbimPackedNormal>();

                                foreach (var face in faces)
                                {
                                    foreach (var index in face.Indices)
                                    {
                                        indices.Add(index);
                                    }
                                    if (face.Indices.Count % 3 != 0)
                                    {
                                        Console.WriteLine("Face not a triangle!: " + face.Indices.Count.ToString());
                                    }
                                    foreach (var normal in face.Normals)
                                    {
                                        normals.Add(normal);
                                    }
                                }

                                IfcObjectShapeRepresentation objShape = new IfcObjectShapeRepresentation();
                                objShape.Faces    = faces;
                                objShape.Indices  = indices;
                                objShape.Normals  = normals;
                                objShape.Vertices = vertices;

                                eleRep.shapes.Add(objShape);
                            }
                        geometrycount++;
                    }
                }
                elementReps.Add(eleRep);
            }

            return(elementReps);
        }
示例#2
0
        public List <IfcObjectShapeRepresentation> GetShapesOfProduct(string guid)
        {
            List <IfcObjectShapeRepresentation> shapes = new List <IfcObjectShapeRepresentation>();
            IIfcProduct prod = _model.Instances.OfType <IIfcProduct>().Where(x => x.GlobalId.Equals(guid)).First();
            IIfcProductRepresentation prodrep = prod.Representation;

            //https://github.com/xBimTeam/XbimGeometry/issues/131
            using (var modgeom = _model.GeometryStore.BeginRead())
            {
                var         instances  = modgeom.ShapeInstancesOfEntity(prod.EntityLabel);
                var         geometries = instances.Select(i => modgeom.ShapeGeometryOfInstance(i) as IXbimShapeGeometryData);
                List <bool> geometryHasTransformation = new List <bool>();
                foreach (var instance in instances)
                {
                    if (instance.Transformation.Str() == "")
                    {
                        geometryHasTransformation.Add(false);
                    }
                    else
                    {
                        geometryHasTransformation.Add(true);
                    }
                }

                int geometrycount = 0;
                foreach (var geometry in geometries)
                {
                    List <List <double> > vertices = new List <List <double> >();
                    using (var ms = new MemoryStream(geometry.ShapeData))
                        using (var br = new BinaryReader(ms))
                        {
                            var mesh = br.ReadShapeTriangulation();
                            if (mesh == null)
                            {
                                continue;
                            }

                            if (geometryHasTransformation.ElementAt(geometrycount))
                            {
                                var transform = instances.ElementAt(0).Transformation;

                                mesh = mesh.Transform(transform);
                            }

                            foreach (var Meshvertex in mesh.Vertices)
                            {
                                List <double> vertex = new List <double>
                                {
                                    Math.Round(Meshvertex.X, 4),
                                    Math.Round(Meshvertex.Y, 4),
                                    Math.Round(Meshvertex.Z, 4)
                                };
                                vertices.Add(vertex);
                            }

                            var faces   = mesh.Faces;
                            var indices = new List <int>();
                            var normals = new List <XbimPackedNormal>();

                            foreach (var face in faces)
                            {
                                foreach (var index in face.Indices)
                                {
                                    indices.Add(index);
                                }
                                foreach (var normal in face.Normals)
                                {
                                    normals.Add(normal);
                                }
                            }

                            IfcObjectShapeRepresentation objShape = new IfcObjectShapeRepresentation();
                            objShape.Faces    = faces;
                            objShape.Indices  = indices;
                            objShape.Normals  = normals;
                            objShape.Vertices = vertices;

                            shapes.Add(objShape);
                        }
                    geometrycount++;
                }
            }

            return(shapes);
        }