int interpret_from_keyboard( /* ARGUMENTS */ int block_delete, /* switch which is ON or OFF */ int print_stack) /* option which is ON or OFF */ { char line[RS274NGC_TEXT_SIZE]; int status; for(; ;) { printf("READ => "); fgets(line, sizeof(line), stdin); if (strcmp (line, "quit") IS 0) return 0; status SET_TO rs274ngc_read(line); if ((status IS RS274NGC_EXECUTE_FINISH) AND (block_delete IS ON)); else if (status IS RS274NGC_ENDFILE); else if ((status ISNT RS274NGC_EXECUTE_FINISH) AND (status ISNT RS274NGC_OK)) report_error(status, print_stack); else { status SET_TO rs274ngc_execute(); if ((status IS RS274NGC_EXIT) OR (status IS RS274NGC_EXECUTE_FINISH)); else if (status ISNT RS274NGC_OK) report_error(status, print_stack); } } }
int CGCodeInterpreter::DoExecute() { int status; char trash[INTERP_TEXT_SIZE]; char * read_ok; int program_status; ErrorOutput[0]='\0'; program_status = RS274NGC_OK; // This doesn't work yet and results in a memory leak // so leave this static for now // int * device = new int; // *device=(int)this; // SET_CANON_DEVICE(device); if (DoResumeSafe()) return 1; // check for re-position motions // initialize everything if we are starting at the beginning if (m_restart || m_InitializeOnExecute) { status = InitializeInterp(); if (status) return status; } CoordMotion->m_Stopping = STOPPED_NONE; // always read the tool file now (some users set it externally) // if (m_restart || m_ReadToolFile) { if (ToolFile[0]!=0) { status = read_tool_file(ToolFile, &_setup); if (status != RS274NGC_OK) return rs274ErrorExit(status); } m_ReadToolFile = false; } if (!CoordMotion->m_Simulate) { int result; // find out which axis is which // force refresh and save results CoordMotion->m_DefineCS_valid = false; if (CoordMotion->m_PreviouslyStopped) result = CoordMotion->ReadCurAbsPosition(&CoordMotion->current_x,&CoordMotion->current_y,&CoordMotion->current_z, &CoordMotion->current_a,&CoordMotion->current_b,&CoordMotion->current_c,true); else result = ReadAndSyncCurPositions(&_setup.current_x,&_setup.current_y,&_setup.current_z,&_setup.AA_current,&_setup.BB_current,&_setup.CC_current); if (result == 1) { if (CoordMotion->m_AxisDisabled) strcpy(ErrorOutput,"Unable to read defined coordinate system axis positions - Axis Disabled "); else strcpy(ErrorOutput,"Unable to read defined coordinate system axis positions "); } if (result != 0) return 1005; } status = rs274ngc_open(m_InFile); if (status != RS274NGC_OK) return rs274ErrorExit(status); if (_setup.percent_flag == ON) m_GCodeReads = 1; else m_GCodeReads = 0; for( ; m_GCodeReads<m_CurrentLine ; m_GCodeReads++) { read_ok = fgets(trash, INTERP_TEXT_SIZE,_setup.file_pointer); if (!read_ok) { strcpy(ErrorOutput,"Error while reading GCode file "); return NCE_A_FILE_IS_ALREADY_OPEN; } } CoordMotion->m_realtime_Sequence_number_valid=false; ExecutionInProgress=true; SetupTracker.ClearHistory(); _setup.current_line=m_GCodeReads; for( ; ; m_GCodeReads++) { // record what line we are doing in the GCode _setup.current_line = m_CurrentLine = m_GCodeReads; SetupTracker.InsertState(&_setup); // save the interpreter state // give output to caller #ifndef _KMOTIONX //not needed on linux CString tmpStr = Output; tmpStr.Replace("\n","\r\n"); strcpy(Output, tmpStr); #endif m_StatusFn(m_CurrentLine,Output); Output[0]='\0'; // clear it if (((m_end!=-1)&&(m_CurrentLine>m_end)) || (CoordMotion->m_Simulate && m_Halt) || CoordMotion->GetAbort() || m_HaltNextLine) { rs274ngc_close(); return 0; } status = rs274ngc_read(); if (status == RS274NGC_ENDFILE) { rs274ngc_close(); return RS274NGC_ENDFILE; } if (status != RS274NGC_OK) return rs274ErrorExit(status); status = rs274ngc_execute(NULL); SaveStateOnceOnly(); // if it wasn't saved before first motion then save it now if (m_CurrentLine != _setup.current_line) { // line number changed if (m_end!=-1) m_end += _setup.current_line - m_CurrentLine; m_GCodeReads = m_CurrentLine = _setup.current_line; // get back line no (in case we called a subroutine) } if (CoordMotion->GetAbort()) return rs274ErrorExit(status); if (status == RS274NGC_EXIT) { rs274ngc_close(); _setup.current_line = m_CurrentLine = 0; // reset back to the beginning return program_status; } if (status != RS274NGC_OK) return rs274ErrorExit(status); // successfully executed one line clear stopped status if (!CoordMotion->GetAbort()) CoordMotion->m_PreviouslyStopped = STOPPED_NONE; } return 0; }
int CGCodeInterpreter::DoReverseSearch(const char * InFile, int CurrentLine) { int GCodeReads,status; char trash[INTERP_TEXT_SIZE]; char * read_ok; char s[MAX_LINE]; if (CurrentLine==0) return 0; // should always be ok to set the first line CoordMotion->m_Stopping = STOPPED_NONE; // always read the tool file now (some users set it externally) // if (m_restart || m_ReadToolFile) { if (ToolFile[0]!=0) { status = read_tool_file(ToolFile, &_setup); if (status != RS274NGC_OK) return rs274ErrorExit(status); } m_ReadToolFile = false; } if (!CoordMotion->m_Simulate) { int result; // find out which axis is which // force refresh and save results CoordMotion->m_DefineCS_valid = false; if (CoordMotion->m_PreviouslyStopped) result = CoordMotion->ReadCurAbsPosition(&CoordMotion->current_x,&CoordMotion->current_y,&CoordMotion->current_z, &CoordMotion->current_a,&CoordMotion->current_b,&CoordMotion->current_c,true); else result = ReadAndSyncCurPositions(&_setup.current_x,&_setup.current_y,&_setup.current_z,&_setup.AA_current,&_setup.BB_current,&_setup.CC_current); if (result == 1) { if (CoordMotion->m_AxisDisabled) strcpy(ErrorOutput,"Unable to read defined coordinate system axis positions - Axis Disabled "); else strcpy(ErrorOutput,"Unable to read defined coordinate system axis positions "); } if (result != 0) return 1005; } status = rs274ngc_open(InFile); if (status != RS274NGC_OK) return rs274ErrorExit(status); if (_setup.percent_flag == ON) GCodeReads = 1; else GCodeReads = 0; // read the entire file into this array of lines // up until the line we wish to start #ifdef _KMOTIONX //char *LineArray = new char[CurrentLine+1][INTERP_TEXT_SIZE+1]; //this works on linux due to a C++ extension char LineArray[CurrentLine+1][INTERP_TEXT_SIZE+1]; #define CLEAN_ARRAY do{ } while( false ) #else CString *LineArray = new CString[CurrentLine+1]; if (!LineArray) return 1; #define CLEAN_ARRAY do{ delete [] LineArray; } while( false ) #endif for( ; GCodeReads<=CurrentLine ; GCodeReads++) { read_ok = fgets(trash, INTERP_TEXT_SIZE,_setup.file_pointer); if (!read_ok) { rs274ngc_close(); CLEAN_ARRAY; AfxMessageBox("Error while reading GCode file "); return 1; } #ifdef _KMOTIONX strcpy(LineArray[GCodeReads],trash); #else LineArray[GCodeReads] = trash; #endif } rs274ngc_close(); bool Done=false; bool FoundG=false; bool FoundF=false; bool FoundUnits=false; bool FoundX=CoordMotion->x_axis < 0; bool FoundY=CoordMotion->y_axis < 0; bool FoundZ=CoordMotion->z_axis < 0; bool FoundA=CoordMotion->a_axis < 0; bool FoundB=CoordMotion->b_axis < 0; bool FoundC=CoordMotion->c_axis < 0; bool FoundX0,FoundY0,FoundZ0,FoundA0,FoundB0,FoundC0,FoundG0,FoundF0,FoundUnits0; int G=-1; int Units=-1; double x=0,y=0,z=0,a=0,b=0,c=0,f=0; block *BL=&p_setup->block1; block block0; bool FirstLoop=true; if (--GCodeReads<0) {CLEAN_ARRAY; return 1;} status = rs274ngc_read(LineArray[GCodeReads]); if (status == RS274NGC_ENDFILE) { rs274ngc_close(); CLEAN_ARRAY; return RS274NGC_ENDFILE; } // save what is on the line the User wants to switch to block0=p_setup->block1; // we need to determine what everthing would be // after the previous line was executed while (!FoundG || (!FoundF && (G==-1 || G==G_1 || G==G_2 || G==G_3)) || !FoundX || !FoundY || !FoundZ || !FoundA || !FoundB || !FoundC || !FoundUnits) { if (--GCodeReads<0) break; status = rs274ngc_read(LineArray[GCodeReads]); if (status == RS274NGC_ENDFILE) { rs274ngc_close(); CLEAN_ARRAY; return RS274NGC_ENDFILE; } if (!FoundG) { if (BL->motion_to_be == G_0 || BL->motion_to_be == G_1 || BL->motion_to_be == G_2 || BL->motion_to_be == G_3 || BL->motion_to_be == G_4) FoundG=true; if (FoundG) G=BL->motion_to_be; } if (!FoundUnits) { if (BL->g_modes[6] == G_20 || BL->g_modes[6] == G_21) FoundUnits=true; if (FoundUnits) Units=BL->g_modes[6]; } if (!FoundF && BL->f_number >= 0.0) {FoundF=true; f=BL->f_number;} if (!FoundX && BL->x_flag) {FoundX=true; x=BL->x_number;} if (!FoundY && BL->y_flag) {FoundY=true; y=BL->y_number;} if (!FoundZ && BL->z_flag) {FoundZ=true; z=BL->z_number;} if (!FoundA && BL->a_flag) {FoundA=true; a=BL->a_number;} if (!FoundB && BL->b_flag) {FoundB=true; b=BL->b_number;} if (!FoundC && BL->c_flag) {FoundC=true; c=BL->c_number;} if (FirstLoop) { FirstLoop=false; FoundX0=FoundX; FoundY0=FoundY; FoundZ0=FoundZ; FoundA0=FoundA; FoundB0=FoundB; FoundC0=FoundC; FoundG0=FoundG; FoundF0=FoundF; FoundUnits0=FoundUnits; } } CLEAN_ARRAY; if ((block0.g_modes[6]!=G_20 || block0.g_modes[6]!=G_21) && FoundUnits) // if units not specified on current line and different { if (Units==G_20 && p_setup->length_units!=CANON_UNITS_INCHES) { if (AfxMessageBox("Backward scan found G20 Inches Mode. Switch to Inches?" ,MB_YESNO)==IDYES) { p_setup->length_units=CANON_UNITS_INCHES; } } if (Units==G_21 && p_setup->length_units!=CANON_UNITS_MM) { if (AfxMessageBox("Backward scan found G21 MM Mode. Switch to MM?" ,MB_YESNO)==IDYES) { p_setup->length_units=CANON_UNITS_MM; } } } // if jumping to a G0 assume User knows what is intended and it is ok to rapid to that location if (block0.motion_to_be==G_0) return 1; if (GCodeReads<0) { strcpy(s,"Unable to determine starting conditions for this line.\r\r"); if (!FoundX) strcat(s,"X? "); if (!FoundY) strcat(s,"Y? "); if (!FoundZ) strcat(s,"Z? "); if (!FoundA) strcat(s,"A? "); if (!FoundB) strcat(s,"B? "); if (!FoundC) strcat(s,"C? "); if (!FoundF && (G==-1 || G==G_1 || G==G_2 || G==G_3))strcat(s,"F? "); if (!FoundUnits) strcat(s,"G20/21? "); AfxMessageBox(s); return 1; } if (block0.motion_to_be==-1) { sprintf(s, "New Line does not contain a G mode. Backward scan found:\r\rG%d\r\rUse this mode?",G/10); if (AfxMessageBox(s,MB_YESNO)==IDNO) { return 1; } } if (block0.f_number==-1) { if (FoundF) { sprintf(s, "New Line does not contain a Feedrate F command. Backward scan found:\r\rF%g\r\rUse this feedrate?",f); if (AfxMessageBox(s,MB_YESNO)==IDYES) { p_setup->feed_rate=m_StoppedInterpState.feed_rate=f; } else { m_StoppedInterpState.feed_rate=p_setup->feed_rate; } } else { AfxMessageBox("New Line does not contain a Feedrate F command. Unable to determine previous feedrate"); } } else { p_setup->feed_rate=m_StoppedInterpState.feed_rate=block0.f_number; } if (!FoundX0 || !FoundY0 ||!FoundZ0 ||!FoundA0 ||!FoundB0 ||!FoundC0) { char v[32]; strcpy(s,"Backward scan found previous position as:\r\r"); if (CoordMotion->x_axis >= 0){ sprintf(v," X%g",x); strcat(s,v);} if (CoordMotion->y_axis >= 0){ sprintf(v," Y%g",y); strcat(s,v);} if (CoordMotion->z_axis >= 0){ sprintf(v," Z%g",z); strcat(s,v);} if (CoordMotion->a_axis >= 0){ sprintf(v," A%g",a); strcat(s,v);} if (CoordMotion->b_axis >= 0){ sprintf(v," B%g",b); strcat(s,v);} if (CoordMotion->c_axis >= 0){ sprintf(v," C%g",c); strcat(s,v);} strcat(s,"\rShould a Safe Z move be made using these coordinates?"); if (AfxMessageBox(s,MB_YESNO)==IDNO) { return 1; } } if (!FoundG0) { int group = _gees[G*10]; if (group==0) // G4 = group0 p_setup->active_g_codes[1]=-1; else // G0,G1,G2,G3 p_setup->active_g_codes[0]=-1; p_setup->motion_mode=G; } if (FoundX) {CoordMotion->m_Stoppedx=x; p_setup->current_x=x;} if (FoundY) {CoordMotion->m_Stoppedy=y; p_setup->current_y=y;} if (FoundZ) {CoordMotion->m_Stoppedz=z; p_setup->current_z=z;} if (FoundA) {CoordMotion->m_Stoppeda=a; p_setup->AA_current=a;} if (FoundB) {CoordMotion->m_Stoppedb=b; p_setup->BB_current=b;} if (FoundC) {CoordMotion->m_Stoppedc=c; p_setup->CC_current=c;} CoordMotion->m_StoppedMachinex = UserUnitsToInchesX(CoordMotion->m_Stoppedx+_setup.axis_offset_x+_setup.origin_offset_x+_setup.tool_xoffset); CoordMotion->m_StoppedMachiney = UserUnitsToInches(CoordMotion->m_Stoppedy+_setup.axis_offset_y+_setup.origin_offset_y+_setup.tool_yoffset); CoordMotion->m_StoppedMachinez = UserUnitsToInches(CoordMotion->m_Stoppedz+_setup.axis_offset_z+_setup.origin_offset_z+_setup.tool_length_offset); CoordMotion->m_StoppedMachinea = InchesOrDegToUserUnitsA(CoordMotion->m_Stoppeda) - _setup.AA_axis_offset - _setup.AA_origin_offset; CoordMotion->m_StoppedMachineb = InchesOrDegToUserUnitsB(CoordMotion->m_Stoppedb) - _setup.BB_axis_offset - _setup.BB_origin_offset; CoordMotion->m_StoppedMachinec = InchesOrDegToUserUnitsC(CoordMotion->m_Stoppedc) - _setup.CC_axis_offset - _setup.CC_origin_offset; CoordMotion->m_PreviouslyStopped = CoordMotion->m_Stopping = STOPPED_INDEP; return 0; }
int interpret_from_file( /* ARGUMENTS */ int do_next, /* what to do if error */ int block_delete, /* switch which is ON or OFF */ int print_stack) /* option which is ON or OFF */ { int status; char line[RS274NGC_TEXT_SIZE]; for(; ;) { status SET_TO rs274ngc_read(NULL); if ((status IS RS274NGC_EXECUTE_FINISH) AND (block_delete IS ON)) continue; else if (status IS RS274NGC_ENDFILE) break; if ((status ISNT RS274NGC_OK) AND // should not be EXIT (status ISNT RS274NGC_EXECUTE_FINISH)) { report_error(status, print_stack); if ((status IS NCE_FILE_ENDED_WITH_NO_PERCENT_SIGN) OR (do_next IS 2)) /* 2 means stop */ { status SET_TO 1; break; } else if (do_next IS 1) /* 1 means MDI */ { fprintf(stderr, "starting MDI\n"); interpret_from_keyboard(block_delete, print_stack); fprintf(stderr, "continue program? y/n =>"); fgets(line, sizeof(line), stdin); if (line[0] ISNT 'y') { status SET_TO 1; break; } else continue; } else /* if do_next IS 0 -- 0 means continue */ continue; } status SET_TO rs274ngc_execute(); if ((status ISNT RS274NGC_OK) AND (status ISNT RS274NGC_EXIT) AND (status ISNT RS274NGC_EXECUTE_FINISH)) { report_error(status, print_stack); status SET_TO 1; if (do_next IS 1) /* 1 means MDI */ { fprintf(stderr, "starting MDI\n"); interpret_from_keyboard(block_delete, print_stack); fprintf(stderr, "continue program? y/n =>"); fgets(line,sizeof(line), stdin); if (line[0] ISNT 'y') break; } else if (do_next IS 2) /* 2 means stop */ break; } else if (status IS RS274NGC_EXIT) break; } return ((status IS 1) ? 1 : 0); }