/// <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()); } }
private List <GroupPoint> GetVectorDXF(string FileNAME) { textBoxInfo.Text = ""; DXFDocument doc = new DXFDocument(); doc.Load(FileNAME); List <GroupPoint> ListLines; List <cncPoint> ListPoint; ListLines = new List <GroupPoint>(); int count_DXFLWPolyLine = 0; int count_DXFPolyLine = 0; int count_DXFLine = 0; int count_DXFCircle = 0; int count_DXFSpline = 0; int count_DXFArc = 0; double minX = 99999; double maxX = -99999; double deltaX = 0; double minY = 99999; double maxY = -99999; double deltaY = 0; foreach (DXFEntity VARIABLE in doc.Entities) { bool typeDetected = false; if (VARIABLE.GetType() == typeof(DXFLWPolyLine)) { typeDetected = true; count_DXFLWPolyLine++; DXFLWPolyLine lp = (DXFLWPolyLine)VARIABLE; ListPoint = new List <cncPoint>(); foreach (DXFLWPolyLine.Element pl_e in lp.Elements) { ListPoint.Add(new cncPoint((double)pl_e.Vertex.X, (double)pl_e.Vertex.Y)); } ListLines.Add(new GroupPoint(ListPoint)); ListPoint = new List <cncPoint>(); } if (VARIABLE.GetType() == typeof(DXFPolyLine)) { typeDetected = true; count_DXFPolyLine++; DXFPolyLine lp = (DXFPolyLine)VARIABLE; ListPoint = new List <cncPoint>(); foreach (DXFVertex pl_e in lp.Children) { if (pl_e.GetType() == typeof(DXFVertex)) { if (pl_e.Location.X != null && pl_e.Location.Y != null) { ListPoint.Add(new cncPoint((double)pl_e.Location.X, (double)pl_e.Location.Y)); } } } ListLines.Add(new GroupPoint(ListPoint)); ListPoint = new List <cncPoint>(); } if (VARIABLE.GetType() == typeof(DXFLine)) { typeDetected = true; count_DXFLine++; DXFLine line = (DXFLine)VARIABLE; ListPoint = new List <cncPoint>(); ListPoint.Add(new cncPoint((double)line.Start.X, (double)line.Start.Y)); ListPoint.Add(new cncPoint((double)line.End.X, (double)line.End.Y)); ListLines.Add(new GroupPoint(ListPoint)); ListPoint = new List <cncPoint>(); } if (VARIABLE.GetType() == typeof(DXFSpline)) { typeDetected = true; count_DXFSpline++; DXFSpline spline = (DXFSpline)VARIABLE; ListPoint = new List <cncPoint>(); foreach (DXFPoint ppoint in spline.ControlPoints) { ListPoint.Add(new cncPoint((double)ppoint.X, (double)ppoint.Y)); } ListLines.Add(new GroupPoint(ListPoint)); ListPoint = new List <cncPoint>(); } if (VARIABLE.GetType() == typeof(DXFCircle)) { typeDetected = true; count_DXFCircle++; DXFCircle circle = (DXFCircle)VARIABLE; double X = (double)circle.Center.X; double Y = (double)circle.Center.Y; float R = (float)circle.Radius; //тут определим необходимый угол //и вычислим количество сегментов double angle = GetAngleArcSegment((decimal)R, Settings.Default.page11arcMaxLengLine); if (angle < 1) { angle = 1; } int segmentsCount = 360 / (int)angle; ListPoint = new List <cncPoint>(); for (int i = 0; i < segmentsCount; i++) { float rx = R * (float)Math.Cos(2 * (float)Math.PI / segmentsCount * i); float ry = R * (float)Math.Sin(2 * (float)Math.PI / segmentsCount * i); ListPoint.Add(new cncPoint(X + (double)rx, Y + (double)ry)); } ListPoint.Add(new cncPoint(ListPoint[0].X, ListPoint[0].Y)); ListLines.Add(new GroupPoint(ListPoint)); ListPoint = new List <cncPoint>(); // //ListPoint.Add(new Location((decimal)line.Start.X, (decimal)line.Start.Y)); //ListPoint.Add(new Location((decimal)line.End.X, (decimal)line.End.Y)); // } if (VARIABLE.GetType() == typeof(DXFArc)) { typeDetected = true; count_DXFArc++; DXFArc arc = (DXFArc)VARIABLE; double X = (double)arc.Center.X; double Y = (double)arc.Center.Y; double R = arc.Radius; double startAngle = arc.StartAngle; double endAngle = arc.EndAngle; if (startAngle > endAngle) { endAngle += 360; } //тут определим необходимый угол //и вычислим количество сегментов float StepAngle = (float)GetAngleArcSegment((decimal)R, Settings.Default.page11arcMaxLengLine); ListPoint = new List <cncPoint>(); double currAngle = startAngle; 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)); ListPoint.Add(new cncPoint(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)); ListPoint.Add(new cncPoint(rx2, ry2)); } } // int segmentsCount = 360 / (int)angle; //for (float i = startAngle; i < endAngle; i+= (float)angle) //{ // //} ListLines.Add(new GroupPoint(ListPoint)); ListPoint = new List <cncPoint>(); } if (!typeDetected) { textBoxInfo.Text += "Не распознан тип данных: " + VARIABLE.GetType().ToString() + Environment.NewLine; } } //ListLines = new List<Segment>(); textBoxInfo.Text += @"Обработано DXFLWPolyLine: " + count_DXFLWPolyLine.ToString() + Environment.NewLine; textBoxInfo.Text += @"Обработано DXFPolyLine: " + count_DXFPolyLine.ToString() + Environment.NewLine; textBoxInfo.Text += @"Обработано DXFLine: " + count_DXFLine.ToString() + Environment.NewLine; textBoxInfo.Text += @"Обработано DXFCircle: " + count_DXFCircle.ToString() + Environment.NewLine; textBoxInfo.Text += @"Обработано DXFSpline: " + count_DXFSpline.ToString() + Environment.NewLine; textBoxInfo.Text += @"Обработано DXFArc: " + count_DXFArc.ToString() + Environment.NewLine; foreach (GroupPoint vSegment in ListLines) { foreach (cncPoint vLocation in vSegment.Points) { if (vLocation.X < minX) { minX = vLocation.X; } if (vLocation.Y < minY) { minY = vLocation.Y; } if (vLocation.X > maxX) { maxX = vLocation.X; } if (vLocation.Y > maxY) { maxY = vLocation.Y; } } } deltaX = maxX - minX; deltaY = maxY - minY; textBoxInfo.Text += @"Размер по X: " + deltaX.ToString() + Environment.NewLine; textBoxInfo.Text += @"Размер по Y: " + deltaY.ToString() + Environment.NewLine; return(ListLines); }
/// <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()); } }
/// <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()); } }