Example #1
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" });
            }
        }