Beispiel #1
0
        /// <summary>
        /// Process entities
        /// </summary>
        private static void processEntities(DXFEntity entity, double offsetX = 0, double offsetY = 0)
        {
            int    index = 0;
            double x, y, x2 = 0, y2 = 0, bulge;

            if (dxfComments)
            {
                gcode.Comment(gcodeString[gcodeStringIndex], "Entity: " + entity.GetType().ToString());
                gcode.Comment(gcodeString[gcodeStringIndex], "Color:  " + entity.ColorNumber.ToString());
            }

            if (entity.GetType() == typeof(DXFPointEntity))
            {
                DXFPointEntity point = (DXFPointEntity)entity;
                x = (float)point.Location.X + (float)offsetX;
                y = (float)point.Location.Y + (float)offsetY;
                gcodeStartPath(x, y, "Start Point");
                gcodeStopPath();
            }

            #region DXFLWPolyline
            else if (entity.GetType() == typeof(DXFLWPolyLine))
            {
                DXFLWPolyLine lp = (DXFLWPolyLine)entity;
                index = 0; bulge = 0;
                DXFLWPolyLine.Element coordinate;
                bool roundcorner = false;
                for (int i = 0; i < lp.VertexCount; i++)
                {
                    coordinate = lp.Elements[i];
                    bulge      = coordinate.Bulge;
                    x          = (double)coordinate.Vertex.X + (double)offsetX;
                    y          = (double)coordinate.Vertex.Y + (double)offsetY;
                    if (i == 0)
                    {
                        gcodeStartPath(x, y, "Start LWPolyLine");
                        isReduceOk = true;
                    }
                    else
                    {
                        if (!roundcorner)
                        {
                            gcodeMoveTo(x, y, "");
                        }
                        if (bulge != 0)
                        {
                            if (i < (lp.VertexCount - 1))
                            {
                                AddRoundCorner(lp.Elements[i], lp.Elements[i + 1]);
                            }
                            else
                            {
                                AddRoundCorner(lp.Elements[i], lp.Elements[0]);
                            }
                            roundcorner = true;
                        }
                        else
                        {
                            roundcorner = false;
                        }
                    }
                    x2 = x; y2 = y;
                }
                if (lp.Flags > 0)
                {
                    gcodeMoveTo((float)lp.Elements[0].Vertex.X, (float)lp.Elements[0].Vertex.Y, "End LWPolyLine");
                }
                gcodeStopPath();
            }
            #endregion
            #region DXFPolyline
            else if (entity.GetType() == typeof(DXFPolyLine))
            {
                DXFPolyLine lp = (DXFPolyLine)entity;
                index = 0;
                foreach (DXFVertex coordinate in lp.Children)
                {
                    if (coordinate.GetType() == typeof(DXFVertex))
                    {
                        if (coordinate.Location.X != null && coordinate.Location.Y != null)
                        {
                            x = (float)coordinate.Location.X + (float)offsetX;
                            y = (float)coordinate.Location.Y + (float)offsetY;
                            if (index == 0)
                            {
                                gcodeStartPath(x, y, "Start PolyLine");
                            }
                            else
                            {
                                gcodeMoveTo(x, y, "");
                            }
                            index++;
                        }
                    }
                }
                gcodeStopPath();
            }
            #endregion
            #region DXFLine
            else if (entity.GetType() == typeof(DXFLine))
            {
                DXFLine line = (DXFLine)entity;
                x          = (float)line.Start.X + (float)offsetX;
                y          = (float)line.Start.Y + (float)offsetY;
                x2         = (float)line.End.X + (float)offsetX;
                y2         = (float)line.End.Y + (float)offsetY;
                isReduceOk = false;
                gcodeStartPath(x, y, "Start Line");
                gcodeMoveTo(x2, y2, "");
                gcodeStopPath();
            }
            #endregion
            #region DXFSpline
            else if (entity.GetType() == typeof(DXFSpline))
            {
                DXFSpline spline = (DXFSpline)entity;
                index = 0;
                double cx0, cy0, cx1, cy1, cx2, cy2, cx3, cy3, cxMirror, cyMirror, lastX, lastY;
                lastX = (double)spline.ControlPoints[0].X + offsetX;
                lastY = (double)spline.ControlPoints[0].Y + offsetY;
                string cmt = "Start Spline " + spline.KnotValues.Count.ToString() + " " + spline.ControlPoints.Count.ToString() + " " + spline.FitPoints.Count.ToString();
                gcodeStartPath(lastX, lastY, cmt);
                isReduceOk = true;

                for (int rep = 0; rep < spline.ControlPointCount; rep += 4)
                {
                    cx0       = (double)spline.ControlPoints[rep].X + offsetX; cy0 = (double)spline.ControlPoints[rep].Y + offsetY;
                    cx1       = (double)spline.ControlPoints[rep + 1].X + offsetX; cy1 = (double)spline.ControlPoints[rep + 1].Y + offsetY;
                    cx2       = (double)spline.ControlPoints[rep + 2].X + offsetX; cy2 = (double)spline.ControlPoints[rep + 2].Y + offsetY;
                    cx3       = (double)spline.ControlPoints[rep + 3].X + offsetX; cy3 = (double)spline.ControlPoints[rep + 3].Y + offsetY;
                    points    = new System.Windows.Point[4];
                    points[0] = new System.Windows.Point(cx0, cy0); //(qpx1, qpy1);
                    points[1] = new System.Windows.Point(cx1, cy1); //(qpx1, qpy1);
                    points[2] = new System.Windows.Point(cx2, cy2); //(qpx2, qpy2);
                    points[3] = new System.Windows.Point(cx3, cy3);
                    cxMirror  = cx3 - (cx2 - cx3); cyMirror = cy3 - (cy2 - cy3);
                    lastX     = cx3; lastY = cy3;
                    var b = GetBezierApproximation(points, dxfBezierAccuracy);
                    for (int i = 1; i < b.Points.Count; i++)
                    {
                        gcodeMoveTo((float)b.Points[i].X, (float)b.Points[i].Y, "");
                    }
                }
                gcodeStopPath();
            }
            #endregion
            #region DXFCircle
            else if (entity.GetType() == typeof(DXFCircle))
            {
                DXFCircle circle = (DXFCircle)entity;
                x = (float)circle.Center.X + (float)offsetX;
                y = (float)circle.Center.Y + (float)offsetY;
                gcodeStartPath(x + circle.Radius, y, "Start Circle");
                gcode.Arc(gcodeString[gcodeStringIndex], 2, (float)x + (float)circle.Radius, (float)y, -(float)circle.Radius, 0, "");
                gcodeStopPath();
            }
            #endregion

            else if (entity.GetType() == typeof(DXFEllipse))
            {
                DXFEllipse circle = (DXFEllipse)entity;
                gcode.Comment(gcodeString[gcodeStringIndex], "Ellipse: " + circle.ColorNumber.ToString());
            }
            #region DXFArc
            else if (entity.GetType() == typeof(DXFArc))
            {
                DXFArc arc = (DXFArc)entity;

                double X          = (double)arc.Center.X + offsetX;
                double Y          = (double)arc.Center.Y + offsetY;
                double R          = arc.Radius;
                double startAngle = arc.StartAngle;
                double endAngle   = arc.EndAngle;
                if (startAngle > endAngle)
                {
                    endAngle += 360;
                }
                double stepwidth = (double)Properties.Settings.Default.importGCSegment;
                float  StepAngle = (float)(Math.Asin(stepwidth / R) * 180 / Math.PI);// Settings.Default.page11arcMaxLengLine);
                double currAngle = startAngle;
                index = 0;
                while (currAngle < endAngle)
                {
                    double angle = currAngle * Math.PI / 180;
                    double rx    = (double)(X + R * Math.Cos(angle));
                    double ry    = (double)(Y + R * Math.Sin(angle));
                    if (index == 0)
                    {
                        gcodeStartPath(rx, ry, "Start Arc");
                        isReduceOk = true;
                    }
                    else
                    {
                        gcodeMoveTo(rx, ry, "");
                    }
                    currAngle += StepAngle;
                    if (currAngle > endAngle)
                    {
                        double angle2 = endAngle * Math.PI / 180;
                        double rx2    = (double)(X + R * Math.Cos(angle2));
                        double ry2    = (double)(Y + R * Math.Sin(angle2));

                        if (index == 0)
                        {
                            gcodeStartPath(rx2, ry2, "Start Arc");
                        }
                        else
                        {
                            gcodeMoveTo(rx2, ry2, "");
                        }
                    }
                    index++;
                }
                gcodeStopPath();
            }
            #endregion
            #region DXFMText
            else if (entity.GetType() == typeof(DXFMText))
            {   // https://www.autodesk.com/techpubs/autocad/acad2000/dxf/mtext_dxf_06.htm
                DXFMText txt    = (DXFMText)entity;
                xyPoint  origin = new xyPoint(0, 0);
                GCodeFromFont.reset();

                foreach (var entry in txt.Entries)
                {
                    if (entry.GroupCode == 1)
                    {
                        GCodeFromFont.gcText = entry.Value.ToString();
                    }
                    else if (entry.GroupCode == 40)
                    {
                        GCodeFromFont.gcHeight = Convert.ToDouble(entry.Value);
                    }                                                                                          // gcode.Comment(gcodeString[gcodeStringIndex], "Height "+entry.Value); }
                    else if (entry.GroupCode == 41)
                    {
                        GCodeFromFont.gcWidth = Convert.ToDouble(entry.Value);
                    }                                                                                        // gcode.Comment(gcodeString[gcodeStringIndex], "Width "+entry.Value); }
                    else if (entry.GroupCode == 71)
                    {
                        GCodeFromFont.gcAttachPoint = Convert.ToInt16(entry.Value);
                    }                                                                                             // gcode.Comment(gcodeString[gcodeStringIndex], "Origin " + entry.Value); }
                    else if (entry.GroupCode == 10)
                    {
                        GCodeFromFont.gcOffX = Convert.ToDouble(entry.Value);
                    }
                    else if (entry.GroupCode == 20)
                    {
                        GCodeFromFont.gcOffY = Convert.ToDouble(entry.Value);
                    }
                    else if (entry.GroupCode == 50)
                    {
                        GCodeFromFont.gcAngle = Convert.ToDouble(entry.Value);
                    }                                                                                        // gcode.Comment(gcodeString[gcodeStringIndex], "Angle " + entry.Value); }
                    else if (entry.GroupCode == 44)
                    {
                        GCodeFromFont.gcSpacing = Convert.ToDouble(entry.Value);
                    }
                    else if (entry.GroupCode == 7)
                    {
                        GCodeFromFont.gcFontName = entry.Value.ToString();
                    }
                }
                GCodeFromFont.getCode(gcodeString[gcodeStringIndex]);
            }
            #endregion
            else
            {
                gcode.Comment(gcodeString[gcodeStringIndex], "Unknown: " + entity.GetType().ToString());
            }
        }
