mirror of
				https://github.com/SoftFever/OrcaSlicer.git
				synced 2025-10-30 12:11:15 -06:00 
			
		
		
		
	Added cancel mechanism to GCodeAnalyzer::calc_gcode_preview_data()
This commit is contained in:
		
							parent
							
								
									09c9f567f2
								
							
						
					
					
						commit
						f5b5e48ad7
					
				
					 3 changed files with 50 additions and 15 deletions
				
			
		|  | @ -487,7 +487,7 @@ void GCode::do_export(Print *print, const char *path, GCodePreviewData *preview_ | |||
|     // starts analyzer calculations
 | ||||
|     if (m_enable_analyzer) { | ||||
|         BOOST_LOG_TRIVIAL(debug) << "Preparing G-code preview data"; | ||||
|         m_analyzer.calc_gcode_preview_data(*preview_data); | ||||
|         m_analyzer.calc_gcode_preview_data(*preview_data, [print]() { print->throw_if_canceled(); }); | ||||
|         m_analyzer.reset(); | ||||
|     } | ||||
| 
 | ||||
|  |  | |||
|  | @ -137,22 +137,22 @@ const std::string& GCodeAnalyzer::process_gcode(const std::string& gcode) | |||
|     return m_process_output; | ||||
| } | ||||
| 
 | ||||
| void GCodeAnalyzer::calc_gcode_preview_data(GCodePreviewData& preview_data) | ||||
| void GCodeAnalyzer::calc_gcode_preview_data(GCodePreviewData& preview_data, std::function<void()> cancel_callback) | ||||
| { | ||||
|     // resets preview data
 | ||||
|     preview_data.reset(); | ||||
| 
 | ||||
|     // calculates extrusion layers
 | ||||
|     _calc_gcode_preview_extrusion_layers(preview_data); | ||||
|     _calc_gcode_preview_extrusion_layers(preview_data, cancel_callback); | ||||
| 
 | ||||
|     // calculates travel
 | ||||
|     _calc_gcode_preview_travel(preview_data); | ||||
|     _calc_gcode_preview_travel(preview_data, cancel_callback); | ||||
| 
 | ||||
|     // calculates retractions
 | ||||
|     _calc_gcode_preview_retractions(preview_data); | ||||
|     _calc_gcode_preview_retractions(preview_data, cancel_callback); | ||||
| 
 | ||||
|     // calculates unretractions
 | ||||
|     _calc_gcode_preview_unretractions(preview_data); | ||||
|     _calc_gcode_preview_unretractions(preview_data, cancel_callback); | ||||
| } | ||||
| 
 | ||||
| bool GCodeAnalyzer::is_valid_extrusion_role(ExtrusionRole role) | ||||
|  | @ -676,7 +676,7 @@ bool GCodeAnalyzer::_is_valid_extrusion_role(int value) const | |||
|     return ((int)erNone <= value) && (value <= (int)erMixed); | ||||
| } | ||||
| 
 | ||||
