示例#1
0
        private static Layer GetOrCreateLayer(ref ReadOnlyTree <string, Layer> layers, string layerName)
        {
            Layer layer = null;

            // entities without a layer go to '0'
            layerName = layerName ?? "0";
            if (layers.KeyExists(layerName))
            {
                layer = layers.GetValue(layerName);
            }
            else
            {
                // add the layer if previously undefined
                layer  = new Layer(layerName);
                layers = layers.Insert(layer.Name, layer);
            }

            return(layer);
        }
示例#2
0
        public bool ReadDrawing(string fileName, Stream fileStream, out Drawing drawing, out ViewPort viewPort)
        {
            ReadOnlyTree <string, Layer> layers = null;

            using (var reader = new StreamReader(fileStream, encoding: Encoding.UTF8, detectEncodingFromByteOrderMarks: true, bufferSize: 1024, leaveOpen: true))
            {
                var json        = reader.ReadToEnd();
                var jsonDrawing = JsonConvert.DeserializeObject <JsonDrawing>(json, Settings);
                var layerList   = new List <Layer>();
                foreach (var jsonLayer in jsonDrawing.Floorplan.Layers)
                {
                    var layer = new Layer(jsonLayer.Name, color: CadColor.FromArgb(255, jsonLayer.R, jsonLayer.G, jsonLayer.B));
                    layerList.Add(layer);
                }

                layers = ReadOnlyTree <string, Layer> .FromEnumerable(layerList, l => l.Name);

                void AddEntityToLayer(string layerName, Entity entity)
                {
                    var layer = layers.GetValue(layerName);

                    layer  = layer.Add(entity);
                    layers = layers.Insert(layerName, layer);
                }

                foreach (var block in jsonDrawing.Floorplan.Blocks)
                {
                    switch (block.Name.ToUpperInvariant())
                    {
                    case "*MODEL_SPACE":
                        foreach (var jsonEntity in block.Entities)
                        {
                            switch (jsonEntity)
                            {
                            case JsonArc jsonArc:
                                var arc = new Arc(jsonArc.Center, jsonArc.Radius, jsonArc.StartAngle * MathHelper.RadiansToDegrees, jsonArc.EndAngle * MathHelper.RadiansToDegrees, Vector.ZAxis);
                                AddEntityToLayer(jsonEntity.Layer, arc);
                                break;

                            case JsonLine jsonLine:
                                for (int i = 0; i < jsonLine.Points.Count - 1; i++)
                                {
                                    AddEntityToLayer(jsonEntity.Layer, new Line(jsonLine.Points[i], jsonLine.Points[i + 1]));
                                }

                                if (jsonLine.Points.Count > 2)
                                {
                                    AddEntityToLayer(jsonEntity.Layer, new Line(jsonLine.Points.Last(), jsonLine.Points.First()));
                                }
                                break;

                            default:
                                break;
                            }
                        }
                        break;
                        // TODO: handle other blocks?
                    }
                }
            }

            drawing  = new Drawing().Update(layers: layers);
            viewPort = null; // auto-generate later

            return(true);
        }