Beispiel #1
0
        /// <summary>
        /// Creates a block from the content of a <see cref="DxfDocument">document</see>.
        /// </summary>
        /// <param name="doc">A <see cref="DxfDocument">DxfDocument</see> instance.</param>
        /// <param name="name">Name of the new block.</param>
        /// <returns>The block build from the <see cref="DxfDocument">document</see> content.</returns>
        /// <remarks>Only the entities contained in ModelSpace will make part of the block.</remarks>
        public static Block Create(DxfDocument doc, string name)
        {
            if (doc == null)
            {
                throw new ArgumentNullException(nameof(doc));
            }

            Block block = new Block(name)
            {
                Origin = doc.DrawingVariables.InsBase
            };

            block.Record.Units = doc.DrawingVariables.InsUnits;
            List <DxfObject> entities = doc.Layouts.GetReferences(Layout.ModelSpaceName);

            foreach (DxfObject dxfObject in entities)
            {
                EntityObject entity = dxfObject as EntityObject;
                if (entity == null)
                {
                    continue;
                }
                EntityObject        clone  = (EntityObject)entity.Clone();
                AttributeDefinition attdef = clone as AttributeDefinition;
                if (attdef != null)
                {
                    block.AttributeDefinitions.Add(attdef);
                    continue;
                }
                block.Entities.Add(clone);
            }
            return(block);
        }
Beispiel #2
0
        /// <summary>
        /// build a clone of the given entity with coord transformed accordingly given function.
        /// </summary>
        public static EntityObject CoordTransform(this EntityObject eo, Func <Vector3D, Vector3D> transform, Vector3D origin = null)
        {
            switch (eo.Type)
            {
            case EntityType.Insert:
            {
                var ins = (Insert)eo.Clone();
                ins.Position = transform(ins.Position);
                return(ins);
            }

            case EntityType.Line:
            {
                var line = (Line)eo.Clone();
                line.StartPoint = transform(line.StartPoint);
                line.EndPoint   = transform(line.EndPoint);
                return(line);
            }

            case EntityType.Text:
            {
                var text = (Text)eo.Clone();
                text.Position = transform(text.Position);
                return(text);
            }

            case EntityType.MText:
            {
                var mtext = (MText)eo.Clone();
                mtext.Position = transform(mtext.Position);
                return(mtext);
            }

            case EntityType.Circle:
            {
                var circle = (Circle)eo.Clone();
                {
                    var c = transform(circle.Center);
                    if (origin != null)
                    {
                        c -= origin;
                    }
                    circle.Center = c;
                }
                {
                    var r = transform(new Vector3D(circle.Radius, 0));
                    if (origin != null)
                    {
                        r -= origin;
                    }
                    circle.Radius = r.Length;
                }
                return(circle);
            }

            case EntityType.Point:
            {
                var point = (Point)eo.Clone();
                point.Position = transform(point.Position);
                return(point);
            }

            case EntityType.LightWeightPolyline:
            {
                var lw = (LwPolyline)eo.Clone();
                lw.Vertexes.ForEach(w =>
                    {
                        w.Position = transform(w.Position.ToVector3D()).ToVector2();
                    });
                return(lw);
            }

            default: throw new NotImplementedException($"not implemented coord transform for entity [{eo.Type}]");
            }
        }