public static Polygons GetExtrusionPolygonsForLayer(this string[] layerGCode, ref MovementInfo movementInfo, bool validateOverlaps = true) { var foundPolygons = new Polygons(); bool extruding = false; int movementCount = 0; double movementAmount = double.MaxValue / 2; // always add a new extrusion the first time MovementInfo lastMovement = movementInfo; MovementInfo lastLastMovement = movementInfo; foreach (MovementInfo currentMovement in TestUtilities.GetLayerMovements(layerGCode, lastMovement)) { bool isRetraction = currentMovement.extrusion != lastMovement.extrusion && (currentMovement.position == lastLastMovement.position); bool isZLift = currentMovement.position.x == lastLastMovement.position.x && currentMovement.position.y == lastLastMovement.position.y && currentMovement.position.z != lastLastMovement.position.z; bool isExtrude = !isRetraction && !isZLift && currentMovement.extrusion != lastMovement.extrusion; if (extruding) { if (isExtrude) { // add to the extrusion foundPolygons[foundPolygons.Count - 1].Add(new IntPoint( (long)(currentMovement.position.x * 1000), (long)(currentMovement.position.y * 1000), (long)(currentMovement.position.z * 1000))); } else { // we are switching so add in the point to the last extrude extruding = false; movementAmount = 0; if (foundPolygons[foundPolygons.Count - 1].Count == 1) { foundPolygons[foundPolygons.Count - 1].Add(new IntPoint( (long)(lastLastMovement.position.x * 1000), (long)(lastLastMovement.position.y * 1000), (long)(lastLastMovement.position.z * 1000))); } } } else // not extruding { if (isExtrude) { if (movementAmount >= 0) { // starting a new extrusion foundPolygons.Add(new Polygon()); } foundPolygons[foundPolygons.Count - 1].Add(new IntPoint( (long)(currentMovement.position.x * 1000), (long)(currentMovement.position.y * 1000), (long)(currentMovement.position.z * 1000))); extruding = true; } else // do nothing waiting for extrude { movementAmount += (currentMovement.position - lastMovement.position).Length; } } lastLastMovement = lastMovement; lastMovement = currentMovement; movementCount++; } for (int i = foundPolygons.Count - 1; i >= 0; i--) { if (!extruding && foundPolygons[i].Count == 1) { foundPolygons.RemoveAt(i); } else if (foundPolygons[foundPolygons.Count - 1].Count == 1) { foundPolygons[foundPolygons.Count - 1].Add(new IntPoint( (long)(lastLastMovement.position.x * 1000), (long)(lastLastMovement.position.y * 1000), (long)(lastLastMovement.position.z * 1000))); break; } } movementInfo = lastMovement; // validate that the polygons do not double extrude if (validateOverlaps) { Assert.IsFalse(HasOverlapingSegments(foundPolygons)); } return(foundPolygons); }