Thursday, August 7, 2025

Slot Cutter; Rectangle Cutter

 This seems a bit obvious of a need.  I wanted to cut arbitrary slots.


/* Cut a slot */
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <float.h>
#include "helperdefs.h"

int main (int argc, char* argv[])
{
  if (argc < 13 || argc > 14)
  {
    printf("wrong number of arguments\n");
    displayValidArgs();
    exit(2);
  }
  int i = 1;
  float xStart=atof(argv[i++]);
  float yStart=atof(argv[i++]);
  float xEnd=atof(argv[i++]);  
  float yEnd=atof(argv[i++]);
  float zStart=atof(argv[i++]);
  float zStep=atof(argv[i++]);
  float zEnd=atof(argv[i++]);
  float xyFeedRate=atof(argv[i++]);
  float zFeed=atof(argv[i++]);
  float millDiameter=atof(argv[i++]);
  float millRadius=millDiameter/2.0;
  char* outputFileName=argv[i++];
  boolean append = FALSE;
  if (argc == 13 && argv[i] != NULL)
    {
      /* See if this is an append request */
      if (0 == strcmp(argv[i], "-a"))
    append = TRUE;
      else
    {
      fprintf(stderr, "unrecognized argument: %s\n", argv[11]);
      displayValidArgs();
      exit (1);
    }
    }
  /* is the requested slot wide enough for this mill? */
  if((yEnd - yStart) <= millDiameter)
    {
      fprintf(stderr, "millDiameter %.4f greater than slot width %.4f\n",
          millDiameter, yEnd-yStart);
      exit(1);
    }
  FILE* outputFile;
  if (append)
    outputFile = fopen(outputFileName, "a");
  else
    outputFile = fopen(outputFileName, "w");
  if (outputFile == NULL)
  {
    printf("unable to create file %s\n", outputFileName);
    exit(2);
  }
  showArgs(argc, argv);
  /* raise tool */
  fprintf(outputFile,
      "( cut slot from %.4f,%.4f to %.4f,%.4f with mill diameter %.4f )\n",
      xStart, yStart, xEnd, yEnd, millDiameter);
  fprintf(outputFile, "G1 Z1 F25\n");
  fprintf(outputFile, "G1 X%0.4f Y%0.4f F25\n", xStart+millRadius,
      yStart+millRadius);
  /* lower to just above contact */
  fprintf(outputFile, "G1 Z0.05 F25\n");
  /* to contact */
  fprintf(outputFile, "G1 Z0 F25\n");
  float z;
  for (z=zStart; z > zEnd; z += zStep)
    {
      fprintf(outputFile, "G1 Z%0.4f F%0.4f\n", z, zFeed);
      /* This loop moves across y, cutting slots in X axis */
      float y;
      for(y = yStart+millRadius; y < (yEnd - millRadius + 0.0001); y += millRadius)
    {
      fprintf(outputFile, "(start point)\nG1 X%0.4f Y%0.4f F%0.4F\n",
          xStart+millRadius, y, xyFeedRate);
      fprintf(outputFile, "(cut slot)\nG1 X%0.4f Y%0.4f F%0.4F\n",
          xEnd-millRadius, y, xyFeedRate);
    }
      /* if we still have room in the slot, do that reduced cut */
      if ((y - millRadius) < yEnd)
    {
    y = yEnd - millRadius;
      /* position to start point */
      fprintf(outputFile, "(start point)\nG1 X%0.4f Y%.4f F%0.4F\n",
          xStart+millRadius, y, xyFeedRate);
      /* cut slot to end */
      fprintf(outputFile, "(cut slot)\nG1 X%0.4f Y%.4f F%0.4F\n",
          xEnd+millRadius, y, xyFeedRate);
    }
      /* down one step */
      z+=zStep;
      fprintf(outputFile, "G1 Z%0.4f F%0.4f\n", z, zFeed);
      /* cut back to start */
      fprintf(outputFile, "(cut back to start)\nG1 X%0.4f Y%0.4f F%0.4f\n",
          xStart + millRadius, y, xyFeedRate);
    }
  fprintf(outputFile, "G1 Z2 F25\n");
  fprintf(outputFile, "G1 x0 Y0 F25\n");
}


