示例#1
4
        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");
        }
示例#2
1
        private static void Polyline()
        {
            DxfDocument dxf = new DxfDocument();
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2010;
            Polyline poly = new Polyline();
            poly.Vertexes.Add(new PolylineVertex(0, 0, 0));
            poly.Vertexes.Add(new PolylineVertex(10, 10, 0));
            poly.Vertexes.Add(new PolylineVertex(20, 0, 0));
            poly.Vertexes.Add(new PolylineVertex(30, 10, 0));
            dxf.AddEntity(poly);

            dxf.Save("polyline.dxf");

        }
示例#3
1
        static void doDXF(List<PolylineVertex> vertexes, double[] x)
        {
            // create a dxf for those who want to "see" the calibration
            netDxf.DxfDocument dxf = new netDxf.DxfDocument();

            Polyline polyline = new Polyline(vertexes, true);
            polyline.Layer = new Layer("polyline");
            polyline.Layer.Color.Index = 24;
            dxf.AddEntity(polyline);

            var pnt = new Point(new netDxf.Vector3(-(float) x[0], -(float) x[1], -(float) x[2]));
            pnt.Layer = new Layer("new offset");
            pnt.Layer.Color.Index = 21;
            dxf.AddEntity(pnt);

            dxf.Save(Settings.GetUserDataDirectory() + "magoffset.dxf");

            log.Info("dxf Done " + DateTime.Now);
        }
示例#4
0
        /// <summary>
        /// Convert to a DXF Polyline entity.
        /// </summary>
        public static dxf.Polyline ToDxf(this Polyline polyline)
        {
            IEnumerable <netDxf.Vector3> vertices = polyline.Vertices.Select(v => v.ToDxf());
            var dxf = new dxf.Polyline(vertices, true);

            return(dxf);
        }
示例#5
0
        void ReadInsert(netDxf.Entities.Insert insert)
        {
            List <netDxf.Entities.IEntityObject> entities = insert.Block.Entities;

            netDxf.Entities.IEntityObject entity = null;

            for (int index = 1; index < entities.Count; index++)
            {
                entity = entities[index];

                if (entity is Line)
                {
                    ReadLine((Line)entity, insert.InsertionPoint.X, insert.InsertionPoint.Y);
                }
                else if (entity is netDxf.Entities.Arc)
                {
                    ReadArc((netDxf.Entities.Arc)entity, insert.InsertionPoint.X, insert.InsertionPoint.Y);
                }
                else if (entity is netDxf.Entities.Circle)
                {
                    ReadCircle((netDxf.Entities.Circle)entity, insert.InsertionPoint.X, insert.InsertionPoint.Y);
                }
                else if (entity is IPolyline)
                {
                    netDxf.Entities.Polyline polyline = CastPolyline((IPolyline)entity);
                    if (polyline != null)
                    {
                        ReadPolyline(polyline.Vertexes, polyline.IsClosed, 0, 0);
                    }
                }
            }
        }
示例#6
0
文件: ToDXF.cs 项目: lulzzz/Nucleus
        /// <summary>
        /// Convert a FreeBuid polyline to a netDXF one
        /// </summary>
        /// <param name="polyline"></param>
        /// <returns></returns>
        public static nDE.Polyline Convert(PolyLine polyline)
        {
            var result = new nDE.Polyline(Convert(polyline.Vertices.ExtractPoints()));

            result.IsClosed = polyline.Closed;
            SetAttributes(result, polyline.Attributes);
            return(result);
        }
示例#7
0
 /// <summary>
 /// Convert a netDXF polyline to a Nucleus one
 /// </summary>
 /// <param name="polyLine"></param>
 /// <returns></returns>
 public static PolyLine Convert(netDxf.Entities.Polyline polyLine)
 {
     var pts = new List<Vector>();
     foreach (netDxf.Entities.PolylineVertex plV in polyLine.Vertexes)
     {
         pts.Add(Convert(plV.Position));
     }
     return new PolyLine(pts, polyLine.IsClosed, ExtractAttributes(polyLine));
 }
示例#8
0
 void ReadPolylines()
 {
     foreach (var item in doc.Polylines)
     {
         netDxf.Entities.Polyline polyline = CastPolyline(item);
         if (polyline != null)
         {
             ReadPolyline(polyline.Vertexes, polyline.IsClosed, 0, 0);
         }
     }
 }
示例#9
0
        void ReadEntities()
        {
            foreach (IEntityObject aEntity in doc.Entities)
            {
                switch (aEntity.Type)
                {
                case EntityType.Arc:
                    ReadArc(aEntity as netDxf.Entities.Arc, 0, 0);
                    (drawing.Figures[drawing.Figures.Count - 1] as FigureBase).Tag = aEntity;
                    break;

                case EntityType.Circle:
                    ReadCircle(aEntity as netDxf.Entities.Circle, 0, 0);
                    (drawing.Figures[drawing.Figures.Count - 1] as FigureBase).Tag = aEntity;
                    break;

                //case EntityType.Ellipse:
                //    ReadEllipse(aEntity as netDxf.Entities.Ellipse, 0, 0);
                //    break;
                case EntityType.Line:
                    ReadLine(aEntity as netDxf.Entities.Line, 0, 0);
                    (drawing.Figures[drawing.Figures.Count - 1] as FigureBase).Tag = aEntity;
                    break;

                case EntityType.Polyline:
                    netDxf.Entities.Polyline polyline = CastPolyline(aEntity);
                    if (polyline != null)
                    {
                        ReadPolyline(polyline.Vertexes, polyline.IsClosed, 0, 0);
                    }
                    (drawing.Figures[drawing.Figures.Count - 1] as FigureBase).Tag = aEntity;
                    break;

                case EntityType.Insert:
                    netDxf.Entities.Insert insert = aEntity as netDxf.Entities.Insert;
                    foreach (netDxf.Entities.Line aLine in insert.Block.Entities)
                    {
                        ReadLine(aLine, insert.InsertionPoint.X, insert.InsertionPoint.Y);
                        (drawing.Figures[drawing.Figures.Count - 1] as FigureBase).Tag = aLine;
                    }
                    break;

                default:
                    break;
                }
            }
        }
