/// <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 static string GeomInfoBoundBox(XbimModel model, int iEntLabel) { XbimGeometryData geomdata = model.GetGeometryData(iEntLabel, XbimGeometryType.BoundingBox).FirstOrDefault(); if (geomdata == null) { return("<not found>"); } XbimRect3D r3d = XbimRect3D.FromArray(geomdata.ShapeData); return(string.Format("Bounding box (position, size): {0}", r3d.ToString())); }
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); }
/// <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); }
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 void Run() { //to start we need an ifc file, here it is Clinic_Example.ifc string ifcFile = @"IfcFiles/Clinic_Example.ifc"; string xbimFile = Path.ChangeExtension(ifcFile, "xBIM"); //will generate if not existing if (File.Exists(ifcFile)) { using (XbimModel model = new XbimModel()) { if (File.Exists(xbimFile)) { //assume the xbim file has the geometry already generated from ifc file, as below model.Open(xbimFile, XbimDBAccess.Read); } else { //create the xbim file from the ifc file model.CreateFrom(ifcFile, xbimFile, delegate(int percentProgress, object userState) { Console.Write("\rReading File {0}", percentProgress); }); model.Open(xbimFile, XbimDBAccess.ReadWrite); //readwrite as we need to add the geometry //add the the geometry information to the model int total = (int)model.Instances.CountOf <IfcProduct>(); ReportProgressDelegate progDelegate = delegate(int percentProgress, object userState) { Console.Write("\rGeometry {0} / {1}", total, (total * percentProgress / 100)); }; XbimMesher.GenerateGeometry(model, null, progDelegate); } //get all the IfcDoors in the model IEnumerable <IfcDoor> ifcDoors = model.IfcProducts.OfType <IfcDoor>(); //get all the ifcdoors for this model if (ifcDoors.Any()) { IfcDoor ifcDoor = ifcDoors.First(); //we use the first door to get the bounding box from XbimGeometryData geoData = model.GetGeometryData(ifcDoor, XbimGeometryType.BoundingBox).FirstOrDefault(); if (geoData != null) { XbimRect3D boundBox = XbimRect3D.FromArray(geoData.ShapeData); //size information for the IfcDoor, but the information is for the bounding box which encloses the door //if want want in World space XbimMatrix3D worldMatrix = geoData.Transform; //if we want to convert to World space we can use the geoData.Transform property and create the world matrix XbimPoint3D MinPtOCS = new XbimPoint3D(boundBox.X, boundBox.Y, boundBox.Z); XbimPoint3D MaxPtOCS = new XbimPoint3D(boundBox.X + boundBox.SizeX, boundBox.Y + boundBox.SizeY, boundBox.Z + boundBox.SizeZ); //transformed values, may no longer a valid bounding box in the new space if any Pitch or Yaw, i.e. stairs ceiling supports XbimPoint3D MinPtWCS = worldMatrix.Transform(MinPtOCS); XbimPoint3D MaxPtWCS = worldMatrix.Transform(MaxPtOCS); //if you product is at any angle to the World space then the bounding box can be recalculated, //a example of this can be found here https://sbpweb.svn.codeplex.com/svn/SBPweb.Workbench/Workbench%20Framework%202.0.0.x/Presentation/Windows.WPF/Utils/Maths.cs //in the TransformBounds function Console.WriteLine("\n-------------Bounding Box Information-------------"); Console.WriteLine("Entity Type = {0}", IfcMetaData.GetType(geoData.IfcTypeId).Name); Console.WriteLine("Entity Label = {0}", Math.Abs(ifcDoor.EntityLabel).ToString()); Console.WriteLine("Size X = {0:F2}", boundBox.SizeX.ToString()); Console.WriteLine("Size Y = {0:F2}", boundBox.SizeY.ToString()); Console.WriteLine("Size Z = {0:F2}", boundBox.SizeZ.ToString()); Console.WriteLine("Object space minimum point {0}", MinPtOCS); Console.WriteLine("Object space maximum point {0}", MaxPtOCS); Console.WriteLine("World space minimum point {0}", MinPtWCS); Console.WriteLine("World space maximum point {0}", MaxPtWCS); Console.WriteLine("---------------------------------------------"); } } else { Console.WriteLine(string.Format("Failed to find any IfcDoor's in {0}", ifcFile)); return; //exit } } } else { Console.WriteLine(string.Format("Failed to find {0} in executable directory", ifcFile)); } Console.WriteLine("\nFinished"); }
/// <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); } }