コード例 #1
0
ファイル: gcode.cpp プロジェクト: Funny-DK/repsnapper
void GCode::Read(Model *model, const vector<char> E_letters,
		 ViewProgress *progress, string filename)
{
	clear();

	ifstream file;
	file.open(filename.c_str());		//open a file
	file.seekg (0, ios::end);
	double filesize = double(file.tellg());
	file.seekg (0);

	progress->start(_("Loading GCode"), filesize);
	int progress_steps=(int)(filesize/1000);
	if (progress_steps==0) progress_steps=1;

	buffer_zpos_lines.clear();

	if(!file.good())
	{
//		MessageBrowser->add(str(boost::format("Error opening file %s") % Filename).c_str());
		return;
	}

	set_locales("C");

	uint LineNr = 0;

	string s;

	bool relativePos = false;
	Vector3d globalPos(0,0,0);
	Min.set(99999999.0,99999999.0,99999999.0);
	Max.set(-99999999.0,-99999999.0,-99999999.0);

	std::vector<Command> loaded_commands;

	double lastZ=0.;
	double lastE=0.;
	double lastF=0.;
	layerchanges.clear();

	stringstream alltext;

	int current_extruder = 0;

	while(getline(file,s))
	{
	  alltext << s << endl;

		LineNr++;
		unsigned long fpos = file.tellg();
		if (fpos%progress_steps==0) if (!progress->update(fpos)) break;

		Command command;

		if (relativePos)
		  command = Command(s, Vector3d::ZERO, E_letters);
		else
		  command = Command(s, globalPos, E_letters);

		if (command.Code == COMMENT) {
		  continue;
		}
		if (command.Code == UNKNOWN) {
		  cerr << "Unknown GCode " << s << endl;
		  continue;
		}
		if (command.Code == RELATIVEPOSITIONING) {
		  relativePos = true;
		  continue;
		}
		if (command.Code == ABSOLUTEPOSITIONING) {
		  relativePos = false;
		  continue;
		}
		if (command.Code == SELECTEXTRUDER) {
		  current_extruder = command.extruder_no;
		  continue;
		}
		command.extruder_no = current_extruder;

		// not used yet
		// if (command.Code == ABSOLUTE_ECODE) {
		//   relativeE = false;
		//   continue;
		// }
		// if (command.Code == RELATIVE_ECODE) {
		//   relativeE = true;
		//   continue;
		// }

		if (command.e == 0)
		  command.e  = lastE;
		else
		  lastE = command.e;

		if (command.f != 0)
		  lastF = command.f;
		else
		  command.f = lastF;

		// cout << s << endl;
		//cerr << command.info()<< endl;
		// if(command.where.x() < -100)
		//   continue;
		// if(command.where.y() < -100)
		//   continue;

		if (command.Code == SETCURRENTPOS) {
		  continue;//if (relativePos) globalPos = command.where;
		}
		else {
		  command.addToPosition(globalPos, relativePos);
		}

		if (globalPos.z() < 0){
		  cerr << "GCode below zero!"<< endl;
		  continue;
		}

		if ( command.Code == RAPIDMOTION ||
		     command.Code == COORDINATEDMOTION ||
		     command.Code == ARC_CW ||
		     command.Code == ARC_CCW ||
		     command.Code == GOHOME ) {

		  if(globalPos.x() < Min.x())
		    Min.x() = globalPos.x();
		  if(globalPos.y() < Min.y())
		    Min.y() = globalPos.y();
		  if(globalPos.z() < Min.z())
		    Min.z() = globalPos.z();
		  if(globalPos.x() > Max.x())
		    Max.x() = globalPos.x();
		  if(globalPos.y() > Max.y())
		    Max.y() = globalPos.y();
		  if(globalPos.z() > Max.z())
		    Max.z() = globalPos.z();
		  if (globalPos.z() > lastZ) {
		    // if (lastZ > 0){ // don't record first layer
		    unsigned long num = loaded_commands.size();
		    layerchanges.push_back(num);
		    loaded_commands.push_back(Command(LAYERCHANGE, layerchanges.size()));
		    // }
		    lastZ = globalPos.z();
		    buffer_zpos_lines.push_back(LineNr-1);
		  }
		  else if (globalPos.z() < lastZ) {
		    lastZ = globalPos.z();
		    if (layerchanges.size()>0)
		      layerchanges.erase(layerchanges.end()-1);
		  }
		}
		loaded_commands.push_back(command);
	}

	file.close();
	reset_locales();

	commands = loaded_commands;

	buffer->set_text(alltext.str());

	Center = (Max + Min)/2;

	model->m_signal_gcode_changed.emit();

	double time = GetTimeEstimation();
	int h = (int)time/3600;
	int min = ((int)time%3600)/60;
	int sec = ((int)time-3600*h-60*min);
	cerr << "GCode Time Estimation "<< h <<"h "<<min <<"m " <<sec <<"s" <<endl;
	//??? to statusbar or where else?
}
コード例 #2
0
ファイル: gcode.cpp プロジェクト: Funny-DK/repsnapper
GCodeIter *GCode::get_iter ()
{
  GCodeIter *iter = new GCodeIter (buffer);
  iter->time_estimation = GetTimeEstimation();
  return iter;
}
コード例 #3
0
void GCode::Read(Model *model, ViewProgress *progress, string filename)
{
	clear();

	ifstream file;
	file.open(filename.c_str());		//open a file
	file.seekg (0, ios::end);
	double filesize = double(file.tellg());
	file.seekg (0);

	progress->start(_("Loading GCode"), filesize);
	int progress_steps=(int)(filesize/1000);
	if (progress_steps==0) progress_steps=1;

	buffer_zpos_lines.clear();
	

	if(!file.good())
	{
//		MessageBrowser->add(str(boost::format("Error opening file %s") % Filename).c_str());
		return;
	}

	uint LineNr = 0;

	string s;

	Vector3d globalPos(0,0,0);
	Min.set(99999999.0,99999999.0,99999999.0);
	Max.set(-99999999.0,-99999999.0,-99999999.0);

	std::vector<Command> loaded_commands;

	double lastZ=0.;
	double lastE=0.;
	double lastF=0.;
	layerchanges.clear();
	bool belowzero = false;

	stringstream alltext;

	while(getline(file,s))
	{
	  alltext << s << endl;
	  
		LineNr++;
		unsigned long fpos = file.tellg();
		if (fpos%progress_steps==0) if (!progress->update(fpos)) break;

		Command command;
		try {
		  command = Command(s, globalPos);
		} catch (Glib::OptionError e) {
		  if (!belowzero) // only once
		    cerr << "GCode below zero!"<< endl;// ; //alert("GCode problem");
		  belowzero = true;
		}
		if (command.e==0)
		  command.e= lastE;
		else
		  lastE=command.e;
		if (command.f==0)
		  command.f= lastF;
		else
		  lastF=command.f;
		// cout << s << endl;
		//cerr << command.info()<< endl;
		if(command.where.x() < -100)
		  continue;
		if(command.where.y() < -100)
		  continue;
		globalPos = command.where;
		if(command.where.x() < Min.x())
		  Min.x() = command.where.x();
		if(command.where.y() < Min.y())
		  Min.y() = command.where.y();
		if(command.where.z() < Min.z())
		  Min.z() = command.where.z();
		if(command.where.x() > Max.x())
		  Max.x() = command.where.x();
		if(command.where.y() > Max.y())
		  Max.y() = command.where.y();
		if(command.where.z() > Max.z())
		  Max.z() = command.where.z();
		if (command.where.z() > lastZ) {
		  // if (lastZ > 0){ // don't record first layer
		    unsigned long num = loaded_commands.size();
		    layerchanges.push_back(num);
		  // }
		  lastZ = command.where.z();
		  buffer_zpos_lines.push_back(LineNr-1);
		}
		else if (command.where.z() < lastZ) {
		  lastZ=command.where.z();
		  if (layerchanges.size()>0)
		    layerchanges.erase(layerchanges.end()-1);
		}
		loaded_commands.push_back(command);
	}

	commands = loaded_commands;

	buffer->set_text(alltext.str());
	
	Center.x() = (Max.x() + Min.x() )/2;
	Center.y() = (Max.y() + Min.y() )/2;
	Center.z() = (Max.z() + Min.z() )/2;
	
	model->m_signal_gcode_changed.emit();

	double time = GetTimeEstimation();
	int h = (int)time/3600;
	int min = ((int)time%3600)/60;
	int sec = ((int)time-3600*h-60*min);
	cout << "GCode Time Estimation "<< h <<"h "<<min <<"m " <<sec <<"s" <<endl; 
	//??? to statusbar or where else?
}