Beispiel #2
0
        public DXFEntity CreateEntity()
        {
            DXFEntity E;

            switch (this.FValue)
            {
            case "ENDSEC":
            case "ENDTAB":
            case "ENDBLK":
            case "SEQEND":
                return(null);

            case "SECTION":
                E = new DXFSection();
                break;

            case "TABLE":
                E = new DXFTable();
                break;

            case "BLOCK":
                E = new DXFBlock();
                break;

            case "ATTDEF":
            case "ATTRIB":
                E = new DXFAttribute();
                break;

            case "INSERT":
                E = new DXFInsert();
                break;

            case "LAYER":
                E = new DXFLayer();
                break;

            case "LINE":
                E = new DXFLine();
                break;

            case "CIRCLE":
            case "ARC":
                E = new DXFCircle();
                break;

            case "ELLIPSE":
                E = new DXFEllipse();
                break;

            case "LWPOLYLINE":
                E = new DXFLWPolyLine();
                break;

            case "TEXT":
                E = new DXFText();
                break;

            case "MTEXT":
                E = new DXFMText();
                break;

            case "POLYLINE":
                E = new DXFPolyLine();
                break;

            case "VERTEX":
                E = new DXFPositionable();
                break;

            default:
                E = new DXFDummy(this.FValue);
                break;
            }
            E.Converter = this;
            return(E);
        }