示例#10
0
 netDxf.Entities.Polyline CastPolyline(IPolyline item)
 {
     netDxf.Entities.Polyline polyline = null;
     if (item is LightWeightPolyline)
     {
         polyline = ((LightWeightPolyline)item).ToPolyline();
     }
     else if (item is Polyline)
     {
         polyline = (netDxf.Entities.Polyline)item;
     }
     else
     {
         polyline = null;
     }
     return(polyline);
 }
示例#11
0
 netDxf.Entities.Polyline CastPolyline(IEntityObject item) //IPolyline item)
 {
     netDxf.Entities.Polyline polyline = null;
     //if (item is LightWeightPolyline)
     //{
     //    polyline = ((LightWeightPolyline)item).ToPolyline();
     //}
     //else
     if (item is Polyline)
     {
         polyline = (netDxf.Entities.Polyline)item;
     }
     else
     {
         polyline = null;
     }
     return(polyline);
 }
示例#12
0
文件: Spline.cs 项目: Core2D/netdxf
        /// <summary>
        /// Converts the spline in a Polyline.
        /// </summary>
        /// <param name="precision">Number of vertexes generated.</param>
        /// <returns>A new instance of <see cref="Polyline">Polyline</see> that represents the spline.</returns>
        public Polyline ToPolyline(int precision)
        {
            IEnumerable<Vector3> vertexes = this.PolygonalVertexes(precision);

            Polyline poly = new Polyline
            {
                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,
                IsClosed = this.isClosed
            };
            foreach (Vector3 v in vertexes)
            {
                poly.Vertexes.Add(new PolylineVertex(v));
            }
            return poly;
        }
示例#13
0
        private void AddPolylines()
        {
            foreach (var p in dxf.Polylines)
            {
                if (p.Type == netDxf.Entities.EntityType.Polyline)
                {
                    netDxf.Entities.Polyline polygon = (netDxf.Entities.Polyline)p;
                    PathFigure           path        = new PathFigure();
                    float                bulge       = 0;
                    System.Windows.Point prePoint    = new System.Windows.Point();
                    System.Windows.Point point       = new System.Windows.Point();

                    path.IsClosed = polygon.IsClosed;

                    for (int i = 0; i < polygon.Vertexes.Count(); ++i)
                    {
                        var seg = polygon.Vertexes[i];
                        point = new System.Windows.Point(seg.Position.X, -seg.Position.Y);

                        if (i == 0)
                        {
                            path.StartPoint = point;
                            prePoint        = point;
                            //bulge = seg.Bulge;
                            //angle = 4 * System.Math.Atan(seg.Bulge) / Math.PI * 180;
                        }
                        else
                        {
                            ArcSegment arc = new ArcSegment();
                            arc.Point = point;

                            //if (angle != 0)
                            if (bulge != 0)
                            {
                                double angle  = 4 * Math.Atan(Math.Abs(bulge)) / Math.PI * 180;
                                double length = Math.Sqrt((point.X - prePoint.X) * (point.X - prePoint.X) + (point.Y - prePoint.Y) * (point.Y - prePoint.Y));
                                //double radius = length / (Math.Sqrt(2 * (1 - Math.Cos(angle / 180 * Math.PI))));

                                double radius = Math.Abs(length / (2 * Math.Sin(angle / 360 * Math.PI)));

                                arc.Size          = new System.Windows.Size(radius, radius);
                                arc.RotationAngle = angle;

                                arc.SweepDirection = bulge < 0 ? SweepDirection.Clockwise : SweepDirection.Counterclockwise;
                                arc.IsLargeArc     = Math.Abs(bulge) > 1 ? true : false;
                            }

                            prePoint = point;
                            //bulge = seg.Bulge;
                            //angle = 4 * System.Math.Atan(seg.Bulge) / Math.PI * 180;
                            path.Segments.Add(arc);
                        }
                    }
                    PathGeometry pathgeo = new PathGeometry();
                    pathgeo.Figures.Add(path);

                    //foreach (var _p in MyCanvas.Children)
                    //{
                    //    System.Windows.Shapes.Path _path = (System.Windows.Shapes.Path)_p;

                    //    if ((string)_path.Tag == p.Layer.Name)
                    //    {

                    //        ((GeometryGroup)_path.Data).Children.Add(pathgeo);
                    //    }
                    //}
                    System.Windows.Shapes.Path _path = new System.Windows.Shapes.Path();

                    GeometryGroup GeoGroup = new GeometryGroup();
                    GeoGroup.Children.Add(pathgeo);
                    _path.Stroke = new SolidColorBrush(System.Windows.Media.Colors.White);

                    _path.Data                 = GeoGroup;
                    _path.Tag                  = "polylines";
                    _path.StrokeThickness      = 2;
                    _path.MouseLeftButtonDown += (o, s) =>
                    {
                        CalculateIntersection(_path);
                    };


                    MyCanvas.Children.Add(_path);
                }
                //if (p.Flags == PolylineTypeFlags.OpenPolyline || p.Flags == PolylineTypeFlags.ClosedPolylineOrClosedPolygonMeshInM)
                //{
                //    LightWeightPolyline polygon = (LightWeightPolyline)p;
                //    PathFigure path = new PathFigure();
                //    float bulge = 0;
                //    System.Windows.Point prePoint = new System.Windows.Point();
                //    System.Windows.Point point = new System.Windows.Point();

                //    path.IsClosed = polygon.IsClosed;

                //    for (int i = 0; i < polygon.Vertexes.Count(); ++i)
                //    {
                //        var seg = polygon.Vertexes[i];
                //        point = new System.Windows.Point(seg.Location.X, -seg.Location.Y);

                //        if (i == 0)
                //        {
                //            path.StartPoint = point;
                //            prePoint = point;
                //            bulge = seg.Bulge;
                //            //angle = 4 * System.Math.Atan(seg.Bulge) / Math.PI * 180;
                //        }
                //        else
                //        {
                //            ArcSegment arc = new ArcSegment();
                //            arc.Point = point;

                //            //if (angle != 0)
                //            if (bulge != 0)
                //            {
                //                double angle = 4 * Math.Atan(Math.Abs(bulge)) / Math.PI * 180;
                //                double length = Math.Sqrt((point.X - prePoint.X) * (point.X - prePoint.X) + (point.Y - prePoint.Y) * (point.Y - prePoint.Y));
                //                //double radius = length / (Math.Sqrt(2 * (1 - Math.Cos(angle / 180 * Math.PI))));

                //                double radius = Math.Abs(length / (2 * Math.Sin(angle / 360 * Math.PI)));

                //                arc.Size = new System.Windows.Size(radius, radius);
                //                arc.RotationAngle = angle;

                //                arc.SweepDirection = bulge < 0 ? SweepDirection.Clockwise : SweepDirection.Counterclockwise;
                //                arc.IsLargeArc = Math.Abs(bulge) > 1 ? true : false;
                //            }

                //            prePoint = point;
                //            bulge = seg.Bulge;
                //            //angle = 4 * System.Math.Atan(seg.Bulge) / Math.PI * 180;
                //            path.Segments.Add(arc);

                //        }
                //    }
                //    PathGeometry pathgeo = new PathGeometry();
                //    pathgeo.Figures.Add(path);

                //    //foreach (var _p in MyCanvas.Children)
                //    //{
                //    //    System.Windows.Shapes.Path _path = (System.Windows.Shapes.Path)_p;

                //    //    if ((string)_path.Tag == p.Layer.Name)
                //    //    {

                //    //        ((GeometryGroup)_path.Data).Children.Add(pathgeo);
                //    //    }
                //    //}
                //    System.Windows.Shapes.Path _path = new System.Windows.Shapes.Path();

                //    GeometryGroup GeoGroup = new GeometryGroup();
                //    GeoGroup.Children.Add(pathgeo);
                //    _path.Stroke = new SolidColorBrush(System.Windows.Media.Colors.White);

                //    _path.Data = GeoGroup;
                //    _path.Tag = "polylines";
                //    _path.StrokeThickness = 2;
                //    _path.MouseLeftButtonDown += (o, s) =>
                //    {
                //        CalculateIntersection(_path);
                //    };


                //    MyCanvas.Children.Add(_path);
                //}
            }
        }
