Пример #1
0
        private JsonLine makeJsonLine(ICurve curve3D)
        {
            ICurve path = curve3D.Approximate(true, precision);

            if (path is Polyline)
            {
                JsonLine pl = new JsonLine();
                pl.vertices = new List <JsonPoint>();
                for (int i = 0; i < (path as Polyline).Vertices.Length; i++)
                {
                    pl.vertices.Add(new JsonPoint((path as Polyline).Vertices[i]));
                }
                pl.color = 0; // all edges are black
                return(pl);
            }
            else if (path is GeoObject.Path)
            {
                JsonLine pl = new JsonLine();
                pl.vertices = new List <JsonPoint>();
                for (int i = 0; i < (path as GeoObject.Path).CurveCount; i++)
                {
                    pl.vertices.Add(new JsonPoint((path as GeoObject.Path).Curves[i].StartPoint));
                }
                pl.vertices.Add(new JsonPoint((path as GeoObject.Path).Curves.Last().EndPoint));
                pl.color = 0;
                return(pl);
            }
            else if (path is Line)
            {
                JsonLine pl = new JsonLine();
                pl.vertices = new List <JsonPoint>();
                pl.vertices.Add(new JsonPoint(curve3D.StartPoint));
                pl.vertices.Add(new JsonPoint(curve3D.EndPoint));
                pl.color = 0;
                return(pl);
            }
            return(new JsonLine
            {
                vertices = new List <JsonPoint>(),
                color = 0
            });
        }
Пример #2
0
        public bool WriteDrawing(string fileName, Stream fileStream, Drawing drawing, ViewPort viewPort, INotifyPropertyChanged fileSettings)
        {
            var jsonDrawing = new JsonDrawing();
            var modelSpace  = new JsonBlock()
            {
                Name = "*Model_Space"
            };

            jsonDrawing.Floorplan.Blocks.Add(modelSpace);
            foreach (var layer in drawing.GetLayers())
            {
                var jsonLayer = new JsonLayer()
                {
                    Name = layer.Name,
                    R    = layer.Color?.R ?? 0,
                    G    = layer.Color?.G ?? 0,
                    B    = layer.Color?.B ?? 0,
                };
                jsonDrawing.Floorplan.Layers.Add(jsonLayer);
                foreach (var entity in layer.GetEntities())
                {
                    switch (entity.Kind)
                    {
                    case EntityKind.Arc:
                        var arc     = (Arc)entity;
                        var jsonArc = new JsonArc()
                        {
                            Layer      = layer.Name,
                            Center     = arc.Center,
                            Radius     = arc.Radius,
                            StartAngle = arc.StartAngle * MathHelper.DegreesToRadians,
                            EndAngle   = arc.EndAngle * MathHelper.DegreesToRadians
                        };
                        modelSpace.Entities.Add(jsonArc);
                        break;

                    case EntityKind.Line:
                        var line     = (Line)entity;
                        var jsonLine = new JsonLine()
                        {
                            Layer = layer.Name
                        };
                        jsonLine.Points.Add(line.P1);
                        jsonLine.Points.Add(line.P2);
                        modelSpace.Entities.Add(jsonLine);
                        break;
                    }
                }

                foreach (var jsonEntity in modelSpace.Entities)
                {
                    jsonEntity.BeforeWrite();
                }
            }

            var json = JsonConvert.SerializeObject(jsonDrawing, Settings);

            using (var writer = new StreamWriter(fileStream, encoding: Encoding.UTF8, bufferSize: 1024, leaveOpen: true))
            {
                writer.Write(json);
            }

            return(true);
        }
