private static void WriteDxfFile()
        {
            DxfDocument dxf = new DxfDocument();

            //arc
            Arc arc = new Arc(new Vector3(10, 10, 0), 10, 45, 135);
            arc.Layer = new Layer("arc");
            arc.Layer.Color.Index = 1;
            dxf.AddEntity(arc);

            //xData sample
            XData xdata = new XData(new ApplicationRegistry("netDxf"));
            xdata.XDataRecord.Add(new XDataRecord(XDataCode.String, "extended data with netDxf"));
            xdata.XDataRecord.Add(XDataRecord.OpenControlString);
            xdata.XDataRecord.Add(new XDataRecord(XDataCode.WorldSpacePositionX, 0));
            xdata.XDataRecord.Add(new XDataRecord(XDataCode.WorldSpacePositionY, 0));
            xdata.XDataRecord.Add(new XDataRecord(XDataCode.WorldSpacePositionZ, 0));
            xdata.XDataRecord.Add(XDataRecord.CloseControlString);

            XData xdata2 = new XData(new ApplicationRegistry("other application"));
            xdata2.XDataRecord.Add(new XDataRecord(XDataCode.String, "extended data with netDxf"));
            xdata2.XDataRecord.Add(XDataRecord.OpenControlString);
            xdata2.XDataRecord.Add(new XDataRecord(XDataCode.String, "string record"));
            xdata2.XDataRecord.Add(new XDataRecord(XDataCode.Real, 15.5));
            xdata2.XDataRecord.Add(new XDataRecord(XDataCode.Int32, 350));
            xdata2.XDataRecord.Add(XDataRecord.CloseControlString);

            //circle
            Vector3 extrusion = new Vector3(1, 1, 1);
            Vector3 centerWCS = new Vector3(1, 1, 1);
            Vector3 centerOCS = MathHelper.Transform(centerWCS,
                                                        extrusion,
                                                        CoordinateSystem.World,
                                                        CoordinateSystem.Object);

            Circle circle = new Circle(centerOCS, 5);
            circle.Layer = new Layer("circle with spaces");
            circle.Layer.Color=AciColor.Yellow;
            circle.LineType = LineType.Dashed;
            circle.Normal = extrusion;
            circle.XData.Add(xdata);
            circle.XData.Add(xdata2);

            dxf.AddEntity(circle);

            //points
            Point point1 = new Point(new Vector3(-3, -3, 0));
            point1.Layer = new Layer("point");
            point1.Color = new AciColor(30);
            Point point2 = new Point(new Vector3(1, 1, 1));
            point2.Layer = point1.Layer;
            point2.Layer.Color.Index = 9;
            point2.Normal = new Vector3(1, 1, 1);
            dxf.AddEntity(point1);
            dxf.AddEntity(point2);

            //3dface
            Face3d face3D = new Face3d(new Vector3(-5, -5, 5),
                                        new Vector3(5, -5, 5),
                                        new Vector3(5, 5, 5),
                                        new Vector3(-5, 5, 5));
            face3D.Layer = new Layer("3dface");
            face3D.Layer.Color.Index = 3;
            dxf.AddEntity(face3D);
            
            //polyline
            LwPolylineVertex polyVertex;
            List<LwPolylineVertex> polyVertexes = new List<LwPolylineVertex>();
            polyVertex = new LwPolylineVertex(new Vector2(-50, -50));
            polyVertex.StartWidth = 2;
            polyVertexes.Add(polyVertex);
            polyVertex = new LwPolylineVertex(new Vector2(50, -50));
            polyVertex.StartWidth = 1;
            polyVertexes.Add(polyVertex);
            polyVertex = new LwPolylineVertex(new Vector2(50, 50));
            polyVertex.Bulge = 1;
            polyVertexes.Add(polyVertex);
            polyVertex = new LwPolylineVertex(new Vector2(-50, 50));
            polyVertexes.Add(polyVertex);
            LwPolyline polyline2d = new LwPolyline(polyVertexes, true);
            polyline2d.Layer = new Layer("polyline2d");
            polyline2d.Layer.Color.Index = 5;
            polyline2d.Normal = new Vector3(1, 1, 1);
            polyline2d.Elevation = 100.0f;
            dxf.AddEntity(polyline2d);

            //lightweight polyline
            LwPolylineVertex lwVertex;
            List<LwPolylineVertex> lwVertexes = new List<LwPolylineVertex>();
            lwVertex = new LwPolylineVertex(new Vector2(-25, -25));
            lwVertex.StartWidth = 2;
            lwVertexes.Add(lwVertex);
            lwVertex = new LwPolylineVertex(new Vector2(25, -25));
            lwVertex.StartWidth = 1;
            lwVertexes.Add(lwVertex);
            lwVertex = new LwPolylineVertex(new Vector2(25, 25));
            lwVertex.Bulge = 1;
            lwVertexes.Add(lwVertex);
            lwVertex = new LwPolylineVertex(new Vector2(-25, 25));
            lwVertexes.Add(lwVertex);
            LwPolyline lwPolyline = new LwPolyline(lwVertexes, true);
            lwPolyline.Layer = new Layer("lwpolyline");
            lwPolyline.Layer.Color.Index = 5;
            lwPolyline.Normal = new Vector3(1, 1, 1);
            lwPolyline.Elevation = 100.0f;
            dxf.AddEntity(lwPolyline);

            // polyfaceMesh
            List<PolyfaceMeshVertex> meshVertexes = new List<PolyfaceMeshVertex>
                                                    {
                                                        new PolyfaceMeshVertex(0, 0, 0),
                                                        new PolyfaceMeshVertex(10, 0, 0),
                                                        new PolyfaceMeshVertex(10, 10, 0),
                                                        new PolyfaceMeshVertex(5, 15, 0),
                                                        new PolyfaceMeshVertex(0, 10, 0)
                                                    };
            List<PolyfaceMeshFace> faces = new List<PolyfaceMeshFace>
                                                {
                                                    new PolyfaceMeshFace(new short[] {1, 2, -3}),
                                                    new PolyfaceMeshFace(new short[] {-1, 3, -4}),
                                                    new PolyfaceMeshFace(new short[] {-1, 4, 5})
                                                };

            PolyfaceMesh mesh = new PolyfaceMesh(meshVertexes, faces);
            mesh.Layer = new Layer("polyfacemesh");
            mesh.Layer.Color.Index = 104;
            dxf.AddEntity(mesh);

            //line
            Line line = new Line(new Vector3(0, 0, 0), new Vector3(10, 10, 10));
            line.Layer = new Layer("line");
            line.Layer.Color.Index = 6;
            dxf.AddEntity(line);

            //3d polyline
            PolylineVertex vertex;
            List<PolylineVertex> vertexes = new List<PolylineVertex>();
            vertex = new PolylineVertex(new Vector3(-50, -50, 0));
            vertexes.Add(vertex);
            vertex = new PolylineVertex(new Vector3(50, -50, 10));
            vertexes.Add(vertex);
            vertex = new PolylineVertex(new Vector3(50, 50, 25));
            vertexes.Add(vertex);
            vertex = new PolylineVertex(new Vector3(-50, 50, 50));
            vertexes.Add(vertex);
            Polyline polyline = new Polyline(vertexes, true);
            polyline.Layer = new Layer("polyline3d");
            polyline.Layer.Color.Index = 24;
            dxf.AddEntity(polyline);

            //block definition
            Block block = new Block("TestBlock");
            block.Entities.Add(new Line(new Vector3(-5, -5, 5), new Vector3(5, 5, 5)));
            block.Entities.Add(new Line(new Vector3(5, -5, 5), new Vector3(-5, 5, 5)));
           
            //insert
            Insert insert = new Insert(block, new Vector3(5, 5, 5));
            insert.Layer = new Layer("insert");
            insert.Layer.Color.Index = 4;
            dxf.AddEntity(insert);

            //text
            TextStyle style=new TextStyle("True type font","Arial.ttf");
            Text text = new Text("Hello world!", Vector3.Zero, 10.0f,style);
            text.Layer = new Layer("text");
            text.Layer.Color.Index = 8;
            text.Alignment = TextAlignment.TopRight;
            dxf.AddEntity(text);

            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2010;
            dxf.Save("AutoCad2010.dxf");
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save("AutoCad2007.dxf");
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2004;
            dxf.Save("AutoCad2004.dxf");
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2000;
            dxf.Save("AutoCad2000.dxf");
            dxf = DxfDocument.Load("AutoCad2000.dxf");
            dxf.Save("AutoCad2000 result.dxf");
        }
        private static void AssociativeHatches()
        {
            DxfDocument dxf = new DxfDocument(DxfVersion.AutoCad2010);

            LwPolyline poly = new LwPolyline();
            poly.Vertexes.Add(new LwPolylineVertex(-10, -10));
            poly.Vertexes.Add(new LwPolylineVertex(10, -10));
            poly.Vertexes.Add(new LwPolylineVertex(10, 10));
            poly.Vertexes.Add(new LwPolylineVertex(-10, 10));
            // optionally you can the normal of the polyline, by default it is the UnitZ vector
            //poly.Normal = new Vector3(1.0);
            poly.IsClosed = true;


            HatchBoundaryPath boundary = new HatchBoundaryPath(new List<EntityObject> { poly });
            HatchPattern pattern = HatchPattern.Line;
            pattern.Scale = 10;
            pattern.Angle = 45;

            // the hatch boundary can be set in the hatch constructor or it can be added later
            //Hatch hatch = new Hatch(pattern, new[]{boundary}, true);

            Hatch hatch = new Hatch(pattern, true);
            // you will need to manually set the hatch normal to the boundary normal if it is not the UnitZ,
            // to work properly all boundary entities must belong to the same plane
            //hatch.Normal = poly.Normal;

            // the hatch boundary can be set in the hatch constructor or it can be added later, remember hatches with no boundaries will not be saved
            hatch.BoundaryPaths.Add(boundary);
            Circle circle = new Circle(Vector2.Zero, 5);
            // all boundary entities should have the same normal, by default it is the UnitZ
            // the hatch will not handle the normals of the different boundary path, you will have to make sure they all lay on the same plane
            //circle.Normal = poly.Normal;

            hatch.BoundaryPaths.Add(new HatchBoundaryPath(new List<EntityObject> { circle }));
            // when an associative hatch is added to a document the referenced boundary entities will be added too
            dxf.AddEntity(hatch);
            dxf.Save("Hatch.dxf");


            DxfDocument dxf2 = DxfDocument.Load("Hatch.dxf");
            // you can remove boundaries from a hatch
            dxf2.Hatches[0].BoundaryPaths.Remove(dxf2.Hatches[0].BoundaryPaths[1]);
            // and add new ones
            LwPolyline p = new LwPolyline();
            p.Vertexes.Add(new LwPolylineVertex(-20, -20));
            p.Vertexes.Add(new LwPolylineVertex(20, -20));
            p.Vertexes.Add(new LwPolylineVertex(20, 20));
            p.Vertexes.Add(new LwPolylineVertex(-20, 20));
            p.IsClosed = true;
            dxf2.Hatches[0].BoundaryPaths.Add(new HatchBoundaryPath(new List<EntityObject> { p }));
            dxf2.Save("Hatch add and remove boundaries.dxf");


            DxfDocument dxf3 = DxfDocument.Load("Hatch.dxf");
            // unlinking the boundary entities from a hatch will not automatically remove them from the document, you can use the returned list to delete them
            // unlinking the boundary will make the hatch non-associative 
            List<EntityObject> oldBoundary = dxf3.Hatches[0].UnLinkBoundary();
            dxf3.RemoveEntity(oldBoundary);

            // we can recreate the hatch boundary and optionally linking it, thus making it associative,
            // if the hatch is associative and belongs to a document the new entities will also be automatically added to the same document
            List<EntityObject> newBoundary = dxf3.Hatches[0].CreateBoundary(true);

            dxf3.Save("Hatch new contour.dxf");

            DxfDocument dxf4 = DxfDocument.Load("Hatch.dxf");
            // if the hatch is associative, it is possible to modify the entities that make the boundary
            // for non-associative the list of entities will contain zero items
            if (dxf4.Hatches[0].Associative)
            {
                // this will only work for associative hatches
                HatchBoundaryPath path = dxf4.Hatches[0].BoundaryPaths[0];
                LwPolyline entity = (LwPolyline) path.Entities[0];
                entity.Vertexes[2].Position = new Vector2(15, 15);
                // after modifying the boundary entities, it is necessary to rebuild the edges
                path.Update();
                dxf4.Save("Hatch change boundary.dxf");
            }
        }
        private void SetInternalInfo(IEnumerable <EntityObject> entities)
        {
            bool containsClosedPolyline = false;

            this.edges.Clear();

            foreach (EntityObject entity in entities)
            {
                if (containsClosedPolyline)
                {
                    throw new ArgumentException("Closed polylines cannot be combined with other entities to make a hatch boundary path.");
                }

                // it seems that AutoCad does not have problems on creating loops that theoretically does not make sense, like, for example an internal loop that is made of a single arc.
                // so if AutoCAD is OK with that I am too, the program that make use of this information will take care of this inconsistencies
                switch (entity.Type)
                {
                case EntityType.Arc:
                    this.edges.Add(Arc.ConvertFrom(entity));
                    break;

                case EntityType.Circle:
                    this.edges.Add(Arc.ConvertFrom(entity));
                    break;

                case EntityType.Ellipse:
                    this.edges.Add(Ellipse.ConvertFrom(entity));
                    break;

                case EntityType.Line:
                    this.edges.Add(Line.ConvertFrom(entity));
                    break;

                case EntityType.LightWeightPolyline:
                    LwPolyline poly = (LwPolyline)entity;
                    if (poly.IsClosed)
                    {
                        if (this.edges.Count != 0)
                        {
                            throw new ArgumentException("Closed polylines cannot be combined with other entities to make a hatch boundary path.");
                        }
                        this.edges.Add(Polyline.ConvertFrom(entity));     // A polyline HatchBoundaryPath must be closed
                        this.pathType         |= HatchBoundaryPathTypeFlags.Polyline;
                        containsClosedPolyline = true;
                    }
                    else
                    {
                        this.SetInternalInfo(poly.Explode());     // open polylines will always be exploded, only one polyline can be present in a path
                    }
                    break;

                case EntityType.Spline:
                    this.edges.Add(Spline.ConvertFrom(entity));
                    break;

                default:
                    throw new ArgumentException(string.Format("The entity type {0} cannot be part of a hatch boundary.", entity.Type));
                }
            }
        }
