Beispiel #1
0
        private void PrepareStyleMaterial(IModel model, int styleId)
        {
            if (styleDic.ContainsKey(styleId))
            {
                return;
            }
            var sStyle  = model.Instances[styleId] as IIfcSurfaceStyle;
            var name    = sStyle.Name;
            var texture = XbimTexture.Create(sStyle);

            var r = 0.8f;
            var g = 0.8f;
            var b = 0.8f;
            var a = 1.0f;

            if (texture.ColourMap.Any())
            {
                var colour = texture.ColourMap[0];
                r = colour.Red;
                g = colour.Green;
                b = colour.Blue;
                a = colour.Alpha;
            }
            else
            {
                // if no color defined and no name, just use default
                if (string.IsNullOrEmpty(name))
                {
                    styleDic.Add(styleId, 0);
                }
            }
            // else add it to dictionary and to list
            styleDic.Add(styleId, _materials.Count);
            _materials.Add(CreateMaterial(name, r, g, b, a));
        }
Beispiel #2
0
        public void CreateMaterial(XbimTexture texture)
        {
            if (texture.ColourMap.Count > 1)
            {
                _material = new MaterialGroup();
                var descBuilder = new StringBuilder();
                descBuilder.Append("Texture");

                var transparent = true;
                foreach (var colour in texture.ColourMap)
                {
                    if (!colour.IsTransparent)
                    {
                        transparent = false; //only transparent if everything is transparent
                    }
                    descBuilder.AppendFormat(" {0}", colour);
                    ((MaterialGroup)_material).Children.Add(MaterialFromColour(colour));
                }
                _description  = descBuilder.ToString();
                IsTransparent = transparent;
            }
            else if (texture.ColourMap.Count == 1)
            {
                var colour = texture.ColourMap[0];
                _material     = MaterialFromColour(colour);
                _description  = "Texture " + colour;
                IsTransparent = colour.IsTransparent;
            }
            else
            {
                _material = new MaterialGroup();
            }
            _material.Freeze();
        }
Beispiel #3
0
 public void CreateMaterial(XbimTexture texture)
 {
     if (texture.ColourMap.Count > 1)
     {
         _material    = new MaterialGroup();
         _description = "Texture";
         bool transparent = true;
         foreach (var colour in texture.ColourMap)
         {
             if (!colour.IsTransparent)
             {
                 transparent = false;                        //only transparent if everything is transparent
             }
             _description += " " + colour;
             ((MaterialGroup)_material).Children.Add(MaterialFromColour(colour));
         }
         IsTransparent = transparent;
     }
     else if (texture.ColourMap.Count == 1)
     {
         XbimColour colour = texture.ColourMap[0];
         _material     = MaterialFromColour(colour);
         _description  = "Texture " + colour;
         IsTransparent = colour.IsTransparent;
     }
 }
        /// <summary>
        /// Get the Material of a given element
        /// </summary>
        /// <param name="model"></param>
        /// <param name="styleId"></param>
        /// <returns></returns>
        private static Material GetWpfMaterial(IModel model, int styleId)
        {
            var sStyle      = model.Instances[styleId] as Xbim.Ifc4.Interfaces.IIfcSurfaceStyle;
            var wpfMaterial = new WpfMaterial();

            if (sStyle != null)
            {
                var texture = XbimTexture.Create(sStyle);
                texture.DefinedObjectId = styleId;
                wpfMaterial.CreateMaterial(texture);

                return(wpfMaterial);
            }
            else
            {
                var defautMaterial = Xbim.Presentation.ModelDataProvider.DefaultMaterials;

                Material material;
                if (defautMaterial.TryGetValue(model.GetType().Name, out material))
                {
                    //((System.Windows.Media.Media3D.DiffuseMaterial)material).Brush.Opacity = opacity;
                    return(material);
                }
                else
                {
                    XbimColour color = new XbimColour("red", 1, 1, 1);
                    wpfMaterial.CreateMaterial(color);
                    return(wpfMaterial);
                    // return defautMaterial["IfcProduct"];
                    //((System.Windows.Media.Media3D.DiffuseMaterial)mat).Brush.Opacity = opacity;
                }
            }
        }