示例#14
0
        private void WritePolyline2d(Polyline polyline)
        {
            if (this.activeSection != StringCode.EntitiesSection && !this.isBlockEntities)
            {
                throw new InvalidDxfSectionException(this.activeSection, this.file);
            }

            this.WriteCodePair(0, polyline.CodeName);
            this.WriteCodePair(100, SubclassMarker.Entity);
            this.WriteEntityCommonCodes(polyline);
            this.WriteCodePair(5, polyline.Handle);
            this.WriteCodePair(100, SubclassMarker.Polyline);

            this.WriteCodePair(70, (int) polyline.Flags);

            //dummy point
            this.WriteCodePair(10, 0.0);
            this.WriteCodePair(20, 0.0);

            this.WriteCodePair(30, polyline.Elevation);
            this.WriteCodePair(39, polyline.Thickness);

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

            //Obsolete; formerly an “entities follow flag” (optional; ignore if present)
            //but its needed to load the dxf file in AutoCAD
            this.WriteCodePair(66, "1");

            this.WriteXData(polyline.XData);

            foreach (PolylineVertex v in polyline.Vertexes)
            {

                this.WriteCodePair(0, v.CodeName);
                this.WriteCodePair(5, v.Handle);
                this.WriteCodePair(100, SubclassMarker.Entity);
                this.WriteCodePair(8, v.Layer);
                this.WriteCodePair(100, SubclassMarker.Vertex);
                this.WriteCodePair(100, SubclassMarker.PolylineVertex);
                this.WriteCodePair(70, (int) v.Flags);
                this.WriteCodePair(10, v.Location.X);
                this.WriteCodePair(20, v.Location.Y);
                this.WriteCodePair(40, v.BeginThickness);
                this.WriteCodePair(41, v.EndThickness);
                this.WriteCodePair(42, v.Bulge);

                this.WriteXData(v.XData);
            }

            this.WriteCodePair(0, polyline.EndSequence.CodeName);
            this.WriteCodePair(5, polyline.EndSequence.Handle);
            this.WriteCodePair(100, SubclassMarker.Entity);
            this.WriteCodePair(8, polyline.EndSequence.Layer);
        }
            public override object Clone()
            {
                Polyline copy = new Polyline
                {
                    Vertexes = new Vector3[this.Vertexes.Length],
                    IsClosed = this.IsClosed
                };

                for (int i = 0; i < this.Vertexes.Length; i++)
                {
                    copy.Vertexes[i] = this.Vertexes[i];
                }
                return copy;
            }