Exemple #4
0
        /// <summary>
        /// Creates a new LwPolyline that is a copy of the current instance.
        /// </summary>
        /// <returns>A new LwPolyline that is a copy of this instance.</returns>
        public override object Clone()
        {
            List <LwPolylineVertex> copyVertexes = new List <LwPolylineVertex>();

            foreach (LwPolylineVertex vertex in this.vertexes)
            {
                copyVertexes.Add((LwPolylineVertex)vertex.Clone());
            }

            LwPolyline entity = new LwPolyline
            {
                //EntityObject properties
                Layer         = (Layer)this.layer.Clone(),
                LineType      = (LineType)this.lineType.Clone(),
                Color         = (AciColor)this.color.Clone(),
                Lineweight    = (Lineweight)this.lineweight.Clone(),
                Transparency  = (Transparency)this.transparency.Clone(),
                LineTypeScale = this.lineTypeScale,
                Normal        = this.normal,
                //LwPolyline properties
                Vertexes  = copyVertexes,
                Elevation = this.elevation,
                Thickness = this.thickness,
                Flags     = this.flags
            };

            foreach (XData data in this.XData.Values)
            {
                entity.XData.Add((XData)data.Clone());
            }

            return(entity);
        }
Exemple #5
0
        /// <summary>
        /// Konvertiert alles zu Polylinien
        /// </summary>
        /// <returns> Erzeugte Polylinien</returns>
        private List <Ent.LwPolyline> All_To_Polyline()
        {
            List <Ent.LwPolyline> list_all_Path = new List <Ent.LwPolyline>();

            foreach (List <object> Element in ListElement_Image)
            {
                List <Ent.LwPolylineVertex> list_all_vertex = new List <Ent.LwPolylineVertex>();
                foreach (object element in Element)
                {
                    Type type = element.GetType();
                    if (type == typeof(Beziercurve))
                    {
                        Beziercurve newCurve = element as Beziercurve;
                        foreach (Potrace.DoublePoint pt in newCurve.List_Of_All_Points)
                        {
                            list_all_vertex.Add(new Ent.LwPolylineVertex((double)pt.X, (double)pt.Y));
                        }
                    }
                    else
                    {
                        LineF newLine = element as LineF;
                        list_all_vertex.Add(new Ent.LwPolylineVertex((double)newLine.Point1.x, (double)newLine.Point1.Y));
                        list_all_vertex.Add(new Ent.LwPolylineVertex((double)newLine.Point2.x, (double)newLine.Point2.Y));
                    }
                }
                Ent.LwPolyline newPolyline = new Ent.LwPolyline(list_all_vertex);

                list_all_Path.Add(newPolyline);
            }
            Set_Max_und_Min(list_all_Path);
            RahmenPassend(ref list_all_Path);
            return(allePolylinien = list_all_Path);
        }
        /// <summary>
        /// Converts the circle in a Polyline.
        /// </summary>
        /// <param name="precision">Number of vertexes generated.</param>
        /// <returns>A new instance of <see cref="LwPolyline">LightWeightPolyline</see> that represents the circle.</returns>
        public LwPolyline ToPolyline(int precision)
        {
            IEnumerable <Vector2> vertexes = this.PolygonalVertexes(precision);
            Vector3 ocsCenter = MathHelper.Transform(this.Center, this.normal, MathHelper.CoordinateSystem.World, MathHelper.CoordinateSystem.Object);

            LwPolyline poly = new LwPolyline
            {
                Color         = this.color,
                IsVisible     = this.isVisible,
                Layer         = this.layer,
                LineType      = this.lineType,
                LineTypeScale = this.lineTypeScale,
                Lineweight    = this.lineweight,
                XData         = this.xData,
                Normal        = this.normal,
                Elevation     = ocsCenter.Z,
                Thickness     = this.thickness,
                IsClosed      = true
            };

            foreach (Vector2 v in vertexes)
            {
                poly.Vertexes.Add(new LwPolylineVertex(v.X + ocsCenter.X, v.Y + ocsCenter.Y));
            }
            return(poly);
        }