| void GCodeAnalyzer::_calc_gcode_preview_extrusion_layers(GCodePreviewData& preview_data) | ||||
| void GCodeAnalyzer::_calc_gcode_preview_extrusion_layers(GCodePreviewData& preview_data, std::function<void()> cancel_callback) | ||||
| { | ||||
|     struct Helper | ||||
|     { | ||||
|  | @ -725,9 +725,18 @@ void GCodeAnalyzer::_calc_gcode_preview_extrusion_layers(GCodePreviewData& previ | |||
|     GCodePreviewData::Range feedrate_range; | ||||
|     GCodePreviewData::Range volumetric_rate_range; | ||||
| 
 | ||||
|     // to avoid to call the callback too often
 | ||||
|     unsigned int cancel_callback_threshold = (unsigned int)extrude_moves->second.size() / 25; | ||||
|     unsigned int cancel_callback_curr = 0; | ||||
| 
 | ||||
|     // constructs the polylines while traversing the moves
 | ||||
|     for (const GCodeMove& move : extrude_moves->second) | ||||
|     { | ||||
|         // to avoid to call the callback too often
 | ||||
|         cancel_callback_curr = (cancel_callback_curr + 1) % cancel_callback_threshold; | ||||
|         if (cancel_callback_curr == 0) | ||||
|             cancel_callback(); | ||||
| 
 | ||||
|         if ((data != move.data) || (z != move.start_position.z()) || (position != move.start_position) || (volumetric_rate != move.data.feedrate * (float)move.data.mm3_per_mm)) | ||||
|         { | ||||
|             // store current polyline
 | ||||
|  | @ -769,7 +778,7 @@ void GCodeAnalyzer::_calc_gcode_preview_extrusion_layers(GCodePreviewData& previ | |||
|     preview_data.ranges.volumetric_rate.update_from(volumetric_rate_range); | ||||
| } | ||||
| 
 | ||||
| void GCodeAnalyzer::_calc_gcode_preview_travel(GCodePreviewData& preview_data) | ||||
| void GCodeAnalyzer::_calc_gcode_preview_travel(GCodePreviewData& preview_data, std::function<void()> cancel_callback) | ||||
| { | ||||
|     struct Helper | ||||
|     { | ||||
|  | @ -797,9 +806,17 @@ void GCodeAnalyzer::_calc_gcode_preview_travel(GCodePreviewData& preview_data) | |||
|     GCodePreviewData::Range width_range; | ||||
|     GCodePreviewData::Range feedrate_range; | ||||
| 
 | ||||
|     // to avoid to call the callback too often
 | ||||
|     unsigned int cancel_callback_threshold = (unsigned int)travel_moves->second.size() / 25; | ||||
|     unsigned int cancel_callback_curr = 0; | ||||
| 
 | ||||
|     // constructs the polylines while traversing the moves
 | ||||
|     for (const GCodeMove& move : travel_moves->second) | ||||
|     { | ||||
|         cancel_callback_curr = (cancel_callback_curr + 1) % cancel_callback_threshold; | ||||
|         if (cancel_callback_curr == 0) | ||||
|             cancel_callback(); | ||||
| 
 | ||||
|         GCodePreviewData::Travel::EType move_type = (move.delta_extruder < 0.0f) ? GCodePreviewData::Travel::Retract : ((move.delta_extruder > 0.0f) ? GCodePreviewData::Travel::Extrude : GCodePreviewData::Travel::Move); | ||||
|         GCodePreviewData::Travel::Polyline::EDirection move_direction = ((move.start_position.x() != move.end_position.x()) || (move.start_position.y() != move.end_position.y())) ? GCodePreviewData::Travel::Polyline::Generic : GCodePreviewData::Travel::Polyline::Vertical; | ||||
| 
 | ||||
|  | @ -840,28 +857,44 @@ void GCodeAnalyzer::_calc_gcode_preview_travel(GCodePreviewData& preview_data) | |||
|     preview_data.ranges.feedrate.update_from(feedrate_range); | ||||
| } | ||||
| 
 | ||||
| void GCodeAnalyzer::_calc_gcode_preview_retractions(GCodePreviewData& preview_data) | ||||
| void GCodeAnalyzer::_calc_gcode_preview_retractions(GCodePreviewData& preview_data, std::function<void()> cancel_callback) | ||||
| { | ||||
|     TypeToMovesMap::iterator retraction_moves = m_moves_map.find(GCodeMove::Retract); | ||||
|     if (retraction_moves == m_moves_map.end()) | ||||
|         return; | ||||
| 
 | ||||
|     // to avoid to call the callback too often
 | ||||
|     unsigned int cancel_callback_threshold = (unsigned int)retraction_moves->second.size() / 25; | ||||
|     unsigned int cancel_callback_curr = 0; | ||||
| 
 | ||||
|     for (const GCodeMove& move : retraction_moves->second) | ||||
|     { | ||||
|         cancel_callback_curr = (cancel_callback_curr + 1) % cancel_callback_threshold; | ||||
|         if (cancel_callback_curr == 0) | ||||
|             cancel_callback(); | ||||
| 
 | ||||
|         // store position
 | ||||
|         Vec3crd position(scale_(move.start_position.x()), scale_(move.start_position.y()), scale_(move.start_position.z())); | ||||
|         preview_data.retraction.positions.emplace_back(position, move.data.width, move.data.height); | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| void GCodeAnalyzer::_calc_gcode_preview_unretractions(GCodePreviewData& preview_data) | ||||
| void GCodeAnalyzer::_calc_gcode_preview_unretractions(GCodePreviewData& preview_data, std::function<void()> cancel_callback) | ||||
| { | ||||
|     TypeToMovesMap::iterator unretraction_moves = m_moves_map.find(GCodeMove::Unretract); | ||||
|     if (unretraction_moves == m_moves_map.end()) | ||||
|         return; | ||||
| 
 | ||||
|     // to avoid to call the callback too often
 | ||||
|     unsigned int cancel_callback_threshold = (unsigned int)unretraction_moves->second.size() / 25; | ||||
|     unsigned int cancel_callback_curr = 0; | ||||
| 
 | ||||
|     for (const GCodeMove& move : unretraction_moves->second) | ||||
|     { | ||||
|         cancel_callback_curr = (cancel_callback_curr + 1) % cancel_callback_threshold; | ||||
|         if (cancel_callback_curr == 0) | ||||
|             cancel_callback(); | ||||
| 
 | ||||
|         // store position
 | ||||
|         Vec3crd position(scale_(move.start_position.x()), scale_(move.start_position.y()), scale_(move.start_position.z())); | ||||
|         preview_data.unretraction.positions.emplace_back(position, move.data.width, move.data.height); | ||||
|  |  | |||
|  | @ -122,7 +122,8 @@ public: | |||
|     const std::string& process_gcode(const std::string& gcode); | ||||
| 
 | ||||
|     // Calculates all data needed for gcode visualization
 | ||||
|     void calc_gcode_preview_data(GCodePreviewData& preview_data); | ||||
|     // throws CanceledException through print->throw_if_canceled() (sent by the caller as callback).
 | ||||
|     void calc_gcode_preview_data(GCodePreviewData& preview_data, std::function<void()> cancel_callback = std::function<void()>()); | ||||
| 
 | ||||
|     // Return an estimate of the memory consumed by the time estimator.
 | ||||
|     size_t memory_used() const; | ||||
|  | @ -237,10 +238,11 @@ private: | |||
|     // Checks if the given int is a valid extrusion role (contained into enum ExtrusionRole)
 | ||||
|     bool _is_valid_extrusion_role(int value) const; | ||||
| 
 | ||||
|     void _calc_gcode_preview_extrusion_layers(GCodePreviewData& preview_data); | ||||
|     void _calc_gcode_preview_travel(GCodePreviewData& preview_data); | ||||
|     void _calc_gcode_preview_retractions(GCodePreviewData& preview_data); | ||||
|     void _calc_gcode_preview_unretractions(GCodePreviewData& preview_data); | ||||
|     // All the following methods throw CanceledException through print->throw_if_canceled() (sent by the caller as callback).
 | ||||
|     void _calc_gcode_preview_extrusion_layers(GCodePreviewData& preview_data, std::function<void()> cancel_callback); | ||||
|     void _calc_gcode_preview_travel(GCodePreviewData& preview_data, std::function<void()> cancel_callback); | ||||
|     void _calc_gcode_preview_retractions(GCodePreviewData& preview_data, std::function<void()> cancel_callback); | ||||
|     void _calc_gcode_preview_unretractions(GCodePreviewData& preview_data, std::function<void()> cancel_callback); | ||||
| }; | ||||
| 
 | ||||
| } // namespace Slic3r
 | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Enrico Turri
						Enrico Turri