public Drawing ReadDrawing(string dxfFileName, Canvas canvas)
        {
            doc = new DxfDocument();
            doc.Load(dxfFileName);

            drawing = new Drawing(canvas);

            ReadLines();
            ReadPolylines();
            ReadArcs();
            ReadCircles();
            ReadInserts();

            drawing.Recalculate();
            return drawing;
        }
Example #2
0
        public void Load(string filePath)
        {
            _dxf = new DxfDocument();
            _dxf.Load(filePath);

            foreach (netDxf.Tables.LineType lineType in _dxf.LineTypes)
            {
                _lineTypeDictionary.Add(lineType, PicGraphics.LT.LT_CUT);
                _lineType2GrpDictionary.Add(lineType, 0);
            }
        }
Example #3
0
        public void importFileInternal(string path)
        {
            string ext = System.IO.Path.GetExtension(path).ToUpper();

            try
            {
                string contents = "";

                if (ext.Equals(".DXF"))
                {
                    DXF.DxfDocument doc = new DXF.DxfDocument();
                    doc.Load(path);
                    contents = "\r\n// import: " + System.IO.Path.GetFileName(path) + "\r\n\r\n";

                    double x, y;
                    double minx = double.MaxValue;
                    double miny = double.MaxValue;

                    foreach (DXF.Entities.Arc arc in doc.Arcs)
                    {
                        Arc a2 = new Arc(new PointF((float)arc.Center.X, (float)arc.Center.Y), (float)(arc.Radius * 2), -1, (float)(360 - arc.StartAngle), (float)(360 - arc.EndAngle));
                        PointF[] list = a2.GetPoints();
                        foreach (PointF p in list)
                        {
                            minx = Math.Min(minx, p.X);
                            miny = Math.Min(miny, p.Y);
                        }
                        /*
                        double a = 360 - arc.StartAngle;
                        double b = 360 - arc.EndAngle;

                        while (a < 0 || b < 0) { a += 360; b += 360; }

                        x = arc.Center.X + arc.Radius * Math.Sin( ToolpathUtilities.Radians( (float)a ));
                        y = arc.Center.Y + arc.Radius * Math.Cos( ToolpathUtilities.Radians( (float)a ));
                        minx = Math.Min(x, minx);
                        miny = Math.Min(y, miny);

                        x = arc.Center.X + arc.Radius * Math.Sin(ToolpathUtilities.Radians((float)b));
                        y = arc.Center.Y + arc.Radius * Math.Cos(ToolpathUtilities.Radians((float)b));
                        minx = Math.Min(x, minx);
                        miny = Math.Min(y, miny);

                        //minx = Math.Min(arc.Center.X, minx);
                        //miny = Math.Min(arc.Center.Y, miny);
                        */
                    }
                    foreach (DXF.Entities.Circle circle in doc.Circles)
                    {
                        x = circle.Center.X - circle.Radius;
                        y = circle.Center.Y - circle.Radius;
                        minx = Math.Min(x, minx);
                        miny = Math.Min(y, miny);
                    }
                    foreach (DXF.Entities.Line line in doc.Lines)
                    {
                        x = Math.Min(line.StartPoint.X, line.EndPoint.X);
                        y = Math.Min(line.StartPoint.Y, line.EndPoint.Y);
                        minx = Math.Min(x, minx);
                        miny = Math.Min(y, miny);
                    }

                    Console.WriteLine("Min {0:0.00}, {1:0.00}", minx, miny);
                    //minx = miny = 0;

                    foreach (DXF.Entities.Arc arc in doc.Arcs)
                    {
                        double arcLen = Math.PI * arc.Radius * 25.4 * 2; // diameter
                        double degrees = Math.Abs((0 - arc.EndAngle) - (0 - arc.StartAngle));
                        arcLen *= (degrees / 360);

                        contents += string.Format("\tARC {0:0.00}, {1:0.00}, {2:0.00}, {3:0.00}, {4:0.00} \n", // arc len {5:0.00}\n",
                            (arc.Center.X - minx) * 25.4, (arc.Center.Y - miny) * 25.4, arc.Radius * 25.4 * 2, 360 - arc.StartAngle, 360 - arc.EndAngle); // , arcLen);
                    }
                    foreach (DXF.Entities.Circle circle in doc.Circles)
                    {
                        contents += string.Format("\tCIRCLE {0:0.00}, {1:0.00}, {2:0.00}\n",
                            (circle.Center.X - minx) * 25.4, (circle.Center.Y - miny) * 25.4, circle.Radius * 25.4 * 2);
                    }
                    foreach (DXF.Entities.Line line in doc.Lines)
                    {
                        contents += string.Format("\tLINE {0:0.00}, {1:0.00}, {2:0.00}, {3:0.00}\n",
                            (line.StartPoint.X - minx) * 25.4, (line.StartPoint.Y - miny) * 25.4, (line.EndPoint.X - minx) * 25.4, (line.EndPoint.Y - miny) * 25.4);
                    }
                }
                else // try a gerb import
                {
                    contents = new Gerbers.Gerbers().TranslateGerber(path);
                }

                Invoke(new Action<string>((str) =>
                {
                    if (cleanImport) editor.Text = str;
                    else editor.Text += "\r\n" + str;
                }), new object[] { contents });

                Invoke(new Action<string>((str) => { statusMessage.Text = str; }), new object[] { "Import OK" });
                Invoke(new Action(() => { runParse(); }));

                if (cleanImport)
                {
                    fDirty = false;
                    currentPath = System.IO.Path.GetFileNameWithoutExtension(path);
                    Invoke(new Action(() => { UpdateFormTitle(); }));
                    Unzoom();
                    RecenterImage();
                }

            }
            catch (Exception e)
            {
                logMessage(e.Message);
                logMessage(e.StackTrace);
                Invoke(new Action<string>((str) => { statusMessage.Text = str; ctc.SelectedIndex = 0;  }), new object[] { "Import error, see log" });
            }
        }