Exemple #7
0
        /// <summary>
        /// Moves, scales, and/or rotates the current entity given a 3x3 transformation matrix and a translation vector.
        /// </summary>
        /// <param name="transformation">Transformation matrix.</param>
        /// <param name="translation">Translation vector.</param>
        /// <remarks>Matrix3 adopts the convention of using column vectors to represent a transformation matrix.</remarks>
        public override void TransformBy(Matrix3 transformation, Vector3 translation)
        {
            Vector3 newNormal = transformation * this.Normal;

            if (Vector3.Equals(Vector3.Zero, newNormal))
            {
                newNormal = this.Normal;
            }
            this.Normal = newNormal;

            EntityObject clippingEntity = this.ClippingBoundary;

            if (clippingEntity == null)
            {
                if (transformation.IsIdentity)
                {
                    this.center += translation;
                    return;
                }

                // when a view port is transformed a LwPolyline will be generated
                List <LwPolylineVertex> vertexes = new List <LwPolylineVertex>
                {
                    new LwPolylineVertex(this.center.X - this.width * 0.5, this.center.Y - this.height * 0.5),
                    new LwPolylineVertex(this.center.X + this.width * 0.5, this.center.Y - this.height * 0.5),
                    new LwPolylineVertex(this.center.X + this.width * 0.5, this.center.Y + this.height * 0.5),
                    new LwPolylineVertex(this.center.X - this.width * 0.5, this.center.Y + this.height * 0.5)
                };
                clippingEntity = new LwPolyline(vertexes, true);
            }

            clippingEntity.TransformBy(transformation, translation);
            this.ClippingBoundary = clippingEntity;
        }
Exemple #8
0
        public LwPolyline(netDxf.Entities.LwPolyline inputLwPolyline)
        {
            InputLwPolyline = inputLwPolyline;

            //for (int i = 1; i < InputLwPolyline.Vertexes.Count; i++)
            //{
            //    LinesOrArcsCollection.Add(new Line(InputLwPolyline.Vertexes[i - 1], InputLwPolyline.Vertexes[i]));
            //}
            var temp = InputLwPolyline.Explode();

            foreach (var item in temp)
            {
                if (item.GetType() == typeof(netDxf.Entities.Line))
                {
                    LinesOrArcsCollection.Add(new Line((netDxf.Entities.Line)item));
                }
                else if (item.GetType() == typeof(netDxf.Entities.Arc))
                {
                    LinesOrArcsCollection.Add(new Arc((netDxf.Entities.Arc)item));
                }
            }

            foreach (var item in LinesOrArcsCollection)
            {
                CheckIfNewPointExtendArea(item.MaxXMaxY);
                CheckIfNewPointExtendArea(item.MinXMinY);
                Length += item.Length;
            }
        }
Exemple #9
0
        /// <summary>
        /// Converts the arc in a Polyline.
        /// </summary>
        /// <param name="precision">Number of divisions.</param>
        /// <returns>A new instance of <see cref="LwPolyline">LightWeightPolyline</see> that represents the arc.</returns>
        public LwPolyline ToPolyline(int precision)
        {
            IEnumerable <Vector2> vertexes = this.PolygonalVertexes(precision);
            Vector3 ocsCenter = MathHelper.Transform(this.center, this.Normal, CoordinateSystem.World, CoordinateSystem.Object);

            LwPolyline poly = new LwPolyline
            {
                Layer         = (Layer)this.Layer.Clone(),
                Linetype      = (Linetype)this.Linetype.Clone(),
                Color         = (AciColor)this.Color.Clone(),
                Lineweight    = this.Lineweight,
                Transparency  = (Transparency)this.Transparency.Clone(),
                LinetypeScale = this.LinetypeScale,
                Normal        = this.Normal,
                Elevation     = ocsCenter.Z,
                Thickness     = this.Thickness,
                IsClosed      = false
            };

            foreach (Vector2 v in vertexes)
            {
                poly.Vertexes.Add(new LwPolylineVertex(v.X + ocsCenter.X, v.Y + ocsCenter.Y));
            }
            return(poly);
        }
Exemple #10
0
        /// <summary>
        /// Creates a new LwPolyline that is a copy of the current instance.
        /// </summary>
        /// <returns>A new LwPolyline that is a copy of this instance.</returns>
        public override Object Clone()
        {
            LwPolyline entity = new LwPolyline
            {
                //EntityObject properties
                Layer         = (Layer)this.Layer.Clone(),
                Linetype      = (Linetype)this.Linetype.Clone(),
                Color         = (AciColor)this.Color.Clone(),
                Lineweight    = this.Lineweight,
                Transparency  = (Transparency)this.Transparency.Clone(),
                LinetypeScale = this.LinetypeScale,
                Normal        = this.Normal,
                IsVisible     = this.IsVisible,
                //LwPolyline properties
                Elevation = this.elevation,
                Thickness = this.thickness,
                Flags     = this.flags
            };

            foreach (LwPolylineVertex vertex in this.vertexes)
            {
                entity.Vertexes.Add((LwPolylineVertex)vertex.Clone());
            }

            foreach (XData data in this.XData.Values)
            {
                entity.XData.Add((XData)data.Clone());
            }

            return(entity);
        }
