Ejemplo n.º 1
0
        private ReadOnlyTree <int, int> CreateTree(params int[] values)
        {
            var tree = new ReadOnlyTree <int, int>();

            foreach (var v in values)
            {
                tree = tree.Insert(v, v * v);
            }
            Assert.Equal(values.Length, tree.Count);
            return(tree);
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
        public override void Commit()
        {
            var dwg = workspace.Drawing;

            if (this.viewModel.Layers.Any(layer => layer.IsDirty) ||
                this.viewModel.Layers.Count != dwg.Layers.Count)
            {
                // found changes, need to update
                var newLayers = new ReadOnlyTree <string, Layer>();
                foreach (var layer in from layer in this.viewModel.Layers
                         select layer.GetUpdatedLayer())
                {
                    newLayers = newLayers.Insert(layer.Name, layer);
                }

                dwg = dwg.Update(layers: newLayers);
                workspace.Update(drawing: dwg);
            }
        }
Ejemplo n.º 4
0
        public void LinearInsertTest()
        {
            int treeSize = 20;
            var tree     = new ReadOnlyTree <int, int>();

            for (int i = 0; i < treeSize; i++)
            {
                tree = tree.Insert(i, i * i);
                Assert.Equal(i + 1, tree.Count);
            }

            Assert.Equal(treeSize, tree.Count);
            for (int i = 0; i < treeSize; i++)
            {
                int value;
                Assert.True(tree.TryFind(i, out value));
                Assert.Equal(i * i, value);
            }
        }
Ejemplo n.º 5
0
        public void BalancedInsertionTest()
        {
            //       4
            //      / \
            //     /   \
            //    2     6
            //   / \   / \
            //  1   3 5   7
            var tree = new ReadOnlyTree <int, int>();

            foreach (var i in new[] { 4, 2, 6, 1, 5, 3, 7 })
            {
                tree = tree.Insert(i, i * i);
            }

            Assert.Equal(7, tree.Count);
            for (int i = 1; i <= 7; i++)
            {
                int value;
                Assert.True(tree.TryFind(i, out value));
                Assert.Equal(i * i, value);
            }
        }
Ejemplo n.º 6
0
        public bool ReadDrawing(string fileName, Stream fileStream, out Drawing drawing, out ViewPort viewPort)
        {
            var file   = DxfFile.Load(fileStream);
            var layers = new ReadOnlyTree <string, Layer>();

            foreach (var layer in file.Layers)
            {
                layers = layers.Insert(layer.Name, new Layer(layer.Name, color: layer.Color.ToColor(), isVisible: layer.IsLayerOn));
            }

            foreach (var item in file.Entities)
            {
                var layer = GetOrCreateLayer(ref layers, item.Layer);

                // create the entity
                var entity = item.ToEntity();

                // add the entity to the appropriate layer
                if (entity != null)
                {
                    layer  = layer.Add(entity);
                    layers = layers.Insert(layer.Name, layer);
                }
            }

            foreach (var block in file.Blocks)
            {
                var layer = GetOrCreateLayer(ref layers, block.Layer);

                // create the aggregate entity
                var children = ReadOnlyList <Entity> .Empty();

                foreach (var item in block.Entities)
                {
                    var tempEnt = item.ToEntity();
                    if (tempEnt != null)
                    {
                        children = children.Add(tempEnt);
                    }
                }

                // add the entity to the appropriate layer
                if (children.Count != 0)
                {
                    layer  = layer.Add(new AggregateEntity(block.BasePoint.ToPoint(), children));
                    layers = layers.Insert(layer.Name, layer);
                }
            }

            drawing = new Drawing(
                settings: new DrawingSettings(fileName, file.Header.UnitFormat.ToUnitFormat(), file.Header.UnitPrecision),
                layers: layers,
                currentLayerName: file.Header.CurrentLayer ?? layers.GetKeys().OrderBy(x => x).First(),
                author: null);
            drawing.Tag = file;

            // prefer `*ACTIVE` view port first
            // TODO: use `DxfFile.ActiveViewPort` when available
            var vp = file.ViewPorts.FirstOrDefault(v => string.Compare(v.Name, DxfViewPort.ActiveViewPortName, StringComparison.OrdinalIgnoreCase) == 0)
                     ?? file.ViewPorts.FirstOrDefault();

            if (vp != null)
            {
                viewPort = new ViewPort(
                    vp.LowerLeft.ToPoint(),
                    vp.ViewDirection.ToVector(),
                    Vector.YAxis,
                    vp.ViewHeight);
            }
            else
            {
                viewPort = null; // auto-set it later
            }

            return(true);
        }
Ejemplo n.º 7
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);
        }