Beispiel #3
0
        /// <summary>
        /// Process entities
        /// </summary>
        private static void processEntities(DXFEntity entity, double offsetX = 0, double offsetY = 0, bool updateColor = true)
        {
            int    index = 0;
            double x, y;//, x2 = 0, y2 = 0, bulge;

            if (updateColor)
            {
                dxfColorID       = entity.ColorNumber;
                Plotter.PathName = "Layer:" + entity.LayerName;
            }

            Plotter.PathDashArray = new double[0];                  // default no dashes
            if ((entity.LineType == null) || (entity.LineType == "ByLayer"))
            {
                if (layerLType.ContainsKey(entity.LayerName))        // check if layer name is known
                {
                    string dashType = layerLType[entity.LayerName];  // get name of pattern
                    if (lineTypes.ContainsKey(dashType))             // check if pattern name is known
                    {
                        Plotter.PathDashArray = lineTypes[dashType]; // apply pattern
                    }
                }
            }
            else
            {
                if (lineTypes.ContainsKey(entity.LineType))             // check if pattern name is known
                {
                    Plotter.PathDashArray = lineTypes[entity.LineType]; // apply pattern
                }
            }

            if (dxfColorID > 255)
            {
                if (layerColor.ContainsKey(entity.LayerName))
                {
                    dxfColorID = layerColor[entity.LayerName];
                }
            }

            if (dxfColorID < 0)
            {
                dxfColorID = 0;
            }
            if (dxfColorID > 255)
            {
                dxfColorID = 7;
            }
            if (Properties.Settings.Default.importDXFSwitchWhite && (dxfColorID == 7))
            {
                dxfColorID = 0;
            }

            dxfColorHex       = getColorFromID(dxfColorID);
            Plotter.PathColor = dxfColorHex;

            if (dxfUseColorIndex)
            {
                toolNr = dxfColorID + 1;      // avoid ID=0 to start tool-table with index 1
            }
            else
            {
                toolNr = toolTable.getToolNr(dxfColorHex, 0);
                //Logger.Trace("toolNr = {0}",toolNr);
            }

            Plotter.SetGroup(toolNr);       // set index if grouping and tool

            if (dxfColorIDold != dxfColorID)
            {
                Plotter.PenUp("");

                toolToUse = toolNr;
                if (Properties.Settings.Default.importGCToolTableUse && Properties.Settings.Default.importGCToolDefNrUse)
                {
                    toolToUse = (int)Properties.Settings.Default.importGCToolDefNr;
                }

                Plotter.PathToolNr = toolToUse;

                if (!groupObjects)
                {
                    if (dxfUseColorIndex)
                    {
                        Plotter.ToolChange(toolToUse, dxfColorID.ToString());   // add tool change commands (if enabled) and set XYFeed etc.
                    }
                    else
                    {
                        Plotter.ToolChange(toolToUse, dxfColorHex);
                    }
                }
            }
            dxfColorIDold = dxfColorID;

            Logger.Trace("  Entity: {0}", entity.GetType().ToString());

            if (entity.GetType() == typeof(DXFPointEntity))
            {
                DXFPointEntity point = (DXFPointEntity)entity;
                x = (float)point.Location.X + (float)offsetX;
                y = (float)point.Location.Y + (float)offsetY;
                if (!nodesOnly)
                {
                    dxfStartPath(x, y, "Start Point");
                    dxfStopPath();
                }
                else
                {
                    gcodeDotOnly(x, y, "Start Point");
                }
                Logger.Trace("    Point: {0};{1} ", x, y);
            }

            #region DXFLWPolyline
            else if (entity.GetType() == typeof(DXFLWPolyLine))
            {
                DXFLWPolyLine lp = (DXFLWPolyLine)entity;
                index = 0;
                double bulge = 0;
                DXFLWPolyLine.Element coordinate;
                bool roundcorner = false;
                x = 0; y = 0;
                for (int i = 0; i < lp.VertexCount; i++)
                {
                    coordinate = lp.Elements[i];
                    bulge      = coordinate.Bulge;
                    //            x2 = x; y2 = y;
                    x = (double)coordinate.Vertex.X + (double)offsetX;
                    y = (double)coordinate.Vertex.Y + (double)offsetY;
                    //                  Logger.Trace("    Vertex: {0};{1} ", x, y);

                    if (i == 0)
                    {
                        if (!nodesOnly)
                        {
                            dxfStartPath(x, y, "Start LWPolyLine - Nr pts " + lp.VertexCount.ToString());
                            Plotter.IsPathReduceOk = true;
                        }
                        else
                        {
                            gcodeDotOnly(x, y, "Start LWPolyLine");
                        }
                    }

                    if ((!roundcorner))
                    {
                        dxfMoveTo(x, y, "");
                    }
                    if (bulge != 0)
                    {
                        if (i < (lp.VertexCount - 1))
                        {
                            AddRoundCorner(lp.Elements[i], lp.Elements[i + 1]);
                        }
                        else
                        if (lp.Flags == DXFLWPolyLine.FlagsEnum.closed)
                        {
                            AddRoundCorner(lp.Elements[i], lp.Elements[0]);
                        }
                        roundcorner = true;
                    }
                    else
                    {
                        roundcorner = false;
                    }
                }
                if ((lp.Flags > 0))// && (x2 != x) && (y2 != y))   // only move if prev pos is differnent
                {
                    dxfMoveTo((float)(lp.Elements[0].Vertex.X + offsetX), (float)(lp.Elements[0].Vertex.Y + offsetY), "End LWPolyLine " + lp.Flags.ToString());
                }
                dxfStopPath();
            }
            #endregion
            #region DXFPolyline
            else if (entity.GetType() == typeof(DXFPolyLine))
            {
                DXFPolyLine lp = (DXFPolyLine)entity;
                index = 0;
                foreach (DXFVertex coordinate in lp.Children)
                {
                    if (coordinate.GetType() == typeof(DXFVertex))
                    {
                        if (coordinate.Location.X != null && coordinate.Location.Y != null)
                        {
                            x = (float)coordinate.Location.X + (float)offsetX;
                            y = (float)coordinate.Location.Y + (float)offsetY;
                            //                      Logger.Trace("    Vertex: {0};{1} ", x, y);
                            if (!nodesOnly)
                            {
                                if (index == 0)
                                {
                                    dxfStartPath(x, y, "Start PolyLine");
                                }
                                else
                                {
                                    dxfMoveTo(x, y, "");
                                }
                            }
                            else
                            {
                                gcodeDotOnly(x, y, "PolyLine");
                            }
                            index++;
                        }
                    }
                }
                dxfStopPath();
            }
            #endregion
            #region DXFLine
            else if (entity.GetType() == typeof(DXFLine))
            {
                DXFLine line = (DXFLine)entity;
                x = (double)line.Start.X + offsetX;
                y = (double)line.Start.Y + offsetY;
                double x2 = (double)line.End.X + offsetX;
                double y2 = (double)line.End.Y + offsetY;
                Plotter.IsPathReduceOk = false;
                if (!nodesOnly)
                {
                    dxfStartPath(x, y, "Start Line");
                    dxfMoveTo(x2, y2, "");
                }
                else
                {
                    gcodeDotOnly(x, y, "Start Line");
                    gcodeDotOnly(x2, y2, "End Line");
                }
                dxfStopPath();
                Logger.Trace("    From: {0};{1}  To: {2};{3}", x, y, x2, y2);
            }
            #endregion
            #region DXFSpline
            else if (entity.GetType() == typeof(DXFSpline))
            {   // from Inkscape DXF import - modified
                // https://gitlab.com/inkscape/extensions/blob/master/dxf_input.py#L106
                DXFSpline spline = (DXFSpline)entity;
                index = 0;

                Point  offset = new Point(offsetX, offsetY);
                double lastX  = (double)spline.ControlPoints[0].X + offsetX;
                double lastY  = (double)spline.ControlPoints[0].Y + offsetY;

                string cmt = "Start Spline " + spline.KnotValues.Count.ToString() + " " + spline.ControlPoints.Count.ToString() + " " + spline.FitPoints.Count.ToString();
                Plotter.IsPathReduceOk = true;

                int knots = spline.KnotCount;
                int ctrls = spline.ControlPointCount;
                Logger.Trace("    Spline  ControlPointCnt: {0} KnotsCount: {1}", ctrls, knots);

                if ((ctrls > 3) && (knots == ctrls + 4))    //  # cubic
                {
                    if (ctrls > 4)
                    {
                        for (int i = (knots - 5); i > 3; i--)
                        {
                            if ((spline.KnotValues[i] != spline.KnotValues[i - 1]) && (spline.KnotValues[i] != spline.KnotValues[i + 1]))
                            {
                                double   a0  = (spline.KnotValues[i] - spline.KnotValues[i - 2]) / (spline.KnotValues[i + 1] - spline.KnotValues[i - 2]);
                                double   a1  = (spline.KnotValues[i] - spline.KnotValues[i - 1]) / (spline.KnotValues[i + 2] - spline.KnotValues[i - 1]);
                                DXFPoint tmp = new DXFPoint();
                                tmp.X = (double)((1.0 - a1) * spline.ControlPoints[i - 2].X + a1 * spline.ControlPoints[i - 1].X);
                                tmp.Y = (double)((1.0 - a1) * spline.ControlPoints[i - 2].Y + a1 * spline.ControlPoints[i - 1].Y);
                                spline.ControlPoints.Insert(i - 1, tmp);
                                spline.ControlPoints[i - 2].X = (1.0 - a0) * spline.ControlPoints[i - 3].X + a0 * spline.ControlPoints[i - 2].X;
                                spline.ControlPoints[i - 2].Y = (1.0 - a0) * spline.ControlPoints[i - 3].Y + a0 * spline.ControlPoints[i - 2].Y;
                                spline.KnotValues.Insert(i, spline.KnotValues[i]);
                            }
                        }
                        knots = spline.KnotValues.Count;
                        for (int i = (knots - 6); i > 3; i -= 2)
                        {
                            if ((spline.KnotValues[i] != spline.KnotValues[i - 2]) && (spline.KnotValues[i - 1] != spline.KnotValues[i + 1]) && (spline.KnotValues[i - 2] != spline.KnotValues[i]))
                            {
                                double   a1  = (spline.KnotValues[i] - spline.KnotValues[i - 1]) / (spline.KnotValues[i + 2] - spline.KnotValues[i - 1]);
                                DXFPoint tmp = new DXFPoint();
                                tmp.X = (double)((1.0 - a1) * spline.ControlPoints[i - 2].X + a1 * spline.ControlPoints[i - 1].X);
                                tmp.Y = (double)((1.0 - a1) * spline.ControlPoints[i - 2].Y + a1 * spline.ControlPoints[i - 1].Y);
                                spline.ControlPoints.Insert(i - 1, tmp);
                            }
                        }
                    }
                    ctrls = spline.ControlPoints.Count;
                    dxfStartPath(lastX, lastY, cmt);
                    for (int i = 0; i < Math.Floor((ctrls - 1) / 3d); i++)     // for i in range(0, (ctrls - 1) // 3):
                    {
                        if (!nodesOnly)
                        {
                            importMath.calcCubicBezier(new Point(lastX, lastY), toPoint(spline.ControlPoints[3 * i + 1], offset), toPoint(spline.ControlPoints[3 * i + 2], offset), toPoint(spline.ControlPoints[3 * i + 3], offset), dxfMoveTo, "C");
                        }
                        else
                        {
                            gcodeDotOnly(lastX, lastY, "");
                            gcodeDotOnly(toPoint(spline.ControlPoints[3 * i + 3], offset), "");
                        }
                        lastX = (float)(spline.ControlPoints[3 * i + 3].X + offsetX);
                        lastY = (float)(spline.ControlPoints[3 * i + 3].Y + offsetY);
                        //  path += ' C %f,%f %f,%f %f,%f' % (vals[groups['10']][3 * i + 1], vals[groups['20']][3 * i + 1], vals[groups['10']][3 * i + 2], vals[groups['20']][3 * i + 2], vals[groups['10']][3 * i + 3], vals[groups['20']][3 * i + 3])
                    }
                    dxfStopPath();
                }
                if ((ctrls == 3) && (knots == 6)) //  # quadratic
                {                                 //  path = 'M %f,%f Q %f,%f %f,%f' % (vals[groups['10']][0], vals[groups['20']][0], vals[groups['10']][1], vals[groups['20']][1], vals[groups['10']][2], vals[groups['20']][2])
                    if (!nodesOnly)
                    {
                        dxfStartPath(lastX, lastY, cmt);
                        importMath.calcQuadraticBezier(toPoint(spline.ControlPoints[0], offset), toPoint(spline.ControlPoints[1], offset), toPoint(spline.ControlPoints[2], offset), dxfMoveTo, "Q");
                    }
                    else
                    {
                        gcodeDotOnly(lastX, lastY, "");
                        gcodeDotOnly(toPoint(spline.ControlPoints[2], offset), "");
                    }
                    dxfStopPath();
                }
                if ((ctrls == 5) && (knots == 8)) //  # spliced quadratic
                {                                 //  path = 'M %f,%f Q %f,%f %f,%f Q %f,%f %f,%f' % (vals[groups['10']][0], vals[groups['20']][0], vals[groups['10']][1], vals[groups['20']][1], vals[groups['10']][2], vals[groups['20']][2], vals[groups['10']][3], vals[groups['20']][3], vals[groups['10']][4], vals[groups['20']][4])
                    if (!nodesOnly)
                    {
                        dxfStartPath(lastX, lastY, cmt);
                        importMath.calcQuadraticBezier(toPoint(spline.ControlPoints[0], offset), toPoint(spline.ControlPoints[1], offset), toPoint(spline.ControlPoints[2], offset), dxfMoveTo, "SQ");
                        importMath.calcQuadraticBezier(toPoint(spline.ControlPoints[3], offset), toPoint(spline.ControlPoints[4], offset), toPoint(spline.ControlPoints[5], offset), dxfMoveTo, "SQ");
                    }
                    else
                    {
                        gcodeDotOnly(lastX, lastY, "");
                        gcodeDotOnly(toPoint(spline.ControlPoints[2], offset), "");
                        gcodeDotOnly(toPoint(spline.ControlPoints[5], offset), "");
                    }
                    dxfStopPath();
                }
            }
            #endregion
            #region DXFCircle
            else if (entity.GetType() == typeof(DXFCircle))
            {
                DXFCircle circle = (DXFCircle)entity;
                x = (float)circle.Center.X + (float)offsetX;
                y = (float)circle.Center.Y + (float)offsetY;
                dxfStartPath(x + circle.Radius, y, "Start Circle");
                Plotter.Arc(2, (float)x + (float)circle.Radius, (float)y, -(float)circle.Radius, 0, "");
                dxfStopPath();
                Logger.Trace("    Center: {0};{1}  R: {2}", x, y, circle.Radius);
            }
            #endregion
            #region DXFEllipse
            else if (entity.GetType() == typeof(DXFEllipse))
            {   // from Inkscape DXF import - modified
                // https://gitlab.com/inkscape/extensions/blob/master/dxf_input.py#L341
                DXFEllipse ellipse = (DXFEllipse)entity;
                float      xc      = (float)ellipse.Center.X + (float)offsetX;
                float      yc      = (float)ellipse.Center.Y + (float)offsetY;
                float      xm      = (float)ellipse.MainAxis.X;
                float      ym      = (float)ellipse.MainAxis.Y;
                float      w       = (float)ellipse.AxisRatio;
                double     a2      = -ellipse.StartParam;
                double     a1      = -ellipse.EndParam;

                float  rm   = (float)Math.Sqrt(xm * xm + ym * ym);
                double a    = Math.Atan2(-ym, xm);
                float  diff = (float)((a2 - a1 + 2 * Math.PI) % (2 * Math.PI));

                if ((Math.Abs(diff) > 0.0001) && (Math.Abs(diff - 2 * Math.PI) > 0.0001))
                {
                    int large = 0;
                    if (diff > Math.PI)
                    {
                        large = 1;
                    }
                    float xt = rm * (float)Math.Cos(a1);
                    float yt = w * rm * (float)Math.Sin(a1);
                    float x1 = (float)(xt * Math.Cos(a) - yt * Math.Sin(a));
                    float y1 = (float)(xt * Math.Sin(a) + yt * Math.Cos(a));
                    xt = rm * (float)Math.Cos(a2);
                    yt = w * rm * (float)Math.Sin(a2);
                    float x2 = (float)(xt * Math.Cos(a) - yt * Math.Sin(a));
                    float y2 = (float)(xt * Math.Sin(a) + yt * Math.Cos(a));
                    dxfStartPath(xc + x1, yc - y1, "Start Ellipse 1");
                    importMath.calcArc(xc + x1, yc - y1, rm, w * rm, (float)(-180.0 * a / Math.PI), large, 0, (xc + x2), (yc - y2), dxfMoveTo);
                    //  path = 'M %f,%f A %f,%f %f %d 0 %f,%f' % (xc + x1, yc - y1, rm, w* rm, -180.0 * a / math.pi, large, xc + x2, yc - y2)
                }
                else
                {
                    dxfStartPath(xc + xm, yc + ym, "Start Ellipse 2");
                    importMath.calcArc(xc + xm, yc + ym, rm, w * rm, (float)(-180.0 * a / Math.PI), 1, 0, xc - xm, yc - ym, dxfMoveTo);
                    importMath.calcArc(xc - xm, yc - ym, rm, w * rm, (float)(-180.0 * a / Math.PI), 1, 0, xc + xm, yc + ym, dxfMoveTo);
                    //    path = 'M %f,%f A %f,%f %f 1 0 %f,%f %f,%f %f 1 0 %f,%f z' % (xc + xm, yc - ym, rm, w* rm, -180.0 * a / math.pi, xc - xm, yc + ym, rm, w* rm, -180.0 * a / math.pi, xc + xm, yc - ym)
                }
                dxfStopPath();
                Logger.Trace("    Center: {0};{1}  R1: {2} R2: {3} Start: {4} End: {5}", xc, yc, rm, w * rm, ellipse.StartParam, ellipse.EndParam);
            }
            #endregion
            #region DXFArc
            else if (entity.GetType() == typeof(DXFArc))
            {
                DXFArc arc = (DXFArc)entity;

                double X          = (double)arc.Center.X + offsetX;
                double Y          = (double)arc.Center.Y + offsetY;
                double R          = arc.Radius;
                double startAngle = arc.StartAngle;
                double endAngle   = arc.EndAngle;
                if (startAngle > endAngle)
                {
                    endAngle += 360;
                }
                double stepwidth = (double)Properties.Settings.Default.importGCSegment; // from setup-GCode modification-Avoid G2/3 commands...
                float  StepAngle = (float)(Math.Asin(stepwidth / R) * 180 / Math.PI);
                double currAngle = startAngle;
                index = 0;
                if (!nodesOnly)
                {
                    while (currAngle < endAngle)
                    {
                        double angle = currAngle * Math.PI / 180;
                        double rx    = (double)(X + R * Math.Cos(angle));
                        double ry    = (double)(Y + R * Math.Sin(angle));

                        if (index == 0)
                        {
                            dxfStartPath(rx, ry, "Start Arc");
                            Plotter.IsPathReduceOk = true;
                        }
                        else
                        {
                            dxfMoveTo(rx, ry, "");
                        }
                        currAngle += StepAngle;
                        if (currAngle > endAngle)
                        {
                            double angle2 = endAngle * Math.PI / 180;
                            double rx2    = (double)(X + R * Math.Cos(angle2));
                            double ry2    = (double)(Y + R * Math.Sin(angle2));

                            if (index == 0)
                            {
                                dxfStartPath(rx2, ry2, "Start Arc");
                            }
                            else
                            {
                                dxfMoveTo(rx2, ry2, "");
                            }
                        }
                        index++;
                    }
                    dxfStopPath();
                    Logger.Trace("    Center: {0};{1}  R: {2}", X, Y, R);
                }
            }
            #endregion
            #region DXFMText
            else if (entity.GetType() == typeof(DXFMText))
            {   // https://www.autodesk.com/techpubs/autocad/acad2000/dxf/mtext_dxf_06.htm
                DXFMText txt    = (DXFMText)entity;
                xyPoint  origin = new xyPoint(0, 0);
                GCodeFromFont.reset();

                foreach (var entry in txt.Entries)
                {
                    if (entry.GroupCode == 1)
                    {
                        GCodeFromFont.gcText = entry.Value.ToString();
                    }
                    else if (entry.GroupCode == 40)
                    {
                        GCodeFromFont.gcHeight = double.Parse(entry.Value, CultureInfo.InvariantCulture.NumberFormat);
                    }
                    else if (entry.GroupCode == 41)
                    {
                        GCodeFromFont.gcWidth = double.Parse(entry.Value, CultureInfo.InvariantCulture.NumberFormat);
                    }
                    else if (entry.GroupCode == 71)
                    {
                        GCodeFromFont.gcAttachPoint = Convert.ToInt16(entry.Value);
                    }
                    else if (entry.GroupCode == 10)
                    {
                        GCodeFromFont.gcOffX = double.Parse(entry.Value, CultureInfo.InvariantCulture.NumberFormat) + offsetX;
                    }
                    else if (entry.GroupCode == 20)
                    {
                        GCodeFromFont.gcOffY = double.Parse(entry.Value, CultureInfo.InvariantCulture.NumberFormat) + offsetY;
                    }
                    else if (entry.GroupCode == 50)
                    {
                        GCodeFromFont.gcAngle = double.Parse(entry.Value, CultureInfo.InvariantCulture.NumberFormat);
                    }
                    else if (entry.GroupCode == 44)
                    {
                        GCodeFromFont.gcSpacing = double.Parse(entry.Value, CultureInfo.InvariantCulture.NumberFormat);
                    }
                    else if (entry.GroupCode == 7)
                    {
                        GCodeFromFont.gcFontName = entry.Value.ToString();
                    }
                }
                string tmp = string.Format("Id=\"{0}\" Color=\"#{1}\" ToolNr=\"{2}\"", dxfColorID, dxfColorHex, toolToUse);
                Plotter.InsertText(tmp);
                Plotter.IsPathFigureEnd = true;
                Logger.Trace("    Text: {0}", GCodeFromFont.gcText);
            }
            #endregion
            else
            {
                Plotter.Comment("Unknown: " + entity.GetType().ToString());
            }
        }