Exemple #11
0
 /// <summary>
 /// Convert a netDXF polyline to a Nucleus one
 /// </summary>
 /// <param name="polyLine"></param>
 /// <returns></returns>
 public static PolyLine Convert(netDxf.Entities.LwPolyline polyLine)
 {
     var pts = new List<Vector>();
     foreach (netDxf.Entities.LwPolylineVertex plV in polyLine.Vertexes)
     {
         pts.Add(Convert(plV.Position));
     }
     return new PolyLine(pts, polyLine.IsClosed, ExtractAttributes(polyLine));
 }
            public Polyline(EntityObject entity)
                : base(EdgeType.Polyline)
            {
                if (entity == null)
                {
                    throw new ArgumentNullException("entity");
                }

                if (entity is Entities.LwPolyline)
                {
                    LwPolyline poly = (LwPolyline)entity;
                    if (!poly.IsClosed)
                    {
                        throw new ArgumentException("Only closed polyline are supported as hatch boundary edges.", "entity");
                    }

                    this.Vertexes = new Vector3[poly.Vertexes.Count];
                    for (int i = 0; i < poly.Vertexes.Count; i++)
                    {
                        this.Vertexes[i] = new Vector3(poly.Vertexes[i].Location.X, poly.Vertexes[i].Location.Y, poly.Vertexes[i].Bulge);
                    }
                    this.IsClosed = true;
                }
                else if (entity is Entities.Polyline)
                {
                    Entities.Polyline poly = (Entities.Polyline)entity;
                    if (!poly.IsClosed)
                    {
                        throw new ArgumentException("Only closed polyline are supported as hatch boundary edges.", "entity");
                    }

                    this.Vertexes = new Vector3[poly.Vertexes.Count];
                    for (int i = 0; i < poly.Vertexes.Count; i++)
                    {
                        this.Vertexes[i] = new Vector3(poly.Vertexes[i].Location.X, poly.Vertexes[i].Location.Y, 0.0);
                    }
                    this.IsClosed = true;
                }
                else
                {
                    throw new ArgumentException("The entity is not a LwPolyline or a Polyline", "entity");
                }
            }
            /// <summary>
            /// Initializes a new instance of the the <c>HatchBoundaryPath.Polyline</c> class.
            /// </summary>
            /// <param name="entity"><see cref="EntityObject">Entity</see> that represents the edge.</param>
            public Polyline(EntityObject entity)
                : base(EdgeType.Polyline)
            {
                if (entity == null)
                {
                    throw new ArgumentNullException(nameof(entity));
                }

                if (entity.Type == EntityType.LwPolyline)
                {
                    LwPolyline poly = (LwPolyline)entity;
                    IsClosed = poly.IsClosed;
                    Vertexes = new Vector3[poly.Vertexes.Count];
                    for (int i = 0; i < poly.Vertexes.Count; i++)
                    {
                        Vertexes[i] = new Vector3(poly.Vertexes[i].Position.X, poly.Vertexes[i].Position.Y, poly.Vertexes[i].Bulge);
                    }
                }
                else if (entity.Type == EntityType.Polyline)
                {
                    Matrix3 trans = MathHelper.ArbitraryAxis(entity.Normal).Transpose();

                    Entities.Polyline poly = (Entities.Polyline)entity;
                    IsClosed = poly.IsClosed;
                    Vertexes = new Vector3[poly.Vertexes.Count];
                    for (int i = 0; i < poly.Vertexes.Count; i++)
                    {
                        Vector3 point = trans * poly.Vertexes[i].Position;
                        Vertexes[i] = new Vector3(point.X, point.Y, 0.0);
                    }
                }
                else
                {
                    throw new ArgumentException("The entity is not a LwPolyline or a Polyline", nameof(entity));
                }
            }
        private static void LayerAndLineTypesUsesAndRemove()
        {
            DxfDocument dxf = new DxfDocument();

            Layer layer1 = new Layer("Layer1");
            layer1.Color = AciColor.Blue;
            layer1.LineType = LineType.Center;

            Layer layer2 = new Layer("Layer2");
            layer2.Color = AciColor.Red;

            LwPolyline poly = new LwPolyline();
            poly.Vertexes.Add(new LwPolylineVertex(0, 0));
            poly.Vertexes.Add(new LwPolylineVertex(10, 10));
            poly.Vertexes.Add(new LwPolylineVertex(20, 0));
            poly.Vertexes.Add(new LwPolylineVertex(30, 10));
            poly.Layer = layer1;
            dxf.AddEntity(poly);

            Ellipse ellipse = new Ellipse(new Vector3(2, 2, 0), 5, 3);
            ellipse.Rotation = 30;
            ellipse.Layer = layer1;
            dxf.AddEntity(ellipse);

            Line line = new Line(new Vector2(10, 5), new Vector2(-10, -5));
            line.Layer = layer2;
            line.LineType = LineType.DashDot;
            dxf.AddEntity(line);


            bool ok;

            // this will return false since layer1 is not empty
            ok = dxf.Layers.Remove(layer1.Name);

            List<DxfObject> entities = dxf.Layers.GetReferences(layer1.Name);
            foreach (DxfObject o in entities)
            {
                dxf.RemoveEntity(o as EntityObject);
            }

            // now this should return true since layer1 is empty
            ok = dxf.Layers.Remove(layer1.Name);

            // blocks needs an special attention
            Layer layer3 = new Layer("Layer3");
            layer3.Color = AciColor.Yellow;

            Circle circle = new Circle(Vector3.Zero, 15);
            // it is always recommended that all block entities will be located in layer 0, but this is up to the user.
            circle.Layer = new Layer("circle");
            circle.Layer.Color = AciColor.Green;

            Block block = new Block("MyBlock");
            block.Entities.Add(circle);
            block.Layer = new Layer("blockLayer");
            AttributeDefinition attdef = new AttributeDefinition("NewAttribute");
            attdef.Layer = new Layer("attDefLayer");
            attdef.LineType = LineType.Center;
            block.AttributeDefinitions.Add(attdef);

            Insert insert = new Insert(block, new Vector2(5, 5));
            insert.Layer = layer3;
            insert.Attributes[0].Layer = new Layer("attLayer");
            insert.Attributes[0].LineType = LineType.Dashed;
            dxf.AddEntity(insert);

            dxf.Save("test.dxf");

            DxfDocument dxf2 = DxfDocument.Load("test.dxf");

            // this list will contain the circle entity
            List<DxfObject> dxfObjects;
            dxfObjects = dxf.Layers.GetReferences("circle");

            // but we cannot removed since it is part of a block
            ok = dxf.RemoveEntity(circle);
            // we need to remove first the block, but to do this we need to make sure there are no references of that block in the document
            dxfObjects = dxf.Blocks.GetReferences(block.Name);
            foreach (DxfObject o in dxfObjects)
            {
                dxf.RemoveEntity(o as EntityObject);
            }


            // now it is safe to remove the block since we do not have more references in the document
            ok = dxf.Blocks.Remove(block.Name);
            // now it is safe to remove the layer "circle", the circle entity was removed with the block since it was part of it
            ok = dxf.Layers.Remove("circle");

            // purge all document layers, only empty layers will be removed
            dxf.Layers.Clear();

            // purge all document line types, only line types without references will be removed
            dxf.LineTypes.Clear();

            dxf.Save("test2.dxf");
        }
        /// <summary>
        /// Creates a new LwPolyline that is a copy of the current instance.
        /// </summary>
        /// <returns>A new LwPolyline that is a copy of this instance.</returns>
        public override object Clone()
        {
            LwPolyline entity = new LwPolyline
                {
                    //EntityObject properties
                    Layer = (Layer)this.layer.Clone(),
                    LineType = (LineType)this.lineType.Clone(),
                    Color = (AciColor)this.color.Clone(),
                    Lineweight = (Lineweight)this.lineweight.Clone(),
                    Transparency = (Transparency)this.transparency.Clone(),
                    LineTypeScale = this.lineTypeScale,
                    Normal = this.normal,
                    //LwPolyline properties
                    Elevation = this.elevation,
                    Thickness = this.thickness,
                    Flags = this.flags
                };

            foreach (LwPolylineVertex vertex in this.vertexes)
                entity.Vertexes.Add((LwPolylineVertex) vertex.Clone());

            foreach (XData data in this.XData.Values)
                entity.XData.Add((XData)data.Clone());

            return entity;

        }
Exemple #16
0
 private static LwPolyline ProcessLwPolyline(LwPolyline polyline, Vector3 normal, double elevation)
 {
     polyline.Elevation = elevation;
     polyline.Normal = normal;
     return polyline;
 }
        private static void ExplodeTest()
        {
            DxfDocument dxf = new DxfDocument();
            //polyline
            LwPolylineVertex polyVertex;
            List<LwPolylineVertex> polyVertexes = new List<LwPolylineVertex>();
            polyVertex = new LwPolylineVertex(new Vector2(-50, -23.5));
            polyVertex.Bulge = 1.33;
            polyVertexes.Add(polyVertex);
            polyVertex = new LwPolylineVertex(new Vector2(34.8, -42.7));
            polyVertexes.Add(polyVertex);
            polyVertex = new LwPolylineVertex(new Vector2(65.3, 54.7));
            polyVertex.Bulge = -0.47;
            polyVertexes.Add(polyVertex);
            polyVertex = new LwPolylineVertex(new Vector2(-48.2, 42.5));
            polyVertexes.Add(polyVertex);
            LwPolyline polyline2d = new LwPolyline(polyVertexes);
            polyline2d.Layer = new Layer("polyline2d");
            polyline2d.Layer.Color.Index = 5;
            polyline2d.Normal = new Vector3(1, 1, 1);
            polyline2d.Elevation = 100.0f;

            dxf.AddEntity(polyline2d);
            dxf.AddEntity(polyline2d.Explode());

            dxf.Save("explode.dxf");
        }
