public IXbimMeshGeometry3D GetMeshGeometry3D(XbimMeshFragment frag) { var m3D = new XbimMeshGeometry3D(); var m = Mesh; if (m != null) { for (int i = frag.StartPosition; i <= frag.EndPosition; i++) { Point3D p = m.Positions[i]; Vector3D v = m.Normals[i]; m3D.Positions.Add(new XbimPoint3D(p.X, p.Y, p.Z)); m3D.Normals.Add(new XbimVector3D(v.X, v.Y, v.Z)); } for (int i = frag.StartTriangleIndex; i <= frag.EndTriangleIndex; i++) { m3D.TriangleIndices.Add(m.TriangleIndices[i] - frag.StartPosition); } m3D.Meshes.Add(new XbimMeshFragment(0, 0, 0) { EndPosition = m3D.PositionCount - 1, StartTriangleIndex = frag.StartTriangleIndex - m3D.PositionCount - 1, EndTriangleIndex = frag.EndTriangleIndex - m3D.PositionCount - 1 }); } return(m3D); }
public static void AddElements(this MeshGeometry3D m, IPersistIfcEntity item, XbimMatrix3D wcsTransform) { var fromModel = item.ModelOf as XbimModel; if (fromModel == null || !(item is IfcProduct)) { return; } switch (fromModel.GeometrySupportLevel) { case 2: var context = new Xbim3DModelContext(fromModel); var productShape = context.ShapeInstancesOf((IfcProduct)item) .Where(s => s.RepresentationType != XbimGeometryRepresentationType.OpeningsAndAdditionsExcluded) .ToList(); if (!productShape.Any() && item is IfcFeatureElement) { productShape = context.ShapeInstancesOf((IfcProduct)item) .Where( s => s.RepresentationType == XbimGeometryRepresentationType.OpeningsAndAdditionsExcluded) .ToList(); } if (!productShape.Any()) { return; } foreach (var shapeInstance in productShape) { IXbimShapeGeometryData shapeGeom = context.ShapeGeometry(shapeInstance.ShapeGeometryLabel); switch ((XbimGeometryType)shapeGeom.Format) { case XbimGeometryType.PolyhedronBinary: m.Read(shapeGeom.ShapeData, XbimMatrix3D.Multiply(shapeInstance.Transformation, wcsTransform)); break; case XbimGeometryType.Polyhedron: m.Read(((XbimShapeGeometry)shapeGeom).ShapeData, XbimMatrix3D.Multiply(shapeInstance.Transformation, wcsTransform)); break; } } break; case 1: var xm3d = new XbimMeshGeometry3D(); var geomDataSet = fromModel.GetGeometryData(item.EntityLabel, XbimGeometryType.TriangulatedMesh); foreach (var geomData in geomDataSet) { var gd = geomData.TransformBy(wcsTransform); xm3d.Add(gd); } m.Add(xm3d); break; } }
public static XbimMeshGeometry3D GetMesh(this XbimModel xbimModel, IEnumerable<IPersistIfcEntity> items, XbimMatrix3D wcsTransform) { var m = new XbimMeshGeometry3D(); if (xbimModel.GeometrySupportLevel == 1) { // this is what happens for version 1 of the engine // foreach (var item in items) { var fromModel = item.ModelOf as XbimModel; if (fromModel == null) continue; var geomDataSet = fromModel.GetGeometryData(item.EntityLabel, XbimGeometryType.TriangulatedMesh); foreach (var geomData in geomDataSet) { // todo: add guidance to the TransformBy method so that users can understand how to stop using it (it's marked absolete) geomData.TransformBy(wcsTransform); m.Add(geomData); // todo: what is the modelid value to be passed? } } } else { // this is what happens for version 2 of the engine // foreach (var item in items) { var fromModel = item.ModelOf as XbimModel; if (fromModel == null || !(item is IfcProduct)) continue; var context = new Xbim3DModelContext(fromModel); var productShape = context.ShapeInstancesOf((IfcProduct) item) .Where( s => s.RepresentationType != XbimGeometryRepresentationType.OpeningsAndAdditionsExcluded) .ToList(); foreach (var shapeInstance in productShape) { IXbimShapeGeometryData shapeGeom = context.ShapeGeometry(shapeInstance.ShapeGeometryLabel); switch ((XbimGeometryType) shapeGeom.Format) { case XbimGeometryType.PolyhedronBinary: m.Read(shapeGeom.ShapeData, XbimMatrix3D.Multiply(shapeInstance.Transformation, wcsTransform)); break; case XbimGeometryType.Polyhedron: m.Read(((XbimShapeGeometry) shapeGeom).ShapeData, XbimMatrix3D.Multiply(shapeInstance.Transformation, wcsTransform)); break; } } } } return m; }
/// <summary> /// Gets the geometry of an entity building it up from layers. /// </summary> /// <param name="entity">The entity instance</param> /// <param name="modelId"></param> public IXbimMeshGeometry3D GetMeshGeometry3D(IPersistEntity entity, short modelId) { var geometry = new XbimMeshGeometry3D(); var m = entity.Model; foreach (var layer in Layers) { // an entity model could be spread across many layers (e.g. in case of different materials) if (Equals(layer.Model, m)) { geometry.Add(layer.GetVisibleMeshGeometry3D(entity.EntityLabel, modelId)); } } return(geometry); }
public static void Add(this MeshGeometry3D m3D, XbimMeshGeometry3D addedGeometry3D) { m3D.TriangleIndices = Extensions.Utility.GeomUtils.CombineIndexCollection(m3D.TriangleIndices, addedGeometry3D.TriangleIndices, m3D.Positions.Count); m3D.Positions = Extensions.Utility.GeomUtils.CombinePointCollection(m3D.Positions, addedGeometry3D.Positions); m3D.Normals = Extensions.Utility.GeomUtils.CombineVectorCollection(m3D.Normals, addedGeometry3D.Normals); }
public static void Read(this XbimMeshGeometry3D m3D, byte[] mesh, XbimMatrix3D?transform = null) { var indexBase = m3D.Positions.Count; var qrd = new XbimQuaternion(); XbimMatrix3D?matrix3D = null; if (transform.HasValue) { qrd = transform.Value.GetRotationQuaternion(); matrix3D = transform.Value; } using (var ms = new MemoryStream(mesh)) { using (var br = new BinaryReader(ms)) { // ReSharper disable once UnusedVariable var version = br.ReadByte(); //stream format version var numVertices = br.ReadInt32(); var numTriangles = br.ReadInt32(); var uniqueVertices = new List <XbimPoint3D>(numVertices); var vertices = new List <XbimPoint3D>(numVertices * 4); //approx the size var triangleIndices = new List <int>(numTriangles * 3); var normals = new List <XbimVector3D>(numVertices * 4); for (var i = 0; i < numVertices; i++) { double x = br.ReadSingle(); double y = br.ReadSingle(); double z = br.ReadSingle(); var p = new XbimPoint3D(x, y, z); if (matrix3D.HasValue) { p = matrix3D.Value.Transform(p); } uniqueVertices.Add(p); } var numFaces = br.ReadInt32(); for (var i = 0; i < numFaces; i++) { var numTrianglesInFace = br.ReadInt32(); if (numTrianglesInFace == 0) { continue; } var isPlanar = numTrianglesInFace > 0; numTrianglesInFace = Math.Abs(numTrianglesInFace); if (isPlanar) { var normal = br.ReadPackedNormal().Normal; if (!qrd.IsIdentity()) { var baseVal = new XbimVector3D(normal.X, normal.Y, normal.Z); XbimQuaternion.Transform(ref baseVal, ref qrd, out normal); } var uniqueIndices = new Dictionary <int, int>(); for (var j = 0; j < numTrianglesInFace; j++) { for (var k = 0; k < 3; k++) { var idx = ReadIndex(br, numVertices); int writtenIdx; if (!uniqueIndices.TryGetValue(idx, out writtenIdx)) //we haven't got it, so add it { writtenIdx = vertices.Count; vertices.Add(uniqueVertices[idx]); uniqueIndices.Add(idx, writtenIdx); //add a matching normal normals.Add(normal); } triangleIndices.Add(indexBase + writtenIdx); } } } else { var uniqueIndices = new Dictionary <int, int>(); for (var j = 0; j < numTrianglesInFace; j++) { for (var k = 0; k < 3; k++) { var idx = ReadIndex(br, numVertices); var normal = br.ReadPackedNormal().Normal; int writtenIdx; if (!uniqueIndices.TryGetValue(idx, out writtenIdx)) //we haven't got it, so add it { writtenIdx = vertices.Count; vertices.Add(uniqueVertices[idx]); uniqueIndices.Add(idx, writtenIdx); if (!qrd.IsIdentity()) { var baseVal = new XbimVector3D(normal.X, normal.Y, normal.Z); XbimQuaternion.Transform(ref baseVal, ref qrd, out normal); } normals.Add(normal); } triangleIndices.Add(indexBase + writtenIdx); } } } } m3D.Positions = m3D.Positions.Concat(vertices).ToList(); m3D.TriangleIndices = m3D.TriangleIndices.Concat(triangleIndices).ToList(); m3D.Normals = m3D.Normals.Concat(normals).ToList(); } } }
public static void UpdateElementTransform(IfcStore _model, string projectNumber, string projectName) { DBOperation.beginTransaction(); DBOperation.commitInterval = 5000; string currStep = string.Empty; BIMRLCommon _refBIMRLCommon = new BIMRLCommon(); int commandStatus = -1; int currInsertCount = 0; OracleCommand command = new OracleCommand(" ", DBOperation.DBConn); XbimMatrix3D m3D = new XbimMatrix3D(); if (string.IsNullOrEmpty(projectName)) { projectName = projectNumber + " - Federated"; } string modelName; if (!string.IsNullOrEmpty(_model.FileName)) { modelName = Path.GetFileNameWithoutExtension(_model.FileName); } else { modelName = projectNumber + " - " + projectName; } command.CommandText = "SELECT FEDERATEDID FROM bimrl_federatedmodel WHERE MODELNAME = '" + modelName + "' AND PROJECTNUMBER='" + projectNumber + "' AND PROJECTNAME='" + projectName + "'"; object oFedID = command.ExecuteScalar(); if (oFedID == null) { return; } int fedID = int.Parse(oFedID.ToString()); command.CommandText = "SELECT ELEMENTID, LINENO FROM " + DBOperation.formatTabName("bimrl_element", fedID) + " WHERE GEOMETRYBODY IS NOT NULL"; OracleDataReader reader = command.ExecuteReader(); SortedDictionary <int, string> elemList = new SortedDictionary <int, string>(); while (reader.Read()) { string elemid = reader.GetString(0); int lineNo = reader.GetInt32(1); elemList.Add(lineNo, elemid); } reader.Close(); Xbim3DModelContext context = new Xbim3DModelContext(_model); foreach (KeyValuePair <int, string> elemListItem in elemList) { //IEnumerable<XbimGeometryData> geomDataList = _model.GetGeometryData(elemListItem.Key, XbimGeometryType.TriangulatedMesh); IIfcProduct product = _model.Instances[elemListItem.Key] as IIfcProduct; IEnumerable <XbimShapeInstance> shapeInstances = context.ShapeInstancesOf(product).Where(x => x.RepresentationType == XbimGeometryRepresentationType.OpeningsAndAdditionsIncluded); if (shapeInstances.Count() == 0) { continue; // SKip if the product has no geometry } XbimMeshGeometry3D prodGeom = new XbimMeshGeometry3D(); IXbimShapeGeometryData shapeGeom = context.ShapeGeometry(shapeInstances.FirstOrDefault().ShapeGeometryLabel); //XbimModelExtensions.Read(prodGeom, shapeGeom.ShapeData, shapeInstances.FirstOrDefault().Transformation); //XbimGeometryData sdoGeomData = geomDataList.First(); //m3D = sdoGeomData.Transform; //m3D = XbimMatrix3D.FromArray(sdoGeomData.DataArray2); // Xbim 3.0 removes Tranform property m3D = shapeInstances.FirstOrDefault().Transformation; string sqlStmt = "update " + DBOperation.formatTabName("BIMRL_ELEMENT", fedID) + " set TRANSFORM_COL1=:1, TRANSFORM_COL2=:2, TRANSFORM_COL3=:3, TRANSFORM_COL4=:4" + " Where elementid = '" + elemListItem.Value + "'"; // int status = DBOperation.updateGeometry(sqlStmt, sdoGeomData); currStep = sqlStmt; command.CommandText = sqlStmt; try { OracleParameter[] sdoGeom = new OracleParameter[4]; for (int i = 0; i < sdoGeom.Count(); ++i) { sdoGeom[i] = command.Parameters.Add((i + 1).ToString(), OracleDbType.Object); sdoGeom[i].Direction = ParameterDirection.Input; sdoGeom[i].UdtTypeName = "MDSYS.SDO_GEOMETRY"; sdoGeom[i].Size = 1; } SdoGeometry trcol1 = new SdoGeometry(); trcol1.Dimensionality = 3; trcol1.LRS = 0; trcol1.GeometryType = (int)SdoGeometryTypes.GTYPE.POINT; int gType = trcol1.PropertiesToGTYPE(); SdoPoint trcol1V = new SdoPoint(); trcol1V.XD = m3D.M11; trcol1V.YD = m3D.M12; trcol1V.ZD = m3D.M13; trcol1.SdoPoint = trcol1V; sdoGeom[1].Value = trcol1; SdoGeometry trcol2 = new SdoGeometry(); trcol2.Dimensionality = 3; trcol2.LRS = 0; trcol2.GeometryType = (int)SdoGeometryTypes.GTYPE.POINT; gType = trcol2.PropertiesToGTYPE(); SdoPoint trcol2V = new SdoPoint(); trcol2V.XD = m3D.M21; trcol2V.YD = m3D.M22; trcol2V.ZD = m3D.M23; trcol2.SdoPoint = trcol2V; sdoGeom[2].Value = trcol2; SdoGeometry trcol3 = new SdoGeometry(); trcol3.Dimensionality = 3; trcol3.LRS = 0; trcol3.GeometryType = (int)SdoGeometryTypes.GTYPE.POINT; gType = trcol3.PropertiesToGTYPE(); SdoPoint trcol3V = new SdoPoint(); trcol3V.XD = m3D.M31; trcol3V.YD = m3D.M32; trcol3V.ZD = m3D.M33; trcol3.SdoPoint = trcol3V; sdoGeom[3].Value = trcol3; SdoGeometry trcol4 = new SdoGeometry(); trcol4.Dimensionality = 3; trcol4.LRS = 0; trcol4.GeometryType = (int)SdoGeometryTypes.GTYPE.POINT; gType = trcol4.PropertiesToGTYPE(); SdoPoint trcol4V = new SdoPoint(); trcol4V.XD = m3D.OffsetX; trcol4V.YD = m3D.OffsetY; trcol4V.ZD = m3D.OffsetZ; trcol4.SdoPoint = trcol4V; sdoGeom[4].Value = trcol4; commandStatus = command.ExecuteNonQuery(); command.Parameters.Clear(); currInsertCount++; if (currInsertCount % DBOperation.commitInterval == 0) { //Do commit at interval but keep the long transaction (reopen) DBOperation.commitTransaction(); } } catch (OracleException e) { string excStr = "%%Error - " + e.Message + "\n\t" + currStep; _refBIMRLCommon.StackPushError(excStr); //command.Dispose(); // Log Oracle error and continue command = new OracleCommand(" ", DBOperation.DBConn); // throw; } catch (SystemException e) { string excStr = "%%Insert Error - " + e.Message + "\n\t" + currStep; _refBIMRLCommon.StackPushError(excStr); throw; } DBOperation.commitTransaction(); command.Dispose(); } }
public static XbimMeshGeometry3D GetMesh(this XbimModel xbimModel, IEnumerable <IPersistIfcEntity> items, XbimMatrix3D wcsTransform) { var m = new XbimMeshGeometry3D(); if (xbimModel.GeometrySupportLevel == 1) { // this is what happens for version 1 of the engine // foreach (var item in items) { var fromModel = item.ModelOf as XbimModel; if (fromModel == null) { continue; } var geomDataSet = fromModel.GetGeometryData(item.EntityLabel, XbimGeometryType.TriangulatedMesh); foreach (var geomData in geomDataSet) { // todo: add guidance to the TransformBy method so that users can understand how to stop using it (it's marked absolete) geomData.TransformBy(wcsTransform); m.Add(geomData, xbimModel.UserDefinedId); } } } else { // this is what happens for version 2 of the engine // foreach (var item in items) { var fromModel = item.ModelOf as XbimModel; if (fromModel == null || !(item is IfcProduct)) { continue; } var context = new Xbim3DModelContext(fromModel); var productShape = context.ShapeInstancesOf((IfcProduct)item) .Where(s => s.RepresentationType != XbimGeometryRepresentationType.OpeningsAndAdditionsExcluded) .ToList(); // this also returns shapes of voids foreach (var shapeInstance in productShape) { IXbimShapeGeometryData shapeGeom = context.ShapeGeometry(shapeInstance.ShapeGeometryLabel); switch ((XbimGeometryType)shapeGeom.Format) { case XbimGeometryType.PolyhedronBinary: m.Read(shapeGeom.ShapeData, XbimMatrix3D.Multiply(shapeInstance.Transformation, wcsTransform)); break; case XbimGeometryType.Polyhedron: m.Read(((XbimShapeGeometry)shapeGeom).ShapeData, XbimMatrix3D.Multiply(shapeInstance.Transformation, wcsTransform)); break; } } } } return(m); }