IEnumerator StartMovementAroundPath(List <gcLine> gcodePath)
    {
        int c = 0;

        while (c < gcodePath.Count)
        {
            if (DoneWithLine)
            {
                DoneWithLine = false;
                if (!Cnc_Settings.RelativeMovement || c == 0)
                {
                    StartCoroutine(Move(gcodePath[c]));
                }
                else
                {
                    gcLine relativeLine = new gcLine();
                    relativeLine   = gcodePath[c];
                    relativeLine.X = gcodePath[c].X - gcodePath[c - 1].X;
                    relativeLine.Y = gcodePath[c].Y - gcodePath[c - 1].Y;
                    relativeLine.Z = gcodePath[c].Z - gcodePath[c - 1].Z;
                    StartCoroutine(Move(relativeLine));
                }
            }
            yield return(new WaitUntil(() => DoneWithLine == true));

            c++;
        }
        yield return(null);
    }
    IEnumerator Move(gcLine moveTo)
    {
        float         distanceThreshold = 0.1f;
        CncCartDouble position          = new CncCartDouble();

        position.x = (double)moveTo.X;
        position.y = (double)moveTo.Y;
        position.z = (double)moveTo.Z;
        CncCartBool Cncbool = new CncCartBool();

        Cncbool.x = 1;
        Cncbool.y = 1;
        Cncbool.z = 1;
        Cncbool.a = 1;
        Cncbool.b = 1;
        Cncbool.c = 1;
        while (Vector3.Distance(HeadPosition, new Vector3((float)moveTo.X, (float)moveTo.Y, (float)moveTo.Z)) < distanceThreshold)
        {
            g_JoggingFunctions.MoveTo(position, Cncbool, (double)moveTo.F);
            yield return(null);
        }
        DoneWithLine = true;
        Moving       = false;
        movee        = false;
        yield return(null);
    }
Example #3
0
    internal void ParseFromGcodeFile()
    {               //Initializing arrays to fill
        int c = -1; // counter for line number

        foreach (string line in fileLinebyLine)
        {
            gcLine gcl = new gcLine();
            gcl.linenr = c++;
            gcl.G      = int.Parse(getValue(line, "G", " "));
            gcl.X      = float.Parse(getValue(line, "X", " "));
            gcl.Y      = float.Parse(getValue(line, "Y", " "));
            gcl.Z      = float.Parse(getValue(line, "Z", " "));
            gcl.F      = float.Parse(getValue(line, "F", " "));
            gcl.I      = float.Parse(getValue(line, "I", " "));
            gcl.J      = float.Parse(getValue(line, "J", " "));
            gcl.K      = float.Parse(getValue(line, "K", " "));
            gcl.L      = float.Parse(getValue(line, "L", " "));
            gcl.N      = float.Parse(getValue(line, "B", " "));
            gcl.P      = float.Parse(getValue(line, "P", " "));
            gcl.R      = float.Parse(getValue(line, "R", " "));
            gcl.S      = float.Parse(getValue(line, "S", " "));
            gcl.T      = float.Parse(getValue(line, "T", " "));
            lineList.Add(gcl);
        }
        lineList   = fill(lineList);
        FileLoaded = true;
        Linebuilder.buildlinesFromGcode();
    }
Example #4
0
 gcLine createRandomizedStretchLine(gcLine gcl, lineInfo lineinf)
 {
     lineinf.x = Random.Range(-(Cnc_Settings.WidthInMM / 2), (float)gcl.X);
     return(new gcLine
     {
         G = gcl.G,
         F = gcl.F,
         X = lineinf.x,
         Y = lineinf.y,
         Z = gcl.Z
     });
 }
