public void SetFederationEnvironment(XbimReferencedModel refModel)
 {
     var federationColours = new XbimColourMap(StandardColourMaps.Federation);
     var key = refModel.DocumentInformation.DocumentOwner.RoleName();
     _colour = federationColours[key];
     
 }
        public void SetFederationEnvironment(XbimReferencedModel refModel)
        {
            var federationColours = new XbimColourMap(StandardColourMaps.Federation);
            var key = refModel.DocumentInformation.DocumentOwner.RoleName();

            _colour = federationColours[key];
        }
Пример #3
0
 private void Initialise()
 {
     _colours = new XbimColourMap(StandardColourMaps.IfcProductTypeMap);
     _lightTransparentEntities = new HashSet <int>();
     _lightTransparentTypes    = new HashSet <Type>();
     _hiddenEntities           = new HashSet <int>();
     _hiddenTypes = new HashSet <Type>();
 }
Пример #4
0
        public static WpfMaterial GetWpfMaterialByType(IModel model, short typeid)
        {
            XbimColourMap _colourMap = new XbimColourMap();
            var           prodType   = model.Metadata.ExpressType(typeid);
            var           v          = _colourMap[prodType.Name];
            var           texture    = XbimTexture.Create(v);
            var           material2  = new WpfMaterial();

            material2.CreateMaterial(texture);
            return(material2);
        }
 private void Initialise()
 {
     _colours = new XbimColourMap(StandardColourMaps.IfcProductTypeMap);
     _lightTransparentEntities = new HashSet<int>();
     _lightTransparentTypes = new HashSet<Type>();
     _hiddenEntities = new HashSet<int>();
     _hiddenTypes = new HashSet<Type>();
 }
Пример #6
0
 /// <summary>
 /// Constructs a scene, using the specfified colourmap
 /// </summary>
 /// <param name="model"></param>
 /// <param name="colourMap"></param>
 public XbimScene(IModel model, XbimColourMap colourMap)
 {
     _layerColourMap = colourMap;
     _model          = model;
 }
