Пример #1
0
        protected IEnumerable <XbimShapeInstance> GetShapeInstancesToRender(IGeometryStoreReader geomReader, HashSet <short> excludedTypes)
        {
            var shapeInstances = geomReader.ShapeInstances
                                 .Where(s => s.RepresentationType == XbimGeometryRepresentationType.OpeningsAndAdditionsIncluded
                                        &&
                                        !excludedTypes.Contains(s.IfcTypeId));

            return(shapeInstances);
        }
Пример #2
0
 private IDictionary <int, SceneContext> ContextsCreateFromSettings(IGeometryStoreReader gReader, IfcSceneExportSummary s)
 {
     // Retrieve all context with geometry and match those to pregiven in settings
     return(gReader.ContextIds
            .Select(label => s.Model.Instances[label])
            .OfType <IIfcRepresentationContext>()
            .Select(c => (c.EntityLabel, s.AppliedSettings.UserRepresentationContext.FirstOrDefault(sc => StringComparer.OrdinalIgnoreCase.Equals(sc.Name, c.ContextIdentifier))))
            .Where(t => t.Item2 != null)
            .ToDictionary(t => t.EntityLabel, t => t.Item2));
 }
Пример #3
0
 private IDictionary <int, SceneContext> ContextsCreateFrom(IModel model, IGeometryStoreReader gReader, string[] contextIdentifiers)
 {
     return(gReader.ContextIds
            .Select(label => model.Instances[label])
            .OfType <IIfcRepresentationContext>()
            .Select(c => (c.EntityLabel, contextIdentifiers.FirstOrDefault(id => id == c.ContextIdentifier)))
            .Where(t => t.Item2 != null)
            .ToDictionary(t => t.EntityLabel, t => new SceneContext {
         Name = t.Item2
     }));
 }
Пример #4
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);
            }
        }
Пример #5
0
        static private IEnumerable <XbimShapeInstance> GetShapeInstancesToRender(IGeometryStoreReader geomReader, HashSet <short> excludedTypes, HashSet <int> EntityLebels = null)
        {
            if (EntityLebels == null)
            {
                var shapeInstances = geomReader.ShapeInstances
                                     .Where(s =>
                                            s.RepresentationType == XbimGeometryRepresentationType.OpeningsAndAdditionsIncluded
                                            &&
                                            !excludedTypes.Contains(s.IfcTypeId));
                return(shapeInstances);
            }
            var entityFilter = geomReader.ShapeInstances
                               .Where(s =>
                                      s.RepresentationType == XbimGeometryRepresentationType.OpeningsAndAdditionsIncluded
                                      &&
                                      !excludedTypes.Contains(s.IfcTypeId)
                                      &&
                                      EntityLebels.Contains(s.IfcProductLabel)
                                      );

            return(entityFilter);
        }
Пример #6
0
 private IDictionary <int, SceneContext> ContextsCreateFromModel(IModel model, IGeometryStoreReader gReader)
 {
     return(gReader.ContextIds
            .Select(label => model.Instances[label])
            .OfType <IIfcRepresentationContext>()
            .ToDictionary(c => c.EntityLabel, c => new SceneContext {
         Name = c.ContextIdentifier
     }));
 }
Пример #7
0
        // Compute contexts and related transformation
        private void ComputeContextTransforms(IGeometryStoreReader gReader, IfcSceneExportSummary s, IDictionary <int, SceneContext> contextTable)
        {
            foreach (var cr in gReader.ContextRegions)
            {
                SceneContext sc;
                if (contextTable.TryGetValue(cr.ContextLabel, out sc))
                {
                    XbimVector3D offset = XbimVector3D.Zero;
                    XbimVector3D mean   = XbimVector3D.Zero;
                    foreach (var r in cr)
                    {
                        mean += r.Centre.ToVector();
                        sc.Regions.Add(r.ToRegion(s.Scale));
                    }
                    mean *= 1.0 / cr.Count;

                    switch (s.AppliedSettings.Positioning)
                    {
                    case ScenePositioningStrategy.UserCorrection:
                        // Center at user's center
                        offset = s.AppliedSettings.UserModelCenter.ToXbimVector3DMeter(s.Model.ModelFactors);
                        break;

                    case ScenePositioningStrategy.MostPopulatedRegionCorrection:
                        // Center at most populated
                        offset = cr.MostPopulated().Centre.ToVector();
                        break;

                    case ScenePositioningStrategy.MostExtendedRegionCorrection:
                        // Center at largest
                        offset = cr.Largest().Centre.ToVector();
                        break;

                    case ScenePositioningStrategy.MeanTranslationCorrection:
                        // Use mean correction
                        offset = mean;
                        break;

                    case ScenePositioningStrategy.SignificantPopulationCorrection:
                        var        population = cr.Sum(r => r.Population);
                        XbimRegion rs         = null;
                        double     max        = double.NegativeInfinity;
                        foreach (var r in cr)
                        {
                            // Compute weighted extent by relative population
                            double factor = r.Size.Length * r.Population / population;
                            if (max < factor)
                            {
                                rs  = r;
                                max = factor;
                            }
                        }
                        offset = rs.Centre.ToVector();
                        break;

                    case ScenePositioningStrategy.NoCorrection:
                        // No correction
                        Logger?.LogInformation($"No translation correction applied by settings to context '{cr.ContextLabel}'");
                        break;

                    default:
                        throw new NotImplementedException($"Missing implementation for '{s.AppliedSettings.Positioning}'");
                    }

                    if (s.AppliedSettings.Transforming == SceneTransformationStrategy.Matrix)
                    {
                        // If Matrix or Global use rotation matrix representation
                        sc.Wcs = new XbimMatrix3D(offset).ToRotation(s.Scale);
                    }
                    else
                    {
                        // Otherwise use Quaternion representation
                        sc.Wcs = new XbimMatrix3D(offset).ToQuaternion(s.Scale);
                    }

                    // Set correction to negative offset shift (without scale since in model space units)
                    s.SetRepresentationContext(cr.ContextLabel, sc, new XbimMatrix3D(offset * -1));
                }
                else
                {
                    Logger?.LogWarning("Excluding context label '{0}'. Not mentioned by settings.", cr.ContextLabel);
                }
            }
        }
Пример #8
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);
                        }
                    }
                }
            }
        }