Exemple #18
0
        /// <summary>
        /// Converts the ellipse in a Polyline.
        /// </summary>
        /// <param name="precision">Number of divisions.</param>
        /// <returns>A new instance of <see cref="LwPolyline">LightWeightPolyline</see> that represents the ellipse.</returns>
        public LwPolyline ToPolyline(int precision)
        {
            IEnumerable<Vector2> vertexes = this.PolygonalVertexes(precision);
            Vector3 ocsCenter = MathHelper.Transform(this.center, this.Normal, CoordinateSystem.World, CoordinateSystem.Object);
            LwPolyline poly = new LwPolyline
            {
                Layer = (Layer) this.Layer.Clone(),
                Linetype = (Linetype) this.Linetype.Clone(),
                Color = (AciColor) this.Color.Clone(),
                Lineweight = this.Lineweight,
                Transparency = (Transparency) this.Transparency.Clone(),
                LinetypeScale = this.LinetypeScale,
                Normal = this.Normal,
                Elevation = ocsCenter.Z,
                Thickness = this.Thickness,
                IsClosed = this.IsFullEllipse
            };

            foreach (Vector2 v in vertexes)
            {
                poly.Vertexes.Add(new LwPolylineVertex(v.X + ocsCenter.X, v.Y + ocsCenter.Y));
            }
            return poly;
        }
        private static void CheckReferences()
        {
            DxfDocument dxf = new DxfDocument();
            
            Layer layer1 = new Layer("Layer1");
            layer1.Color = AciColor.Blue;
            layer1.LineType = LineType.Center;

            Layer layer2 = new Layer("Layer2");
            layer2.Color = AciColor.Red;

            LwPolyline poly = new LwPolyline();
            poly.Vertexes.Add(new LwPolylineVertex(0, 0));
            poly.Vertexes.Add(new LwPolylineVertex(10, 10));
            poly.Vertexes.Add(new LwPolylineVertex(20, 0));
            poly.Vertexes.Add(new LwPolylineVertex(30, 10));
            poly.Layer = layer1;
            dxf.AddEntity(poly);

            Ellipse ellipse = new Ellipse(new Vector3(2, 2, 0), 5, 3);
            ellipse.Rotation = 30;
            ellipse.Layer = layer1;
            dxf.AddEntity(ellipse);

            Line line = new Line(new Vector2(10, 5), new Vector2(-10, -5));
            line.Layer = layer2;
            line.LineType = LineType.DashDot;
            dxf.AddEntity(line);

            dxf.Save("test.dxf");

            dxf = DxfDocument.Load("sample.dxf");

            foreach (ApplicationRegistry registry in dxf.ApplicationRegistries)
            {
                foreach (DxfObject o in dxf.ApplicationRegistries.GetReferences(registry))
                {
                    if (o is EntityObject)
                    {
                        foreach (KeyValuePair<string, XData> data in ((EntityObject)o).XData)
                        {
                            if (data.Key == registry.Name)
                                if (!ReferenceEquals(registry, data.Value.ApplicationRegistry))
                                    Console.WriteLine("Application registry {0} not equal entity to {1}", registry.Name, o.CodeName);
                        }
                    }
                }
            }

            foreach (Block block in dxf.Blocks)
            {
                foreach (DxfObject o in dxf.Blocks.GetReferences(block))
                {
                    if (o is Insert)
                        if (!ReferenceEquals(block, ((Insert)o).Block))
                            Console.WriteLine("Block {0} not equal entity to {1}", block.Name, o.CodeName);
                }
            }

            foreach (ImageDefinition def in dxf.ImageDefinitions)
            {
                foreach (DxfObject o in dxf.ImageDefinitions.GetReferences(def))
                {
                    if (o is Image)
                        if (!ReferenceEquals(def, ((Image)o).Definition))
                            Console.WriteLine("Image definition {0} not equal entity to {1}", def.Name, o.CodeName);
                }
            }

            foreach (DimensionStyle dimStyle in dxf.DimensionStyles)
            {
                foreach (DxfObject o in dxf.DimensionStyles.GetReferences(dimStyle))
                {
                    if (o is Dimension)
                        if (!ReferenceEquals(dimStyle, ((Dimension)o).Style))
                            Console.WriteLine("Dimension style {0} not equal entity to {1}", dimStyle.Name, o.CodeName);
                }

            }

            foreach (Group g in dxf.Groups)
            {
                foreach (DxfObject o in dxf.Groups.GetReferences(g))
                {
                    // no references
                }
            }

            foreach (UCS u in dxf.UCSs)
            {
                foreach (DxfObject o in dxf.UCSs.GetReferences(u))
                {
                    
                }
            }

            foreach (TextStyle style in dxf.TextStyles)
            {
                foreach (DxfObject o in dxf.TextStyles.GetReferences(style))
                {
                    if (o is Text)
                        if (!ReferenceEquals(style, ((Text)o).Style))
                            Console.WriteLine("Text style {0} not equal entity to {1}", style.Name, o.CodeName);

                    if (o is MText)
                        if (!ReferenceEquals(style, ((MText)o).Style))
                            Console.WriteLine("Text style {0} not equal entity to {1}", style.Name, o.CodeName);

                    if (o is DimensionStyle)
                        if (!ReferenceEquals(style, ((DimensionStyle)o).DIMTXSTY))
                            Console.WriteLine("Text style {0} not equal entity to {1}", style.Name, o.CodeName);
                }
            }

            foreach (Layer layer in dxf.Layers)
            {
                foreach (DxfObject o in dxf.Layers.GetReferences(layer))
                {
                    if (o is Block)
                        if (!ReferenceEquals(layer, ((Block)o).Layer))
                            Console.WriteLine("Layer {0} not equal entity to {1}", layer.Name, o.CodeName);
                    if (o is EntityObject)
                        if (!ReferenceEquals(layer, ((EntityObject)o).Layer))
                            Console.WriteLine("Layer {0} not equal entity to {1}", layer.Name, o.CodeName);
                }
            }

            foreach (LineType lType in dxf.LineTypes)
            {
                foreach (DxfObject o in dxf.LineTypes.GetReferences(lType))
                {
                    if (o is Layer)
                        if (!ReferenceEquals(lType, ((Layer)o).LineType))
                            Console.WriteLine("Line type {0} not equal to {1}", lType.Name, o.CodeName);
                    if (o is MLineStyle)
                    {
                        foreach (MLineStyleElement e in ((MLineStyle)o).Elements)
                        {
                            if (!ReferenceEquals(lType, e.LineType))
                                Console.WriteLine("Line type {0} not equal to {1}", lType.Name, o.CodeName);
                        }
                    }
                    if (o is EntityObject)
                        if (!ReferenceEquals(lType, ((EntityObject)o).LineType))
                            Console.WriteLine("Line type {0} not equal entity to {1}", lType.Name, o.CodeName);

                }
            }

            Console.WriteLine("Press a key to continue...");
            Console.ReadKey();
        }
