Esempio n. 1
0
        public void WriteGCode(bool liftHeadIfNeeded, int layerThickness)
        {
            GCodePathConfig lastConfig    = null;
            int             extruderIndex = gcode.GetExtruderIndex();

            for (int pathIndex = 0; pathIndex < paths.Count; pathIndex++)
            {
                GCodePath path = paths[pathIndex];
                if (extruderIndex != path.extruderIndex)
                {
                    extruderIndex = path.extruderIndex;
                    gcode.SwitchExtruder(extruderIndex);
                }
                else if (path.Retract)
                {
                    gcode.WriteRetraction();
                }
                if (path.config != travelConfig && lastConfig != path.config)
                {
                    //gcode.WriteComment("TYPE:{0}".FormatWith(path.config.gcodeComment));
                    lastConfig = path.config;

                    ////后加的//只是边框,不填充。
                    if ((path.config.gcodeComment == "SKIRT") || (path.config.gcodeComment == "FILL") || (path.config.gcodeComment == "TOP-FILL"))
                    {
                        return;
                    }
                }

                double speed = path.config.speed;
                if (path.config.lineWidth != 0)
                {
                    // Only apply the extrudeSpeedFactor to extrusion moves
                    speed = speed * extrudeSpeedFactor / 100;
                }
                else
                {
                    speed = speed * travelSpeedFactor / 100;
                }

                if (path.points.Count == 1 &&
                    path.config != travelConfig &&
                    (gcode.GetPositionXY() - path.points[0]).ShorterThen(path.config.lineWidth * 2))
                {
                    //Check for lots of small moves and combine them into one large line
                    IntPoint nextPosition = path.points[0];
                    int      i            = pathIndex + 1;
                    while (i < paths.Count && paths[i].points.Count == 1 && (nextPosition - paths[i].points[0]).ShorterThen(path.config.lineWidth * 2))
                    {
                        nextPosition = paths[i].points[0];
                        i++;
                    }
                    if (paths[i - 1].config == travelConfig)
                    {
                        i--;
                    }

                    if (i > pathIndex + 2)
                    {
                        nextPosition = gcode.GetPositionXY();
                        for (int x = pathIndex; x < i - 1; x += 2)
                        {
                            long     oldLen   = (nextPosition - paths[x].points[0]).Length();
                            IntPoint newPoint = (paths[x].points[0] + paths[x + 1].points[0]) / 2;
                            long     newLen   = (gcode.GetPositionXY() - newPoint).Length();
                            if (newLen > 0)
                            {
                                gcode.WriteMove(newPoint, speed, (int)(path.config.lineWidth * oldLen / newLen));
                            }

                            nextPosition = paths[x + 1].points[0];
                        }

                        gcode.WriteMove(paths[i - 1].points[0], speed, path.config.lineWidth);
                        pathIndex = i - 1;
                        continue;
                    }
                }

                bool spiralize = path.config.spiralize;
                if (spiralize)
                {
                    //Check if we are the last spiralize path in the list, if not, do not spiralize.
                    for (int m = pathIndex + 1; m < paths.Count; m++)
                    {
                        if (paths[m].config.spiralize)
                        {
                            spiralize = false;
                        }
                    }
                }
                if (spiralize)
                {
                    //If we need to spiralize then raise the head slowly by 1 layer as this path progresses.
                    double   totalLength     = 0;
                    int      z               = gcode.GetPositionZ();
                    IntPoint currentPosition = gcode.GetPositionXY();
                    for (int pointIndex = 0; pointIndex < path.points.Count; pointIndex++)
                    {
                        IntPoint nextPosition = path.points[pointIndex];
                        totalLength    += (currentPosition - nextPosition).LengthMm();
                        currentPosition = nextPosition;
                    }

                    double length = 0.0;
                    currentPosition = gcode.GetPositionXY();
                    for (int i = 0; i < path.points.Count; i++)
                    {
                        IntPoint nextPosition = path.points[i];
                        length         += (currentPosition - nextPosition).LengthMm();
                        currentPosition = nextPosition;
                        gcode.setZ((int)(z + layerThickness * length / totalLength + .5));
                        gcode.WriteMove(path.points[i], speed, path.config.lineWidth);
                    }
                }
                else
                {
#if false
                    // This is test code to remove double drawn small perimeter lines.
                    if (path.config.lineWidth > 0 &&
                        path.points.Count > 2 &&                         // If the count is not greater than 2 there is no way it can ovelap itself.
                        gcode.GetPositionXY() == path.points[path.points.Count - 1])
                    {
                        Polygons pathsWithOverlapsRemoved = GetPathsWithOverlapsRemoved(path.points, path.config.lineWidth / 2);
                        if (pathsWithOverlapsRemoved.Count > 0)
                        {
                            for (int polygonIndex = 0; polygonIndex < pathsWithOverlapsRemoved.Count; polygonIndex++)
                            {
                                int     startIndex = 0;
                                Polygon polygon    = pathsWithOverlapsRemoved[polygonIndex];
                                if (polygonIndex > 0)
                                {
                                    gcode.WriteMove(polygon[0], travelConfig.speed, 0);
                                    startIndex = 1;                                     // We skip the first point in the next extrusion, because we just moved to it.
                                }

                                for (int pointIndex = startIndex; pointIndex < polygon.Count; pointIndex++)
                                {
                                    gcode.WriteMove(polygon[pointIndex], speed, path.config.lineWidth);
                                }
                            }
                        }
                    }
                    else
#endif
                    {
                        //TrimPerimeterIfNeeded(path);

                        for (int i = 0; i < path.points.Count; i++)
                        {
                            gcode.WriteMove(path.points[i], speed, path.config.lineWidth);
                        }
                    }
                }
            }

            gcode.UpdateTotalPrintTime();
            if (liftHeadIfNeeded && extraTime > 0.0)
            {
                gcode.WriteComment("Small layer, adding delay of {0}".FormatWith(extraTime));
                gcode.WriteRetraction();
                gcode.setZ(gcode.GetPositionZ() + 3000);
                gcode.WriteMove(gcode.GetPositionXY(), travelConfig.speed, 0);
                gcode.WriteMove(gcode.GetPositionXY() - new IntPoint(-20000, 0), travelConfig.speed, 0);
                gcode.WriteDelay(extraTime);
            }
        }