示例#16
0
		public void WriteProject(WaveguideDesignerProjectData project)
			{
			if( project == null ) return;

			Type type;
			EntityObject obj = null;
			DxfDocument doc = new DxfDocument();

			doc.Name = project.Name;

			Layer dxfLayer;
			LayerData layerData;
			foreach( VirtualLayer vLayer in project.VirtualGraphics.Layers )
				{
				layerData = null;
				foreach( LayerData tmp in project.Layers )
					if( tmp.VirtualLayer == vLayer )
						{
						layerData = tmp;
						break;
						}
				if( layerData == null ) continue;
				dxfLayer = new Layer( layerData.Name );
				dxfLayer.Color.Index = (short)layerData.LayerNumber;
				doc.Layers.Add( dxfLayer );

				foreach( VirtualShapeBase vShape in vLayer.Shapes )
					{
					type = vShape.GetType();
					if( type == typeof( VirtualRectangle ) )
						{
						VirtualRectangle rect = (VirtualRectangle)vShape;
						Polyline dxfrect = new Polyline();
						obj = new Polyline();
						dxfrect.IsClosed = true;
						dxfrect.Vertexes.Add( new PolylineVertex( rect.Location.X, rect.Location.Y, 0 ) );
						dxfrect.Vertexes.Add( new PolylineVertex( rect.Location.X + rect.Size.W, rect.Location.Y, 0 ) );
						dxfrect.Vertexes.Add( new PolylineVertex( rect.Location.X + rect.Size.W, rect.Location.Y + rect.Size.H, 0 ) );
						dxfrect.Vertexes.Add( new PolylineVertex( rect.Location.X, rect.Location.Y + rect.Size.H, 0 ) );
						dxfrect.Vertexes.Add( new PolylineVertex( rect.Location.X, rect.Location.Y, 0 ) );
						obj = dxfrect;
						}
					else if( type == typeof( VirtualPolygon ) )
						{
						VirtualPolygon poly = (VirtualPolygon)vShape;
						Polyline dxfpoly = new Polyline();
						dxfpoly.IsClosed = true;
						foreach( PointD p in poly.Vertices )
							dxfpoly.Vertexes.Add( conv( p ) );
						dxfpoly.Vertexes.Add( conv( poly.Vertices[0] ) );
						obj = dxfpoly;
						}
					else if( type == typeof( VirtualEllipse ) )
						{
						VirtualEllipse elli = (VirtualEllipse)vShape;
						Ellipse dxfelli = new Ellipse();
						dxfelli.Center = new netDxf.Vector3( elli.Center.X, elli.Center.Y, 0 );
						dxfelli.StartAngle = 0;
						dxfelli.EndAngle = 360;
						dxfelli.MajorAxis = Math.Max( elli.Radius.W, elli.Radius.H );
						dxfelli.MinorAxis = Math.Min( elli.Radius.W, elli.Radius.H );
						dxfelli.Rotation = elli.Radius.W >= elli.Radius.H ? 0 : 90;
						obj = dxfelli;
						}
					else if( type == typeof( VirtualPie ) )
						{
						VirtualPie pie = (VirtualPie)vShape;
						Ellipse dxfelli = new Ellipse();
						dxfelli.Center = new netDxf.Vector3( pie.Center.X, pie.Center.Y, 0 );
						dxfelli.StartAngle = pie.StartAngle;
						dxfelli.EndAngle = pie.EndAngle;
						dxfelli.MajorAxis = Math.Max( pie.Radius.W, pie.Radius.H );
						dxfelli.MinorAxis = Math.Min( pie.Radius.W, pie.Radius.H );
						dxfelli.Rotation = pie.Radius.W >= pie.Radius.H ? 0 : 90;
						obj = dxfelli;
						}
					else obj = null;

					if( obj == null )
						continue;
					obj.Layer = dxfLayer;
					doc.AddEntity( obj );
					}
				}

			doc.Save( FileName );
			}