Example #5
0
    public List <gcLine> ExportLinesToGcode(List <gcLine> gCodeLines)
    {
        List <gcLine> toExport = new List <gcLine>();

        for (int i = 0; i < gCodeLines.Count; i++)
        {
            gcLine line = new gcLine();
            line      = gCodeLines[i];
            line.AUX1 = LinePlaceHolder.transform.GetChild(i).GetComponent <LineRenderer>().enabled;
            toExport.Add(line);
        }
        Dictionary <int, List <gcLine> > delayLines = new Dictionary <int, List <gcLine> >();

        for (int i = 0; i < gCodeLines.Count; i++)
        {
            if (i > 0 && i < gCodeLines.Count - 1)
            {
                if (gCodeLines[i].G != 4 && gCodeLines[i + 1].G != 4)
                {
                    if (gCodeLines[i].AUX1 != gCodeLines[i + 1].AUX1)
                    { // indrukken van spuit +- 0.5f ,
                        // loslaten -0.2f
                        gcLine auxline = new gcLine();
                        auxline.AUX1 = gCodeLines[i + 1].AUX1;
                        gcLine delayline = new gcLine();
                        delayline.linenr = i;
                        delayline.G      = 4;
                        delayline.P      = 0.5f;
                        List <gcLine> delayAuxlines = new List <gcLine>();
                        delayAuxlines.Add(delayline);
                        delayAuxlines.Add(auxline);
                        delayAuxlines.Add(delayline);
                        delayLines.Add(i, delayAuxlines);
                    }
                }
            }
        }

        foreach (KeyValuePair <int, List <gcLine> > delayline in delayLines)
        {
            toExport.InsertRange(delayline.Key, delayline.Value);
        }



        return(toExport);
    }
