Esempio n. 1
0
        public void BuildTemplate(GCodeSketch gcodeSketch, bool verticalLayout)
        {
            gcodeSketch.SetTool("T0");
            gcodeSketch.WriteRaw($"G1 Z0.2 F{firstLayerSpeed}");

            if (verticalLayout)
            {
                gcodeSketch.Transform = Affine.NewRotation(MathHelper.DegreesToRadians(90)) * Affine.NewTranslation(105, 45);
            }
            else
            {
                gcodeSketch.Transform = Affine.NewTranslation(75, 175);
            }

            var rect = new RectangleDouble(0, 0, 123, 30);

            var originalRect = rect;

            int towerSize = 10;

            gcodeSketch.Speed = firstLayerSpeed;

            double y1 = rect.Bottom;

            gcodeSketch.MoveTo(rect.Left, y1);

            var towerRect = new RectangleDouble(0, 0, towerSize, towerSize);

            towerRect.Offset(originalRect.Left - towerSize, originalRect.Bottom);

            // Prime
            this.PrimeHotend(gcodeSketch, towerRect);

            // Perimeters
            rect = this.CreatePerimeters(gcodeSketch, rect);

            double x, y2, y3;
            double sectionHeight = rect.Height / 2;
            bool   up            = true;
            var    step          = (rect.Width - 3) / 40;

            if (!this.DebugMode)
            {
                y1 = rect.YCenter + (nozzleWidth / 2);

                // Draw centerline
                gcodeSketch.MoveTo(rect.Left, y1);
                gcodeSketch.LineTo(rect.Right, y1);
                y1 += nozzleWidth;
                gcodeSketch.MoveTo(rect.Right, y1);
                gcodeSketch.LineTo(rect.Left, y1);

                y1 -= nozzleWidth / 2;

                x = rect.Left + 1.5;

                y2 = y1 - sectionHeight - (nozzleWidth * 1.5);
                y3 = y2 - 5;

                bool drawGlpyphs = false;

                // Draw calibration lines
                for (var i = 0; i <= 40; i++)
                {
                    gcodeSketch.MoveTo(x, up ? y1 : y2);

                    if ((i % 5 == 0))
                    {
                        gcodeSketch.LineTo(x, y3);

                        var currentPos = gcodeSketch.CurrentPosition;

                        gcodeSketch.Speed = 500;

                        PrintLineEnd(gcodeSketch, drawGlpyphs, i, currentPos);

                        gcodeSketch.Speed = 1800;

                        gcodeSketch.MoveTo(x, y3);
                        gcodeSketch.MoveTo(x, y2);
                    }

                    gcodeSketch.LineTo(x, up ? y2 : y1);

                    x = x + step;

                    up = !up;
                }
            }

            x  = rect.Left + 1.5;
            y1 = rect.Top + (nozzleWidth * .5);
            y2 = y1 - sectionHeight + (nozzleWidth * .5);

            gcodeSketch.SetTool("T1");

            gcodeSketch.MoveTo(rect.Left, rect.Top);

            gcodeSketch.PenDown();

            towerRect = new RectangleDouble(0, 0, towerSize, towerSize);
            towerRect.Offset(originalRect.Left - towerSize, originalRect.Top - towerSize);

            // Prime
            this.PrimeHotend(gcodeSketch, towerRect);

            if (this.DebugMode)
            {
                // Perimeters
                rect = this.CreatePerimeters(gcodeSketch, rect);
            }
            else
            {
                up = true;

                // Draw calibration lines
                for (var i = 0; i <= 40; i++)
                {
                    gcodeSketch.MoveTo(x + activeOffsets[i], up ? y1 : y2, retract: true);
                    gcodeSketch.LineTo(x + activeOffsets[i], up ? y2 : y1);

                    x = x + step;

                    up = !up;
                }
            }

            gcodeSketch.PenUp();
        }
        private static void PrintLineEnd(GCodeSketch turtle, bool drawGlyphs, int i, Vector2 currentPos, bool lift = false)
        {
            var originalSpeed = turtle.Speed;

            turtle.Speed = Math.Min(700, turtle.Speed);

            if (drawGlyphs && CalibrationLine.Glyphs.TryGetValue(i, out IVertexSource vertexSource))
            {
                turtle.WriteRaw("; LineEnd Marker");
                var flattened = new FlattenCurves(vertexSource);

                var verticies = flattened.Vertices();
                var firstItem = verticies.First();
                var position  = turtle.CurrentPosition;

                var scale = 0.3;

                if (firstItem.command != ShapePath.FlagsAndCommand.MoveTo)
                {
                    if (lift)
                    {
                        turtle.PenUp();
                    }

                    turtle.MoveTo((firstItem.position * scale) + currentPos);
                }

                bool closed = false;

                foreach (var item in verticies)
                {
                    switch (item.command)
                    {
                    case ShapePath.FlagsAndCommand.MoveTo:
                        if (lift)
                        {
                            turtle.PenUp();
                        }

                        turtle.MoveTo((item.position * scale) + currentPos);

                        if (lift)
                        {
                            turtle.PenDown();
                        }
                        break;

                    case ShapePath.FlagsAndCommand.LineTo:
                        turtle.LineTo((item.position * scale) + currentPos);
                        break;

                    case ShapePath.FlagsAndCommand.FlagClose:
                        turtle.LineTo((firstItem.position * scale) + currentPos);
                        closed = true;
                        break;
                    }
                }

                bool atStartingPosition = position.Equals(turtle.CurrentPosition, .1);

                if (!closed &&
                    !atStartingPosition)
                {
                    turtle.LineTo((firstItem.position * scale) + currentPos);
                    atStartingPosition = position.Equals(turtle.CurrentPosition, .1);
                }

                // Restore original speed
                turtle.Speed = originalSpeed;

                if (!atStartingPosition)
                {
                    // Return to original position
                    turtle.PenUp();
                    turtle.MoveTo(currentPos);
                    turtle.PenDown();
                }
            }
        }