Пример #7
0
        /// <summary>
        /// This function builds a scene of all IfcProducts in the model, excluding the geometry of Openings
        /// It will create a scene database, overwriting any of the same name
        /// </summary>
        /// <param name="model">Model containing the model entities</param>
        /// <param name="sceneDbName">Name of scene DB file</param>
        /// <param name="Logger">Logging engine for detailed feedback</param>
        public void BuildGlobalScene(XbimModel model, string sceneDbName, Common.Logging.ILogger Logger = null)
        {
            if (File.Exists(sceneDbName)) 
                File.Delete(sceneDbName);


            //get a connection
            using (var db = new XbimSqliteDB(sceneDbName))
            {
                try
                {
                    short spaceId = IfcMetaData.IfcTypeId(typeof(IfcSpace));
                    XbimGeometryHandleCollection handles = new XbimGeometryHandleCollection(model.GetGeometryHandles()
                                                               .Exclude(IfcEntityNameEnum.IFCFEATUREELEMENT));
                    XbimRect3D modelBounds = XbimRect3D.Empty;
                    XbimColourMap cmap = new XbimColourMap();
                    int layerid = 1;
                    IfcProject project = model.IfcProject;
                    int projectId = 0;
                    if (project != null) projectId = Math.Abs(project.EntityLabel);
                    
                    float mScalingReference = (float)model.ModelFactors.OneMetre;

                    if (Logger != null)
                        Logger.DebugFormat("XbimScene: Scaling reference {0}\r\n", mScalingReference);

                    XbimMatrix3D translate = XbimMatrix3D.Identity;
                    XbimMatrix3D scale = XbimMatrix3D.CreateScale(1 / mScalingReference);
                    XbimMatrix3D composed = translate * scale;
                    XbimGeometryData regionData = model.GetGeometryData(projectId, XbimGeometryType.Region).FirstOrDefault(); //get the region data should only be one
                    if (regionData != null)
                    {
                        // this results in centering the most populated area of the model
                        //
                        XbimRegionCollection regions = XbimRegionCollection.FromArray(regionData.ShapeData);
                        XbimRegion largest = regions.MostPopulated();
                        if (largest != null)
                        {
                            translate = XbimMatrix3D.CreateTranslation(
                                -largest.Centre.X,
                                -largest.Centre.Y,
                                -largest.Centre.Z
                                );
                        }
                        composed = translate * scale;

                        // store region information in Scene
                        if ((Options & GenerateSceneOption.IncludeRegions) == GenerateSceneOption.IncludeRegions)
                        {
                            if (Logger != null)
                                Logger.DebugFormat("XbimScene: Exporting regions.\r\n", mScalingReference);
                            foreach (var item in regions)
                            {
                                // the bounding box needs to be moved/scaled by the transform.
                                //
                                XbimRect3D transformed = item.ToXbimRect3D().Transform(composed);
                                db.AddMetaData(
                                        "Region",
                                        -1,
                                        string.Format("Name:{0};Box:{1};", item.Name, transformed.ToString()), // verbose, but only a few items are expected in the model
                                        item.Name
                                        );
                    }
                        }
                    }

                    if ((Options & GenerateSceneOption.IncludeTransform) == GenerateSceneOption.IncludeTransform)
                    {
                        if (Logger != null)
                            Logger.DebugFormat("XbimScene: Exporting transform.\r\n", mScalingReference);
                        db.AddMetaData(
                                "Transform",
                                -1,
                                composed.ToArray(false),
                                "World"
                                );
                        db.Flush();
                    }

                    if (Logger != null)
                        Logger.DebugFormat("XbimScene: Exporting layers.\r\n", mScalingReference);
                    foreach (var layerContent in handles.GroupByBuildingElementTypes())
                    {
                        string elementTypeName = layerContent.Key;
                        XbimGeometryHandleCollection layerHandles = layerContent.Value;
                        IEnumerable<XbimGeometryData> geomColl = model.GetGeometryData(layerHandles);
                        XbimColour colour = cmap[elementTypeName];
                        XbimMeshLayer<XbimMeshGeometry3D, XbimRenderMaterial> layer = new XbimMeshLayer<XbimMeshGeometry3D, XbimRenderMaterial>(model, colour) { Name = elementTypeName };
                        //add all content initially into the hidden field
                        foreach (var geomData in geomColl)
                        {
                            geomData.TransformBy(composed);
                            if (geomData.IfcTypeId == spaceId)
                                layer.AddToHidden(geomData);
                            else
                                layer.AddToHidden(geomData, model);
                        }

                        if (modelBounds.IsEmpty)
                            modelBounds = layer.BoundingBoxHidden();
                        else
                            modelBounds.Union(layer.BoundingBoxHidden());

                        // add  top level layers
                        layerid = RecursivelyPersistLayer(db, layer, layerid, -1);
                        layerid++;
                    }

                    // create scene row in Scenes tables
                    //
                    if (Logger != null)
                        Logger.DebugFormat("XbimScene: Exporting scene.\r\n", mScalingReference);
                    byte[] boundingBoxFull = modelBounds.ToFloatArray();
                    db.AddScene(1, "MainScene", boundingBoxFull);

                    //now add some meta data about spaces
                    if (
                        (Options & GenerateSceneOption.IncludeSpaces) == GenerateSceneOption.IncludeSpaces
                        ||
                        (Options & GenerateSceneOption.IncludeSpacesBBox) == GenerateSceneOption.IncludeSpacesBBox
                        ||
                        (Options & GenerateSceneOption.IncludeSpacesStoreyId) == GenerateSceneOption.IncludeSpacesStoreyId
                        )
                    {
                        if (Logger != null)
                            Logger.DebugFormat("XbimScene: Exporting spaces.\r\n", mScalingReference);

                        // string data loop
                        foreach (var space in model.Instances.OfType<IfcSpace>())
                        {
                            int iEntLabel = Math.Abs(space.EntityLabel);
                            if ((Options & GenerateSceneOption.IncludeSpaces) == GenerateSceneOption.IncludeSpaces)
                            {
                                db.AddMetaData(
                                    space.GetType().Name,
                                    iEntLabel,
                                    space.Name ?? "Unnamed Space",
                                    iEntLabel.ToString()
                                    );
                            }
                            if ((Options & GenerateSceneOption.IncludeSpacesStoreyId) == GenerateSceneOption.IncludeSpacesStoreyId)
                            {
                                var parent = space.GetContainingStructuralElement();
                                if (parent == null)
                                {
                                    // try with different application behaviours
                                    foreach (var item in space.Decomposes)
                                    {
                                        parent = item.RelatingObject as IfcSpatialStructureElement;
                                        if (parent != null)
                    {
                                            break;
                                        }
                                    }
                                    // parent = space.Decomposes.FirstOrDefault().RelatingObject;
                                    // Decomposes RelatingObject
                                }
                                if (parent != null)
                        {
                                    db.AddMetaData(
                                        "SpaceToStorey",
                                        iEntLabel,
                                        string.Format("StoreyName={0};StoreyLabel={1};", parent.Name, parent.EntityLabel),
                                        iEntLabel.ToString());
                                }
                        }
                    }

                        // binary data loop
                    foreach (var space in model.Instances.OfType<IfcSpace>())
                    {
                            int iEntLabel = Math.Abs(space.EntityLabel);
                            if ((Options & GenerateSceneOption.IncludeSpacesBBox) == GenerateSceneOption.IncludeSpacesBBox)
                            {
                                XbimGeometryData geomdata = model.GetGeometryData(iEntLabel, XbimGeometryType.BoundingBox).FirstOrDefault();
                                if (geomdata != null)
                                {
                                    XbimRect3D r3d = XbimRect3D.FromArray(geomdata.ShapeData);
                                    XbimRect3D transformed = r3d.Transform(composed);
                                    db.AddMetaData(
                                            "SpaceBBox",
                                            iEntLabel,
                                        // string.Format("Box:{1};", transformed.ToString()), // verbose, but only a few items are expected in the model
                                            transformed.ToFloatArray(),
                                            iEntLabel.ToString()
                                            );
                                }
                                // db.AddMetaData(space.GetType().Name, space.Name ?? "Undefined Space", space.EntityLabel.ToString());
                            }
                        }

                    }

                    
                    // Add storey information with elevation.
                    // 
                    IfcBuilding bld = model.IfcProject.GetBuildings().FirstOrDefault();
                    if (bld != null && (Options & GenerateSceneOption.IncludeStoreys) == GenerateSceneOption.IncludeStoreys)
                    {
                        if (Logger != null)
                            Logger.DebugFormat("XbimScene: Exporting storeys.\r\n", mScalingReference);
                        double storeyHeight = 0;//all scenes are in metres
                        int defaultStoreyName = 0;
                        foreach (var storey in bld.GetBuildingStoreys(true))
                        {
                            string cleanName;
                            if(storey.Name.HasValue)
                                cleanName = storey.Name.Value.ToString().Replace(';', ' ');
                            else
                                cleanName = "Floor " + defaultStoreyName++;

                            var spacesCount = storey.SpatialStructuralElementChildren.OfType<IfcSpatialStructureElement>().ToList().Count(); // add for checking spaces


                            // Storey Elevation is optional (and has been found unreliable), therefore
                            // Storey elevation values (in building reference system) are taken from the 
                            // objectplacement through the XbimGeometryType.TransformOnly geometry type
                            //
                            XbimGeometryData geomdata = model.GetGeometryData(storey.EntityLabel, XbimGeometryType.TransformOnly).FirstOrDefault();
                            if (geomdata != null)
                            {
                                storeyHeight = geomdata.Transform.OffsetZ;
                                // apply the transformation previously applied to the building 
                                XbimPoint3D InTranslatedReference = composed.Transform(
                                    new XbimPoint3D(0, 0, storeyHeight)
                                    );

                                double InTranslatedReferenceZ = InTranslatedReference.Z; // then express it in meters.
                                
                                // Logger.DebugFormat("StoreyName: {0}; Model Elevation: {1}; Scene Elevation: {2}", cleanName, storeyHeight, InTranslatedReferenceZ);

                                db.AddMetaData(
                                "Storey",
                                    Math.Abs(storey.EntityLabel),
                                    string.Format("Name:{0};Elevation:{1};SpaceCount:{2};", cleanName, InTranslatedReferenceZ, spacesCount), // storeyHeight),
                                cleanName);
                        }
                    }
                }
                }
                finally
                {
                    db.Flush();
                    GC.Collect();
                }
            }
        }