Example #6
0
    internal void GenerateGcodeFromPath()
    {
        List <Coords> coords = OriginalCoords;

        if (coords.Count == 0)
        {
            return;
        }
        bool          notsafe       = false;
        List <gcLine> gcodeFromPath = new List <gcLine>();

        gcodeFromPathToExport.Clear();

        if (centerPath)
        {
            float midxPath = coords.Min(x => x.X) + ((coords.Max(x => x.X) - coords.Min(x => x.X)) / 2);
            float midyPath = coords.Min(x => x.Y) + ((coords.Max(x => x.Y) - coords.Min(x => x.Y)) / 2);
            float deltaX   = MiddlePointGcode.transform.position.x - midxPath;
            float deltaY   = MiddlePointGcode.transform.position.y - midyPath;
            foreach (Coords coord in coords)
            {
                coord.X += deltaX;
                coord.Y += deltaY;
            }
        }

        // Create gcode from the path you want to draw.. without any manipulations like stretching
        for (int i = 0; i < coords.Count; i++)
        {
            if (i == 0 || i == coords.Count - 1)
            {
                gcLine gcl = new gcLine();
                gcl.X = float.Parse((coords[i].X).ToString("F4"));
                gcl.Y = float.Parse((coords[i].Y).ToString("F4"));
                gcl.Z = float.Parse((coords[i].Z).ToString("F4"));
                gcl.F = StandardFeed;
                gcl.G = 1;

                gcl.AUX1 = (bool)coords[i].Travel == true ? AUXOnInTravels : AUXOnInFigures;

                gcodeFromPath.Add(gcl);
            }
            else
            {
                gcLine gcl = new gcLine();
                gcl.X    = float.Parse((coords[i].X).ToString("F4"));
                gcl.Y    = float.Parse((coords[i].Y).ToString("F4"));
                gcl.Z    = float.Parse((coords[i].Z).ToString("F4"));
                gcl.F    = StandardFeed;
                gcl.G    = 1;
                gcl.AUX1 = (bool)coords[i].Travel == true ? AUXOnInTravels : AUXOnInFigures;

                gcodeFromPath.Add(gcl);
            }
        }



        gcodeFromPath = fill(gcodeFromPath);

        if (Cnc_Settings.ScaleToMax)
        {
            scaleToUseHorizontal = Cnc_Settings.ScaleFactorForMax;
            scaleToUseVertical   = Cnc_Settings.ScaleFactorForMax;
        }
        float midPointX = MiddlePointGcode.transform.position.x;
        float midPointY = MiddlePointGcode.transform.position.y;
        float midPointZ = MiddlePointGcode.transform.position.z;

        if (StretchLines)
        {
            List <gcLine> gcLinesBackup = new List <gcLine>(gcodeFromPath);
            gcodeFromPath.Clear();
            Dictionary <int, gcLine> StretchLineToAdd = new Dictionary <int, gcLine>();
            float lastknownFeed = 0;

            for (int i = 0; i < gcLinesBackup.Count - 1; i++)
            {
                // y = a*x+b
                // a = deltax/deltay
                // b = (insert 1 coordinate)..

                gcodeFromPath.Add(gcLinesBackup[i]);

                if (i > 1 && i < gcLinesBackup.Count - 2 && i % 2 != 0)
                {
                    for (int j = 0; j < 2; j++)
                    {
                        gcLine randomLineA = createRandomizedStretchLine(gcLinesBackup[i + j], lineInfoList[i + j]);
                        randomLineA.X -= coords.Max(x => x.X) - coords.Min(x => x.X);
                        randomLineA.Y -= coords.Max(x => x.Y) - coords.Min(x => x.Y);
                        coords.Add(new Coords {
                            X = (float)randomLineA.X, Y = (float)randomLineA.Y, Z = (float)gcLinesBackup[i + j].Z
                        });
                        gcodeFromPath.Add(randomLineA);
                    }
                }
                gcodeFromPath.Add(gcLinesBackup[i]);
            }
        }
        if (StartFromHome)
        {
            gcLine gcl = new gcLine();
            gcl.G    = 1;
            gcl.X    = HomePositionObj.transform.position.x;
            gcl.Y    = HomePositionObj.transform.position.y;
            gcl.Z    = HomePositionObj.transform.position.z;
            gcl.AUX1 = AUXOnInTravels;
            gcodeFromPath.Insert(0, gcl);
        }
        if (ReturnToHome)
        {
            gcLine gcl = new gcLine();
            gcl.G    = 1;
            gcl.X    = HomePositionObj.transform.position.x;
            gcl.Y    = HomePositionObj.transform.position.y;
            gcl.Z    = HomePositionObj.transform.position.z;
            gcl.AUX1 = AUXOnInTravels;
            gcodeFromPath.Insert(gcodeFromPath.Count - 1, gcl);
        }
        minX = coords.Min(i => i.X);
        maxX = coords.Max(i => i.X);
        minY = coords.Min(i => i.Y);
        maxY = coords.Max(i => i.Y);
        float minZ = coords.Min(i => i.Z);
        float maxZ = coords.Max(i => i.Z);
        float midX = maxX - minX;
        float midY = maxY - minY;
        float midZ = maxZ - minZ;



        minScaleHorizontal = Mathf.Floor((Cnc_Settings.WidthInMM / 2 - Cnc_Settings.HorizontalPaddingInMM) / (minX));
        maxScaleHorizontal = Mathf.Floor((Cnc_Settings.WidthInMM / 2 - Cnc_Settings.HorizontalPaddingInMM) / (maxX));
        minScaleVertical   = Mathf.Floor((Cnc_Settings.HeightInMM / 2 - Cnc_Settings.VerticalPaddingInMM) / minY);
        maxScaleVertical   = Mathf.Floor((Cnc_Settings.HeightInMM / 2 - Cnc_Settings.VerticalPaddingInMM) / maxY);
        float[] allscales   = { minScaleHorizontal, maxScaleHorizontal, minScaleVertical, maxScaleVertical };
        float   safeToScale = Mathf.Floor(allscales.Min(x => Mathf.Abs(x)));

        Cnc_Settings.ScaleFactorForMax = safeToScale;
        if (!Interaction_Controller.scaleSet)
        {
            Interaction_Controller.updateScaleSliders(maxScaleHorizontal, maxScaleVertical, scaleToUseHorizontal, scaleToUseVertical);
            scaleToUseHorizontal = safeToScale * (Cnc_Settings.defaultScalePercentage / 100);
            scaleToUseVertical   = safeToScale * (Cnc_Settings.defaultScalePercentage / 100);
        }



        foreach (gcLine gcl in gcodeFromPath)
        {
            gcl.X *= scaleToUseHorizontal;
            gcl.Y *= scaleToUseVertical;
            gcl.X += midPointX;
            gcl.Y += midPointY;


            if (Mathf.Abs((float)gcl.X) > Mathf.Abs(((Cnc_Settings.WidthInMM - (Cnc_Settings.HorizontalPaddingInMM * 2)) / 2)) || Mathf.Abs((float)gcl.Y) > Mathf.Abs(((Cnc_Settings.HeightInMM - (Cnc_Settings.VerticalPaddingInMM * 2)) / 2)))
            {
                notsafe = true;
            }
        }


        if (notsafe)
        {
            Debug.LogError("Code was not safe. Either reached the X or Y limit");
        }
        else
        {
            gcodeFromPathToExport = gcodeFromPath;
            Interaction_Controller.UpdateMinMaxValues();
            Linebuilder.showOutLinesFromPoints(gcodeFromPath);
            gameObject.GetComponent <FileController>().writeFile(gcodeFromPath, "examp");
        }
    }