Beispiel #5
0
        private static Material GetWpfMaterial(Xbim.Common.IModel model, int styleId)
        {
            var sStyle      = model.Instances[styleId] as IIfcSurfaceStyle;
            var wpfMaterial = new WpfMaterial();

            if (sStyle != null)
            {
                XbimTexture texture = XbimTexture.Create(sStyle);
                texture.DefinedObjectId = styleId;
                wpfMaterial.CreateMaterial(texture);

                return(wpfMaterial);
            }
            MaterialDictionary defaultMaterial = ModelDataProvider.DefaultMaterials;

            Material material;

            if (defaultMaterial.TryGetValue(model.GetType().Name, out material))
            {
                return(material);
            }
            var color = new XbimColour("black", 1, 1, 1);

            wpfMaterial.CreateMaterial(color);
            return(wpfMaterial);
        }
        public static MaterialModel Create(IModel model, short typeId)
        {
            var prodType  = model.Metadata.ExpressType(typeId);
            var texture   = XbimTexture.Create(colourMap[prodType.Name]);
            var xBimColor = texture.ColourMap.FirstOrDefault();

            return(Create(Color.FromScRgb(xBimColor.Alpha, xBimColor.Red, xBimColor.Green, xBimColor.Blue)));
        }
Beispiel #7
0
 public void TolerateSurfaceStyleWithMultipleColors()
 {
     using (var model = new ModelFactory("SubmittedFiles\\SomeBadIFCSURFACESTYLE.ifc").FirstOrDefault())
     {
         var bad     = model.Instances[7946] as IIfcSurfaceStyle;
         var created = XbimTexture.Create(bad);
     }
 }
Beispiel #8
0
        protected WpfMaterial GetWpfMaterial(IModel model, int styleId)
        {
            var sStyle      = model.Instances[styleId] as IIfcSurfaceStyle;
            var wpfMaterial = new WpfMaterial();

            //The style contains a texture
            bool isTexture = false;

            if (sStyle.Styles.Any(x => x is IIfcSurfaceStyleWithTextures))
            {
                IIfcSurfaceStyleWithTextures surfaceStyleWithTexture = (IIfcSurfaceStyleWithTextures)sStyle.Styles.First(x => x is IIfcSurfaceStyleWithTextures);
                if (surfaceStyleWithTexture.Textures.Any(x => x is IIfcImageTexture))
                {
                    IIfcImageTexture imageTexture = surfaceStyleWithTexture.Textures.First(x => x is IIfcImageTexture) as IIfcImageTexture;
                    //generate the correct path
                    Uri imageUri;
                    if (Uri.TryCreate(imageTexture.URLReference, UriKind.Absolute, out imageUri))
                    {
                        wpfMaterial.WpfMaterialFromImageTexture(imageUri);
                    }
                    else if (Uri.TryCreate(imageTexture.URLReference, UriKind.Relative, out imageUri))
                    {
                        Uri modelFileUri   = new Uri(model.Header.FileName.Name);
                        Uri absolutFileUri = new Uri(modelFileUri, imageTexture.URLReference);
                        wpfMaterial.WpfMaterialFromImageTexture(absolutFileUri);
                    }
                    else
                    {
                        Logger.LogWarning(0, "Invalid Uri " + imageTexture.URLReference + " (bad formatted or file not found).", imageTexture);
                    }

                    if (imageTexture.IsMappedBy != null)
                    {
                        wpfMaterial.IfcTextureCoordinate = imageTexture.IsMappedBy.FirstOrDefault();
                    }
                    isTexture = true;
                }
            }

            //The style doesn't contain a texture
            if (isTexture == false)
            {
                var texture = XbimTexture.Create(sStyle);
                if (texture.ColourMap.Count > 0)
                {
                    if (texture.ColourMap[0].Alpha <= 0)
                    {
                        texture.ColourMap[0].Alpha = 0.5f;
                        Logger.LogWarning("Fully transparent style #{styleId} forced to 50% opacity.", styleId);
                    }
                }

                texture.DefinedObjectId = styleId;
                wpfMaterial.CreateMaterial(texture);
            }
            return(wpfMaterial);
        }
        protected WpfMaterial GetWpfMaterialByType(IModel model, short typeid)
        {
            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);
        }
        protected static WpfMaterial GetWpfMaterial(IModel model, int styleId)
        {
            var sStyle  = model.Instances[styleId] as IIfcSurfaceStyle;
            var texture = XbimTexture.Create(sStyle);

            texture.DefinedObjectId = styleId;
            var wpfMaterial = new WpfMaterial();

            wpfMaterial.CreateMaterial(texture);
            return(wpfMaterial);
        }
        protected WpfMaterial GetWpfMaterial(IModel model, int styleId)
        {
            var sStyle  = model.Instances[styleId] as IIfcSurfaceStyle;
            var texture = XbimTexture.Create(sStyle);

            if (texture.ColourMap.Count > 0)
            {
                if (texture.ColourMap[0].Alpha <= 0)
                {
                    texture.ColourMap[0].Alpha = 0.5f;
                    Logger.LogWarning("Fully transparent style #{styleId} forced to 50% opacity.", styleId);
                }
            }

            texture.DefinedObjectId = styleId;
            var wpfMaterial = new WpfMaterial();

            wpfMaterial.CreateMaterial(texture);
            return(wpfMaterial);
        }
Beispiel #12
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);
            }
        }