Beispiel #4
0
        /// <summary>
        /// Process entities
        /// </summary>
        private static void processEntities(DXFEntity entity, double offsetX = 0, double offsetY = 0, bool updateColor = true)
        {
            int    index = 0;
            double x, y, x2 = 0, y2 = 0, bulge;

            if (updateColor)
            {
                dxfColorID       = entity.ColorNumber;
                Plotter.PathName = "Layer:" + entity.LayerName;
            }

            Plotter.PathDashArray = new double[0];                  // default no dashes
            if (entity.LineType == "ByLayer")
            {
                if (layerLType.ContainsKey(entity.LayerName))        // check if layer name is known
                {
                    string dashType = layerLType[entity.LayerName];  // get name of pattern
                    if (lineTypes.ContainsKey(dashType))             // check if pattern name is known
                    {
                        Plotter.PathDashArray = lineTypes[dashType]; // apply pattern
                    }
                }
            }
            else
            {
                if (lineTypes.ContainsKey(entity.LineType))             // check if pattern name is known
                {
                    Plotter.PathDashArray = lineTypes[entity.LineType]; // apply pattern
                }
            }

            if (dxfColorID > 255)
            {
                if (layerColor.ContainsKey(entity.LayerName))
                {
                    dxfColorID = layerColor[entity.LayerName];
                }
            }

            if (dxfColorID < 0)
            {
                dxfColorID = 0;
            }
            if (dxfColorID > 255)
            {
                dxfColorID = 7;
            }
            if (Properties.Settings.Default.importDXFSwitchWhite && (dxfColorID == 7))
            {
                dxfColorID = 0;
            }

            dxfColorHex       = getColorFromID(dxfColorID);
            Plotter.PathColor = dxfColorHex;

            if (dxfUseColorIndex)
            {
                toolNr = dxfColorID + 1;      // avoid ID=0 to start tool-table with index 1
            }
            else
            {
                toolNr = toolTable.getToolNr(dxfColorHex, 0);
                //Logger.Trace("toolNr = {0}",toolNr);
            }

            Plotter.SetGroup(toolNr);       // set index if grouping and tool

            if (dxfColorIDold != dxfColorID)
            {
                Plotter.PenUp("");

                toolToUse = toolNr;
                if (Properties.Settings.Default.importGCToolTableUse && Properties.Settings.Default.importGCToolDefNrUse)
                {
                    toolToUse = (int)Properties.Settings.Default.importGCToolDefNr;
                }

                Plotter.PathToolNr = toolToUse;

                if (!groupObjects)
                {
                    if (dxfUseColorIndex)
                    {
                        Plotter.ToolChange(toolToUse, dxfColorID.ToString());   // add tool change commands (if enabled) and set XYFeed etc.
                    }
                    else
                    {
                        Plotter.ToolChange(toolToUse, dxfColorHex);
                    }
                }
            }
            dxfColorIDold = dxfColorID;

            if (entity.GetType() == typeof(DXFPointEntity))
            {
                DXFPointEntity point = (DXFPointEntity)entity;
                x = (float)point.Location.X + (float)offsetX;
                y = (float)point.Location.Y + (float)offsetY;
                if (!nodesOnly)
                {
                    dxfStartPath(x, y, "Start Point");
                    dxfStopPath();
                }
                else
                {
                    gcodeDotOnly(x, y, "Start Point");
                }
            }

            #region DXFLWPolyline
            else if (entity.GetType() == typeof(DXFLWPolyLine))
            {
                DXFLWPolyLine lp = (DXFLWPolyLine)entity;
                index = 0; bulge = 0;
                DXFLWPolyLine.Element coordinate;
                bool roundcorner = false;
                x = 0; y = 0;
                for (int i = 0; i < lp.VertexCount; i++)
                {
                    coordinate = lp.Elements[i];
                    bulge      = coordinate.Bulge;
                    x2         = x; y2 = y;
                    x          = (double)coordinate.Vertex.X + (double)offsetX;
                    y          = (double)coordinate.Vertex.Y + (double)offsetY;
                    if (i == 0)
                    {
                        if (!nodesOnly)
                        {
                            dxfStartPath(x, y, "Start LWPolyLine - Nr pts " + lp.VertexCount.ToString());
                            Plotter.IsPathReduceOk = true;
                        }
                        else
                        {
                            gcodeDotOnly(x, y, "Start LWPolyLine");
                        }
                    }

                    if ((!roundcorner))
                    {
                        dxfMoveTo(x, y, "");
                    }
                    if (bulge != 0)
                    {
                        if (i < (lp.VertexCount - 1))
                        {
                            AddRoundCorner(lp.Elements[i], lp.Elements[i + 1]);
                        }
                        else
                        if (lp.Flags == DXFLWPolyLine.FlagsEnum.closed)
                        {
                            AddRoundCorner(lp.Elements[i], lp.Elements[0]);
                        }
                        roundcorner = true;
                    }
                    else
                    {
                        roundcorner = false;
                    }
                }
                if ((lp.Flags > 0))// && (x2 != x) && (y2 != y))   // only move if prev pos is differnent
                {
                    dxfMoveTo((float)(lp.Elements[0].Vertex.X + offsetX), (float)(lp.Elements[0].Vertex.Y + offsetY), "End LWPolyLine " + lp.Flags.ToString());
                }
                dxfStopPath();
            }
            #endregion
            #region DXFPolyline
            else if (entity.GetType() == typeof(DXFPolyLine))
            {
                DXFPolyLine lp = (DXFPolyLine)entity;
                index = 0;
                foreach (DXFVertex coordinate in lp.Children)
                {
                    if (coordinate.GetType() == typeof(DXFVertex))
                    {
                        if (coordinate.Location.X != null && coordinate.Location.Y != null)
                        {
                            x = (float)coordinate.Location.X + (float)offsetX;
                            y = (float)coordinate.Location.Y + (float)offsetY;
                            if (!nodesOnly)
                            {
                                if (index == 0)
                                {
                                    dxfStartPath(x, y, "Start PolyLine");
                                }
                                else
                                {
                                    dxfMoveTo(x, y, "");
                                }
                            }
                            else
                            {
                                gcodeDotOnly(x, y, "PolyLine");
                            }
                            index++;
                        }
                    }
                }
                dxfStopPath();
            }
            #endregion
            #region DXFLine
            else if (entity.GetType() == typeof(DXFLine))
            {
                DXFLine line = (DXFLine)entity;
                x  = (float)line.Start.X + (float)offsetX;
                y  = (float)line.Start.Y + (float)offsetY;
                x2 = (float)line.End.X + (float)offsetX;
                y2 = (float)line.End.Y + (float)offsetY;
                Plotter.IsPathReduceOk = false;
                if (!nodesOnly)
                {
                    dxfStartPath(x, y, "Start Line");
                    dxfMoveTo(x2, y2, "");
                }
                else
                {
                    gcodeDotOnly(x, y, "Start Line");
                    gcodeDotOnly(x2, y2, "End Line");
                }
                dxfStopPath();
            }
            #endregion
            #region DXFSpline
            else if (entity.GetType() == typeof(DXFSpline))
            {
                DXFSpline spline = (DXFSpline)entity;
                index = 0;
                double cx0, cy0, cx1, cy1, cx2, cy2, cx3, cy3, cxMirror, cyMirror, lastX, lastY;
                lastX = (double)spline.ControlPoints[0].X + offsetX;
                lastY = (double)spline.ControlPoints[0].Y + offsetY;
                string cmt = "Start Spline " + spline.KnotValues.Count.ToString() + " " + spline.ControlPoints.Count.ToString() + " " + spline.FitPoints.Count.ToString();
                dxfStartPath(lastX, lastY, cmt);
                Plotter.IsPathReduceOk = true;

                for (int rep = 0; rep < spline.ControlPointCount; rep += 4)
                {
                    cx0       = (double)spline.ControlPoints[rep].X + offsetX; cy0 = (double)spline.ControlPoints[rep].Y + offsetY;
                    cx1       = (double)spline.ControlPoints[rep + 1].X + offsetX; cy1 = (double)spline.ControlPoints[rep + 1].Y + offsetY;
                    cx2       = (double)spline.ControlPoints[rep + 2].X + offsetX; cy2 = (double)spline.ControlPoints[rep + 2].Y + offsetY;
                    cx3       = (double)spline.ControlPoints[rep + 3].X + offsetX; cy3 = (double)spline.ControlPoints[rep + 3].Y + offsetY;
                    points    = new System.Windows.Point[4];
                    points[0] = new System.Windows.Point(cx0, cy0); //(qpx1, qpy1);
                    points[1] = new System.Windows.Point(cx1, cy1); //(qpx1, qpy1);
                    points[2] = new System.Windows.Point(cx2, cy2); //(qpx2, qpy2);
                    points[3] = new System.Windows.Point(cx3, cy3);
                    cxMirror  = cx3 - (cx2 - cx3); cyMirror = cy3 - (cy2 - cy3);
                    lastX     = cx3; lastY = cy3;
                    var b = GetBezierApproximation(points, dxfBezierAccuracy);
                    if (!nodesOnly)
                    {
                        for (int i = 1; i < b.Points.Count; i++)
                        {
                            dxfMoveTo((float)b.Points[i].X, (float)b.Points[i].Y, "");
                        }
                    }
                    else
                    {
                        gcodeDotOnly(cx3, cy3, "Bezier");
                    }
                }
                dxfStopPath();
            }
            #endregion
            #region DXFCircle
            else if (entity.GetType() == typeof(DXFCircle))
            {
                DXFCircle circle = (DXFCircle)entity;
                x = (float)circle.Center.X + (float)offsetX;
                y = (float)circle.Center.Y + (float)offsetY;
                dxfStartPath(x + circle.Radius, y, "Start Circle");
                Plotter.Arc(2, (float)x + (float)circle.Radius, (float)y, -(float)circle.Radius, 0, "");
                dxfStopPath();
            }
            #endregion

            else if (entity.GetType() == typeof(DXFEllipse))
            {
                DXFEllipse circle = (DXFEllipse)entity;
                Plotter.Comment("Ellipse: " + circle.ColorNumber.ToString());
            }
            #region DXFArc
            else if (entity.GetType() == typeof(DXFArc))
            {
                DXFArc arc = (DXFArc)entity;

                double X          = (double)arc.Center.X + offsetX;
                double Y          = (double)arc.Center.Y + offsetY;
                double R          = arc.Radius;
                double startAngle = arc.StartAngle;
                double endAngle   = arc.EndAngle;
                if (startAngle > endAngle)
                {
                    endAngle += 360;
                }
                double stepwidth = (double)Properties.Settings.Default.importGCSegment;
                float  StepAngle = (float)(Math.Asin(stepwidth / R) * 180 / Math.PI);// Settings.Default.page11arcMaxLengLine);
                double currAngle = startAngle;
                index = 0;
                if (!nodesOnly)
                {
                    while (currAngle < endAngle)
                    {
                        double angle = currAngle * Math.PI / 180;
                        double rx    = (double)(X + R * Math.Cos(angle));
                        double ry    = (double)(Y + R * Math.Sin(angle));

                        if (index == 0)
                        {
                            dxfStartPath(rx, ry, "Start Arc");
                            Plotter.IsPathReduceOk = true;
                        }
                        else
                        {
                            dxfMoveTo(rx, ry, "");
                        }
                        currAngle += StepAngle;
                        if (currAngle > endAngle)
                        {
                            double angle2 = endAngle * Math.PI / 180;
                            double rx2    = (double)(X + R * Math.Cos(angle2));
                            double ry2    = (double)(Y + R * Math.Sin(angle2));

                            if (index == 0)
                            {
                                dxfStartPath(rx2, ry2, "Start Arc");
                            }
                            else
                            {
                                dxfMoveTo(rx2, ry2, "");
                            }
                        }
                        index++;
                    }
                    dxfStopPath();
                }
            }
            #endregion
            #region DXFMText
            else if (entity.GetType() == typeof(DXFMText))
            {   // https://www.autodesk.com/techpubs/autocad/acad2000/dxf/mtext_dxf_06.htm
                DXFMText txt    = (DXFMText)entity;
                xyPoint  origin = new xyPoint(0, 0);
                GCodeFromFont.reset();

                foreach (var entry in txt.Entries)
                {
                    if (entry.GroupCode == 1)
                    {
                        GCodeFromFont.gcText = entry.Value.ToString();
                    }
                    else if (entry.GroupCode == 40)
                    {
                        GCodeFromFont.gcHeight = double.Parse(entry.Value, CultureInfo.InvariantCulture.NumberFormat);
                    }
                    else if (entry.GroupCode == 41)
                    {
                        GCodeFromFont.gcWidth = double.Parse(entry.Value, CultureInfo.InvariantCulture.NumberFormat);
                    }
                    else if (entry.GroupCode == 71)
                    {
                        GCodeFromFont.gcAttachPoint = Convert.ToInt16(entry.Value);
                    }
                    else if (entry.GroupCode == 10)
                    {
                        GCodeFromFont.gcOffX = double.Parse(entry.Value, CultureInfo.InvariantCulture.NumberFormat) + offsetX;
                    }
                    else if (entry.GroupCode == 20)
                    {
                        GCodeFromFont.gcOffY = double.Parse(entry.Value, CultureInfo.InvariantCulture.NumberFormat) + offsetY;
                    }
                    else if (entry.GroupCode == 50)
                    {
                        GCodeFromFont.gcAngle = double.Parse(entry.Value, CultureInfo.InvariantCulture.NumberFormat);
                    }
                    else if (entry.GroupCode == 44)
                    {
                        GCodeFromFont.gcSpacing = double.Parse(entry.Value, CultureInfo.InvariantCulture.NumberFormat);
                    }
                    else if (entry.GroupCode == 7)
                    {
                        GCodeFromFont.gcFontName = entry.Value.ToString();
                    }
                }
                string tmp = string.Format("Id=\"{0}\" Color=\"#{1}\" ToolNr=\"{2}\"", dxfColorID, dxfColorHex, toolToUse);
                Plotter.InsertText(tmp);
                Plotter.IsPathFigureEnd = true;
            }
            #endregion
            else
            {
                Plotter.Comment("Unknown: " + entity.GetType().ToString());
            }
        }