Пример #3
0
        private JsonEntity makeJsonEntity(IGeoObject go)
        {
            try
            {
                JsonEntity res = new JsonEntity
                {
                    faces    = new List <JsonFace>(),
                    lines    = new List <JsonLine>(),
                    text     = new List <JsonText>(),
                    userData = new Dictionary <string, object>()
                };

                if (go is Solid)
                {
                    foreach (Face fce in (go as Solid).Shells[0].Faces)
                    {
                        res.faces.Add(makeJsonFace(fce));
                    }
                    if (ExportEdges)
                    {
                        foreach (Edge edg in (go as Solid).Shells[0].Edges)
                        {
                            if (edg.Curve3D != null)
                            {
                                res.lines.Add(makeJsonLine(edg.Curve3D));
                            }
                        }
                    }
                }
                else if (go is Shell)
                {
                    foreach (Face fce in (go as Shell).Faces)
                    {
                        res.faces.Add(makeJsonFace(fce));
                    }
                    if (ExportEdges)
                    {
                        foreach (Edge edg in (go as Shell).Edges)
                        {
                            if (edg.Curve3D != null)
                            {
                                res.lines.Add(makeJsonLine(edg.Curve3D));
                            }
                        }
                    }
                }
                else if (go is Face)
                {
                    res.faces.Add(makeJsonFace(go as Face));
                    if (ExportEdges)
                    {
                        foreach (Edge edg in (go as Face).AllEdges)
                        {
                            if (edg.Curve3D != null)
                            {
                                res.lines.Add(makeJsonLine(edg.Curve3D));
                            }
                        }
                    }
                }
                else if (go is Block)
                {
                    for (int i = 0; i < go.NumChildren; i++)
                    {
                        JsonEntity sub = makeJsonEntity(go.Child(i));
                        res.faces.AddRange(sub.faces);
                        res.lines.AddRange(sub.lines);
                        res.text.AddRange(sub.text);
                    }
                }
                else if (go is ICurve)
                {
                    ICurve path = (go as ICurve).Approximate(true, precision);
                    if (path is Polyline)
                    {
                        JsonLine pl = new JsonLine();
                        pl.vertices = new List <JsonPoint>();
                        for (int i = 0; i < (path as Polyline).Vertices.Length; i++)
                        {
                            pl.vertices.Add(new JsonPoint((path as Polyline).Vertices[i]));
                        }
                        pl.color = getColor(go);
                        res.lines.Add(pl);
                    }
                    if (path is GeoObject.Path)
                    {
                        JsonLine pl = new JsonLine();
                        pl.vertices = new List <JsonPoint>();
                        for (int i = 0; i < (path as GeoObject.Path).CurveCount; i++)
                        {
                            pl.vertices.Add(new JsonPoint((path as GeoObject.Path).Curves[i].StartPoint));
                        }
                        pl.vertices.Add(new JsonPoint((path as GeoObject.Path).Curves.Last().EndPoint));
                        pl.color = getColor(go);
                        res.lines.Add(pl);
                    }
                }
                else if (go is Text)
                {
                    Text     txt = (go as Text);
                    JsonText jst = new JsonText();
                    jst.location   = new JsonPoint(txt.Location);
                    jst.directionx = new JsonPoint(txt.LineDirection);
                    jst.directiony = new JsonPoint(txt.GlyphDirection);
                    jst.font       = txt.Font;
                    jst.text       = txt.TextString;
                    jst.color      = getColor(go);
                    res.text.Add(jst);
                }
                if (ExportUserData && go.UserData != null)
                {
                    res.userData = new Dictionary <string, object>();
                    foreach (KeyValuePair <string, object> kv in go.UserData)
                    {
                        if (kv.Value.GetType().IsPrimitive || kv.Value is string || kv.Value is Guid)
                        {
                            if (kv.Value == null)
                            {
                                res.userData[kv.Key] = "null";
                            }
                            else
                            {
                                res.userData[kv.Key] = kv.Value;
                            }
                        }
                        else
                        {
                            TypeAttributes ta = kv.Value.GetType().Attributes;
                        }
                    }
                    // DataContractResolver
#if DEBUG
                    if (go.UserData.Count == 0)
                    {
                        res.userData["key1"] = "val1";
                        res.userData["key2"] = "val2";
                    }
#endif
                }
                return(res);
            }
            catch (Exception ex)
            {
                if (ex is System.Threading.ThreadAbortException)
                {
                    throw (ex);
                }
                throw new ExportToThreeJsException(go);
            }
        }
Пример #4
0
        public Task <bool> WriteDrawing(string fileName, Stream fileStream, Drawing drawing, ViewPort viewPort, object fileSettings)
        {
            var jsonDrawing = new JsonDrawing();
            var modelSpace  = new JsonBlock()
            {
                Name = "*Model_Space"
            };

            jsonDrawing.Floorplan.Blocks.Add(modelSpace);
            foreach (var layer in drawing.GetLayers())
            {
                var jsonLayer = new JsonLayer()
                {
                    Name = layer.Name,
                    R    = layer.Color?.R ?? 0,
                    G    = layer.Color?.G ?? 0,
                    B    = layer.Color?.B ?? 0,
                };
                jsonDrawing.Floorplan.Layers.Add(jsonLayer);
                foreach (var entity in layer.GetEntities())
                {
                    entity.DoEntity(
                        aggregate => { },
                        arc => modelSpace.Entities.Add(new JsonArc()
                    {
                        Layer      = layer.Name,
                        Center     = arc.Center,
                        Radius     = arc.Radius,
                        StartAngle = arc.StartAngle * MathHelper.DegreesToRadians,
                        EndAngle   = arc.EndAngle * MathHelper.DegreesToRadians
                    }),
                        circle => { },
                        ellipse => { },
                        image => { },
                        line =>
                    {
                        var jsonLine = new JsonLine()
                        {
                            Layer = layer.Name
                        };
                        jsonLine.Points.Add(line.P1);
                        jsonLine.Points.Add(line.P2);
                        modelSpace.Entities.Add(jsonLine);
                    },
                        location => { },
                        polyline => { },
                        spline => { },
                        text => { }
                        );
                }

                foreach (var jsonEntity in modelSpace.Entities)
                {
                    jsonEntity.BeforeWrite();
                }
            }

            var json = JsonConvert.SerializeObject(jsonDrawing, Settings);

            using (var writer = new StreamWriter(fileStream, encoding: Encoding.UTF8, bufferSize: 1024, leaveOpen: true))
            {
                writer.Write(json);
            }

            return(Task.FromResult(true));
        }