public XbimGeometryData TransformBy(XbimMatrix3D matrix) { XbimMatrix3D t = XbimMatrix3D.FromArray(DataArray2); t = XbimMatrix3D.Multiply(t, matrix); return(new XbimGeometryData(GeometryLabel, IfcProductLabel, GeometryType, IfcTypeId, ShapeData, t.ToArray(), GeometryHash, StyleLabel, Counter)); }
public void MatrixArrayConversion() { var m = XbimMatrix3D.CreateTranslation(10, 20, 30); m.RotateAroundXAxis(Math.PI / 4); m.Scale(.05); var outM = m.ToArray(true); var rback = XbimMatrix3D.FromArray(outM); Assert.AreEqual(rback.M11, m.M11); Assert.AreEqual(rback.M12, m.M12); Assert.AreEqual(rback.M13, m.M13); Assert.AreEqual(rback.M14, m.M14); Assert.AreEqual(rback.M21, m.M21); Assert.AreEqual(rback.M22, m.M22); Assert.AreEqual(rback.M23, m.M23); Assert.AreEqual(rback.M24, m.M24); Assert.AreEqual(rback.M31, m.M31); Assert.AreEqual(rback.M32, m.M32); Assert.AreEqual(rback.M33, m.M33); Assert.AreEqual(rback.M34, m.M34); Assert.AreEqual(rback.OffsetX, m.OffsetX); Assert.AreEqual(rback.OffsetY, m.OffsetY); Assert.AreEqual(rback.OffsetZ, m.OffsetZ); Assert.AreEqual(rback.M44, m.M44); }
/// <summary> /// Appends a geometry data object to the Mesh, returns false if the mesh would become too big and needs splitting /// </summary> /// <param name="geometryMeshData"></param> public bool Add(XbimGeometryData geometryMeshData, short modelId = 0) { var transform = XbimMatrix3D.FromArray(geometryMeshData.DataArray2); if (geometryMeshData.GeometryType == XbimGeometryType.TriangulatedMesh) { var strm = new XbimTriangulatedModelStream(geometryMeshData.ShapeData); var fragment = strm.BuildWithNormals(this, transform, modelId); if (fragment.EntityLabel == -1) //nothing was added due to size being exceeded { return(false); } fragment.EntityLabel = geometryMeshData.IfcProductLabel; fragment.EntityTypeId = geometryMeshData.IfcTypeId; _meshes.Add(fragment); } else if (geometryMeshData.GeometryType == XbimGeometryType.BoundingBox) { var r3D = XbimRect3D.FromArray(geometryMeshData.ShapeData); Add(MakeBoundingBox(r3D, transform), geometryMeshData.IfcProductLabel, IfcMetaData.GetType(geometryMeshData.IfcTypeId), modelId); } else { throw new XbimException("Illegal geometry type found"); } return(true); }
public void ReadAndWriteWexBimFile() { using (var m = IfcStore.Open("SolidTestFiles\\19 - TwoProxy.ifc")) { var m3D = new Xbim3DModelContext(m); m3D.CreateContext(); using (var bw = new BinaryWriter(new FileStream("test.wexBIM", FileMode.Create))) { m.SaveAsWexBim(bw); bw.Close(); } using (var fs = new FileStream(@"test.wexBIM", FileMode.Open, FileAccess.Read)) { using (var br = new BinaryReader(fs)) { var magicNumber = br.ReadInt32(); var version = br.ReadByte(); var shapeCount = br.ReadInt32(); var vertexCount = br.ReadInt32(); var triangleCount = br.ReadInt32(); var matrixCount = br.ReadInt32(); var productCount = br.ReadInt32(); var styleCount = br.ReadInt32(); var meter = br.ReadSingle(); Assert.IsTrue(meter > 0); var regionCount = br.ReadInt16(); for (int i = 0; i < regionCount; i++) { var population = br.ReadInt32(); var centreX = br.ReadSingle(); var centreY = br.ReadSingle(); var centreZ = br.ReadSingle(); var boundsBytes = br.ReadBytes(6 * sizeof(float)); var modelBounds = XbimRect3D.FromArray(boundsBytes); } for (int i = 0; i < styleCount; i++) { var styleId = br.ReadInt32(); var red = br.ReadSingle(); var green = br.ReadSingle(); var blue = br.ReadSingle(); var alpha = br.ReadSingle(); } for (int i = 0; i < productCount; i++) { var productLabel = br.ReadInt32(); var productType = br.ReadInt16(); var boxBytes = br.ReadBytes(6 * sizeof(float)); XbimRect3D bb = XbimRect3D.FromArray(boxBytes); } for (int i = 0; i < shapeCount; i++) { var shapeRepetition = br.ReadInt32(); Assert.IsTrue(shapeRepetition > 0); if (shapeRepetition > 1) { for (int j = 0; j < shapeRepetition; j++) { var ifcProductLabel = br.ReadInt32(); var instanceTypeId = br.ReadInt16(); var instanceLabel = br.ReadInt32(); var styleId = br.ReadInt32(); var transform = XbimMatrix3D.FromArray(br.ReadBytes(sizeof(float) * 16)); } var triangulation = br.ReadShapeTriangulation(); } else if (shapeRepetition == 1) { var ifcProductLabel = br.ReadInt32(); var instanceTypeId = br.ReadInt16(); var instanceLabel = br.ReadInt32(); var styleId = br.ReadInt32(); var triangulation = br.ReadShapeTriangulation(); } } } } } }
public static void ReadWexBim(string wexBimPath) { //Dictionary<int, BimColor> colors = new Dictionary<int, BimColor>(); List <BimColor> colors = new List <BimColor>(); List <BimRegion> regions = new List <BimRegion>(); List <BimProduct> products = new List <BimProduct>(); using (var fs = new FileStream(wexBimPath, FileMode.Open, FileAccess.Read)) { float scale = 0f;//关系到模型实际大小的比例 Vector3 offset = Vector3.zero; using (var br = new BinaryReader(fs)) { var magicNumber = br.ReadInt32(); var version = br.ReadByte(); var shapeCount = br.ReadInt32(); var vertexCount = br.ReadInt32(); var triangleCount = br.ReadInt32(); var matrixCount = br.ReadInt32(); var productCount = br.ReadInt32(); var styleCount = br.ReadInt32(); var meter = br.ReadSingle(); var regionCount = br.ReadInt16(); scale = meter; for (int i = 0; i < regionCount; i++) { var population = br.ReadInt32(); var centreX = br.ReadSingle(); var centreY = br.ReadSingle(); var centreZ = br.ReadSingle(); var boundsBytes = br.ReadBytes(6 * sizeof(float)); var modelBounds = XbimRect3D.FromArray(boundsBytes); BimRegion region = new BimRegion(population, centreX, centreY, centreZ, (float)modelBounds.SizeX, (float)modelBounds.SizeY, (float)modelBounds.SizeZ); regions.Add(region); } offset = regions[0].position; for (int i = 0; i < styleCount; i++) { var styleId = br.ReadInt32(); var red = br.ReadSingle(); var green = br.ReadSingle(); var blue = br.ReadSingle(); var alpha = br.ReadSingle(); BimColor color = new BimColor(styleId, red, green, blue, alpha); colors.Add(color); } for (int i = 0; i < productCount; i++) { var productLabel = br.ReadInt32(); var productType = br.ReadInt16(); var boxBytes = br.ReadBytes(6 * sizeof(float)); BimProduct product = new BimProduct(productLabel, productType); /* * XbimRect3D bb = XbimRect3D.FromArray(boxBytes); * XbimPoint3D doubleCenter = XbimPoint3D.Add(bb.Min, bb.Max); * BimProduct product = new BimProduct(productLabel, productType, (float)doubleCenter.X / 2, (float)doubleCenter.Y / 2, * (float)doubleCenter.Z / 2, (float)bb.SizeX, (float)bb.SizeY, (float)bb.SizeZ); */ products.Add(product); } for (int i = 0; i < shapeCount; i++) { var shapeRepetition = br.ReadInt32(); if (shapeRepetition > 1) { List <BimShape> thisShape = new List <BimShape>(); for (int j = 0; j < shapeRepetition; j++) { var ifcProductLabel = br.ReadInt32(); var instanceTypeId = br.ReadInt16(); var instanceLabel = br.ReadInt32(); var styleId = br.ReadInt32(); var transform = XbimMatrix3D.FromArray(br.ReadBytes(sizeof(double) * 16)); BimShape shape = new BimShape(ifcProductLabel, instanceTypeId, instanceLabel, styleId, transform); thisShape.Add(shape); var p = products.Find(product => product.entityLabel == ifcProductLabel); p.shapes.Add(shape); } var triangulation = br.ReadShapeTriangulation(); foreach (var s in thisShape) { var tri = new BimTriangulation(triangulation, scale, offset, s.transform, true); s.triangulations.Add(tri); } } else if (shapeRepetition == 1) { var ifcProductLabel = br.ReadInt32(); var instanceTypeId = br.ReadInt16(); var instanceLabel = br.ReadInt32(); var styleId = br.ReadInt32(); XbimShapeTriangulation triangulation = br.ReadShapeTriangulation(); BimShape shape = new BimShape(ifcProductLabel, instanceTypeId, instanceLabel, styleId); var p = products.Find(product => product.entityLabel == ifcProductLabel); p.shapes.Add(shape); var tri = new BimTriangulation(triangulation, scale, offset); shape.triangulations.Add(tri); } } } } ModelCreater.CreateModel(products, colors); }
public void ReadAndWriteWexBimFile() { using (var m = IfcStore.Open("SampleHouse4.ifc")) { var wall = m.Instances[1229] as IIfcWall; Assert.IsNotNull(wall, "No IfcWall found in file"); var brep = m.Instances[1213] as IIfcFacetedBrep; Assert.IsNotNull(brep, "No IfcFacetedBrep found in file"); var styledItem = brep.StyledByItem.FirstOrDefault(); //this is not fast do not do on large models Assert.IsNotNull(styledItem, "No StyledItem found in file"); var repContext = m.Instances[100] as IIfcGeometricRepresentationContext; Assert.IsNotNull(repContext, "No context found in file"); var geomStore = m.GeometryStore; using (var txn = geomStore.BeginInit()) { var tessellator = new XbimTessellator(m, XbimGeometryType.PolyhedronBinary); var geom = tessellator.Mesh(brep); var geomId = txn.AddShapeGeometry(geom); //ADD A SHAPE INSTANCE var shapeInstance = new XbimShapeInstance() { BoundingBox = geom.BoundingBox, IfcProductLabel = wall.EntityLabel, IfcTypeId = m.Metadata.ExpressTypeId("IFCWALL"), StyleLabel = m.Metadata.ExpressTypeId("IFCWALL") * -1, RepresentationType = XbimGeometryRepresentationType.OpeningsAndAdditionsIncluded, ShapeGeometryLabel = geomId, Transformation = XbimMatrix3D.Identity, RepresentationContext = repContext.EntityLabel }; txn.AddShapeInstance(shapeInstance, geomId); txn.Commit(); } using (var bw = new BinaryWriter(new FileStream("test.wexBIM", FileMode.Create))) { m.SaveAsWexBim(bw); bw.Close(); } } using (var fs = new FileStream(@"test.wexBIM", FileMode.Open, FileAccess.Read)) { using (var br = new BinaryReader(fs)) { var magicNumber = br.ReadInt32(); Assert.IsTrue(magicNumber == IfcStoreGeometryExtensions.WexBimId); var version = br.ReadByte(); var shapeCount = br.ReadInt32(); var vertexCount = br.ReadInt32(); var triangleCount = br.ReadInt32(); var matrixCount = br.ReadInt32(); var productCount = br.ReadInt32(); var styleCount = br.ReadInt32(); var meter = br.ReadSingle(); Assert.IsTrue(meter > 0); var regionCount = br.ReadInt16(); for (int i = 0; i < regionCount; i++) { var population = br.ReadInt32(); var centreX = br.ReadSingle(); var centreY = br.ReadSingle(); var centreZ = br.ReadSingle(); var boundsBytes = br.ReadBytes(6 * sizeof(float)); var modelBounds = XbimRect3D.FromArray(boundsBytes); } for (int i = 0; i < styleCount; i++) { var styleId = br.ReadInt32(); var red = br.ReadSingle(); var green = br.ReadSingle(); var blue = br.ReadSingle(); var alpha = br.ReadSingle(); } for (int i = 0; i < productCount; i++) { var productLabel = br.ReadInt32(); var productType = br.ReadInt16(); var boxBytes = br.ReadBytes(6 * sizeof(float)); XbimRect3D bb = XbimRect3D.FromArray(boxBytes); } for (int i = 0; i < shapeCount; i++) { var shapeRepetition = br.ReadInt32(); Assert.IsTrue(shapeRepetition > 0); if (shapeRepetition > 1) { for (int j = 0; j < shapeRepetition; j++) { var ifcProductLabel = br.ReadInt32(); var instanceTypeId = br.ReadInt16(); var instanceLabel = br.ReadInt32(); var styleId = br.ReadInt32(); var transform = XbimMatrix3D.FromArray(br.ReadBytes(sizeof(double) * 16)); } var triangulation = br.ReadShapeTriangulation(); Assert.IsTrue(triangulation.Vertices.Count > 0, "Number of vertices should be greater than zero"); } else if (shapeRepetition == 1) { var ifcProductLabel = br.ReadInt32(); var instanceTypeId = br.ReadInt16(); var instanceLabel = br.ReadInt32(); var styleId = br.ReadInt32(); var triangulation = br.ReadShapeTriangulation(); Assert.IsTrue(triangulation.Vertices.Count > 0, "Number of vertices should be greater than zero"); } } } } }
public const int WexBimId = 94132117; // Magic /// <summary> /// This function is used to generate the .wexbim model files. /// </summary> /// <param name="model">The model to export</param> /// <param name="binaryStream">An open writable streamer.</param> /// <param name="products">Optional products to be written to the wexBIM file. If null, all products from the model will be saved</param> /// <param name="translation">Optional 3D vector to apply</param> public static void SaveAsWexBim(this IModel model, BinaryWriter binaryStream, IEnumerable <IIfcProduct> products = null, IVector3D translation = null) { products = products ?? model.Instances.OfType <IIfcProduct>(); // ReSharper disable RedundantCast if (model.GeometryStore == null) { throw new XbimException("Geometry store has not been initialised"); } // ReSharper disable once CollectionNeverUpdated.Local var colourMap = new XbimColourMap(); using (var geomRead = model.GeometryStore.BeginRead()) { var lookup = geomRead.ShapeGeometries; var styles = geomRead.StyleIds; var regions = geomRead.ContextRegions.SelectMany(r => r).ToList(); //we need to get all the default styles for various products var defaultStyles = geomRead.ShapeInstances.Select(i => - (int)i.IfcTypeId).Distinct(); var allStyles = defaultStyles.Concat(styles).ToList(); int numberOfGeometries = 0; int numberOfVertices = 0; int numberOfTriangles = 0; int numberOfMatrices = 0; int numberOfProducts = 0; int numberOfStyles = allStyles.Count; //start writing out binaryStream.Write((Int32)WexBimId); //magic number binaryStream.Write((byte)2); //version of stream, arrays now packed as doubles var start = (int)binaryStream.Seek(0, SeekOrigin.Current); binaryStream.Write((Int32)0); //number of shapes binaryStream.Write((Int32)0); //number of vertices binaryStream.Write((Int32)0); //number of triangles binaryStream.Write((Int32)0); //number of matrices binaryStream.Write((Int32)0); //number of products binaryStream.Write((Int32)numberOfStyles); //number of styles binaryStream.Write(Convert.ToSingle(model.ModelFactors.OneMetre)); //write out conversion to meter factor binaryStream.Write(Convert.ToInt16(regions.Count)); //write out the population data var t = XbimMatrix3D.Identity; t = Translate(t, translation); foreach (var r in regions) { binaryStream.Write((Int32)(r.Population)); var bounds = r.ToXbimRect3D(); var centre = t.Transform(r.Centre); //write out the centre of the region binaryStream.Write((Single)centre.X); binaryStream.Write((Single)centre.Y); binaryStream.Write((Single)centre.Z); //bounding box of largest region binaryStream.Write(bounds.ToFloatArray()); } //textures foreach (var styleId in allStyles) { XbimColour colour; if (styleId > 0) { var ss = (IIfcSurfaceStyle)model.Instances[styleId]; var texture = XbimTexture.Create(ss); colour = texture.ColourMap.FirstOrDefault(); } else //use the default in the colour map for the enetity type { var theType = model.Metadata.GetType((short)Math.Abs(styleId)); colour = colourMap[theType.Name]; } if (colour == null) { colour = XbimColour.DefaultColour; } binaryStream.Write((Int32)styleId); //style ID binaryStream.Write((Single)colour.Red); binaryStream.Write((Single)colour.Green); binaryStream.Write((Single)colour.Blue); binaryStream.Write((Single)colour.Alpha); } //write out all the product bounding boxes var prodIds = new HashSet <int>(); foreach (var product in products) { if (product is IIfcFeatureElement) { continue; } prodIds.Add(product.EntityLabel); var bb = XbimRect3D.Empty; foreach (var si in geomRead.ShapeInstancesOfEntity(product)) { var transformation = Translate(si.Transformation, translation); var bbPart = XbimRect3D.TransformBy(si.BoundingBox, transformation); //make sure we put the box in the right place and then convert to axis aligned if (bb.IsEmpty) { bb = bbPart; } else { bb.Union(bbPart); } } //do not write out anything with no geometry if (bb.IsEmpty) { continue; } binaryStream.Write((Int32)product.EntityLabel); binaryStream.Write((UInt16)model.Metadata.ExpressTypeId(product)); binaryStream.Write(bb.ToFloatArray()); numberOfProducts++; } //projections and openings have already been applied, var toIgnore = new short[4]; toIgnore[0] = model.Metadata.ExpressTypeId("IFCOPENINGELEMENT"); toIgnore[1] = model.Metadata.ExpressTypeId("IFCPROJECTIONELEMENT"); if (model.SchemaVersion == XbimSchemaVersion.Ifc4 || model.SchemaVersion == XbimSchemaVersion.Ifc4x1) { toIgnore[2] = model.Metadata.ExpressTypeId("IFCVOIDINGFEATURE"); toIgnore[3] = model.Metadata.ExpressTypeId("IFCSURFACEFEATURE"); } foreach (var geometry in lookup) { if (geometry.ShapeData.Length <= 0) //no geometry to display so don't write out any products for it { continue; } var instances = geomRead.ShapeInstancesOfGeometry(geometry.ShapeLabel); var xbimShapeInstances = instances.Where(si => !toIgnore.Contains(si.IfcTypeId) && si.RepresentationType == XbimGeometryRepresentationType .OpeningsAndAdditionsIncluded && prodIds.Contains(si.IfcProductLabel)).ToList(); if (!xbimShapeInstances.Any()) { continue; } numberOfGeometries++; binaryStream.Write(xbimShapeInstances.Count); //the number of repetitions of the geometry if (xbimShapeInstances.Count > 1) { foreach (IXbimShapeInstanceData xbimShapeInstance in xbimShapeInstances) //write out each of the ids style and transforms { binaryStream.Write(xbimShapeInstance.IfcProductLabel); binaryStream.Write((UInt16)xbimShapeInstance.IfcTypeId); binaryStream.Write((UInt32)xbimShapeInstance.InstanceLabel); binaryStream.Write((Int32)xbimShapeInstance.StyleLabel > 0 ? xbimShapeInstance.StyleLabel : xbimShapeInstance.IfcTypeId * -1); var transformation = Translate(XbimMatrix3D.FromArray(xbimShapeInstance.Transformation), translation); binaryStream.Write(transformation.ToArray()); numberOfTriangles += XbimShapeTriangulation.TriangleCount(((IXbimShapeGeometryData)geometry).ShapeData); numberOfMatrices++; } numberOfVertices += XbimShapeTriangulation.VerticesCount(((IXbimShapeGeometryData)geometry).ShapeData); // binaryStream.Write(geometry.ShapeData); var ms = new MemoryStream(((IXbimShapeGeometryData)geometry).ShapeData); var br = new BinaryReader(ms); var tr = br.ReadShapeTriangulation(); tr.Write(binaryStream); } else //now do the single instances { var xbimShapeInstance = xbimShapeInstances[0]; // IXbimShapeGeometryData geometry = ShapeGeometry(kv.Key); binaryStream.Write((Int32)xbimShapeInstance.IfcProductLabel); binaryStream.Write((UInt16)xbimShapeInstance.IfcTypeId); binaryStream.Write((Int32)xbimShapeInstance.InstanceLabel); binaryStream.Write((Int32)xbimShapeInstance.StyleLabel > 0 ? xbimShapeInstance.StyleLabel : xbimShapeInstance.IfcTypeId * -1); //Read all vertices and normals in the geometry stream and transform var ms = new MemoryStream(((IXbimShapeGeometryData)geometry).ShapeData); var br = new BinaryReader(ms); var tr = br.ReadShapeTriangulation(); var transformation = Translate(xbimShapeInstance.Transformation, translation); var trTransformed = tr.Transform(transformation); trTransformed.Write(binaryStream); numberOfTriangles += XbimShapeTriangulation.TriangleCount(((IXbimShapeGeometryData)geometry).ShapeData); numberOfVertices += XbimShapeTriangulation.VerticesCount(((IXbimShapeGeometryData)geometry).ShapeData); } } binaryStream.Seek(start, SeekOrigin.Begin); binaryStream.Write((Int32)numberOfGeometries); binaryStream.Write((Int32)numberOfVertices); binaryStream.Write((Int32)numberOfTriangles); binaryStream.Write((Int32)numberOfMatrices); binaryStream.Write((Int32)numberOfProducts); binaryStream.Seek(0, SeekOrigin.End); //go back to end // ReSharper restore RedundantCast } }
/// <summary> /// Get the space name holding the element /// </summary> /// <param name="el">IfcElement to get containing space for</param> /// <returns>Space name</returns> internal string GetSpaceHoldingElement(IfcElement el) { //see if we have space information, if not fill information list if (SpaceBoundingBoxInfo.Count == 0) { if (ifcSpaces == null) { ifcSpaces = Model.Instances.OfType <IfcSpace>().ToList(); } //get Geometry for spaces SpaceBoundingBoxInfo = Model.GetGeometryData(XbimGeometryType.BoundingBox) .Where(bb => bb.IfcTypeId == IfcMetaData.IfcTypeId(typeof(IfcSpace))) .Select(bb => new SpaceInfo { Rectangle = XbimRect3D.FromArray(bb.ShapeData), Matrix = XbimMatrix3D.FromArray(bb.DataArray2), Name = ifcSpaces.Where(sp => (sp.EntityLabel == bb.IfcProductLabel)).Select(sp => sp.Name.ToString()).FirstOrDefault() }).ToList(); } string spaceName = string.Empty; //only if we have any space information if (SpaceBoundingBoxInfo.Any()) { //find the IfcElement Bounding Box and To WCS Matrix XbimGeometryData elGeoData = Model.GetGeometryData(el, XbimGeometryType.BoundingBox).FirstOrDefault(); //check to see if we have any geometry within the file if (elGeoData == null) { return(string.Empty); //No geometry } XbimRect3D elBoundBox = XbimRect3D.FromArray(elGeoData.ShapeData); XbimMatrix3D elWorldMatrix = XbimMatrix3D.FromArray(elGeoData.DataArray2); //Get object space top and bottom points of the bounding box List <XbimPoint3D> elBoxPts = new List <XbimPoint3D>(); elBoxPts.Add(new XbimPoint3D(elBoundBox.X, elBoundBox.Y, elBoundBox.Z)); elBoxPts.Add(new XbimPoint3D(elBoundBox.X + elBoundBox.SizeX, elBoundBox.Y + elBoundBox.SizeY, elBoundBox.Z + elBoundBox.SizeZ)); elBoxPts.Add(elBoundBox.Centroid()); //convert points of the bounding box to WCS IEnumerable <XbimPoint3D> elBoxPtsWCS = elBoxPts.Select(pt => elWorldMatrix.Transform(pt)); //see if we hit any spaces spaceName = GetSpaceFromPoints(elBoxPtsWCS); //if we failed to get space on min points then use the remaining corner points if (string.IsNullOrEmpty(spaceName)) { XbimPoint3D elMinPt = elBoxPts[0]; XbimPoint3D elMaxPt = elBoxPts[1]; //elBoxPts.Clear(); //already tested points in list so clear them //Extra testing on remaining corner points on the top and bottom plains elBoxPts.Add(new XbimPoint3D(elMaxPt.X, elMaxPt.Y, elMinPt.Z)); elBoxPts.Add(new XbimPoint3D(elMaxPt.X, elMinPt.Y, elMinPt.Z)); elBoxPts.Add(new XbimPoint3D(elMinPt.X, elMaxPt.Y, elMinPt.Z)); elBoxPts.Add(new XbimPoint3D((elMaxPt.X - elMinPt.X) / 2.0, (elMaxPt.Y - elMinPt.Y) / 2.0, elMinPt.Z)); //centre face point elBoxPts.Add(new XbimPoint3D(elMinPt.X, elMinPt.Y, elMaxPt.Z)); elBoxPts.Add(new XbimPoint3D(elMaxPt.X, elMinPt.Y, elMaxPt.Z)); elBoxPts.Add(new XbimPoint3D(elMinPt.X, elMaxPt.Y, elMaxPt.Z)); elBoxPts.Add(new XbimPoint3D((elMaxPt.X - elMinPt.X) / 2.0, (elMaxPt.Y - elMinPt.Y) / 2.0, elMaxPt.Z)); //centre face point //convert points of the bounding box to WCS elBoxPtsWCS = elBoxPts.Select(pt => elWorldMatrix.Transform(pt)); //see if we hit any spaces spaceName = GetSpaceFromPoints(elBoxPtsWCS); } if (string.IsNullOrEmpty(spaceName)) { //Get tolerance size from element, 1% of smallest side size double tol = elBoundBox.SizeX * 0.001; if ((elBoundBox.SizeY * 0.001) < tol) { tol = elBoundBox.SizeY * 0.001; } if ((elBoundBox.SizeZ * 0.001) < tol) { tol = elBoundBox.SizeZ * 0.001; } if ((tol == 0.0) && //if tol 0.0 ((Context.WorkBookUnits.LengthUnit.Equals("meters", StringComparison.OrdinalIgnoreCase)) || (Context.WorkBookUnits.LengthUnit.Equals("metres", StringComparison.OrdinalIgnoreCase)) ) ) { tol = 0.001; } spaceName = GetSpaceFromClosestPoints(elBoxPtsWCS, tol); } } return(spaceName); }
/// <summary> /// read geomotry data from wexbim file /// </summary> /// <param name="fileName"></param> public static void ReadWexbimFile(string fileName) { Vector3 offsite; using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { using (var br = new BinaryReader(fs)) { var magicalNum = br.ReadInt32(); var version = br.ReadByte(); var shapeCount = br.ReadInt32(); var vertexCount = br.ReadInt32(); var triangleCount = br.ReadInt32(); var matrixCount = br.ReadInt32(); var productCount = br.ReadInt32(); var styleCount = br.ReadInt32(); var scale = br.ReadSingle(); var regionCount = br.ReadInt16(); //Region for (int i = 0; i < regionCount; i++) { var population = br.ReadInt32(); var centreX = br.ReadSingle(); centreX /= scale; var centreY = br.ReadSingle(); centreY /= scale; var centreZ = br.ReadSingle(); centreZ /= scale; var boundsBytes = br.ReadBytes(6 * sizeof(float)); var bounds = XbimRect3D.FromArray(boundsBytes); bounds.X /= scale; bounds.Y /= scale; bounds.Z /= scale; bounds.SizeX /= scale; bounds.SizeY /= scale; bounds.SizeZ /= scale; MyBimGeomorty.regions.Add(new MyBimRegion(population, centreX, centreY, centreZ, (float)bounds.SizeX, (float)bounds.SizeY, (float)bounds.SizeZ)); } offsite = MyBimGeomorty.regions[0].position; //texture for (int i = 0; i < styleCount; i++) { var styleId = br.ReadInt32(); var red = br.ReadSingle(); var green = br.ReadSingle(); var blue = br.ReadSingle(); var alpha = br.ReadSingle(); MyBimGeomorty.colors.Add(new MyBimColor(styleId, red, green, blue, alpha)); } //product for (int i = 0; i < productCount; i++) { var entityLabel = br.ReadInt32(); var typeId = br.ReadInt16(); var boxBytes = br.ReadBytes(6 * sizeof(float)); XbimRect3D bb = XbimRect3D.FromArray(boxBytes); //float x = (float)bb.X, y = (float)bb.Y, z = (float)bb.Z; //float sizeX = (float)bb.SizeX, sizeY = (float)bb.SizeY, sizeZ = (float)bb.SizeZ; MyBimGeomorty.products.Add(new MyBimProduct(entityLabel, typeId)); } //shape for (int i = 0; i < shapeCount; i++) { var shapeRepetition = br.ReadInt32(); MyBimShapeInstance si; if (shapeRepetition > 1) { List <MyBimShapeInstance> curShapeInstances = new List <MyBimShapeInstance>(); for (int j = 0; j < shapeRepetition; j++) { var ifcProductLabel = br.ReadInt32(); var ifcTypeId = br.ReadInt16(); var instanceLabel = br.ReadInt32(); var styleLabel = br.ReadInt32(); var transform = XbimMatrix3D.FromArray(br.ReadBytes(sizeof(double) * 16)); si = new MyBimShapeInstance(ifcProductLabel, ifcTypeId, instanceLabel, styleLabel, transform); //MyBimGeomorty.shapeInstances.Add(si); curShapeInstances.Add(si); var p = MyBimGeomorty.products.Find(product => product.entityLabel == ifcProductLabel); p.AddShapeInstance(si); } var triangulation = br.ReadShapeTriangulation(); foreach (var csi in curShapeInstances) { var tri = new MyBimTriangulation(triangulation, offsite, scale, csi.transform, true); csi.AddTriangulation(tri); //MyBimGeomorty.triangulations.Add(tri); } } else if (shapeRepetition == 1) { var ifcProductLabel = br.ReadInt32(); var ifcTypeId = br.ReadInt16(); var instanceLabel = br.ReadInt32(); var styleLabel = br.ReadInt32(); si = new MyBimShapeInstance(ifcProductLabel, ifcTypeId, instanceLabel, styleLabel); //MyBimGeomorty.shapeInstances.Add(si); var p = MyBimGeomorty.products.Find(product => product.entityLabel == ifcProductLabel); p.AddShapeInstance(si); XbimShapeTriangulation triangulation = br.ReadShapeTriangulation(); var tri = new MyBimTriangulation(triangulation, offsite, scale); //MyBimGeomorty.triangulations.Add(tri); si.AddTriangulation(tri); } } } } }
void Awake() { Material true_mat = new Material(Shader.Find("Standard")); GameObject parent = new GameObject("WexbimModel"); GameObject go; Mesh mesh; const string fileName = "IFCBuilding"; using (var fs = new FileStream(fileName + ".wexBIM", FileMode.Open, FileAccess.Read)) { using (var br = new BinaryReader(fs)) { var magicNumber = br.ReadInt32(); if (magicNumber != 94132117) { throw new ArgumentException("Magic number mismatch."); } // Assert.IsTrue(magicNumber == IfcStore.WexBimId); var version = br.ReadByte(); var shapeCount = br.ReadInt32(); var vertexCount = br.ReadInt32(); var triangleCount = br.ReadInt32(); var matrixCount = br.ReadInt32(); var productCount = br.ReadInt32(); var styleCount = br.ReadInt32(); var meter = br.ReadSingle(); // Assert.IsTrue(meter > 0); var regionCount = br.ReadInt16(); for (int i = 0; i < regionCount; i++) { var population = br.ReadInt32(); var centreX = br.ReadSingle(); var centreY = br.ReadSingle(); var centreZ = br.ReadSingle(); var boundsBytes = br.ReadBytes(6 * sizeof(float)); var modelBounds = XbimRect3D.FromArray(boundsBytes); } for (int i = 0; i < styleCount; i++) { var styleId = br.ReadInt32(); var red = br.ReadSingle(); var green = br.ReadSingle(); var blue = br.ReadSingle(); var alpha = br.ReadSingle(); true_mat.SetColor(styleId, new Color(red, green, blue, alpha)); } for (int i = 0; i < productCount; i++) { var productLabel = br.ReadInt32(); var productType = br.ReadInt16(); var boxBytes = br.ReadBytes(6 * sizeof(float)); XbimRect3D bb = XbimRect3D.FromArray(boxBytes); //is this the factor to magnify/demise an ifcproduct? // Debug.Log("product Label: " + productLabel + " ,productType: " + productType); } for (int i = 0; i < shapeCount; i++) { var shapeRepetition = br.ReadInt32(); // Assert.IsTrue(shapeRepetition > 0); if (shapeRepetition > 1) { GameObject[] repes = new GameObject[shapeRepetition]; int[] ColorStyleId = new int[shapeRepetition]; for (int j = 0; j < shapeRepetition; j++) { var ifcProductLabel = br.ReadInt32(); var instanceTypeId = br.ReadInt16(); var instanceLabel = br.ReadInt32(); var styleId = br.ReadInt32(); var transform = XbimMatrix3D.FromArray(br.ReadBytes(sizeof(double) * 16)); // Debug.Log("ifcProductLabel: " + ifcProductLabel+ " ,instanceTypeId: " + instanceTypeId + " ,instanceLabel: " + instanceLabel); go = new GameObject(ifcProductLabel.ToString()); go.transform.parent = parent.transform; repes[j] = go; //order of ro/po makes no diference go.transform.rotation = new Quaternion(Flo_convert(transform.GetRotationQuaternion().X), Flo_convert(transform.GetRotationQuaternion().Y), Flo_convert(transform.GetRotationQuaternion().Z), Flo_convert(transform.GetRotationQuaternion().W)); go.transform.position = new Vector3(Flo_convert(transform.Translation.X / meter), Flo_convert(transform.Translation.Y / meter), Flo_convert(transform.Translation.Z / meter)); ColorStyleId[j] = styleId; } var triangulation = br.ReadShapeTriangulation(); int TriangleIndexCount = 0; foreach (var face in triangulation.Faces) { foreach (var triIndex in face.Indices) { ++TriangleIndexCount; } } int VerticesCount = triangulation.Vertices.Count; int FaceCount = triangulation.Faces.Count; Vector3[] vertices = new Vector3[VerticesCount]; //these eventully write into unity gameobject int[] triangleIndices = new int[TriangleIndexCount]; int TriangleIndex = 0; for (int j = 0; j < VerticesCount; j++) { var vert = new Vector3(Flo_convert(triangulation.Vertices[j].X), Flo_convert(triangulation.Vertices[j].Y), Flo_convert(triangulation.Vertices[j].Z)); vertices[j] = vert / meter; } for (int j = 0; j < FaceCount; j++) { for (int k = 0; k < triangulation.Faces[j].Indices.Count; k++) { triangleIndices[TriangleIndex++] = triangulation.Faces[j].Indices[k]; } } foreach (var repete in repes) { int id = 0; repete.AddComponent <MeshFilter>(); repete.AddComponent <MeshRenderer>(); mesh = new Mesh(); repete.GetComponent <MeshFilter>().mesh = mesh; MeshRenderer meshRenderer = repete.GetComponent <MeshRenderer>(); meshRenderer.material.color = true_mat.GetColor(ColorStyleId[id++]); mesh.vertices = vertices;// in unity vertices must defined before triangles mesh.triangles = triangleIndices; mesh.RecalculateNormals(); repete.AddComponent <MeshCollider>(); } // Assert.IsTrue(triangulation.Vertices.Count > 0, "Number of vertices should be greater than zero"); } else if (shapeRepetition == 1) { var ifcProductLabel = br.ReadInt32(); var instanceTypeId = br.ReadInt16(); var instanceLabel = br.ReadInt32(); var styleId = br.ReadInt32(); XbimShapeTriangulation triangulation = br.ReadShapeTriangulation(); // Assert.IsTrue(triangulation.Vertices.Count > 0, "Number of vertices should be greater than zero"); //Debug.Log("ifcProductLabel: " + ifcProductLabel + " ,instanceTypeId: " + instanceTypeId + " ,instanceLabel: " + instanceLabel); go = new GameObject(ifcProductLabel.ToString()); go.transform.parent = parent.transform; mesh = new Mesh(); go.AddComponent <MeshFilter>(); go.AddComponent <MeshRenderer>(); go.GetComponent <MeshFilter>().mesh = mesh; MeshRenderer meshRenderer = go.GetComponent <MeshRenderer>(); meshRenderer.material.color = true_mat.GetColor(styleId); int TriangleIndexCount = 0; int TriangleIndex = 0; foreach (var face in triangulation.Faces) { foreach (var triIndex in face.Indices) { ++TriangleIndexCount; } } int VerticesCount = triangulation.Vertices.Count; int FaceCount = triangulation.Faces.Count; Vector3[] vertices = new Vector3[VerticesCount]; //these eventully write into unity gameobject int[] triangleIndices = new int[TriangleIndexCount]; for (int j = 0; j < VerticesCount; j++) { vertices[j] = new Vector3(Flo_convert(triangulation.Vertices[j].X), Flo_convert(triangulation.Vertices[j].Y), Flo_convert(triangulation.Vertices[j].Z)); vertices[j] /= meter; } mesh.vertices = vertices; for (int j = 0; j < FaceCount; j++) { for (int k = 0; k < triangulation.Faces[j].Indices.Count; k++) { triangleIndices[TriangleIndex++] = triangulation.Faces[j].Indices[k]; } } mesh.triangles = triangleIndices; mesh.RecalculateNormals(); go.AddComponent <MeshCollider>(); } } } } Debug.Log("finish start meathod"); }
public static void ReadWexbim(string fileName) { Vector3 offset; using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { using (var br = new BinaryReader(fs)) { var magicalNum = br.ReadInt32(); var version = br.ReadByte(); var shapeCount = br.ReadInt32(); var vertexCount = br.ReadInt32(); var triangleCount = br.ReadInt32(); var matrixCount = br.ReadInt32(); var productCount = br.ReadInt32(); var styleCount = br.ReadInt32(); var meter = br.ReadSingle(); var regionCount = br.ReadInt16(); for (int i = 0; i < regionCount; i++) { var population = br.ReadInt32(); var centreX = br.ReadSingle(); centreX /= meter; var centreY = br.ReadSingle(); centreY /= meter; var centreZ = br.ReadSingle(); centreZ /= meter; var boundsBytes = br.ReadBytes(6 * sizeof(float)); var modelBounds = XbimRect3D.FromArray(boundsBytes); modelBounds.X /= meter; modelBounds.Y /= meter; modelBounds.Z /= meter; modelBounds.SizeX /= meter; modelBounds.SizeY /= meter; modelBounds.SizeZ /= meter; BimGeomorty.regions.Add(new BimRegion(population, centreX, centreY, centreZ, (float)modelBounds.SizeX, (float)modelBounds.SizeY, (float)modelBounds.SizeZ)); } //中心偏移 offset = BimGeomorty.regions[0].position; for (int i = 0; i < styleCount; i++) { var styleId = br.ReadInt32(); var red = br.ReadSingle(); var green = br.ReadSingle(); var blue = br.ReadSingle(); var alpha = br.ReadSingle(); BimGeomorty.styles.Add(new BimStyle(styleId, red, green, blue, alpha)); } for (int i = 0; i < productCount; i++) { var productLabel = br.ReadInt32(); var productType = br.ReadInt16(); var boxBytes = br.ReadBytes(6 * sizeof(float)); XbimRect3D bb = XbimRect3D.FromArray(boxBytes); BimGeomorty.products.Add(new BimProduct(productLabel, productType)); } for (int i = 0; i < shapeCount; i++) { var shapeRepetition = br.ReadInt32(); BimShapeInstance shapeInstance; if (shapeRepetition > 1) { List <BimShapeInstance> tempInstances = new List <BimShapeInstance>(); for (int j = 0; j < shapeRepetition; j++) { var ifcProductLabel = br.ReadInt32(); var ifcTypeId = br.ReadInt16(); var instanceLabel = br.ReadInt32(); var styleLabel = br.ReadInt32(); var transform = XbimMatrix3D.FromArray(br.ReadBytes(sizeof(double) * 16)); shapeInstance = new BimShapeInstance(ifcProductLabel, ifcTypeId, instanceLabel, styleLabel, transform); BimGeomorty.shapeInstances.Add(shapeInstance); tempInstances.Add(shapeInstance); var p = BimGeomorty.products.Find(product => product.productLabel == ifcProductLabel); p.AddShapeInstance(shapeInstance); } var triangulation = br.ReadShapeTriangulation(); foreach (var temp in tempInstances) { var tri = new BimTriangulation(triangulation, offset, meter, temp.xbimMatrix, true); temp.AddTriangulation(tri); BimGeomorty.triangulations.Add(tri); } } else if (shapeRepetition == 1) { var ifcProductLabel = br.ReadInt32(); var ifcTypeId = br.ReadInt16(); var instanceLabel = br.ReadInt32(); var styleLabel = br.ReadInt32(); shapeInstance = new BimShapeInstance(ifcProductLabel, ifcTypeId, instanceLabel, styleLabel); BimGeomorty.shapeInstances.Add(shapeInstance); var p = BimGeomorty.products.Find(product => product.productLabel == ifcProductLabel); p.AddShapeInstance(shapeInstance); XbimShapeTriangulation triangulation = br.ReadShapeTriangulation(); var tri = new BimTriangulation(triangulation, offset, meter); BimGeomorty.triangulations.Add(tri); shapeInstance.AddTriangulation(tri); } } } } }
void Start() { //Mesh mesh = new Mesh(); //List<> string path = @"E:\Unity\BIMForUnity\Assets\test1.wexbim"; using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read)) { using (var br = new BinaryReader(fs)) { var magicNumber = br.ReadInt32(); var version = br.ReadByte(); var shapeCount = br.ReadInt32(); var vertexCount = br.ReadInt32(); var triangleCount = br.ReadInt32(); var matrixCount = br.ReadInt32(); var productCount = br.ReadInt32(); var styleCount = br.ReadInt32(); var meter = br.ReadSingle(); scale = meter; var regionCount = br.ReadInt16(); for (int i = 0; i < regionCount; i++) { var population = br.ReadInt32(); var centreX = br.ReadSingle(); var centreY = br.ReadSingle(); var centreZ = br.ReadSingle(); var boundsBytes = br.ReadBytes(6 * sizeof(float)); var modelBounds = XbimRect3D.FromArray(boundsBytes); } for (int i = 0; i < styleCount; i++) { var styleId = br.ReadInt32(); var red = br.ReadSingle(); var green = br.ReadSingle(); var blue = br.ReadSingle(); var alpha = br.ReadSingle(); styleDictionary.Add(styleId, new Vector4(red, green, blue, alpha)); } for (int i = 0; i < productCount; i++) { var productLabel = br.ReadInt32(); var productType = br.ReadInt16(); var boxBytes = br.ReadBytes(6 * sizeof(float)); XbimRect3D bb = XbimRect3D.FromArray(boxBytes); GameObject go = new GameObject(productLabel.ToString()); go.transform.SetParent(GameObject.Find("Model").transform); //储存productLabel productDictionary.Add(productLabel, go); } for (int i = 0; i < shapeCount; i++) { var shapeRepetition = br.ReadInt32(); // Assert.IsTrue(shapeRepetition > 0); if (shapeRepetition > 1) { Material material = new Material(Shader.Find("CrossSection/OnePlaneBSP")); Vector4 color = new Vector4(); List <int> ifcProductLabels = new List <int>(); List <int> styleIds = new List <int>(); List <XbimMatrix3D> transforms = new List <XbimMatrix3D>(); for (int j = 0; j < shapeRepetition; j++) { var ifcProductLabel = br.ReadInt32(); var instanceTypeId = br.ReadInt16(); var instanceLabel = br.ReadInt32(); var styleId = br.ReadInt32(); var transform = XbimMatrix3D.FromArray(br.ReadBytes(sizeof(double) * 16)); styleDictionary.TryGetValue(styleId, out color); ifcProductLabels.Add(ifcProductLabel); styleIds.Add(styleId); transforms.Add(transform); //Debug.Log(transform.ToString()); } var triangulation = br.ReadShapeTriangulation(); //// Assert.IsTrue(triangulation.Vertices.Count > 0, "Number of vertices should be greater than zero"); for (int j = 0; j < shapeRepetition; j++) { GetModel(triangulation.Transform(transforms[j]), styleIds[j], ifcProductLabels[j]); } } else if (shapeRepetition == 1) { var ifcProductLabel = br.ReadInt32(); var instanceTypeId = br.ReadInt16(); var instanceLabel = br.ReadInt32(); var styleId = br.ReadInt32(); XbimShapeTriangulation triangulation = br.ReadShapeTriangulation(); // Assert.IsTrue(triangulation.Vertices.Count > 0, "Number of vertices should be greater than zero"); //--------------------------------------------- //GetModel(triangulation, styleId, ifcProductLabel); } } } GameObject.Find("Model").transform.eulerAngles = new Vector3(-90, 0, 0); } }