Example #4
0
        private static void BlockAttributes()
        {
            DxfDocument dxf = new DxfDocument( );
            Block block = new Block("BlockWithAttributes");
            block.Layer = new Layer("BlockSample");

            AttributeDefinition attdef = new AttributeDefinition("NewAttribute");
            attdef.Text = "InfoText";
            attdef.BasePoint = new Vector3d(1, 1, 1);
            attdef.Style.IsVertical = true;
            attdef.Rotation = 45;

            block.Attributes.Add(attdef.Id, attdef);
            block.Entities.Add(new Line(new Vector3d(-5, -5, 0), new Vector3d(5, 5, 0)));
            block.Entities.Add(new Line(new Vector3d(5, -5, 0), new Vector3d(-5, 5, 0)));

            Insert insert = new Insert(block, new Vector3d(5, 5, 5));
            insert.Layer = new Layer("insert");
            insert.Rotation = 45;
            insert.Layer.Color.Index = 4;
            insert.Attributes[0].Value = 24;

            Insert insert2 = new Insert(block, new Vector3d(-5, -5, -5));
            insert2.Attributes[0].Value = 34;

            XData xdata1 = new XData(new ApplicationRegistry("netDxf"));
            xdata1.XDataRecord.Add(new XDataRecord(XDataCode.String, "extended data with netDxf"));
            xdata1.XDataRecord.Add(XDataRecord.OpenControlString);
            xdata1.XDataRecord.Add(new XDataRecord(XDataCode.WorldSpacePositionX, 0));
            xdata1.XDataRecord.Add(new XDataRecord(XDataCode.WorldSpacePositionY, 0));
            xdata1.XDataRecord.Add(new XDataRecord(XDataCode.WorldSpacePositionZ, 0));
            xdata1.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);

            insert.XData = new Dictionary<ApplicationRegistry, XData>
                             {
                                 {xdata1.ApplicationRegistry, xdata1},
                             };
            dxf.AddEntity(insert);
            dxf.AddEntity(insert2);

            Circle circle = new Circle(Vector3d.Zero, 5);
            circle.Layer = new Layer("circle");
            circle.Layer.Color.Index = 2;
            circle.XData = new Dictionary<ApplicationRegistry, XData>
                             {
                                 {xdata2.ApplicationRegistry, xdata2},
                             };
            dxf.AddEntity(circle);

            dxf.Save("Block with attributes.dxf", DxfVersion.AutoCad2000);
            dxf.Load("Block with attributes.dxf");
            dxf.Save("Block with attributes result.dxf", DxfVersion.AutoCad2000); // both results must be equal only the handles might be different
        }
