private void GetGeometryData(XbimModel model, IfcProduct product) { var context = new Xbim3DModelContext(model); //TODO: WCS var metre = model.ModelFactors.OneMetre; var units = model.IfcProject.UnitsInContext.Units .Where <IfcSIUnit>(u => u.UnitType == IfcUnitEnum.LENGTHUNIT) .ToList(); string defaultLengthUnit = ""; if (units.Count > 0) { defaultLengthUnit = units.First().GetSymbol(); } var styles = context.SurfaceStyles().ToList(); var productShape = context.ShapeInstancesOf(product) .Where(p => p.RepresentationType != XbimGeometryRepresentationType.OpeningsAndAdditionsExcluded) .Distinct(); if (productShape.Any()) { foreach (var shapeInstance in productShape) { var shapeGeometry = context.ShapeGeometry(shapeInstance.ShapeGeometryLabel); XbimColour style = XbimColour.Default; if (shapeInstance.HasStyle) { style = styles.First(s => s.DefinedObjectId == shapeInstance.StyleLabel).ColourMap.FirstOrDefault(); } Console.WriteLine("--Style: {0}", style); Console.WriteLine("-- x:{0:0.0000} \n-- y:{1:0.0000} \n-- z:{2:0.0000} \n", shapeGeometry.BoundingBox.Location.X, shapeGeometry.BoundingBox.Location.Y, shapeGeometry.BoundingBox.Location.Z); Console.WriteLine("-- sx:{0:0.0000} {3} \n-- sy:{1:0.0000} {3} \n-- sz:{2:0.0000} {3} \n", shapeGeometry.BoundingBox.SizeX, shapeGeometry.BoundingBox.SizeY, shapeGeometry.BoundingBox.SizeZ, defaultLengthUnit); } } }
/// <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> /// 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> private void BuildScene(Xbim3DModelContext context) { //get a list of all the unique styles Dictionary<int, WpfMaterial> styles = new Dictionary<int, WpfMaterial>(); Dictionary<int, MeshGeometry3D> shapeGeometries = new Dictionary<int, MeshGeometry3D>(); Dictionary<int, WpfMeshGeometry3D> meshSets = new Dictionary<int, WpfMeshGeometry3D>(); Model3DGroup opaques = new Model3DGroup(); Model3DGroup transparents = new Model3DGroup(); foreach (var shapeGeom in context.ShapeGeometries()) { MeshGeometry3D wpfMesh = new MeshGeometry3D(); wpfMesh.SetValue(TagProperty, shapeGeom); //don't read the geometry into the mesh yet as we do not know where the instance is located shapeGeometries.Add(shapeGeom.ShapeLabel, wpfMesh); } foreach (var style in context.SurfaceStyles()) { WpfMaterial wpfMaterial = new WpfMaterial(); wpfMaterial.CreateMaterial(style); styles.Add(style.DefinedObjectId, wpfMaterial); WpfMeshGeometry3D mg = new WpfMeshGeometry3D(wpfMaterial, wpfMaterial); meshSets.Add(style.DefinedObjectId, mg); if (style.IsTransparent) transparents.Children.Add(mg); else opaques.Children.Add(mg); } ////if (!styles.Any()) return ; //this should always have something unless the model is empty ////double metre = model.ModelFactors.OneMetre; ////XbimMatrix3D wcsTransform = XbimMatrix3D.CreateTranslation(_modelTranslation) * XbimMatrix3D.CreateScale((float)(1 / metre)); ////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)))) ////{ //// int styleId = shapeInstance.StyleLabel > 0 ? shapeInstance.StyleLabel : shapeInstance.IfcTypeId * -1; //// //GET THE ACTUAL GEOMETRY //// MeshGeometry3D wpfMesh; //// //see if we have already read it //// if (shapeGeometries.TryGetValue(shapeInstance.ShapeGeometryLabel, out wpfMesh)) //// { //// GeometryModel3D mg = new GeometryModel3D(wpfMesh, styles[styleId]); //// mg.SetValue(TagProperty, new XbimInstanceHandle(model, shapeInstance.IfcProductLabel, shapeInstance.IfcTypeId)); //// mg.BackMaterial = mg.Material; //// mg.Transform = XbimMatrix3D.Multiply(shapeInstance.Transformation, wcsTransform).ToMatrixTransform3D(); //// if (styles[styleId].IsTransparent) //// transparents.Children.Add(mg); //// else //// opaques.Children.Add(mg); //// } //// else //we need to get the shape geometry //// { //// XbimShapeGeometry shapeGeom = context.ShapeGeometry(shapeInstance.ShapeGeometryLabel); //// if (shapeGeom.ReferenceCount > 1) //only store if we are going to use again //// { //// wpfMesh = new MeshGeometry3D(); //// wpfMesh.Read(shapeGeom.ShapeData); //// shapeGeometries.Add(shapeInstance.ShapeGeometryLabel, wpfMesh); //// GeometryModel3D mg = new GeometryModel3D(wpfMesh, styles[styleId]); //// mg.SetValue(TagProperty, new XbimInstanceHandle(model, shapeInstance.IfcProductLabel, shapeInstance.IfcTypeId)); //// mg.BackMaterial = mg.Material; //// mg.Transform = XbimMatrix3D.Multiply(shapeInstance.Transformation, wcsTransform).ToMatrixTransform3D(); //// if (styles[styleId].IsTransparent) //// transparents.Children.Add(mg); //// else //// opaques.Children.Add(mg); //// } //// else //it is a one off, merge it with shapes of a similar material //// { //// WpfMeshGeometry3D mg = meshSets[styleId]; //// mg.Add(shapeGeom.ShapeData, //// shapeInstance.IfcTypeId, //// shapeInstance.IfcProductLabel, //// shapeInstance.InstanceLabel, //// XbimMatrix3D.Multiply(shapeInstance.Transformation, wcsTransform), 0); //// } //// } ////} if (opaques.Children.Any()) { ModelVisual3D mv = new ModelVisual3D(); mv.Content = opaques; Opaques.Children.Add(mv); if (_modelBounds.IsEmpty) _modelBounds = mv.Content.Bounds.ToXbimRect3D(); else _modelBounds.Union(mv.Content.Bounds.ToXbimRect3D()); } if (transparents.Children.Any()) { ModelVisual3D mv = new ModelVisual3D(); mv.Content = transparents; Transparents.Children.Add(mv); if (_modelBounds.IsEmpty) _modelBounds = mv.Content.Bounds.ToXbimRect3D(); else _modelBounds.Union(mv.Content.Bounds.ToXbimRect3D()); } }
/// <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> private void BuildScene(Xbim3DModelContext context) { //get a list of all the unique styles Dictionary <int, WpfMaterial> styles = new Dictionary <int, WpfMaterial>(); Dictionary <int, MeshGeometry3D> shapeGeometries = new Dictionary <int, MeshGeometry3D>(); Dictionary <int, WpfMeshGeometry3D> meshSets = new Dictionary <int, WpfMeshGeometry3D>(); Model3DGroup opaques = new Model3DGroup(); Model3DGroup transparents = new Model3DGroup(); foreach (var shapeGeom in context.ShapeGeometries()) { MeshGeometry3D wpfMesh = new MeshGeometry3D(); wpfMesh.SetValue(TagProperty, shapeGeom); //don't read the geometry into the mesh yet as we do not know where the instance is located shapeGeometries.Add(shapeGeom.ShapeLabel, wpfMesh); } foreach (var style in context.SurfaceStyles()) { WpfMaterial wpfMaterial = new WpfMaterial(); wpfMaterial.CreateMaterial(style); styles.Add(style.DefinedObjectId, wpfMaterial); WpfMeshGeometry3D mg = new WpfMeshGeometry3D(wpfMaterial, wpfMaterial); meshSets.Add(style.DefinedObjectId, mg); if (style.IsTransparent) { transparents.Children.Add(mg); } else { opaques.Children.Add(mg); } } ////if (!styles.Any()) return ; //this should always have something unless the model is empty ////double metre = model.ModelFactors.OneMetre; ////XbimMatrix3D wcsTransform = XbimMatrix3D.CreateTranslation(_modelTranslation) * XbimMatrix3D.CreateScale((float)(1 / metre)); ////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)))) ////{ //// int styleId = shapeInstance.StyleLabel > 0 ? shapeInstance.StyleLabel : shapeInstance.IfcTypeId * -1; //// //GET THE ACTUAL GEOMETRY //// MeshGeometry3D wpfMesh; //// //see if we have already read it //// if (shapeGeometries.TryGetValue(shapeInstance.ShapeGeometryLabel, out wpfMesh)) //// { //// GeometryModel3D mg = new GeometryModel3D(wpfMesh, styles[styleId]); //// mg.SetValue(TagProperty, new XbimInstanceHandle(model, shapeInstance.IfcProductLabel, shapeInstance.IfcTypeId)); //// mg.BackMaterial = mg.Material; //// mg.Transform = XbimMatrix3D.Multiply(shapeInstance.Transformation, wcsTransform).ToMatrixTransform3D(); //// if (styles[styleId].IsTransparent) //// transparents.Children.Add(mg); //// else //// opaques.Children.Add(mg); //// } //// else //we need to get the shape geometry //// { //// XbimShapeGeometry shapeGeom = context.ShapeGeometry(shapeInstance.ShapeGeometryLabel); //// if (shapeGeom.ReferenceCount > 1) //only store if we are going to use again //// { //// wpfMesh = new MeshGeometry3D(); //// wpfMesh.Read(shapeGeom.ShapeData); //// shapeGeometries.Add(shapeInstance.ShapeGeometryLabel, wpfMesh); //// GeometryModel3D mg = new GeometryModel3D(wpfMesh, styles[styleId]); //// mg.SetValue(TagProperty, new XbimInstanceHandle(model, shapeInstance.IfcProductLabel, shapeInstance.IfcTypeId)); //// mg.BackMaterial = mg.Material; //// mg.Transform = XbimMatrix3D.Multiply(shapeInstance.Transformation, wcsTransform).ToMatrixTransform3D(); //// if (styles[styleId].IsTransparent) //// transparents.Children.Add(mg); //// else //// opaques.Children.Add(mg); //// } //// else //it is a one off, merge it with shapes of a similar material //// { //// WpfMeshGeometry3D mg = meshSets[styleId]; //// mg.Add(shapeGeom.ShapeData, //// shapeInstance.IfcTypeId, //// shapeInstance.IfcProductLabel, //// shapeInstance.InstanceLabel, //// XbimMatrix3D.Multiply(shapeInstance.Transformation, wcsTransform), 0); //// } //// } ////} if (opaques.Children.Any()) { ModelVisual3D mv = new ModelVisual3D(); mv.Content = opaques; Opaques.Children.Add(mv); if (_modelBounds.IsEmpty) { _modelBounds = mv.Content.Bounds.ToXbimRect3D(); } else { _modelBounds.Union(mv.Content.Bounds.ToXbimRect3D()); } } if (transparents.Children.Any()) { ModelVisual3D mv = new ModelVisual3D(); mv.Content = transparents; Transparents.Children.Add(mv); if (_modelBounds.IsEmpty) { _modelBounds = mv.Content.Bounds.ToXbimRect3D(); } else { _modelBounds.Union(mv.Content.Bounds.ToXbimRect3D()); } } }
/// <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 WriteGeometry(JsonWriter writer) { XbimMatrix3D globalTrans = GetGlobalModelTransform(); //write out the material nodes and then the instances for each material Dictionary <int, XbimTexture> styles = _context.SurfaceStyles().ToDictionary(s => s.DefinedObjectId); foreach (var instanceGroup in _context.ShapeInstancesGroupByStyle()) { if (!instanceGroup.Any()) { continue; //skip emmpty instances; } int styleId = instanceGroup.Key; XbimTexture style = styles[styleId]; writer.WriteStartObject(); //Material node { writer.WritePropertyName("type"); writer.WriteValue("material"); writer.WritePropertyName("id"); writer.WriteValue("M" + styleId); writer.WritePropertyName("color"); writer.WriteStartObject(); //begin color { writer.WritePropertyName("r"); writer.WriteValue(style.ColourMap[0].Red); writer.WritePropertyName("g"); writer.WriteValue(style.ColourMap[0].Green); writer.WritePropertyName("b"); writer.WriteValue(style.ColourMap[0].Blue); } writer.WriteEndObject(); //end color writer.WritePropertyName("alpha"); writer.WriteValue(style.ColourMap[0].Alpha); //all instances of this style writer.WritePropertyName("nodes"); //beginning of the instance nodes writer.WriteStartArray(); foreach (var shapeInstance in instanceGroup) { writer.WriteStartObject(); //start of instance transform { if (_maps.Contains(shapeInstance.ShapeGeometryLabel)) //it is a reference { //write the transform writer.WritePropertyName("type"); //geometry node writer.WriteValue("matrix"); writer.WritePropertyName("id"); writer.WriteValue("I" + shapeInstance.InstanceLabel); writer.WritePropertyName("elements"); XbimMatrix3D m = XbimMatrix3D.Multiply(shapeInstance.Transformation, globalTrans); WriteMatrix(writer, m); writer.WritePropertyName("nodes"); //beginning of the instance nodes writer.WriteStartArray(); //write the map writer.WriteStartObject(); //start of map { writer.WritePropertyName("type"); //geometry node writer.WriteValue("geometry"); writer.WritePropertyName("coreId"); writer.WriteValue("L" + shapeInstance.ShapeGeometryLabel); } writer.WriteEndObject(); //end of map writer.WriteEndArray(); //end of instance tranform nodes } else //write the actual geometry { writer.WritePropertyName("type"); //geometry node writer.WriteValue("geometry"); writer.WritePropertyName("primitive"); //primitive: " writer.WriteValue("triangles"); writer.WritePropertyName("id"); writer.WriteValue("I" + shapeInstance.InstanceLabel); WriteShapeGeometry(writer, _context.ShapeGeometry(shapeInstance), XbimMatrix3D.Multiply(shapeInstance.Transformation, globalTrans)); } } writer.WriteEndObject(); //end of instance transform } writer.WriteEndArray(); //end of instance transform nodes } writer.WriteEndObject(); // end of the material node } }
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 ); } }