Exemple #20
0
 public HndzPolyline(string name, string description, DXF.LwPolyline dxfLwPolyline) : base(name, description)
 {
     DxfLwPolyline = dxfLwPolyline;
 }
        private static void TestOCStoWCS()
        {
            // vertexes of the light weight polyline, they are defined in OCS (Object Coordinate System)
            LwPolylineVertex v1 = new LwPolylineVertex(1, -5);
            LwPolylineVertex v2 = new LwPolylineVertex(-3, 2);
            LwPolylineVertex v3 = new LwPolylineVertex(8, 15);

            LwPolyline lwp = new LwPolyline(new List<LwPolylineVertex> {v1, v2, v3});
            // the normal will define the plane where the lwpolyline is defined
            lwp.Normal = new Vector3(1, 1, 0);
            // the entity elevation defines the z vector of the vertexes along the entity normal
            lwp.Elevation = 2.5;

            DxfDocument dxf = new DxfDocument();
            dxf.AddEntity(lwp);
            dxf.Save("OCStoWCS.dxf");

            // if you want to convert the vertexes of the polyline to WCS (World Coordinate System), you can
            Vector3 v1OCS = new Vector3(v1.Position.X, v1.Position.Y, lwp.Elevation);
            Vector3 v2OCS = new Vector3(v2.Position.X, v2.Position.Y, lwp.Elevation);
            Vector3 v3OCS = new Vector3(v3.Position.X, v3.Position.Y, lwp.Elevation);
            IList<Vector3> vertexesWCS = MathHelper.Transform(new List<Vector3> { v1OCS, v2OCS, v3OCS }, lwp.Normal, CoordinateSystem.Object, CoordinateSystem.World);


        }
        private static void WriteGradientPattern()
        {
            List<LwPolylineVertex> vertexes = new List<LwPolylineVertex>
                                            {
                                                new LwPolylineVertex(new Vector2(0, 0)),
                                                new LwPolylineVertex(new Vector2(0, 150)),
                                                new LwPolylineVertex(new Vector2(150, 150)),
                                                new LwPolylineVertex(new Vector2(150, 0))
                                            };
            LwPolyline pol = new LwPolyline(vertexes, true);


            Line line1 = new Line(new Vector2(0, 0), new Vector2(0, 150));
            Line line2 = new Line(new Vector2(0, 150), new Vector2(150, 150));
            Line line3 = new Line(new Vector2(150, 150), new Vector2(150, 0));
            Line line4 = new Line(new Vector2(150, 0), new Vector2(0, 0));

            AciColor color = new AciColor(63, 79, 127);
            HatchGradientPattern gradient = new HatchGradientPattern(color, AciColor.Blue, HatchGradientPatternType.Linear);
            //HatchGradientPattern gradient = new HatchGradientPattern(AciColor.Red, 0.75, HatchGradientPatternType.Linear);
            gradient.Angle = 30;

            List<HatchBoundaryPath> boundary = new List<HatchBoundaryPath>
                                                    {
                                                        new HatchBoundaryPath(new List<EntityObject> {pol})
                                                    };
            Hatch hatch = new Hatch(gradient, boundary, true);
            
            // gradients are only supported for AutoCad2004 and later
            DxfDocument dxf = new DxfDocument(DxfVersion.AutoCad2004);
            dxf.AddEntity(hatch);
            dxf.Save("gradient test.dxf");

            //DxfDocument dxf2 = DxfDocument.Load("gradient test.dxf");

            //dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2000;

            //dxf.Save("gradient test 2000.dxf");

        }
        private static void LineWidth()
        {
            // the line thickness works as expected, according to the AutoCAD way of doing things
            Line thickLine = new Line(new Vector3(0,10,0),  new Vector3(10,20,0));

            // when you assign a thickness to a line, the result is like a wall, it is like a 3d face whose vertexes are defined by the
            // start and end points of the line and the thickness along the normal of the line.
            thickLine.Thickness = 5;

            // maybe what you are trying to do is create a line with a width (something that we can read it as a line with thickness), the only way to do this is to create a polyline
            // the kind of result you will get if you give a width to a 2d polyline 
            // you can only give a width to a vertex of a Polyline or a LightweigthPolyline
            // I am planning to drop support to AutoCAD 12 dxf files, so to define a bidimensional polyline the only way will be to use lightweight polyline
            // (the Polyline class and the LightWeightPolyline are basically the same).
            LwPolyline widthLine = new LwPolyline();
            LwPolylineVertex startVertex = new LwPolylineVertex(new Vector2(0, 0));
            LwPolylineVertex endVertex = new LwPolylineVertex(new Vector2(10, 10));
            widthLine.Vertexes.AddRange(new[] { startVertex, endVertex });

            // the easy way to give a constant width to a polyline, but you can also give a polyline width by vertex
            // there is a mistake on my part, following the AutoCAD documentation I should have called the PolylineVertex.StartThickness and PolylineVertex.EndThickness as
            // PolylineVertex.StartWidth and PolylineVertex.EndWidth
            // SetConstantWidth is a sort cut that will assign the given value to every start width and end width of every vertex of the polyline
            widthLine.SetConstantWidth(0.5);

            DxfDocument dxf = new DxfDocument();

            // add the entities to the document (both of them to see the difference)
            dxf.AddEntity(thickLine);
            dxf.AddEntity(widthLine);

            dxf.Save("line width.dxf");

        }
        private static void HatchTest3()
        {
            DxfDocument dxf = new DxfDocument();

            LwPolyline poly = new LwPolyline();
            poly.Vertexes.Add(new LwPolylineVertex(-10, -10));
            poly.Vertexes.Add(new LwPolylineVertex(10, -10));
            poly.Vertexes.Add(new LwPolylineVertex(10, 10));
            poly.Vertexes.Add(new LwPolylineVertex(-10, 10));
            poly.Vertexes[2].Bulge = 1;
            poly.IsClosed = true;

            Ellipse ellipse = new Ellipse(Vector3.Zero, 16, 10);
            ellipse.Rotation = 0;
            ellipse.StartAngle = 0;
            ellipse.EndAngle = 180;

            LwPolyline poly2 = new LwPolyline();
            poly2.Vertexes.Add(new LwPolylineVertex(-8, 0));
            poly2.Vertexes.Add(new LwPolylineVertex(0, -4));
            poly2.Vertexes.Add(new LwPolylineVertex(8, 0));

            //Arc arc = new Arc(Vector3.Zero,8,180,0);
            //Line line =new Line(new Vector3(8,0,0), new Vector3(-8,0,0));

            List<HatchBoundaryPath> boundary = new List<HatchBoundaryPath>{
                                                                            new HatchBoundaryPath(new List<EntityObject>{poly}),
                                                                            new HatchBoundaryPath(new List<EntityObject>{poly2, ellipse})
                                                                            };

            Hatch hatch = new Hatch(HatchPattern.Line, boundary, true);
            hatch.Pattern.Angle = 45;
            //dxf.AddEntity(poly);
            //dxf.AddEntity(ellipse);
            ////dxf.AddEntity(arc);
            ////dxf.AddEntity(line);
            //dxf.AddEntity(poly2);
            dxf.AddEntity(hatch);


            dxf.Save("hatchTest3.dxf");
        }
        private static void HatchTest2()
        {
            DxfDocument dxf = new DxfDocument();

            LwPolyline poly = new LwPolyline();
            poly.Vertexes.Add(new LwPolylineVertex(-10, -10));
            poly.Vertexes.Add(new LwPolylineVertex(10, -10));
            poly.Vertexes.Add(new LwPolylineVertex(10, 10));
            poly.Vertexes.Add(new LwPolylineVertex(-10, 10));
            poly.Vertexes[2].Bulge = 1;
            poly.IsClosed = true;

            Circle circle = new Circle(Vector3.Zero, 5);

            Ellipse ellipse = new Ellipse(Vector3.Zero,16,10);
            ellipse.Rotation = 30;
            List<HatchBoundaryPath> boundary = new List<HatchBoundaryPath>{
                                                                            new HatchBoundaryPath(new List<EntityObject>{poly}),
                                                                            new HatchBoundaryPath(new List<EntityObject>{circle}),
                                                                            new HatchBoundaryPath(new List<EntityObject>{ellipse})
                                                                            };

            Hatch hatch = new Hatch(HatchPattern.Line, boundary, false);
            hatch.Pattern.Angle = 150;
            hatch.Pattern.Scale = 5;
            //hatch.Normal = new Vector3(1,1,1);
            //hatch.Elevation = 23;
            //dxf.AddEntity(poly);
            //dxf.AddEntity(circle);
            //dxf.AddEntity(ellipse);
            dxf.AddEntity(hatch);
            hatch.CreateBoundary(true);
            dxf.Save("hatchTest2.dxf");
            dxf = DxfDocument.Load("hatchTest2.dxf");
            dxf.Save("hatchTest2 copy.dxf");
        }
        private static void HatchTest1()
        {
            DxfDocument dxf = new DxfDocument();

            LwPolyline poly = new LwPolyline();
            poly.Vertexes.Add(new LwPolylineVertex(-10, -10));
            poly.Vertexes.Add(new LwPolylineVertex(10, -10));
            poly.Vertexes.Add(new LwPolylineVertex(10, 10));
            poly.Vertexes.Add(new LwPolylineVertex(-10, 10));
            poly.Vertexes[2].Bulge = 1;
            poly.IsClosed = true;

            LwPolyline poly2 = new LwPolyline();
            poly2.Vertexes.Add(new LwPolylineVertex(-5, -5));
            poly2.Vertexes.Add(new LwPolylineVertex(5, -5));
            poly2.Vertexes.Add(new LwPolylineVertex(5, 5));
            poly2.Vertexes.Add(new LwPolylineVertex(-5, 5));
            poly2.Vertexes[1].Bulge = -0.25;
            poly2.IsClosed = true;

            LwPolyline poly3 = new LwPolyline();
            poly3.Vertexes.Add(new LwPolylineVertex(-8, -8));
            poly3.Vertexes.Add(new LwPolylineVertex(-6, -8));
            poly3.Vertexes.Add(new LwPolylineVertex(-6, -6));
            poly3.Vertexes.Add(new LwPolylineVertex(-8, -6));
            poly3.IsClosed = true;

            Line line = new Line(new Vector2(-5, -5), new Vector2(5, -5));
            List<HatchBoundaryPath> boundary = new List<HatchBoundaryPath>{
                                                                            new HatchBoundaryPath(new List<EntityObject>{line, poly}),
                                                                            new HatchBoundaryPath(new List<EntityObject>{poly2}),
                                                                            new HatchBoundaryPath(new List<EntityObject>{poly3}),
                                                                            };
            Hatch hatch = new Hatch(HatchPattern.Net, boundary, true);
            hatch.Layer = new Layer("hatch")
                                {
                                    Color = AciColor.Red,
                                    LineType = LineType.Continuous
                                };
            hatch.Pattern.Angle = 30;
            hatch.Elevation = 52;
            hatch.Normal = new Vector3(1,1,0);
            hatch.Pattern.Scale = 1 / hatch.Pattern.LineDefinitions[0].Delta.Y;
            //dxf.AddEntity(poly);
            //dxf.AddEntity(poly2);
            //dxf.AddEntity(poly3);
            dxf.AddEntity(hatch);
            dxf.AddEntity(hatch.CreateBoundary(true));

            dxf.Save("hatchTest1.dxf");
            dxf = DxfDocument.Load("hatchTest1.dxf");
        }
        private void WriteLightWeightPolyline(LwPolyline polyline)
        {
            this.chunk.Write(100, SubclassMarker.LightWeightPolyline);
            this.chunk.Write(90, polyline.Vertexes.Count);
            this.chunk.Write(70, (short) polyline.Flags);

            this.chunk.Write(38, polyline.Elevation);
            this.chunk.Write(39, polyline.Thickness);


            foreach (LwPolylineVertex v in polyline.Vertexes)
            {
                this.chunk.Write(10, v.Position.X);
                this.chunk.Write(20, v.Position.Y);
                this.chunk.Write(40, v.StartWidth);
                this.chunk.Write(41, v.EndWidth);
                this.chunk.Write(42, v.Bulge);
            }

            this.chunk.Write(210, polyline.Normal.X);
            this.chunk.Write(220, polyline.Normal.Y);
            this.chunk.Write(230, polyline.Normal.Z);

            this.WriteXData(polyline.XData);
        }
        private static void ComplexHatch()
        {
            HatchPattern pattern = HatchPattern.FromFile("hatch\\acad.pat", "ESCHER");
            pattern.Scale = 1.5;
            pattern.Angle = 30;

            LwPolyline poly = new LwPolyline();
            poly.Vertexes.Add(new LwPolylineVertex(-10, -10));
            poly.Vertexes.Add(new LwPolylineVertex(10, -10));
            poly.Vertexes.Add(new LwPolylineVertex(10, 10));
            poly.Vertexes.Add(new LwPolylineVertex(-10, 10));
            poly.IsClosed = true;

            List<HatchBoundaryPath> boundary = new List<HatchBoundaryPath>
                {
                    new HatchBoundaryPath(new List<EntityObject> {poly})
                };
            Hatch hatch = new Hatch(pattern, boundary, true);
            
            DxfDocument dxf = new DxfDocument();
            dxf.AddEntity(poly);
            dxf.AddEntity(hatch);
            dxf.Save("complexhatch.dxf");

            DxfDocument dxf2 = DxfDocument.Load("complexhatch.dxf");
            dxf2.Save("complexhatch2.dxf");

        }