void displayValidArgs(void)
{
  printf("xstart ystart ystart yend zstart zstep zend xyfeed zfeed milldiameter outputFilename\n");
}

void showArgs(int argc, char* argv[])
{
  int i;

  for (i=1; i < argc; i++)
    {
      printf("%s ", argv[i]);
    }
  putchar('\n');
}
 

/* Cut a square (interior or exterior).  This uses the two vertices. */
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <float.h>
#include "helperdefs.h"

int main (int argc, char* argv[])
{
  if (argc < 12 || argc > 14)
  {
    printf("wrong number of arguments\n");
    displayValidArgs();
    exit(2);
  }
  float xStart=atof(argv[1]);
  float yStart=atof(argv[2]);  
  float xEnd=atof(argv[3]);  
  float yEnd=atof(argv[4]);
  float zStart=atof(argv[5]);
  float zStep=atof(argv[6]);
  float zEnd=atof(argv[7]);
  float xyFeedRate=atof(argv[8]);
  float zFeedRate=atof(argv[9]);
  float millDiameter=atof(argv[10]);
  float millRadius=millDiameter/2.0;
  char* outputFileName=argv[11];
  boolean append = FALSE;
  boolean interior = FALSE;
  int i;
  for(i = 12; i < argc; i++)
    {
      if (argv[i] != NULL)
    {
      if (0 == strncmp(argv[i], "-a", 2))
        append = TRUE;
      else if (0 == strncmp(argv[i], "-i", 2))
        interior = TRUE; /* cut on the interior of the square */
      else
        {
          fprintf(stderr, "Unrecognized argument: %s\n", argv[i]);
          exit(1);
        }
    }
    }

    FILE* fp;
    if(append)
      fp = fopen(outputFileName, "a");
    else
      fp = fopen(outputFileName, "w");
    if (!fp)
      {
        perror("Error opening output file");
        return 1;
      }
    /* if not appending, we need prolog; we rely on calling script to append
       epilog */
    if(append == FALSE)
      fprintf(fp, "%c\nG17 G20 G54\n", '%');
    /* clear top of workpiece */
    fprintf(fp, "G0 Z1 F25\nG0 z1 F25\n");
    showArgs(argc, argv);
    float offset;
    if (interior)
      offset = millRadius;
    else
      offset = -millRadius;
    fprintf(fp, "G1 X%0.4f Y%.4f F%.4f\n", xStart+offset,
        yStart+offset, xyFeedRate);
    fprintf(fp, "G1 Z.1 F25\n");
    float z;
    for (z=zStart; z > zEnd; z += zStep)
      {
    fprintf(fp, "G1 Z%.4f F%.4f\n", z, zFeedRate);
    /* positition to corner 1 */
    fprintf(fp, "G1 X%0.4f Y%.4f F%.4f\n", xStart+offset,
        yStart+offset, xyFeedRate);
    /* on to corner 2 */
    fprintf(fp, "G1 x%.4f Y%.4f F%.4f\n", xStart+offset,
        yEnd-offset, xyFeedRate);
    /* to corner 3 */
    fprintf(fp, "G1 x%.4f Y%.4f F%.4f\n", xEnd-offset,
        yEnd-offset, xyFeedRate);
    /* corner 4 */
    fprintf(fp, "G1 X%.4f Y%.4f F%.4f\n", xEnd-offset, yStart+offset,
        xyFeedRate);
    /* back to start (corner 1) */
    fprintf(fp, "G1 X%0.4f Y%.4f F%.4f\n", xStart+offset,
        yStart+offset, xyFeedRate);
      }
    fprintf(fp, "G1 Z1 F25\nm2\n%c\n", '%');
    }


void displayValidArgs(void)
{
  printf("xstart ystart xend yend zstart zstep zend xyfeed zfeed milldiameter fileOutputName\n");
}

void showArgs(int argc, char* argv[])
{
  int i;

  for (i=0; i < argc; i++)
    {
      printf("%s ", argv[i]);
    }
  putchar('\n');
}
 


No comments:

Post a Comment