示例#17
0
        private static void WriteDxfFile()
        {
            DxfDocument dxf = new DxfDocument();

            //arc
            Arc arc = new Arc(new Vector3d(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.Long, 350));
            xdata2.XDataRecord.Add(XDataRecord.CloseControlString);

            //circle
            Vector3d extrusion = new Vector3d(1, 1, 1);
            Vector3d centerWCS = new Vector3d(1, 1, 1);
            Vector3d centerOCS = MathHelper.Transform(centerWCS,
                                                      extrusion,
                                                      MathHelper.CoordinateSystem.World,
                                                      MathHelper.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=new Dictionary<ApplicationRegistry, XData>
                             {
                                 {xdata.ApplicationRegistry, xdata},
                                 {xdata2.ApplicationRegistry, xdata2}
                             };

            dxf.AddEntity(circle);

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

            //3dface
            Face3d face3D = new Face3d(new Vector3d(-5, -5, 5),
                                       new Vector3d(5, -5, 5),
                                       new Vector3d(5, 5, 5),
                                       new Vector3d(-5, 5, 5));
            face3D.Layer = new Layer("3dface");
            face3D.Layer.Color.Index = 3;
            dxf.AddEntity(face3D);

            //polyline
            PolylineVertex polyVertex;
            List<PolylineVertex> polyVertexes = new List<PolylineVertex>();
            polyVertex = new PolylineVertex(new Vector2d(-50, -50));
            polyVertex.BeginThickness = 2;
            polyVertexes.Add(polyVertex);
            polyVertex = new PolylineVertex(new Vector2d(50, -50));
            polyVertex.BeginThickness = 1;
            polyVertexes.Add(polyVertex);
            polyVertex = new PolylineVertex(new Vector2d(50, 50));
            polyVertex.Bulge = 1;
            polyVertexes.Add(polyVertex);
            polyVertex = new PolylineVertex(new Vector2d(-50, 50));
            polyVertexes.Add(polyVertex);
            Polyline polyline2d = new Polyline(polyVertexes, true);
            polyline2d.Layer = new Layer("polyline2d");
            polyline2d.Layer.Color.Index = 5;
            polyline2d.Normal = new Vector3d(1, 1, 1);
            polyline2d.Elevation = 100.0;
            dxf.AddEntity(polyline2d);

            //lightweight polyline
            LightWeightPolylineVertex lwVertex;
            List<LightWeightPolylineVertex> lwVertexes = new List<LightWeightPolylineVertex>();
            lwVertex = new LightWeightPolylineVertex(new Vector2d(-25, -25));
            lwVertex.BeginThickness = 2;
            lwVertexes.Add(lwVertex);
            lwVertex = new LightWeightPolylineVertex(new Vector2d(25, -25));
            lwVertex.BeginThickness = 1;
            lwVertexes.Add(lwVertex);
            lwVertex = new LightWeightPolylineVertex(new Vector2d(25, 25));
            lwVertex.Bulge = 1;
            lwVertexes.Add(lwVertex);
            lwVertex = new LightWeightPolylineVertex(new Vector2d(-25, 25));
            lwVertexes.Add(lwVertex);
            LightWeightPolyline lwPolyline = new LightWeightPolyline(lwVertexes, true);
            lwPolyline.Layer = new Layer("lwpolyline");
            lwPolyline.Layer.Color.Index = 5;
            lwPolyline.Normal = new Vector3d(1, 1, 1);
            lwPolyline.Elevation = 100.0;
            dxf.AddEntity(lwPolyline);

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

            //3d polyline
            Polyline3dVertex vertex;
            List<Polyline3dVertex> vertexes = new List<Polyline3dVertex>();
            vertex = new Polyline3dVertex(new Vector3d(-50, -50, 0));
            vertexes.Add(vertex);
            vertex = new Polyline3dVertex(new Vector3d(50, -50, 10));
            vertexes.Add(vertex);
            vertex = new Polyline3dVertex(new Vector3d(50, 50, 25));
            vertexes.Add(vertex);
            vertex = new Polyline3dVertex(new Vector3d(-50, 50, 50));
            vertexes.Add(vertex);
            Polyline3d polyline = new Polyline3d(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 Vector3d(-5, -5, 5), new Vector3d(5, 5, 5)));
            block.Entities.Add(new Line(new Vector3d(5, -5, 5), new Vector3d(-5, 5, 5)));

            //insert
            Insert insert = new Insert(block, new Vector3d(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!", Vector3d.Zero, 10.0f,style);
            text.Layer = new Layer("text");
            text.Layer.Color.Index = 8;
            text.Alignment = TextAlignment.TopRight;
            dxf.AddEntity(text);

            dxf.Save("AutoCad2007.dxf", DxfVersion.AutoCad2007);
            dxf.Save("AutoCad2004.dxf", DxfVersion.AutoCad2004);
            dxf.Save("AutoCad2000.dxf", DxfVersion.AutoCad2000);
            dxf.Save("AutoCad12.dxf", DxfVersion.AutoCad12);
        }
示例#18
0
        private static void Polyline()
        {
            DxfDocument dxf = new DxfDocument();

            Polyline poly = new Polyline();
            poly.Vertexes.Add(new PolylineVertex(0,0));
            poly.Vertexes.Add(new PolylineVertex(10, 10));
            poly.Vertexes.Add(new PolylineVertex(20, 0));
            poly.Vertexes.Add(new PolylineVertex(30, 10));

            dxf.AddEntity(poly);

            dxf.Save("polyline.dxf", DxfVersion.AutoCad2000);
        }
示例#19
0
        /// <summary>
        /// Creates a new Polyline that is a copy of the current instance.
        /// </summary>
        /// <returns>A new Polyline that is a copy of this instance.</returns>
        public override object Clone()
        {
            List<PolylineVertex> copyVertexes = new List<PolylineVertex>();
            foreach (PolylineVertex vertex in this.vertexes)
            {
                copyVertexes.Add((PolylineVertex) vertex.Clone());
            }

            Polyline entity = new Polyline
            {
                //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,
                //Polyline properties
                Vertexes = copyVertexes,
                Flags = this.flags
            };

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

            return entity;

        }
        private void SetInternalInfo(IList <EntityObject> entities)
        {
            bool containsClosedPolyline = false;

            this.edges.Clear();

            foreach (EntityObject entity in entities)
            {
                if ((this.pathTypeFlag & HatchBoundaryPathTypeFlags.Polyline) == HatchBoundaryPathTypeFlags.Polyline)
                {
                    if (this.edges.Count >= 1)
                    {
                        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:
                    if (containsClosedPolyline)
                    {
                        throw new ArgumentException("Closed polylines cannot be combined with other entities to make a hatch boundary path.");
                    }
                    this.edges.Add(Arc.ConvertFrom(entity));
                    break;

                case EntityType.Circle:
                    if (containsClosedPolyline)
                    {
                        throw new ArgumentException("Closed polylines cannot be combined with other entities to make a hatch boundary path.");
                    }
                    this.edges.Add(Arc.ConvertFrom(entity));
                    break;

                case EntityType.Ellipse:
                    if (containsClosedPolyline)
                    {
                        throw new ArgumentException("Closed polylines cannot be combined with other entities to make a hatch boundary path.");
                    }
                    this.edges.Add(Ellipse.ConvertFrom(entity));
                    break;

                case EntityType.Line:
                    if (containsClosedPolyline)
                    {
                        throw new ArgumentException("Closed polylines cannot be combined with other entities to make a hatch boundary path.");
                    }
                    this.edges.Add(Line.ConvertFrom(entity));
                    break;

                case EntityType.LightWeightPolyline:
                    if (containsClosedPolyline)
                    {
                        throw new ArgumentException("Closed polylines cannot be combined with other entities to make a hatch boundary path.");
                    }
                    LwPolyline poly = (LwPolyline)entity;
                    if (poly.IsClosed)
                    {
                        this.edges.Add(Polyline.ConvertFrom(entity));     // A polyline HatchBoundaryPath must be closed
                        this.pathTypeFlag     |= 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:
                    if (containsClosedPolyline)
                    {
                        throw new ArgumentException("Closed polylines cannot be combined with other entities to make a hatch boundary path.");
                    }
                    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));
                }
            }
        }
示例#21
0
文件: Polyline.cs 项目: Core2D/netdxf
        /// <summary>
        /// Creates a new Polyline that is a copy of the current instance.
        /// </summary>
        /// <returns>A new Polyline that is a copy of this instance.</returns>
        public override object Clone()
        {
            Polyline entity = new Polyline
            {
                //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,
                //Polyline properties
                Flags = this.flags
            };

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

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

            return entity;
        }
示例#22
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;
        }
示例#23
0
        void WriteMultiSegment(MultiSegmentGeometry line)
        {
            IPointGeometry[] pts = line.Data;
            List<PolylineVertex> acVertexList = new List<PolylineVertex>(pts.Length);
            foreach (IPointGeometry p in pts)
                acVertexList.Add(new PolylineVertex((float)p.X, (float)p.Y));

            Polyline acLine = new Polyline(acVertexList);
            acLine.Layer = m_Layer;
            m_Dxf.AddEntity(acLine);
        }
示例#24
0
        private void SetInternalInfo(IEnumerable <EntityObject> entities)
        {
            bool containsPolyline = false;

            edges.Clear();

            foreach (EntityObject entity in entities)
            {
                if (containsPolyline)
                {
                    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:
                    edges.Add(Arc.ConvertFrom(entity));
                    break;

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

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

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

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

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

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

                default:
                    throw new ArgumentException(string.Format("The entity type {0} cannot be part of a hatch boundary. Only Arc, Circle, Ellipse, Line, LwPolyline, and Spline entities are allowed.", entity.Type));
                }
            }
        }
示例#25
0
        private void WritePolyline(Polyline polyline)
        {
            this.chunk.Write(100, SubclassMarker.Polyline3d);

            //dummy point
            this.chunk.Write(10, 0.0);
            this.chunk.Write(20, 0.0);
            this.chunk.Write(30, 0.0);

            this.chunk.Write(70, (short) polyline.Flags);
            this.chunk.Write(75, (short) polyline.SmoothType);

            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);

            string layerName = this.EncodeNonAsciiCharacters(polyline.Layer.Name);

            foreach (PolylineVertex v in polyline.Vertexes)
            {
                this.chunk.Write(0, v.CodeName);
                this.chunk.Write(5, v.Handle);
                this.chunk.Write(100, SubclassMarker.Entity);

                this.chunk.Write(8, layerName); // the vertex layer should be the same as the polyline layer

                this.chunk.Write(62, polyline.Color.Index); // the vertex color should be the same as the polyline color
                if (polyline.Color.UseTrueColor)
                    this.chunk.Write(420, AciColor.ToTrueColor(polyline.Color));
                this.chunk.Write(100, SubclassMarker.Vertex);
                this.chunk.Write(100, SubclassMarker.Polyline3dVertex);
                this.chunk.Write(10, v.Position.X);
                this.chunk.Write(20, v.Position.Y);
                this.chunk.Write(30, v.Position.Z);
                this.chunk.Write(70, (short) v.Flags);
            }

            this.chunk.Write(0, polyline.EndSequence.CodeName);
            this.chunk.Write(5, polyline.EndSequence.Handle);
            this.chunk.Write(100, SubclassMarker.Entity);
            this.chunk.Write(8, layerName); // the polyline EndSequence layer should be the same as the polyline layer
        }
