//const string file = "F:\\TUM\\STUDIES\\THESIS\\BIMtoUnity\\xBIM\\IFC_samplefiles\\2011-09-14-Clinic-IFC\\simplegeometry_openings_4.ifc"; //const string file = "F:\\TUM\\STUDIES\\THESIS\\BIMtoUnity\\xBIM\\IFC_samplefiles\\2011-09-14-Clinic-IFC\\20160414office_model_CV2_fordesign.ifc"; //const string file = "F:\\TUM\\STUDIES\\THESIS\\BIMtoUnity\\xBIM\\IFC_samplefiles\\2011-09-14-Clinic-IFC\\wall.ifc"; public static void Main() { using (var model = IfcStore.Open(file)) { Console.WriteLine("\n" + "---------------------------------------S T A R T---------------------------------------" + "\n"); Dictionary <string, IfcSpace> spaceids; Dictionary <string, IfcBuildingStorey> storeyids; var project = model.Instances.FirstOrDefault <IIfcProject>(); IEnumerable <IfcSpace> spaces = model.Instances.OfType <IfcSpace>(); spaceids = getspaceelementids(spaces); IEnumerable <IfcBuildingStorey> storeys = model.Instances.OfType <IfcBuildingStorey>(); storeyids = getstoreyelementids(storeys); var context = new Xbim3DModelContext(model); context.CreateContext(); var productshape = context.ShapeInstances(); var _productShape = context.ShapeInstances().Where(s => s.RepresentationType != XbimGeometryRepresentationType.OpeningsAndAdditionsExcluded).ToList(); Console.WriteLine("OPENED MODEL : " + file.Split(new char[] { '\\' }).Last() + " | No of shape Instances in the model is : " + _productShape.Count() + "\n"); PrintHierarchy(project, 0, spaceids, storeyids, _productShape, context); Console.WriteLine("\n" + "---------------------------------------E N D---------------------------------------" + "\n"); /********************************************************************************/ } }
//Use automatic indentation for readability. public static void Main() { using (var model = IfcStore.Open(file)) { Console.WriteLine("\n" + "---------------------------------------S T A R T---------------------------------------" + "\n"); Dictionary <string, IfcSpace> spaceids; Dictionary <string, IfcBuildingStorey> storeyids; var project = model.Instances.FirstOrDefault <IIfcProject>(); IEnumerable <IfcSpace> spaces = model.Instances.OfType <IfcSpace>(); spaceids = getspaceelementids(spaces); IEnumerable <IfcBuildingStorey> storeys = model.Instances.OfType <IfcBuildingStorey>(); storeyids = getstoreyelementids(storeys); var context = new Xbim3DModelContext(model); context.CreateContext(); var productshape = context.ShapeInstances(); var _productShape = context.ShapeInstances().Where(s => s.RepresentationType != XbimGeometryRepresentationType.OpeningsAndAdditionsExcluded).ToList(); //name of the model var name_of_model = file.Split(new char[] { '\\' }).Last(); //number of shapes in the model var number_of_shapes = _productShape.Count(); Console.WriteLine("OPENED MODEL : " + name_of_model + " | No of shape Instances in the model is : " + number_of_shapes + "\n"); //get the name of the model without the ifc extention var name_of_file = name_of_model.Split('.')[0]; //creating the xml file in the project directory named after the name of the model xmlWriter = new XmlTextWriter(name_of_file + ".xml", null); //in order to have the correct xml format xmlWriter.Formatting = Formatting.Indented; xmlWriter.WriteStartDocument(); //writing the start element Shapes with his attribut NumShapes xmlWriter.WriteStartElement("Shapes"); xmlWriter.WriteAttributeString("NumShapes", number_of_shapes.ToString()); PrintHierarchy(project, 0, spaceids, storeyids, _productShape, number_of_shapes, context); xmlWriter.WriteEndDocument(); xmlWriter.Close(); Console.WriteLine("\n" + "---------------------------------------E N D---------------------------------------" + "\n"); /********************************************************************************/ } Console.ReadLine(); }
//Use automatic indentation for readability. public static void Main() { using (var model = IfcStore.Open(file)) { xmlWriter.Formatting = Formatting.Indented; Console.WriteLine("\n" + "---------------------------------------S T A R T---------------------------------------" + "\n"); Dictionary <string, IfcSpace> spaceids; Dictionary <string, IfcBuildingStorey> storeyids; var project = model.Instances.FirstOrDefault <IIfcProject>(); IEnumerable <IfcSpace> spaces = model.Instances.OfType <IfcSpace>(); spaceids = getspaceelementids(spaces); IEnumerable <IfcBuildingStorey> storeys = model.Instances.OfType <IfcBuildingStorey>(); storeyids = getstoreyelementids(storeys); var context = new Xbim3DModelContext(model); context.CreateContext(); var productshape = context.ShapeInstances(); var _productShape = context.ShapeInstances().Where(s => s.RepresentationType != XbimGeometryRepresentationType.OpeningsAndAdditionsExcluded).ToList(); var Number_OF_Walls = _productShape.Count(); Console.WriteLine("OPENED MODEL : " + file.Split(new char[] { '\\' }).Last() + " | No of shape Instances in the model is : " + Number_OF_Walls + "\n"); xmlWriter.WriteStartDocument(); xmlWriter.WriteStartElement("Wall"); xmlWriter.WriteAttributeString("NumWall", Number_OF_Walls.ToString()); PrintHierarchy(project, 0, spaceids, storeyids, _productShape, Number_OF_Walls, context); xmlWriter.WriteEndDocument(); xmlWriter.Close(); Console.WriteLine("\n" + "---------------------------------------E N D---------------------------------------" + "\n"); /********************************************************************************/ } Console.ReadLine(); }
internal static List <Data> HierarchyRun(IfcStore model) { var project = model.Instances.FirstOrDefault <IIfcProject>(); var context = new Xbim3DModelContext(model); context.CreateContext(); List <XbimShapeInstance> allShapeInstances = context.ShapeInstances().ToList(); List <Data> data = GetIfc4.GetHierarchialData(project, 0, allShapeInstances); return(data); }
public static XbimGeometryHandleCollection GetApproximateGeometryHandles(this Xbim3DModelContext context, IEnumerable <int> loadLabels = null) { var retCollection = new XbimGeometryHandleCollection(); if (loadLabels != null) { foreach (var shapeInstance in context.ShapeInstances() .Where(x => loadLabels.Contains(x.InstanceLabel))) { retCollection.Add( new XbimGeometryHandle( shapeInstance.InstanceLabel, XbimGeometryType.TriangulatedMesh, shapeInstance.IfcProductLabel, shapeInstance.IfcTypeId, shapeInstance.StyleLabel ) ); } } else { foreach (var shapeInstance in context.ShapeInstances() .Where(s => s.RepresentationType == XbimGeometryRepresentationType.OpeningsAndAdditionsIncluded)) { retCollection.Add( new XbimGeometryHandle( shapeInstance.InstanceLabel, XbimGeometryType.TriangulatedMesh, shapeInstance.IfcProductLabel, shapeInstance.IfcTypeId, shapeInstance.StyleLabel ) ); } } return(retCollection); }
private static void TestForClassificationOfIfcFeatureElements(Xbim3DModelContext context) { var excludedTypes = new HashSet <short> { IfcMetaData.IfcType(typeof(IfcFeatureElement)).TypeId, IfcMetaData.IfcType(typeof(IfcOpeningElement)).TypeId, IfcMetaData.IfcType(typeof(IfcProjectionElement)).TypeId }; var shapeInstances = context.ShapeInstances().Where(s => excludedTypes.Contains(s.IfcTypeId) && // ifcfeatures s.RepresentationType != XbimGeometryRepresentationType.OpeningsAndAdditionsOnly); // that are not classified as OpeningsAndAdditionsOnly Assert.IsFalse(shapeInstances.Any(), "Should not find any shapes of with this classification of typeid and representationType."); }
static void Main(string[] args) { const string file = @"4walls1floorSite.ifc"; var model = IfcStore.Open(file); var context = new Xbim3DModelContext(model); context.CreateContext(); var instances = context.ShapeInstances(); foreach (var instance in instances) { var geometry = context.ShapeGeometry(instance); var data = ((IXbimShapeGeometryData)geometry).ShapeData; using (var stream = new MemoryStream(data)) { using (var reader = new BinaryReader(stream)) { var mesh = reader.ReadShapeTriangulation(); } } } }
public XbimScene<WpfMeshGeometry3D, WpfMaterial> BuildScene(XbimModel model, Xbim3DModelContext context, List<Type> exclude = null) { var tmpOpaquesGroup = new Model3DGroup(); var retScene = new XbimScene<WpfMeshGeometry3D, WpfMaterial>(model); var red = PrepareMesh(new XbimColour("Red", 1.0, 0.0, 0.0)); var green = PrepareMesh(new XbimColour("Green", 0.0, 1.0, 0.0)); var amber =PrepareMesh(new XbimColour("Amber", 0.0, 0.0, 1.0, 0.9)); tmpOpaquesGroup.Children.Add(red); tmpOpaquesGroup.Children.Add(green); tmpOpaquesGroup.Children.Add(amber); var i = 0; foreach (var shapeInstance in context.ShapeInstances() .Where(s => s.RepresentationType == XbimGeometryRepresentationType.OpeningsAndAdditionsIncluded && !typeof (IfcFeatureElement).IsAssignableFrom(IfcMetaData.GetType(s.IfcTypeId)) /*&& !typeof(IfcSpace).IsAssignableFrom(IfcMetaData.GetType(s.IfcTypeId))*/)) { IXbimShapeGeometryData shapeGeom = context.ShapeGeometry(shapeInstance.ShapeGeometryLabel); WpfMeshGeometry3D targetMergeMesh; switch (i++%3) { case 0: targetMergeMesh = red; break; case 1: targetMergeMesh = green; break; default: targetMergeMesh = amber; break; } switch ((XbimGeometryType)shapeGeom.Format) { case XbimGeometryType.Polyhedron: var shapePoly = (XbimShapeGeometry)shapeGeom; targetMergeMesh.Add( shapePoly.ShapeData, shapeInstance.IfcTypeId, shapeInstance.IfcProductLabel, shapeInstance.InstanceLabel, XbimMatrix3D.Multiply(shapeInstance.Transformation, Control.ModelPositions[model].Transfrom), model.UserDefinedId ); break; case XbimGeometryType.PolyhedronBinary: targetMergeMesh.Add( shapeGeom.ShapeData, shapeInstance.IfcTypeId, shapeInstance.IfcProductLabel, shapeInstance.InstanceLabel, XbimMatrix3D.Multiply(shapeInstance.Transformation, Control.ModelPositions[model].Transfrom), model.UserDefinedId); break; default: throw new ArgumentOutOfRangeException(); } } var mv = new ModelVisual3D {Content = tmpOpaquesGroup}; Control.OpaquesVisual3D.Children.Add(mv); Control.ModelBounds = mv.Content.Bounds.ToXbimRect3D(); return retScene; }
public XbimScene <WpfMeshGeometry3D, WpfMaterial> BuildScene(XbimModel model, Xbim3DModelContext context, List <Type> exclude = null) { var tmpOpaquesGroup = new Model3DGroup(); var retScene = new XbimScene <WpfMeshGeometry3D, WpfMaterial>(model); var red = PrepareMesh(new XbimColour("Red", 1.0, 0.0, 0.0, 0.5)); var green = PrepareMesh(new XbimColour("Green", 0.0, 1.0, 0.0, 0.5)); red.WpfModel.SetValue(FrameworkElement.TagProperty, red); green.WpfModel.SetValue(FrameworkElement.TagProperty, green); red.BeginUpdate(); green.BeginUpdate(); tmpOpaquesGroup.Children.Add(red); tmpOpaquesGroup.Children.Add(green); var i = 0; foreach (var shapeInstance in context.ShapeInstances() .Where(s => s.RepresentationType == XbimGeometryRepresentationType.OpeningsAndAdditionsIncluded && !typeof(IfcFeatureElement).IsAssignableFrom(IfcMetaData.GetType(s.IfcTypeId)) /*&& * !typeof(IfcSpace).IsAssignableFrom(IfcMetaData.GetType(s.IfcTypeId))*/)) { IXbimShapeGeometryData shapeGeom = context.ShapeGeometry(shapeInstance.ShapeGeometryLabel); WpfMeshGeometry3D targetMergeMesh; switch (i++ % 2) { case 0: targetMergeMesh = red; break; default: targetMergeMesh = green; break; } switch ((XbimGeometryType)shapeGeom.Format) { case XbimGeometryType.Polyhedron: var shapePoly = (XbimShapeGeometry)shapeGeom; targetMergeMesh.Add( shapePoly.ShapeData, shapeInstance.IfcTypeId, shapeInstance.IfcProductLabel, shapeInstance.InstanceLabel, XbimMatrix3D.Multiply(shapeInstance.Transformation, Control.ModelPositions[model].Transfrom), model.UserDefinedId); break; case XbimGeometryType.PolyhedronBinary: targetMergeMesh.Add( shapeGeom.ShapeData, shapeInstance.IfcTypeId, shapeInstance.IfcProductLabel, shapeInstance.InstanceLabel, XbimMatrix3D.Multiply(shapeInstance.Transformation, Control.ModelPositions[model].Transfrom), model.UserDefinedId ); break; default: throw new ArgumentOutOfRangeException(); } } red.EndUpdate(); green.EndUpdate(); var mv = new ModelVisual3D { Content = tmpOpaquesGroup }; Control.OpaquesVisual3D.Children.Add(mv); // Control.ModelBounds = mv.Content.Bounds.ToXbimRect3D(); return(retScene); }
internal static List <Data> Run(Xbim.Ifc.IfcStore model) { // Specify elements List <Xbim.Ifc2x3.Interfaces.IIfcWall> allWalls = model.Instances.OfType <Xbim.Ifc2x3.Interfaces.IIfcWall>().ToList(); int a = allWalls.Count(); List <Xbim.Ifc2x3.Interfaces.IIfcOpeningElement> allOpenigs = model.Instances.OfType <Xbim.Ifc2x3.Interfaces.IIfcOpeningElement>().ToList(); int b = allOpenigs.Count(); List <Xbim.Ifc2x3.Interfaces.IIfcBeamType> allBeams = model.Instances.OfType <Xbim.Ifc2x3.Interfaces.IIfcBeamType>().ToList(); int c = allBeams.Count(); List <Xbim.Ifc2x3.Interfaces.IIfcColumnType> allColumns = model.Instances.OfType <Xbim.Ifc2x3.Interfaces.IIfcColumnType>().ToList(); int d = allColumns.Count(); List <Xbim.Ifc2x3.Interfaces.IIfcElement> ifcElements = model.Instances.OfType <Xbim.Ifc2x3.Interfaces.IIfcElement>().ToList(); int e = ifcElements.Count(); // Extract geometry var context = new Xbim3DModelContext(model); context.CreateContext(); var instances = context.ShapeInstances().ToList(); IEnumerator <XbimShapeInstance> allShapeInstances = instances.GetEnumerator(); List <Data> allData = new List <Data>(); while (allShapeInstances.MoveNext()) { // get data from shape instance XbimShapeInstance xbimShapeInstance = allShapeInstances.Current; Xbim.Ifc2x3.Interfaces.IIfcElement element = ifcElements.FirstOrDefault(x => x.EntityLabel == xbimShapeInstance.IfcProductLabel); string name = element.Name; string id = element.GlobalId; var ro = xbimShapeInstance.Transformation; var translation = ro.Translation; var decompose = ro.Decompose(out XbimVector3D scale, out XbimQuaternion rot, out XbimVector3D trans); Matrix <double> matrixData = DenseMatrix.OfArray(new double[, ] { { ro.M11, ro.M12, ro.M13, ro.M14 }, { ro.M21, ro.M22, ro.M23, ro.M24 }, { ro.M31, ro.M32, ro.M33, ro.M34 }, { ro.OffsetX, ro.OffsetY, ro.OffsetZ, ro.M44 } }); matrixData = matrixData.Transpose(); Console.WriteLine(matrixData.ToString()); XbimRect3D rect = xbimShapeInstance.BoundingBox; XbimPoint3D Min = rect.Min; XbimPoint3D Max = rect.Max; Data data = new Data ( name, id, new BoundingBox(new XYZ(Min.X, Min.Y, Min.Z), new XYZ(Max.X, Max.Y, Max.Z)), new XYZ(translation.X, translation.Y, translation.Z), matrixData ); allData.Add(data); } return(allData); }
/// <summary> /// This version uses the new Geometry representation /// </summary> /// <param name="model"></param> /// <param name="context"></param> /// <param name="exclude">List of type to exclude, by default excplict openings and spaces are excluded if exclude = null</param> /// <returns></returns> public XbimScene<WpfMeshGeometry3D, WpfMaterial> BuildScene(XbimModel model, Xbim3DModelContext context, List<Type> exclude = null) { var scene = new XbimScene<WpfMeshGeometry3D, WpfMaterial>(model); if (context == null) return scene; //get a list of all the unique styles var styles = new Dictionary<int, WpfMaterial>(); var repeatedShapeGeometries = new Dictionary<int, MeshGeometry3D>(); var styleMeshSets = new Dictionary<int, WpfMeshGeometry3D>(); var tmpOpaquesGroup = new Model3DGroup(); var tmpTransparentsGroup = new Model3DGroup(); var sstyles = context.SurfaceStyles(); foreach (var style in sstyles) { var wpfMaterial = new WpfMaterial(); wpfMaterial.CreateMaterial(style); styles.Add(style.DefinedObjectId, wpfMaterial); var mg = new WpfMeshGeometry3D(wpfMaterial, wpfMaterial); mg.WpfModel.SetValue(FrameworkElement.TagProperty, mg); styleMeshSets.Add(style.DefinedObjectId, mg); mg.BeginUpdate(); if (style.IsTransparent) tmpTransparentsGroup.Children.Add(mg); else tmpOpaquesGroup.Children.Add(mg); } if (!styles.Any()) return scene; //this should always return something int i = 0; var shapeInstances = context.ShapeInstances() .Where(s => s.RepresentationType == XbimGeometryRepresentationType.OpeningsAndAdditionsIncluded && !typeof (IfcFeatureElement).IsAssignableFrom(IfcMetaData.GetType(s.IfcTypeId)) /*&& !typeof(IfcSpace).IsAssignableFrom(IfcMetaData.GetType(s.IfcTypeId))*/); foreach (var shapeInstance in shapeInstances) { Console.WriteLine(i++); var styleId = shapeInstance.StyleLabel > 0 ? shapeInstance.StyleLabel : shapeInstance.IfcTypeId * -1; //GET THE ACTUAL GEOMETRY MeshGeometry3D wpfMesh; //see if we have already read it if (repeatedShapeGeometries.TryGetValue(shapeInstance.ShapeGeometryLabel, out wpfMesh)) { var mg = new GeometryModel3D(wpfMesh, styles[styleId]); mg.SetValue(FrameworkElement.TagProperty, new XbimInstanceHandle(model, shapeInstance.IfcProductLabel, shapeInstance.IfcTypeId)); mg.BackMaterial = mg.Material; mg.Transform = XbimMatrix3D.Multiply(shapeInstance.Transformation, Control.WcsTransform).ToMatrixTransform3D(); if (styles[styleId].IsTransparent) tmpTransparentsGroup.Children.Add(mg); else tmpOpaquesGroup.Children.Add(mg); } else //we need to get the shape geometry { IXbimShapeGeometryData shapeGeom = context.ShapeGeometry(shapeInstance.ShapeGeometryLabel); if (shapeGeom.ReferenceCount > 1) //only store if we are going to use again { wpfMesh = new MeshGeometry3D(); switch ((XbimGeometryType)shapeGeom.Format) { case XbimGeometryType.PolyhedronBinary: wpfMesh.Read(shapeGeom.ShapeData); break; case XbimGeometryType.Polyhedron: wpfMesh.Read(((XbimShapeGeometry)shapeGeom).ShapeData); break; } repeatedShapeGeometries.Add(shapeInstance.ShapeGeometryLabel, wpfMesh); var mg = new GeometryModel3D(wpfMesh, styles[styleId]); mg.SetValue(FrameworkElement.TagProperty, new XbimInstanceHandle(model, shapeInstance.IfcProductLabel, shapeInstance.IfcTypeId)); mg.BackMaterial = mg.Material; mg.Transform = XbimMatrix3D.Multiply(shapeInstance.Transformation, Control.WcsTransform).ToMatrixTransform3D(); if (styles[styleId].IsTransparent) tmpTransparentsGroup.Children.Add(mg); else tmpOpaquesGroup.Children.Add(mg); } else //it is a one off, merge it with shapes of a similar material { var targetMergeMeshByStyle = styleMeshSets[styleId]; switch ((XbimGeometryType)shapeGeom.Format) { case XbimGeometryType.Polyhedron: var shapePoly = (XbimShapeGeometry)shapeGeom; targetMergeMeshByStyle.Add( shapePoly.ShapeData, shapeInstance.IfcTypeId, shapeInstance.IfcProductLabel, shapeInstance.InstanceLabel, XbimMatrix3D.Multiply(shapeInstance.Transformation, Control.WcsTransform)); break; case XbimGeometryType.PolyhedronBinary: targetMergeMeshByStyle.Add( shapeGeom.ShapeData, shapeInstance.IfcTypeId, shapeInstance.IfcProductLabel, shapeInstance.InstanceLabel, XbimMatrix3D.Multiply(shapeInstance.Transformation, Control.WcsTransform)); break; default: throw new ArgumentOutOfRangeException(); } } } } foreach (var wpfMeshGeometry3D in styleMeshSets.Values) { wpfMeshGeometry3D.EndUpdate(); } //} if (tmpOpaquesGroup.Children.Any()) { var mv = new ModelVisual3D(); mv.Content = tmpOpaquesGroup; Control.Opaques.Children.Add(mv); Control.ModelBounds = mv.Content.Bounds.ToXbimRect3D(); } if (tmpTransparentsGroup.Children.Any()) { var mv = new ModelVisual3D(); mv.Content = tmpTransparentsGroup; Control.Transparents.Children.Add(mv); if (Control.ModelBounds.IsEmpty) Control.ModelBounds = mv.Content.Bounds.ToXbimRect3D(); else Control.ModelBounds.Union(mv.Content.Bounds.ToXbimRect3D()); } return scene; }
/// <summary> /// Retrieves model metrics for future analysis /// </summary> /// <param name="args"></param> static void Main(string[] args) { Params arguments = Params.ParseParams(args); if (arguments.IsValid) { var metrics = new XbimModelMetrics(); using (var model = IfcStore.Open(arguments.SourceModelName)) { if (!arguments.SourceIsXbimFile) //need to parse the file { Console.WriteLine("Generating Geometry ..."); var m3D = new Xbim3DModelContext(model); m3D.CreateContext(); } Console.WriteLine("Analysing....."); metrics["Number Of IfcProducts"] = model.Instances.OfType <IfcProduct>().Count(); var shapeDefinitions = model.Instances.OfType <IfcProductDefinitionShape>().ToList(); metrics["Number Of IfcProductDefinitionShape"] = shapeDefinitions.Count(); var rep = shapeDefinitions.SelectMany(shape => shape.Representations.SelectMany(a => a.Items)); //.Where(s=>s.IsSolidModel())); metrics["Number Of Shape Items"] = rep.Count(); metrics["Number Of IfcCartesianPoint"] = model.Instances.OfType <IfcCartesianPoint>().Count(); metrics["Number Of IfcFaceBasedSurfaceModel"] = model.Instances.OfType <IfcFaceBasedSurfaceModel>().Count(); metrics["Number Of IfcShellBasedSurfaceModel"] = model.Instances.OfType <IfcShellBasedSurfaceModel>().Count(); metrics["Number Of IfcSolidModel"] = model.Instances.OfType <IfcSolidModel>().Count(); metrics["Number Of IfcHalfSpaceSolid"] = model.Instances.OfType <IfcHalfSpaceSolid>().Count(); metrics["Number Of IfcBooleanResult"] = model.Instances.OfType <IfcBooleanResult>().Count(); metrics["Number Of IfcMappedItem"] = model.Instances.OfType <IfcMappedItem>().Count(); double totalVoids = 0; double maxVoidsPerElement = 0; double totalElements = 0; foreach (var relVoids in model.Instances.OfType <IfcRelVoidsElement>().GroupBy(r => r.RelatingBuildingElement)) { var voidCount = relVoids.Count(); totalVoids += voidCount; totalElements++; double newmaxVoidsPerElement = Math.Max(maxVoidsPerElement, voidCount); if (newmaxVoidsPerElement != maxVoidsPerElement) { maxVoidsPerElement = newmaxVoidsPerElement; Console.WriteLine("Element is #{0}", relVoids.Key.EntityLabel); } } metrics["Total Of Openings Cut"] = totalVoids; metrics["Number Of Element with Openings"] = totalElements; metrics["Maximum openings in an Element"] = maxVoidsPerElement; metrics["Average openings in an Element"] = totalVoids == 0 ? 0.0 : totalElements / totalVoids; //if the model has shape geometry report on that var context = new Xbim3DModelContext(model); metrics["Number of Shape Geometries"] = context.ShapeGeometries().Count(); metrics["Number of Shape Instances"] = context.ShapeInstances().Count(); // metrics["Number of Surface Styles"] = context.().Count(); model.Close(); } foreach (var metric in metrics) { Console.WriteLine("{0} = {1}", metric.Key, metric.Value); } } Console.WriteLine("Press Enter to continue..."); Console.ReadLine(); }
/// <summary> /// This version uses the new Geometry representation /// </summary> /// <param name="model"></param> /// <param name="context"></param> /// <param name="exclude">List of type to exclude, by default excplict openings and spaces are excluded if exclude = null</param> /// <returns></returns> public XbimScene <WpfMeshGeometry3D, WpfMaterial> BuildScene(XbimModel model, Xbim3DModelContext context, List <Type> exclude = null) { var excludedTypes = new HashSet <short>(); if (exclude == null) { exclude = new List <Type>() { typeof(IfcSpace) // , typeof(IfcFeatureElement) } } ; foreach (var excludedT in exclude) { var ifcT = IfcMetaData.IfcType(excludedT); foreach (var exIfcType in ifcT.NonAbstractSubTypes.Select(IfcMetaData.IfcType)) { excludedTypes.Add(exIfcType.TypeId); } } var scene = new XbimScene <WpfMeshGeometry3D, WpfMaterial>(model); if (context == null) { return(scene); } //get a list of all the unique styles var styles = new Dictionary <int, WpfMaterial>(); var repeatedShapeGeometries = new Dictionary <int, MeshGeometry3D>(); var styleMeshSets = new Dictionary <int, WpfMeshGeometry3D>(); var tmpOpaquesGroup = new Model3DGroup(); var tmpTransparentsGroup = new Model3DGroup(); var sstyles = context.SurfaceStyles(); foreach (var style in sstyles) { var wpfMaterial = new WpfMaterial(); wpfMaterial.CreateMaterial(style); styles.Add(style.DefinedObjectId, wpfMaterial); var mg = new WpfMeshGeometry3D(wpfMaterial, wpfMaterial); mg.WpfModel.SetValue(FrameworkElement.TagProperty, mg); styleMeshSets.Add(style.DefinedObjectId, mg); mg.BeginUpdate(); if (style.IsTransparent) { tmpTransparentsGroup.Children.Add(mg); } else { tmpOpaquesGroup.Children.Add(mg); } } if (!styles.Any()) { return(scene); //this should always return something } var shapeInstances = context.ShapeInstances() .Where(s => s.RepresentationType == XbimGeometryRepresentationType.OpeningsAndAdditionsIncluded && !excludedTypes.Contains(s.IfcTypeId)); // !typeof (IfcFeatureElement).IsAssignableFrom(IfcMetaData.GetType(s.IfcTypeId)) /*&& // !typeof(IfcSpace).IsAssignableFrom(IfcMetaData.GetType(s.IfcTypeId))*/); foreach (var shapeInstance in shapeInstances) { var styleId = shapeInstance.StyleLabel > 0 ? shapeInstance.StyleLabel : shapeInstance.IfcTypeId * -1; //GET THE ACTUAL GEOMETRY MeshGeometry3D wpfMesh; //see if we have already read it if (repeatedShapeGeometries.TryGetValue(shapeInstance.ShapeGeometryLabel, out wpfMesh)) { var mg = new GeometryModel3D(wpfMesh, styles[styleId]); mg.SetValue(FrameworkElement.TagProperty, new XbimInstanceHandle(model, shapeInstance.IfcProductLabel, shapeInstance.IfcTypeId)); mg.BackMaterial = mg.Material; mg.Transform = XbimMatrix3D.Multiply(shapeInstance.Transformation, Control.ModelPositions[model].Transfrom).ToMatrixTransform3D(); if (styles[styleId].IsTransparent) { tmpTransparentsGroup.Children.Add(mg); } else { tmpOpaquesGroup.Children.Add(mg); } } else //we need to get the shape geometry { IXbimShapeGeometryData shapeGeom = context.ShapeGeometry(shapeInstance.ShapeGeometryLabel); if (shapeGeom.ReferenceCount > 1) //only store if we are going to use again { wpfMesh = new MeshGeometry3D(); switch ((XbimGeometryType)shapeGeom.Format) { case XbimGeometryType.PolyhedronBinary: wpfMesh.Read(shapeGeom.ShapeData); break; case XbimGeometryType.Polyhedron: wpfMesh.Read(((XbimShapeGeometry)shapeGeom).ShapeData); break; } repeatedShapeGeometries.Add(shapeInstance.ShapeGeometryLabel, wpfMesh); var mg = new GeometryModel3D(wpfMesh, styles[styleId]); mg.SetValue(FrameworkElement.TagProperty, new XbimInstanceHandle(model, shapeInstance.IfcProductLabel, shapeInstance.IfcTypeId)); mg.BackMaterial = mg.Material; mg.Transform = XbimMatrix3D.Multiply(shapeInstance.Transformation, Control.ModelPositions[model].Transfrom).ToMatrixTransform3D(); if (styles[styleId].IsTransparent) { tmpTransparentsGroup.Children.Add(mg); } else { tmpOpaquesGroup.Children.Add(mg); } } else //it is a one off, merge it with shapes of a similar material { var targetMergeMeshByStyle = styleMeshSets[styleId]; switch ((XbimGeometryType)shapeGeom.Format) { case XbimGeometryType.Polyhedron: // var shapePoly = (XbimShapeGeometry)shapeGeom; var asString = Encoding.UTF8.GetString(shapeGeom.ShapeData.ToArray()); targetMergeMeshByStyle.Add( asString, shapeInstance.IfcTypeId, shapeInstance.IfcProductLabel, shapeInstance.InstanceLabel, XbimMatrix3D.Multiply(shapeInstance.Transformation, Control.ModelPositions[model].Transfrom), model.UserDefinedId); break; case XbimGeometryType.PolyhedronBinary: var transform = XbimMatrix3D.Multiply(shapeInstance.Transformation, Control.ModelPositions[model].Transfrom); targetMergeMeshByStyle.Add( shapeGeom.ShapeData, shapeInstance.IfcTypeId, shapeInstance.IfcProductLabel, shapeInstance.InstanceLabel, transform, model.UserDefinedId); break; default: throw new ArgumentOutOfRangeException(); } } } } foreach (var wpfMeshGeometry3D in styleMeshSets.Values) { wpfMeshGeometry3D.EndUpdate(); } //} if (tmpOpaquesGroup.Children.Any()) { var mv = new ModelVisual3D(); mv.Content = tmpOpaquesGroup; Control.OpaquesVisual3D.Children.Add(mv); // Control.ModelBounds = mv.Content.Bounds.ToXbimRect3D(); } if (tmpTransparentsGroup.Children.Any()) { var mv = new ModelVisual3D(); mv.Content = tmpTransparentsGroup; Control.TransparentsVisual3D.Children.Add(mv); //if (Control.ModelBounds.IsEmpty) Control.ModelBounds = mv.Content.Bounds.ToXbimRect3D(); //else Control.ModelBounds.Union(mv.Content.Bounds.ToXbimRect3D()); } return(scene); }
private void ShowGeometrySummary(XbimModel model) { var context = new Xbim3DModelContext(model); Console.WriteLine(); Console.WriteLine("Styles"); foreach (var styles in context.SurfaceStyles()) { Console.WriteLine("{0,-6} {1,-30} {2,-6}", styles.DefinedObjectId, styles.ColourMap.First(), styles.IsTransparent); } var largest = context.GetLargestRegion(); Console.WriteLine(); Console.WriteLine("Regions"); foreach (var region in context.GetRegions()) { Console.WriteLine("{0,-20} {1,6} {2}", region.Name, region.Population, (region == largest)); } Console.WriteLine("{0,-8}, {1,-8}, {2,6} {3,8}", "Ifc Label", "Label", "Ref Count", "Bytes"); var geometries = GetGeom(model) .OrderByDescending(o => o.Cost) .Distinct() .Take(10); var items = from inst in context.ShapeInstances() join prod in model.Instances.OfType <IfcProduct>() on inst.IfcProductLabel equals prod.EntityLabel select new { Instance = inst, Geometry = context.ShapeGeometry(inst.InstanceLabel), Product = prod }; var expensiveProducts = items .OrderByDescending(o => (o.Geometry.IsValid ? o.Geometry.Cost : 0)) .Take(20); foreach (var geometry in geometries) { Console.WriteLine("{0, -8}, {1,-8}, {2,6} {3,8}", geometry.IfcShapeLabel, geometry.ShapeLabel, geometry.ReferenceCount, geometry.Cost); } Console.WriteLine(); Console.WriteLine("Top 20 expensive items"); foreach (var item in expensiveProducts) { Console.WriteLine("{0,-6} {1,-40} {2,-20} {3,6}", item.Product.EntityLabel, item.Product.Name.ToString().Truncate(40), item.Product.GetType().Name, item.Geometry.Cost ); } }
private static void TestForClassificationOfIfcFeatureElements(Xbim3DModelContext context) { var excludedTypes = new HashSet<short> { IfcMetaData.IfcType(typeof (IfcFeatureElement)).TypeId, IfcMetaData.IfcType(typeof (IfcOpeningElement)).TypeId, IfcMetaData.IfcType(typeof (IfcProjectionElement)).TypeId }; var shapeInstances = context.ShapeInstances().Where(s => excludedTypes.Contains(s.IfcTypeId) && // ifcfeatures s.RepresentationType != XbimGeometryRepresentationType.OpeningsAndAdditionsOnly); // that are not classified as OpeningsAndAdditionsOnly Assert.IsFalse(shapeInstances.Any(), "Should not find any shapes of with this classification of typeid and representationType."); }
/// <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); } }