From: <emc...@us...> - 2004-02-29 20:43:02
|
Update of /cvsroot/emc/emc/src/ncutils/flipcnc In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv17489 Added Files: flipcnc.c Log Message: Adding ncutils to the project. --- NEW FILE: flipcnc.c --- /* Copyright (C) Dean L. Hedin This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ /***************************************************************************** * * NAME: FlipCNC * * PURPOSE: Flips a 2d NC file about a line along the X or Y axis * * example usage: * flipcnc [-i infile] [-o outfile] [-a (x or y)] [-p n.nnn] * * "p" is defines the line (x=p,or y=p) about which the file will be fliped * z coordinates are not effected. * * * * RESTRICTIONS: Haven't tested it for all cases but "It works for me"® * * * REVISION: DLH - ORIGINAL VERSION 1/20/02 11:14AM * ***************************************************************************** * * ****************************************************************************/ #include <stdio.h> #include <string.h> #include <stdlib.h> #include <signal.h> #include <math.h> #include <ctype.h> #include <time.h> #ifdef __GNUC__ #include <sys/wait.h> #include <unistd.h> #endif #ifndef __GNUC__ /***************************************************************************** * non-linux getopt provided by: * Author: Hi_Tech_Assassin * Email : Hi_...@ha... ****************************************************************************/ char *optarg; int getopt (int argc, char *const *argv, const char *shortopts) { int x; int step=0; int i; unsigned int y; int found; static unsigned int optPosition; /* used to keep position of shortopts on each call*/ /* this loop enssures that if a switch is not found it loops until one is found in list or EOF when all shortopts have been searched */ while(optPosition<=strlen(shortopts)-1) { if(shortopts[optPosition+1]!=':') // locate the next : char { do { step++; } while(shortopts[optPosition+step]!=':'); step++; } else step=2; optPosition+=step; /* we do this at start of each iteration because if function return occurs we miss our chance */ for(x=1;x<argc;x++) { if(argv[x][0]=='-') { /* check if switch is in our list, otherwise alert caller with return of NULL */ found=0; for(y=0; !found && y<=strlen(shortopts)-1;y++) if(shortopts[y]!=':') if(argv[x][1]==shortopts[y]) found=1; if(!found) { optarg=NULL; return NULL; } /* this is loop necessary because of the multi char support between : s */ for(i=0; i<=step-1; i++) { if(argv[x][1]==shortopts[optPosition-(step-i)]) { if(x+1 < argc) { if(argv[x+1][0]!='-') optarg=argv[x+1]; else optarg=NULL; } else optarg=NULL; return shortopts[optPosition-(step-i)]; } } } } } return EOF; } #endif /***************************************************************************** * * NAME: get_g_val, put_g_val * * PURPOSE: Places or extract the x,y,z value in a passed string * Put_g_val will add the value to the end if it was not previously * present. * INPUT: * * * OUTPUT: * * RESTRICTIONS: * * * REVISION: DLH - ORIGINAL VERSION 1/20/02 11:14AM * ***************************************************************************** * * ****************************************************************************/ #define X_VAL 0 #define Y_VAL 1 #define Z_VAL 2 char * which_char[] = {"x","X","y","Y","z","Z"}; float get_g_val(int which_xyz, char *instring) { char inpstr[255]; char *numstr; char *prevstr; char *strend; char *remainingstr; char spacestr[3]; strcpy(spacestr," "); strcpy(inpstr,instring); strend=strstr(inpstr,"\n"); *strend=0x00; if (strstr(inpstr,which_char[which_xyz*2])) { numstr=strstr(inpstr,which_char[which_xyz*2])+1; } else { numstr=strstr(inpstr,which_char[(which_xyz*2)+1])+1; } *(numstr-2)=0x00; prevstr=inpstr; if ( strstr(numstr," ") !=0 ) { strend=strstr(numstr," "); *strend=0x00; remainingstr=strend+1; } else { remainingstr=spacestr; } return(atof(numstr)); } void put_g_val(int which_xyz,float val, char *inpstr) { char *numstr; char *prevstr; char *strend; char *remainingstr; char newnumstr[30]; char emptystr[3]; char outstr[255]; strcpy(emptystr,""); strend=strstr(inpstr,"\n"); //null out carrige return *strend=0x00; //is the character in the string? if ( (strstr(inpstr,which_char[which_xyz*2]) || strstr(inpstr,which_char[(which_xyz*2)+1])) ) { if (strstr(inpstr,which_char[which_xyz*2])) { numstr=strstr(inpstr,which_char[which_xyz*2])+1; //set the pointer to just after "X", etc.. } else { numstr=strstr(inpstr,which_char[(which_xyz*2)+1])+1; } *(numstr-2)=0x00; //and null out just before the "X",etc.. prevstr=inpstr; if ( strstr(numstr," ") !=0 ) //find the space after the X,Y,Z value { strend=strstr(numstr," "); *strend=0x00; remainingstr=strend+1; } else //there is no space after X { remainingstr=emptystr; } sprintf(newnumstr,"%-4.4f",val); if (remainingstr[0]) { sprintf(outstr,"%s %s%s %s\n", prevstr,which_char[(which_xyz*2)+1],newnumstr,remainingstr); } else { sprintf(outstr,"%s %s%s\n", prevstr,which_char[(which_xyz*2)+1],newnumstr); } strcpy(inpstr,outstr); } else { sprintf(newnumstr,"%-4.4f",val); sprintf(outstr,"%s %s%s\n",inpstr,which_char[(which_xyz*2)+1],newnumstr); strcpy(inpstr,outstr); } } int main(int argc, char *argv[]) { int c; char *args[64]; char infilename[255]; char outfilename[255]; char inpstr[255]; char flipaxisstr[5]; FILE *infile; FILE *outfile; float flipcenter,outval,curval; strcpy(infilename,""); strcpy(outfilename,""); while ((c = getopt(argc, argv, "i:o:a:p:")) != EOF) { switch(c) { case 'i': strcpy(infilename,optarg); break; case 'o': strcpy(outfilename,optarg); break; case 'a': strcpy(flipaxisstr,optarg); break; case 'p': flipcenter=atof(optarg); break; default: fprintf(stderr, "Invalid option specified\n"); fprintf(stderr, "%s [-i infile] [-o outfile] [-a (x or y)] [-p n.nnn]\n", argv[0]); return(1); } } if (infilename[0]) { if ( ( infile = fopen( infilename, "rt" ) ) == NULL ) { fprintf( stderr,"Can't open infile: %s \n", infilename); return ( 0 ); } } else { infile = stdin; } if (outfilename[0]) { if ( ( outfile = fopen( outfilename, "wt" ) ) == NULL ) { fprintf( stderr,"Can't open outfile: %s \n", outfilename); return ( 0 ); } } else { outfile = stdout; } do { memset( inpstr, 0x00, sizeof( inpstr ) ); fgets( inpstr, 255, infile ); //keep reading lines.. //printf(inpstr); if (strstr(inpstr,"(") == 0) { if ( (strstr(flipaxisstr,"y") || strstr(flipaxisstr,"Y")) ) { if ( (strstr(inpstr,"y") || strstr(inpstr,"Y")) ) { curval=get_g_val(Y_VAL,inpstr); if (curval > flipcenter) { outval= flipcenter - (curval-flipcenter); } else { outval= flipcenter + (flipcenter-curval); } put_g_val(Y_VAL,outval,inpstr); } } else { if ( (strstr(inpstr,"x") || strstr(inpstr,"X")) ) { curval=get_g_val(X_VAL,inpstr); if (curval > flipcenter) { outval= flipcenter - (curval-flipcenter); } else { outval= flipcenter + (flipcenter-curval); } put_g_val(X_VAL,outval,inpstr); } } } fputs(inpstr,outfile); } while ( !feof( infile ) ); fclose(infile); fclose(outfile); return(0); } |