示例#26
0
        private static void WritePolyline3d()
        {
            DxfDocument dxf = new DxfDocument();

            List<PolylineVertex> vertexes = new List<PolylineVertex>{
                                                                        new PolylineVertex(0, 0, 0), 
                                                                        new PolylineVertex(10, 0, 10), 
                                                                        new PolylineVertex(10, 10, 20), 
                                                                        new PolylineVertex(0, 10, 30)
                                                                        };

            Polyline poly = new Polyline(vertexes, true);

            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.String, "netDxf polyline3d"));
            xdata.XDataRecord.Add(new XDataRecord(XDataCode.Int16, poly.Vertexes.Count));
            xdata.XDataRecord.Add(XDataRecord.CloseControlString);

            poly.XData.Add(xdata);

            dxf.AddEntity(poly);

            dxf.Save("polyline.dxf");           
        }
示例#27
0
        private static void AppRegUsesAndRemove()
        {
            DxfDocument dxf = new DxfDocument();

            List<PolylineVertex> vertexes = new List<PolylineVertex>{
                                                                        new PolylineVertex(0, 0, 0), 
                                                                        new PolylineVertex(10, 0, 10), 
                                                                        new PolylineVertex(10, 10, 20), 
                                                                        new PolylineVertex(0, 10, 30)
                                                                        };

            Polyline poly = new Polyline(vertexes, true);

            XData xdata1 = new XData(new ApplicationRegistry("netDxf"));
            xdata1.XDataRecord.Add(new XDataRecord(XDataCode.String, "extended data with netDxf"));

            poly.XData.Add(xdata1);

            dxf.AddEntity(poly);

            Line line = new Line(new Vector2(10, 5), new Vector2(-10, -5));

            ApplicationRegistry myAppReg = new ApplicationRegistry("MyAppReg");
            XData xdata2 = new XData(myAppReg);
            xdata2.XDataRecord.Add(new XDataRecord(XDataCode.Distance, Vector3.Distance(line.StartPoint, line.EndPoint)));
            line.XData.Add(xdata2);

            dxf.AddEntity(line);

            Circle circle = new Circle(Vector3.Zero, 15);
            XData xdata3 = new XData(myAppReg);
            xdata3.XDataRecord.Add(new XDataRecord(XDataCode.Real, circle.Radius));
            circle.XData.Add(xdata3);

            dxf.AddEntity(circle);

            dxf.Save("appreg.dxf");

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

            // will return false the "MyAppReg" is in use
            bool ok;
            ok = dxf.ApplicationRegistries.Remove(myAppReg.Name);
            dxf.RemoveEntity(line);
            dxf.RemoveEntity(circle);
            // "MyAppReg" is not used anymore
            IList<DxfObject> uses = dxf.ApplicationRegistries.GetReferences(myAppReg.Name);
            // it is safe to delete it
            ok = dxf.ApplicationRegistries.Remove(myAppReg.Name);
            
            // we can even make a full cleanup
            dxf.ApplicationRegistries.Clear();

            dxf.Save("appreg2.dxf");


        }
