Exemple #1
0
        internal void AddShape(IGeometryStoreReader geomReader, XbimShapeInstance shapeInstance, CoordinatesMode mode)
        {
            // XbimMatrix3D modelTransform = XbimMatrix3D.Identity;
            IXbimShapeGeometryData shapeGeom = geomReader.ShapeGeometry(shapeInstance.ShapeGeometryLabel);

            if (shapeGeom.Format != (byte)XbimGeometryType.PolyhedronBinary)
            {
                return;
            }
            // var transform = XbimMatrix3D.Multiply(, modelTransform);
            if (mode == CoordinatesMode.IncludeShapeTransform)
            {
                AddMesh(shapeGeom.ShapeData, shapeInstance.Transformation);
            }
            else
            {
                AddMesh(shapeGeom.ShapeData);
            }
        }
Exemple #2
0
        /// <summary>
        /// 获取几何数据
        /// </summary>
        /// <param name="model"></param>
        /// <param name="shapeInstances"></param>
        /// <param name="geomReader"></param>
        /// <param name="materialsByStyleId"></param>
        public static void GetIFCPointFaceMaterial(IModel model, IEnumerable <XbimShapeInstance> shapeInstances, IGeometryStoreReader geomReader, Dictionary <int, WpfMaterial> materialsByStyleId)
        {
            if (shapeInstances != null)
            {
                int index = 0;
                //string jsonString = "";
                StringBuilder jsonString = new StringBuilder();

                foreach (var shapeInstance in shapeInstances)
                {
                    //string faces_json = "";
                    StringBuilder faces_json = new StringBuilder();
                    string        point_json = "";

                    //globalId
                    var         productId = shapeInstance.IfcProductLabel;
                    IIfcProduct product   = model.Instances[productId] as IIfcProduct;
                    var         globalId  = product.GlobalId;


                    //获取该实例的材质信息
                    var styleId = shapeInstance.StyleLabel > 0
                        ? shapeInstance.StyleLabel
                        : shapeInstance.IfcTypeId * -1;

                    var materialsByStyle = materialsByStyleId[styleId];
                    //var isTransparent = materialsByStyle.IsTransparent.ToString().ToLower();//是否透明
                    var materialsItem = materialsByStyle.Description.Split(' ');//材质描述

                    string materialString = "";
                    for (int i = 0; i < materialsItem.Length; i++)
                    {
                        if (i == 1 || i == 2 || i == 3 || i == 4)
                        {
                            materialString += ",\"" + materialsItem[i].Replace(":", "\":") + "";
                        }
                    }

                    if (materialString.Length > 0)
                    {
                        materialString = materialString.Substring(1).ToString();
                    }

                    //求单个模型4向矩阵信息
                    var    transfor  = shapeInstance.Transformation;
                    string matrixA   = transfor.M11.ToString() + "," + transfor.M12.ToString() + "," + transfor.M13.ToString() + "," + transfor.M14.ToString() + ",";
                    string matrixB   = transfor.M21.ToString() + "," + transfor.M22.ToString() + "," + transfor.M23.ToString() + "," + transfor.M24.ToString() + ",";
                    string matrixC   = transfor.M31.ToString() + "," + transfor.M32.ToString() + "," + transfor.M33.ToString() + "," + transfor.M34.ToString() + ",";
                    string matrixXYZ = transfor.OffsetX.ToString() + "," + transfor.OffsetY.ToString() + "," + transfor.OffsetZ.ToString() + ",";
                    string matrixD   = transfor.M44.ToString();

                    //拼接4向矩阵数据
                    string matrixString = matrixA + matrixB + matrixC + matrixXYZ + matrixD;
                    var    matrixItem   = matrixString.Split(',');//字符串数组

                    //添加每个模型的position数据
                    double[] position = new double[3];


                    //转换成Int数组
                    double[] intItem = new double[16];
                    for (int i = 0; i < matrixItem.Length; i++)
                    {
                        intItem[i] = Convert.ToDouble(matrixItem[i]);
                    }
                    position[0] = intItem[12];
                    position[1] = intItem[13];
                    position[2] = intItem[14];
                    string matrix4        = JsonConvert.SerializeObject(intItem);
                    string positionString = JsonConvert.SerializeObject(position);

                    IXbimShapeGeometryData geometrya = geomReader.ShapeGeometry(shapeInstance.ShapeGeometryLabel);
                    var data = ((IXbimShapeGeometryData)geometrya).ShapeData;

                    using (var stream = new MemoryStream(data))
                    {
                        using (var reader = new BinaryReader(stream))
                        {
                            var mesh = reader.ReadShapeTriangulation();

                            //查找模型所有的面
                            List <XbimFaceTriangulation> Faces = mesh.Faces as List <XbimFaceTriangulation>;
                            if (Faces.Count > 0)
                            {
                                foreach (var item in Faces)
                                {
                                    faces_json.Append(",{");
                                    faces_json.Append("\"IsPlanar\":" + item.IsPlanar.ToString().ToLower());
                                    faces_json.Append(",\"TriangleCount\":" + item.TriangleCount);
                                    faces_json.Append(",\"NormalCount\":" + 1);
                                    //faces_json.Append(",\"NormalCount\":" + item.NormalCount);
                                    faces_json.Append(",\"Indices\":" + JsonConvert.SerializeObject(item.Indices));
                                    faces_json.Append(",\"Normals\":[");
                                    var normals = item.Normals;
                                    //normal由于重复暂时只取一个,能极大减小数据量
                                    faces_json.Append("{\"X\":" + normals[0].Normal.X + ",\"Y\":" + normals[0].Normal.Y + ",\"Z\":" + normals[0].Normal.Z
                                                      + ",\"Length\":" + normals[0].Normal.Length + ",\"Modulus\":" + normals[0].Normal.Modulus
                                                      + ",\"U\":" + normals[0].U + ",\"V\":" + normals[0].V + "}");

                                    faces_json.Append("]}");
                                }
                            }
                            //faces_json = JsonConvert.SerializeObject(Faces);

                            //查找模型所有的点坐标
                            List <XbimPoint3D> Point = mesh.Vertices as List <XbimPoint3D>;
                            point_json = JsonConvert.SerializeObject(Point);

                            //将模型数据转换成json格式,保存至xxx.json文件
                            if (index != 0)
                            {
                                jsonString.Append(",");
                            }
                            jsonString.Append("{\"ObjectID\":\"" + globalId + "\",\"Position\":" + positionString + ",\"Matrix4\":" + matrix4 + ",\"Faces\":[" + faces_json.ToString().Substring(1) + "],\"Points\":" + point_json + ",\"Material\":{" + materialString + "}}");
                            //jsonString += ",{\"ObjectID\":\"" + globalId + "\",\"Matrix4\":" + matrix4 + ",\"Faces\":[" + faces_json.Substring(1) + "],\"Points\":" + point_json + ",\"Material\":{\"IsTransparent\":" + isTransparent + "," + materialString + "}}";
                            index++;
                        }
                    }
                }

                if (jsonString.Length > 0)
                {
                    string jsonObject = "{\"ID\":\"" + GuidTo16String() + "\",\"Name\":\"" + IfcFileName + "\",\"Position\":" + JsonConvert.SerializeObject(namePosition[IfcFileName]) + ",\"Geometry\":[";
                    jsonObject += jsonString.ToString();
                    //jsonObject += jsonString.ToString().Substring(1).ToString();
                    jsonObject += "]}";

                    if (jsonObject.Length > 2)
                    {
                        string fileNewName = filePath.Replace(".ifc", "") + "_M.json";
                        byte[] myByte      = System.Text.Encoding.UTF8.GetBytes(jsonObject);
                        using (FileStream fs = new FileStream(fileNewName, FileMode.Create))
                        {
                            fs.Write(myByte, 0, myByte.Length);
                        }
                    }
                }
            }
        }