Example #5
0
        private static void SpeedTest()
        {
            Stopwatch crono = new Stopwatch();
            float totalTime=0;

            crono.Start();
            DxfDocument dxf = new DxfDocument();
            /// create 100,000 lines
            for (int i=0; i<100000;i++)
            {
                 //line
                Line line = new Line(new Vector3d(0, i, 0), new Vector3d(5, i, 0));
                line.Layer = new Layer("line");
                line.Layer.Color.Index = 6;
                dxf.AddEntity(line);
            }

            Console.WriteLine("Time creating entities : " + crono.ElapsedMilliseconds / 1000.0f);
            totalTime += crono.ElapsedMilliseconds;
            crono.Reset();

            crono.Start();
            dxf.Save("speedtest.dxf", DxfVersion.AutoCad12);
            Console.WriteLine("Time saving file : " + crono.ElapsedMilliseconds / 1000.0f);
               totalTime += crono.ElapsedMilliseconds;
            crono.Reset();

            crono.Start();
            dxf.Load("speedtest.dxf");
            Console.WriteLine("Time loading file : " + crono.ElapsedMilliseconds / 1000.0f);
            totalTime += crono.ElapsedMilliseconds;
            crono.Stop();

            Console.WriteLine("Total time : " + totalTime / 1000.0f);
            Console.ReadLine();
        }
Example #6
0
        private static void ReadDxfFile()
        {
            DxfDocument dxf = new DxfDocument();
            //dxf.Load("AutoCad2007.dxf");
            //dxf.Load("AutoCad2004.dxf");
            dxf.Load("AutoCad2000.dxf");
            dxf.Save("AutoCad2000 result.dxf", DxfVersion.AutoCad2000);
            //dxf.Load("AutoCad12.dxf");
            //dxf.Load("Tablet.dxf");

            //dxf.Save("Tablet result.dxf", DxfVersion.AutoCad2000);
        }
Example #7
0
        private static void Face3d()
        {
            DxfDocument dxf = new DxfDocument();

            Face3d face3d = new Face3d();
            face3d.FirstVertex = new Vector3d(0, 0, 0);
            face3d.SecondVertex = new Vector3d(1, 0, 0);
            face3d.ThirdVertex = new Vector3d(1, 1, 0);
            face3d.FourthVertex = new Vector3d(0, 1, 0);
            dxf.AddEntity(face3d);

            dxf.Save("face.dxf", DxfVersion.AutoCad2000);
            dxf.Load("face.dxf");
            dxf.Save("face return.dxf", DxfVersion.AutoCad2000);
        }
Example #8
0
        private static void Ellipse()
        {
            DxfDocument dxf = new DxfDocument();

            Line line = new Line(new Vector3d(0, 0, 0), new Vector3d((2 * Math.Cos(Math.PI / 4)), (2 * Math.Cos(Math.PI / 4)), 0));
            dxf.AddEntity(line);

            Line line2 = new Line(new Vector3d(0, 0, 0), new Vector3d(0, -2, 0));
            dxf.AddEntity(line2);

            Arc arc=new Arc(Vector3d.Zero,2,45,270);
            dxf.AddEntity(arc);

            // ellipses are saved as polylines
            Ellipse ellipse = new Ellipse(new Vector3d(2,2,0), 5,3);
            ellipse.Rotation = 30;
            ellipse.Normal=new Vector3d(1,1,1);
            ellipse.Thickness = 2;
            dxf.AddEntity(ellipse);

            dxf.Save("ellipse.dxf", DxfVersion.AutoCad2000);
            dxf = new DxfDocument();
            dxf.Load("ellipse.dxf");
        }