Exemple #29
0
 public HndzPolyline(DXF.LwPolyline dxfLwPolyline) : this(HndzResources.DefaultName, HndzResources.DefaultDescription, dxfLwPolyline)
 {
 }
Exemple #30
0
        private LwPolyline ReadLwPolyline()
        {
            double elevation = 0.0;
            double thickness = 0.0;
            PolylinetypeFlags flags = PolylinetypeFlags.OpenPolyline;
            double constantWidth = -1.0;
            List<LwPolylineVertex> polVertexes = new List<LwPolylineVertex>();
            LwPolylineVertex v = null;
            double vX = 0.0;
            Vector3 normal = Vector3.UnitZ;

            List<XData> xData = new List<XData>();

            this.chunk.Next();

            while (this.chunk.Code != 0)
            {
                switch (this.chunk.Code)
                {
                    case 38:
                        elevation = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 39:
                        thickness = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 43:
                        // constant width (optional; default = 0). If present it will override any vertex width (codes 40 and/or 41)
                        constantWidth = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 70:
                        flags = (PolylinetypeFlags) this.chunk.ReadShort();
                        this.chunk.Next();
                        break;
                    case 90:
                        //numVertexes = int.Parse(code.Value);
                        this.chunk.Next();
                        break;
                    case 10:
                        vX = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 20:
                        double vY = this.chunk.ReadDouble();
                        v = new LwPolylineVertex(vX, vY);
                        polVertexes.Add(v);
                        this.chunk.Next();
                        break;
                    case 40:
                        double startWidth = this.chunk.ReadDouble();
                        if (v != null)
                            if (startWidth >= 0.0)
                                v.StartWidth = startWidth;
                        this.chunk.Next();
                        break;
                    case 41:
                        double endWidth = this.chunk.ReadDouble();
                        if (v != null)
                            if (endWidth >= 0.0)
                                v.EndWidth = endWidth;
                        this.chunk.Next();
                        break;
                    case 42:
                        if (v != null)
                            v.Bulge = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 210:
                        normal.X = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 220:
                        normal.Y = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 230:
                        normal.Z = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 1001:
                        string appId = this.DecodeEncodedNonAsciiCharacters(this.chunk.ReadString());
                        XData data = this.ReadXDataRecord(this.GetApplicationRegistry(appId));
                        xData.Add(data);
                        break;
                    default:
                        if (this.chunk.Code >= 1000 && this.chunk.Code <= 1071)
                            throw new Exception("The extended data of an entity must start with the application registry code.");

                        this.chunk.Next();
                        break;
                }
            }

            LwPolyline entity = new LwPolyline
            {
                Elevation = elevation,
                Thickness = thickness,
                Flags = flags,
                Normal = normal
            };

            entity.Vertexes.AddRange(polVertexes);

            if (constantWidth >= 0.0)
                entity.SetConstantWidth(constantWidth);

            entity.XData.AddRange(xData);

            return entity;
        }