示例#28
0
        private IPolyline ReadPolyline(ref CodeValuePair code)
        {
            string handle = string.Empty;
            Layer layer = Layer.Default;
            AciColor color = AciColor.ByLayer;
            LineType lineType = LineType.ByLayer;
            PolylineTypeFlags flags = PolylineTypeFlags.OpenPolyline;
            double elevation = 0.0;
            float thickness = 0.0f;
            Vector3d normal = Vector3d.UnitZ;
            List<Vertex> vertexes = new List<Vertex>();
            Dictionary<ApplicationRegistry, XData> xData = new Dictionary<ApplicationRegistry, XData>();
            //int numVertexes = -1;
            //int numFaces = -1;

            code = this.ReadCodePair();

            while (code.Code != 0)
            {
                switch (code.Code)
                {
                    case 5:
                        handle = code.Value;
                        code = this.ReadCodePair();
                        break;
                    case 8:
                        layer = this.GetLayer(code.Value);
                        code = this.ReadCodePair();
                        break;
                    case 62:
                        color = new AciColor(short.Parse(code.Value));
                        code = this.ReadCodePair();
                        break;
                    case 6:
                        lineType = this.GetLineType(code.Value);
                        code = this.ReadCodePair();
                        break;
                    case 30:
                        elevation = double.Parse(code.Value);
                        code = this.ReadCodePair();
                        break;
                    case 39:
                        thickness = float.Parse(code.Value);
                        code = this.ReadCodePair();
                        break;
                    case 70:
                        flags = (PolylineTypeFlags) (int.Parse(code.Value));
                        code = this.ReadCodePair();
                        break;
                    case 71:
                        //this field might not exist for polyface meshes, we cannot depend on it
                        //numVertexes = int.Parse(code.Value); code = this.ReadCodePair();
                        code = this.ReadCodePair();
                        break;
                    case 72:
                        //this field might not exist for polyface meshes, we cannot depend on it
                        //numFaces  = int.Parse(code.Value);
                        code = this.ReadCodePair();
                        break;
                    case 210:
                        normal.X = double.Parse(code.Value);
                        code = this.ReadCodePair();
                        break;
                    case 220:
                        normal.Y = double.Parse(code.Value);
                        code = this.ReadCodePair();
                        break;
                    case 230:
                        normal.Z = double.Parse(code.Value);
                        code = this.ReadCodePair();
                        break;
                    case 1001:
                        XData xDataItem = this.ReadXDataRecord(code.Value, ref code);
                        xData.Add(xDataItem.ApplicationRegistry, xDataItem);
                        break;
                    default:
                        if (code.Code >= 1000 && code.Code <= 1071)
                            throw new DxfInvalidCodeValueEntityException(code.Code, code.Value, this.file,
                                                                         "The extended data of an entity must start with the application registry code " + this.fileLine);

                        code = this.ReadCodePair();
                        break;
                }
            }

            //begin to read the vertex list
            if (code.Value != DxfObjectCode.Vertex)
                throw new DxfEntityException(DxfObjectCode.Polyline, this.file, "Vertex not found in line " + this.fileLine);
            while (code.Value != StringCode.EndSequence)
            {
                if (code.Value == DxfObjectCode.Vertex)
                {
                    Debug.Assert(code.Code == 0);
                    Vertex vertex = this.ReadVertex(ref code);
                    vertexes.Add(vertex);
                }
            }

            // read the end end sequence object until a new element is found
            if (code.Value != StringCode.EndSequence)
                throw new DxfEntityException(DxfObjectCode.Polyline, this.file, "End sequence entity not found in line " + this.fileLine);
            code = this.ReadCodePair();
            string endSequenceHandle = string.Empty;
            Layer endSequenceLayer = layer;
            while (code.Code != 0)
            {
                switch (code.Code)
                {
                    case 5:
                        endSequenceHandle = code.Value;
                        code = this.ReadCodePair();
                        break;
                    case 8:
                        endSequenceLayer = this.GetLayer(code.Value);
                        code = this.ReadCodePair();
                        break;
                    default:
                        code = this.ReadCodePair();
                        break;
                }
            }

            IPolyline pol;
            bool isClosed = false;

            if ((flags & PolylineTypeFlags.ClosedPolylineOrClosedPolygonMeshInM) == PolylineTypeFlags.ClosedPolylineOrClosedPolygonMeshInM)
            {
                isClosed = true;
            }

            //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 & PolylineTypeFlags.Polyline3D) == PolylineTypeFlags.Polyline3D)
            {
                List<Polyline3dVertex> polyline3dVertexes = new List<Polyline3dVertex>();
                foreach (Vertex v in vertexes)
                {
                    Polyline3dVertex vertex = new Polyline3dVertex
                                                  {
                                                      Color = v.Color,
                                                      Layer = v.Layer,
                                                      LineType = v.LineType,
                                                      Location = v.Location,
                                                      Handle = v.Handle
                                                  };
                    vertex.XData = v.XData;
                    polyline3dVertexes.Add(vertex);
                }

                ////posible error avoidance, the polyline is marked as polyline3d code:(70,8) but the vertex is marked as PolylineVertex code:(70,0)
                //if (v.Type == EntityType.PolylineVertex)
                //{
                //    Polyline3dVertex polyline3dVertex = new Polyline3dVertex(((PolylineVertex)v).Location.X, ((PolylineVertex)v).Location.Y,0);
                //    polyline3dVertexes.Add(polyline3dVertex);
                //}
                //else
                //{
                //    polyline3dVertexes.Add((Polyline3dVertex)v);
                //}
                //}
                pol = new Polyline3d(polyline3dVertexes, isClosed)
                          {
                              Handle = handle
                          };
                ((Polyline3d) pol).EndSequence.Handle = endSequenceHandle;
                ((Polyline3d) pol).EndSequence.Layer = endSequenceLayer;
            }
            else if ((flags & PolylineTypeFlags.PolyfaceMesh) == 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 & (VertexTypeFlags.PolyfaceMeshVertex | VertexTypeFlags.Polygon3dMesh)) == (VertexTypeFlags.PolyfaceMeshVertex | VertexTypeFlags.Polygon3dMesh))
                    {
                        PolyfaceMeshVertex vertex = new PolyfaceMeshVertex
                                                        {
                                                            Color = v.Color,
                                                            Layer = v.Layer,
                                                            LineType = v.LineType,
                                                            Location = v.Location,
                                                            Handle = v.Handle
                                                        };
                        vertex.XData = xData;
                        polyfaceVertexes.Add(vertex);
                    }
                    else if ((v.Flags & (VertexTypeFlags.PolyfaceMeshVertex)) == (VertexTypeFlags.PolyfaceMeshVertex))
                    {
                        PolyfaceMeshFace vertex = new PolyfaceMeshFace
                                                      {
                                                          Color = v.Color,
                                                          Layer = v.Layer,
                                                          LineType = v.LineType,
                                                          VertexIndexes = v.VertexIndexes,
                                                          Handle = v.Handle
                                                      };
                        vertex.XData = xData;
                        polyfaceFaces.Add(vertex);
                    }

                    //if (v.Type == EntityType.PolyfaceMeshVertex)
                    //{
                    //    polyfaceVertexes.Add((PolyfaceMeshVertex) v);
                    //}
                    //else if (v.Type == EntityType.PolyfaceMeshFace)
                    //{
                    //    polyfaceFaces.Add((PolyfaceMeshFace) v);
                    //}
                    //else
                    //{
                    //    throw new EntityDxfException(v.Type.ToString(), this.file, "Error in vertex type.");
                    //}
                }
                pol = new PolyfaceMesh(polyfaceVertexes, polyfaceFaces)
                          {
                              Handle = handle
                          };
                ((PolyfaceMesh) pol).EndSequence.Handle = endSequenceHandle;
                ((PolyfaceMesh) pol).EndSequence.Layer = endSequenceLayer;
            }
            else
            {
                List<PolylineVertex> polylineVertexes = new List<PolylineVertex>();
                foreach (Vertex v in vertexes)
                {
                    PolylineVertex vertex = new PolylineVertex
                                                {
                                                    Location = new Vector2d(v.Location.X, v.Location.Y),
                                                    BeginThickness = v.BeginThickness,
                                                    Bulge = v.Bulge,
                                                    Color = v.Color,
                                                    EndThickness = v.EndThickness,
                                                    Layer = v.Layer,
                                                    LineType = v.LineType,
                                                    Handle = v.Handle
                                                };
                    vertex.XData = xData;

                    ////posible error avoidance, the polyline is marked as polyline code:(70,0) but the vertex is marked as Polyline3dVertex code:(70,32)
                    //if (v.Type==EntityType.Polyline3dVertex)
                    //{
                    //    PolylineVertex polylineVertex = new PolylineVertex(((Polyline3dVertex)v).Location.X, ((Polyline3dVertex)v).Location.Y);
                    //    polylineVertexes.Add(polylineVertex);
                    //}
                    //else
                    //{
                    //    polylineVertexes.Add((PolylineVertex) v);
                    //}
                    polylineVertexes.Add(vertex);
                }

                pol = new Polyline(polylineVertexes, isClosed)
                          {
                              Thickness = thickness,
                              Elevation = elevation,
                              Normal = normal,
                              Handle = handle
                          };
                ((Polyline) pol).EndSequence.Handle = endSequenceHandle;
                ((Polyline) pol).EndSequence.Layer = endSequenceLayer;
            }

            pol.Color = color;
            pol.Layer = layer;
            pol.LineType = lineType;
            pol.XData = xData;

            return pol;
        }
示例#29
0
        /// <summary>
        /// Converts the ellipse in a Polyline.
        /// </summary>
        /// <param name="precision">Number of vertexes generated.</param>
        /// <returns>A new instance of <see cref="Polyline">Polyline</see> that represents the ellipse.</returns>
        public Polyline ToPolyline(int precision)
        {
            List<Vector2d> vertexes = this.PolygonalVertexes(precision);
            Vector3d ocsCenter = MathHelper.Transform(this.center,
                                                      this.normal, MathHelper.CoordinateSystem.World, MathHelper.CoordinateSystem.Object);
            Polyline poly = new Polyline
            {
                Color = this.color,
                Layer = this.layer,
                LineType = this.lineType,
                Normal = this.normal,
                Elevation = ocsCenter.Z,
                Thickness=this.thickness
            };
            poly.XData=this.xData;

            foreach (Vector2d v in vertexes)
            {
                poly.Vertexes.Add(new PolylineVertex(v.X + ocsCenter.X, v.Y + ocsCenter.Y));
            }
            if (this.IsFullEllipse)
                poly.IsClosed = true;

            return poly;
        }