예제 #1
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);
        }
예제 #2
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());
            }
        }