Exemple #31
0
        private EntityObject ReadPolyline()
        {
            // the entity Polyline in dxf can actually hold three kinds of entities
            // 3d polyline is the generic polyline
            // polyface mesh
            // polylines 2d is the old way of writing polylines the AutoCAD2000 and newer always use LwPolylines to define a 2d polyline
            // this way of reading 2d polylines is here for compatibility reasons with older dxf versions.
            PolylinetypeFlags flags = PolylinetypeFlags.OpenPolyline;
            PolylineSmoothType smoothType = PolylineSmoothType.NoSmooth;
            double elevation = 0.0;
            double thickness = 0.0;
            Vector3 normal = Vector3.UnitZ;
            List<Vertex> vertexes = new List<Vertex>();
            List<XData> xData = new List<XData>();

            this.chunk.Next();

            while (this.chunk.Code != 0)
            {
                switch (this.chunk.Code)
                {
                    case 30:
                        elevation = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 39:
                        thickness = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 70:
                        flags = (PolylinetypeFlags) this.chunk.ReadShort();
                        this.chunk.Next();
                        break;
                    case 75:
                        smoothType = (PolylineSmoothType) this.chunk.ReadShort();
                        this.chunk.Next();
                        break;
                    case 71:
                        //this field might not exist for polyface meshes, we cannot depend on it
                        //numVertexes = int.Parse(code.Value); code = this.ReadCodePair();
                        this.chunk.Next();
                        break;
                    case 72:
                        //this field might not exist for polyface meshes, we cannot depend on it
                        this.chunk.Next();
                        break;
                    case 210:
                        normal.X = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 220:
                        normal.Y = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 230:
                        normal.Z = this.chunk.ReadDouble();
                        this.chunk.Next();
                        break;
                    case 1001:
                        string appId = this.DecodeEncodedNonAsciiCharacters(this.chunk.ReadString());
                        XData data = this.ReadXDataRecord(this.GetApplicationRegistry(appId));
                        xData.Add(data);
                        break;
                    default:
                        if (this.chunk.Code >= 1000 && this.chunk.Code <= 1071)
                            throw new Exception("The extended data of an entity must start with the application registry code.");
                        this.chunk.Next();
                        break;
                }
            }

            //begin to read the vertex list (although it is not recommended the vertex list might have 0 entries)
            while (this.chunk.ReadString() != DxfObjectCode.EndSequence)
            {
                if (this.chunk.ReadString() == DxfObjectCode.Vertex)
                {
                    Vertex vertex = this.ReadVertex();
                    vertexes.Add(vertex);
                }
            }

            // read the end sequence object until a new element is found
            this.chunk.Next();
            string endSequenceHandle = null;
            while (this.chunk.Code != 0)
            {
                switch (this.chunk.Code)
                {
                    case 5:
                        endSequenceHandle = this.chunk.ReadHex();
                        this.chunk.Next();
                        break;
                    case 8:
                        // the polyline EndSequence layer should be the same as the polyline layer
                        this.chunk.Next();
                        break;
                    default:
                        this.chunk.Next();
                        break;
                }
            }

            EntityObject pol;
            bool isClosed = flags.HasFlag(PolylinetypeFlags.ClosedPolylineOrClosedPolygonMeshInM);

            //to avoid possible error between the vertex type and the polyline type
            //the polyline type will decide which information to use from the read vertex
            if (flags.HasFlag(PolylinetypeFlags.Polyline3D))
            {
                List<PolylineVertex> polyline3dVertexes = new List<PolylineVertex>();
                foreach (Vertex v in vertexes)
                {
                    PolylineVertex vertex = new PolylineVertex
                    {
                        Flags = v.Flags,
                        Position = v.Position,
                        Handle = v.Handle,
                    };
                    polyline3dVertexes.Add(vertex);
                }

                pol = new Polyline(polyline3dVertexes, isClosed)
                {
                    Flags = flags,
                    SmoothType = smoothType,
                    Normal = normal
                };
                ((Polyline) pol).EndSequence.Handle = endSequenceHandle;
            }
            else if (flags.HasFlag(PolylinetypeFlags.PolyfaceMesh))
            {
                //the vertex list created contains vertex and face information
                List<PolyfaceMeshVertex> polyfaceVertexes = new List<PolyfaceMeshVertex>();
                List<PolyfaceMeshFace> polyfaceFaces = new List<PolyfaceMeshFace>();
                foreach (Vertex v in vertexes)
                {
                    if (v.Flags.HasFlag(VertexTypeFlags.PolyfaceMeshVertex | VertexTypeFlags.Polygon3dMesh))
                    {
                        PolyfaceMeshVertex vertex = new PolyfaceMeshVertex
                        {
                            Location = v.Position,
                            Handle = v.Handle,
                        };
                        polyfaceVertexes.Add(vertex);
                    }
                    else if (v.Flags.HasFlag(VertexTypeFlags.PolyfaceMeshVertex))
                    {
                        PolyfaceMeshFace vertex = new PolyfaceMeshFace(v.VertexIndexes)
                        {
                            Handle = v.Handle
                        };
                        polyfaceFaces.Add(vertex);
                    }
                }
                pol = new PolyfaceMesh(polyfaceVertexes, polyfaceFaces)
                {
                    Normal = normal
                };
                ((PolyfaceMesh) pol).EndSequence.Handle = endSequenceHandle;
            }
            else
            {
                List<LwPolylineVertex> polylineVertexes = new List<LwPolylineVertex>();
                foreach (Vertex v in vertexes)
                {
                    LwPolylineVertex vertex = new LwPolylineVertex
                    {
                        Position = new Vector2(v.Position.X, v.Position.Y),
                        StartWidth = v.StartWidth,
                        Bulge = v.Bulge,
                        EndWidth = v.EndWidth,
                    };

                    polylineVertexes.Add(vertex);
                }

                pol = new LwPolyline(polylineVertexes, isClosed)
                {
                    Flags = flags,
                    Thickness = thickness,
                    Elevation = elevation,
                    Normal = normal,
                };
            }

            pol.XData.AddRange(xData);

            return pol;
        }
        private static void HatchTest4()
        {
            DxfDocument dxf = new DxfDocument(DxfVersion.AutoCad2010);

            LwPolyline poly = new LwPolyline();
            poly.Vertexes.Add(new LwPolylineVertex(-10, -10));
            poly.Vertexes.Add(new LwPolylineVertex(10, -10));
            poly.Vertexes.Add(new LwPolylineVertex(10, 10));
            poly.Vertexes.Add(new LwPolylineVertex(-10, 10));
            poly.IsClosed = true;

            List<HatchBoundaryPath> boundary = new List<HatchBoundaryPath>() { new HatchBoundaryPath(new List<EntityObject>()) }; ;
            //List<HatchBoundaryPath> boundary = new List<HatchBoundaryPath> {new HatchBoundaryPath(new List<Entity> {poly})};
            HatchGradientPattern pattern = new HatchGradientPattern(AciColor.Yellow, AciColor.Blue, HatchGradientPatternType.Linear);
            pattern.Origin = new Vector2(120, -365);
            Hatch hatch = new Hatch(pattern, boundary, true);
            dxf.AddEntity(hatch);
            dxf.AddEntity(hatch.CreateBoundary(true));
            
            dxf.Save("HatchTest4.dxf");
            dxf = DxfDocument.Load("HatchTest4.dxf");
            dxf.Save("HatchTest4 copy.dxf");

        }
        private static void CustomHatchPattern()
        {
            DxfDocument dxf = new DxfDocument();

            LwPolyline poly = new LwPolyline();
            poly.Vertexes.Add(new LwPolylineVertex(-10, -10));
            poly.Vertexes.Add(new LwPolylineVertex(10, -10));
            poly.Vertexes.Add(new LwPolylineVertex(10, 10));
            poly.Vertexes.Add(new LwPolylineVertex(-10, 10));
            poly.Vertexes[2].Bulge = 1;
            poly.IsClosed = true;

            LwPolyline poly2 = new LwPolyline();
            poly2.Vertexes.Add(new LwPolylineVertex(-5, -5));
            poly2.Vertexes.Add(new LwPolylineVertex(5, -5));
            poly2.Vertexes.Add(new LwPolylineVertex(5, 5));
            poly2.Vertexes.Add(new LwPolylineVertex(-5, 5));
            poly2.Vertexes[1].Bulge = -0.25;
            poly2.IsClosed = true;

            LwPolyline poly3 = new LwPolyline();
            poly3.Vertexes.Add(new LwPolylineVertex(-8, -8));
            poly3.Vertexes.Add(new LwPolylineVertex(-6, -8));
            poly3.Vertexes.Add(new LwPolylineVertex(-6, -6));
            poly3.Vertexes.Add(new LwPolylineVertex(-8, -6));
            poly3.IsClosed = true;

            List<HatchBoundaryPath> boundary = new List<HatchBoundaryPath>{
                                                                            new HatchBoundaryPath(new List<EntityObject>{poly}),
                                                                            new HatchBoundaryPath(new List<EntityObject>{poly2}),
                                                                            new HatchBoundaryPath(new List<EntityObject>{poly3}),
                                                                            };

            HatchPattern pattern = new HatchPattern("MyPattern", "A custom hatch pattern");

            HatchPatternLineDefinition line1 = new HatchPatternLineDefinition();
            line1.Angle = 45;
            line1.Origin = Vector2.Zero;
            line1.Delta=new Vector2(4,4);
            line1.DashPattern.Add(12);
            line1.DashPattern.Add(-4);
            pattern.LineDefinitions.Add(line1);

            HatchPatternLineDefinition line2 = new HatchPatternLineDefinition();
            line2.Angle = 135;
            line2.Origin = new Vector2(2.828427125, 2.828427125);
            line2.Delta = new Vector2(4,-4);
            line2.DashPattern.Add(12);
            line2.DashPattern.Add(-4);
            pattern.LineDefinitions.Add(line2);

            Hatch hatch = new Hatch(pattern, boundary, true);
            hatch.Layer = new Layer("hatch")
            {
                Color = AciColor.Red,
                LineType = LineType.Continuous
            };
            hatch.Pattern.Angle = 0;
            hatch.Pattern.Scale = 1;
            dxf.AddEntity(poly);
            dxf.AddEntity(poly2);
            dxf.AddEntity(poly3);
            dxf.AddEntity(hatch);

            dxf.Save("hatchTest.dxf");
        }
Exemple #34
0
 private static LwPolyline ProcessLwPolyline(LwPolyline polyline, Vector3 normal, double elevation)
 {
     polyline.Elevation = elevation;
     polyline.Normal    = normal;
     return(polyline);
 }
        private static void LwPolyline()
        {
            DxfDocument dxf = new DxfDocument();

            LwPolyline poly = new LwPolyline();
            poly.Vertexes.Add(new LwPolylineVertex(0, 0));
            poly.Vertexes.Add(new LwPolylineVertex(10, 10));
            poly.Vertexes.Add(new LwPolylineVertex(20, 0));
            poly.Vertexes.Add(new LwPolylineVertex(30, 10));
            poly.SetConstantWidth(2);
            //poly.IsClosed = true;
            dxf.AddEntity(poly);

            dxf.Save("lwpolyline.dxf");

            dxf = DxfDocument.Load("lwpolyline.dxf");
        }