Пример #8
0
        /// <summary>
        /// Extract Geometries from IFC file.
        /// </summary>
        /// <param name="model">IfcStore model.</param>
        /// <param name="geometries">Geometries data.</param>
        /// <param name="modelBoundingBox">Bounding box of the ifc model.</param>
        public static void ExtractGeometries(IfcStore model, ref Dictionary <string, GeometryStore> geometries, ref XbimRect3D modelBoundingBox)
        {
            // context is used to extract geometry data
            var context = new Xbim3DModelContext(model);
            var meter   = context.Model.ModelFactors.OneMeter;

            context.CreateContext();

            // start to extract geometry data
            var           instances = context.ShapeInstances();
            XbimColourMap colorMap  = new XbimColourMap();

            foreach (var instance in instances) // each instance is a mesh
            {
                MeshStore meshStore = new MeshStore();

                // get the color of this mesh
                var        ss    = model.Instances[instance.StyleLabel] as IIfcSurfaceStyle;
                XbimColour color = XbimColour.DefaultColour;
                if (ss != null)
                {
                    var texture = XbimTexture.Create(ss);
                    color = texture.ColourMap.FirstOrDefault();
                }
                else
                {
                    var styleId = instance.StyleLabel > 0 ? instance.StyleLabel : instance.IfcTypeId;
                    var theType = model.Metadata.GetType((short)styleId);
                    color = colorMap[theType.Name];
                }

                meshStore.Color = string.Format("{0},{1},{2},{3}", (int)(color.Red * 255), (int)(color.Green * 255), (int)(color.Blue * 255), color.Alpha * 255);

                var geometry = context.ShapeGeometry(instance);
                var data     = (geometry as IXbimShapeGeometryData).ShapeData;

                // multiple geometry may belong to one product, like frame and glass belong to a ifcwindow
                var product = model.Instances[instance.IfcProductLabel] as IIfcProduct;

                if (!geometries.ContainsKey(product.GlobalId))
                {
                    geometries.Add(product.GlobalId, new GeometryStore(product.GlobalId, product.Name));
                }
                var geometryStore = geometries[product.GlobalId];

                // reading the real geometry data
                using (var stream = new MemoryStream(data))
                {
                    using (var reader = new BinaryReader(stream))
                    {
                        // triangle the instance and transform it to the correct position
                        var mesh = reader.ReadShapeTriangulation();
                        mesh = mesh.Transform(instance.Transformation);

                        // geometry data
                        var verticesData = new List <double>();
                        var normalsData  = new List <double>();
                        var indexesData  = new List <int>();

                        // bouding box for instance.
                        double minX = double.MaxValue, minY = double.MaxValue, minZ = double.MaxValue,
                               maxX = double.MinValue, maxY = double.MinValue, maxZ = double.MinValue;

                        var faces = mesh.Faces as List <XbimFaceTriangulation>;
                        foreach (var face in faces)
                        {
                            foreach (var indice in face.Indices)
                            {
                                indexesData.Add(indice);
                            }
                            foreach (var normal in face.Normals)
                            {
                                normalsData.Add(normal.Normal.X);
                                normalsData.Add(normal.Normal.Y);
                                normalsData.Add(normal.Normal.Z);
                            }
                        }
                        var vertices = mesh.Vertices as List <XbimPoint3D>;
                        foreach (var point in vertices)
                        {
                            double x = point.X / meter, y = point.Y / meter, z = point.Z / meter;
                            verticesData.Add(x); // convert to meter
                            verticesData.Add(y);
                            verticesData.Add(z);

                            // update bounding box
                            minX = Math.Min(minX, x);
                            minY = Math.Min(minY, y);
                            minZ = Math.Min(minZ, z);
                            maxX = Math.Max(maxX, x);
                            maxY = Math.Max(maxY, y);
                            maxZ = Math.Max(maxZ, z);
                        }

                        if (minX == double.MaxValue || minY == double.MaxValue || minZ == double.MaxValue ||
                            maxX == double.MinValue || maxY == double.MinValue || maxZ == double.MinValue)
                        {
                            throw new Exception("Invalid Boundingbox");
                        }

                        // do not trust instance.BoundingBox, it seems to be wrong.
                        XbimRect3D bbox = new XbimRect3D(minX, minY, minZ, maxX - minX, maxY - minY, maxZ - minZ);

                        // update boundingbox of current geometryStore
                        geometryStore.BoundBox = GeometryStore.Union(geometryStore.BoundBox, bbox);

                        // update boundingbox of ifc model
                        modelBoundingBox = GeometryStore.Union(modelBoundingBox, bbox);

                        meshStore.Vertexes = verticesData;
                        meshStore.Normals  = normalsData;
                        meshStore.Indexes  = indexesData;
                    }
                }
                geometryStore.Meshes.Add(meshStore);
            }
        }