1 #include "Flow.hpp"
2 #include "I18N.hpp"
3 #include "Print.hpp"
4 #include <cmath>
5 #include <assert.h>
6 
7 #include <boost/algorithm/string/predicate.hpp>
8 
9 // Mark string for localization and translate.
10 #define L(s) Slic3r::I18N::translate(s)
11 
12 namespace Slic3r {
13 
FlowErrorNegativeSpacing()14 FlowErrorNegativeSpacing::FlowErrorNegativeSpacing() :
15 	FlowError("Flow::spacing() produced negative spacing. Did you set some extrusion width too small?") {}
16 
FlowErrorNegativeFlow()17 FlowErrorNegativeFlow::FlowErrorNegativeFlow() :
18     FlowError("Flow::mm3_per_mm() produced negative flow. Did you set some extrusion width too small?") {}
19 
20 // This static method returns a sane extrusion width default.
auto_extrusion_width(FlowRole role,float nozzle_diameter)21 float Flow::auto_extrusion_width(FlowRole role, float nozzle_diameter)
22 {
23     switch (role) {
24     case frSupportMaterial:
25     case frSupportMaterialInterface:
26     case frTopSolidInfill:
27         return nozzle_diameter;
28     default:
29     case frExternalPerimeter:
30     case frPerimeter:
31     case frSolidInfill:
32     case frInfill:
33         return 1.125f * nozzle_diameter;
34     }
35 }
36 
37 // Used by the Flow::extrusion_width() funtion to provide hints to the user on default extrusion width values,
38 // and to provide reasonable values to the PlaceholderParser.
opt_key_to_flow_role(const std::string & opt_key)39 static inline FlowRole opt_key_to_flow_role(const std::string &opt_key)
40 {
41  	if (opt_key == "perimeter_extrusion_width" ||
42  		// or all the defaults:
43  		opt_key == "extrusion_width" || opt_key == "first_layer_extrusion_width")
44         return frPerimeter;
45     else if (opt_key == "external_perimeter_extrusion_width")
46         return frExternalPerimeter;
47     else if (opt_key == "infill_extrusion_width")
48         return frInfill;
49     else if (opt_key == "solid_infill_extrusion_width")
50         return frSolidInfill;
51 	else if (opt_key == "top_infill_extrusion_width")
52 		return frTopSolidInfill;
53 	else if (opt_key == "support_material_extrusion_width")
54     	return frSupportMaterial;
55     else
56     	throw Slic3r::RuntimeError("opt_key_to_flow_role: invalid argument");
57 };
58 
throw_on_missing_variable(const std::string & opt_key,const char * dependent_opt_key)59 static inline void throw_on_missing_variable(const std::string &opt_key, const char *dependent_opt_key)
60 {
61 	throw FlowErrorMissingVariable((boost::format(L("Cannot calculate extrusion width for %1%: Variable \"%2%\" not accessible.")) % opt_key % dependent_opt_key).str());
62 }
63 
64 // Used to provide hints to the user on default extrusion width values, and to provide reasonable values to the PlaceholderParser.
extrusion_width(const std::string & opt_key,const ConfigOptionFloatOrPercent * opt,const ConfigOptionResolver & config,const unsigned int first_printing_extruder)65 double Flow::extrusion_width(const std::string& opt_key, const ConfigOptionFloatOrPercent* opt, const ConfigOptionResolver& config, const unsigned int first_printing_extruder)
66 {
67 	assert(opt != nullptr);
68 
69 	bool first_layer = boost::starts_with(opt_key, "first_layer_");
70 
71 #if 0
72 // This is the logic used for skit / brim, but not for the rest of the 1st layer.
73 	if (opt->value == 0. && first_layer) {
74 		// The "first_layer_extrusion_width" was set to zero, try a substitute.
75 		opt = config.option<ConfigOptionFloatOrPercent>("perimeter_extrusion_width");
76 		if (opt == nullptr)
77     		throw_on_missing_variable(opt_key, "perimeter_extrusion_width");
78 	}
79 #endif
80 
81 	if (opt->value == 0.) {
82 		// The role specific extrusion width value was set to zero, try the role non-specific extrusion width.
83 		opt = config.option<ConfigOptionFloatOrPercent>("extrusion_width");
84 		if (opt == nullptr)
85     		throw_on_missing_variable(opt_key, "extrusion_width");
86     	// Use the "layer_height" instead of "first_layer_height".
87     	first_layer = false;
88 	}
89 
90 	if (opt->percent) {
91 		auto opt_key_layer_height = first_layer ? "first_layer_height" : "layer_height";
92     	auto opt_layer_height = config.option(opt_key_layer_height);
93     	if (opt_layer_height == nullptr)
94     		throw_on_missing_variable(opt_key, opt_key_layer_height);
95     	double layer_height = opt_layer_height->getFloat();
96     	if (first_layer && static_cast<const ConfigOptionFloatOrPercent*>(opt_layer_height)->percent) {
97     		// first_layer_height depends on layer_height.
98 	    	opt_layer_height = config.option("layer_height");
99 	    	if (opt_layer_height == nullptr)
100 	    		throw_on_missing_variable(opt_key, "layer_height");
101 	    	layer_height *= 0.01 * opt_layer_height->getFloat();
102     	}
103 		return opt->get_abs_value(layer_height);
104 	}
105 
106 	if (opt->value == 0.) {
107         // If user left option to 0, calculate a sane default width.
108     	auto opt_nozzle_diameters = config.option<ConfigOptionFloats>("nozzle_diameter");
109     	if (opt_nozzle_diameters == nullptr)
110     		throw_on_missing_variable(opt_key, "nozzle_diameter");
111         return auto_extrusion_width(opt_key_to_flow_role(opt_key), float(opt_nozzle_diameters->get_at(first_printing_extruder)));
112     }
113 
114 	return opt->value;
115 }
116 
117 // Used to provide hints to the user on default extrusion width values, and to provide reasonable values to the PlaceholderParser.
extrusion_width(const std::string & opt_key,const ConfigOptionResolver & config,const unsigned int first_printing_extruder)118 double Flow::extrusion_width(const std::string& opt_key, const ConfigOptionResolver &config, const unsigned int first_printing_extruder)
119 {
120     return extrusion_width(opt_key, config.option<ConfigOptionFloatOrPercent>(opt_key), config, first_printing_extruder);
121 }
122 
123 // This constructor builds a Flow object from an extrusion width config setting
124 // and other context properties.
new_from_config_width(FlowRole role,const ConfigOptionFloatOrPercent & width,float nozzle_diameter,float height,float bridge_flow_ratio)125 Flow Flow::new_from_config_width(FlowRole role, const ConfigOptionFloatOrPercent &width, float nozzle_diameter, float height, float bridge_flow_ratio)
126 {
127     // we need layer height unless it's a bridge
128     if (height <= 0 && bridge_flow_ratio == 0)
129         throw Slic3r::InvalidArgument("Invalid flow height supplied to new_from_config_width()");
130 
131     float w;
132     if (bridge_flow_ratio > 0) {
133         // If bridge flow was requested, calculate the bridge width.
134         height = w = (bridge_flow_ratio == 1.) ?
135             // optimization to avoid sqrt()
136             nozzle_diameter :
137             sqrt(bridge_flow_ratio) * nozzle_diameter;
138     } else if (! width.percent && width.value == 0.) {
139         // If user left option to 0, calculate a sane default width.
140         w = auto_extrusion_width(role, nozzle_diameter);
141     } else {
142         // If user set a manual value, use it.
143         w = float(width.get_abs_value(height));
144     }
145 
146     return Flow(w, height, nozzle_diameter, bridge_flow_ratio > 0);
147 }
148 
149 // This constructor builds a Flow object from a given centerline spacing.
new_from_spacing(float spacing,float nozzle_diameter,float height,bool bridge)150 Flow Flow::new_from_spacing(float spacing, float nozzle_diameter, float height, bool bridge)
151 {
152     // we need layer height unless it's a bridge
153     if (height <= 0 && !bridge)
154         throw Slic3r::InvalidArgument("Invalid flow height supplied to new_from_spacing()");
155     // Calculate width from spacing.
156     // For normal extrusons, extrusion width is wider than the spacing due to the rounding and squishing of the extrusions.
157     // For bridge extrusions, the extrusions are placed with a tiny BRIDGE_EXTRA_SPACING gaps between the threads.
158     float width = float(bridge ?
159         (spacing - BRIDGE_EXTRA_SPACING) :
160 #ifdef HAS_PERIMETER_LINE_OVERLAP
161         (spacing + PERIMETER_LINE_OVERLAP_FACTOR * height * (1. - 0.25 * PI));
162 #else
163         (spacing + height * (1. - 0.25 * PI)));
164 #endif
165     return Flow(width, bridge ? width : height, nozzle_diameter, bridge);
166 }
167 
168 // This method returns the centerline spacing between two adjacent extrusions
169 // having the same extrusion width (and other properties).
170 float Flow::spacing() const
171 {
172 #ifdef HAS_PERIMETER_LINE_OVERLAP
173     if (this->bridge)
174         return this->width + BRIDGE_EXTRA_SPACING;
175     // rectangle with semicircles at the ends
176     float min_flow_spacing = this->width - this->height * (1. - 0.25 * PI);
177     float res = this->width - PERIMETER_LINE_OVERLAP_FACTOR * (this->width - min_flow_spacing);
178 #else
179     float res = float(this->bridge ? (this->width + BRIDGE_EXTRA_SPACING) : (this->width - this->height * (1. - 0.25 * PI)));
180 #endif
181 //    assert(res > 0.f);
182 	if (res <= 0.f)
183 		throw FlowErrorNegativeSpacing();
184 	return res;
185 }
186 
187 // This method returns the centerline spacing between an extrusion using this
188 // flow and another one using another flow.
189 // this->spacing(other) shall return the same value as other.spacing(*this)
190 float Flow::spacing(const Flow &other) const
191 {
192     assert(this->height == other.height);
193     assert(this->bridge == other.bridge);
194     float res = float(this->bridge ?
195         0.5 * this->width + 0.5 * other.width + BRIDGE_EXTRA_SPACING :
196         0.5 * this->spacing() + 0.5 * other.spacing());
197 //    assert(res > 0.f);
198 	if (res <= 0.f)
199 		throw FlowErrorNegativeSpacing();
200 	return res;
201 }
202 
203 // This method returns extrusion volume per head move unit.
204 double Flow::mm3_per_mm() const
205 {
206     float res = this->bridge ?
207         // Area of a circle with dmr of this->width.
208         float((this->width * this->width) * 0.25 * PI) :
209         // Rectangle with semicircles at the ends. ~ h (w - 0.215 h)
210         float(this->height * (this->width - this->height * (1. - 0.25 * PI)));
211     //assert(res > 0.);
212 	if (res <= 0.)
213 		throw FlowErrorNegativeFlow();
214     return res;
215 }
216 
217 Flow support_material_flow(const PrintObject *object, float layer_height)
218 {
219     return Flow::new_from_config_width(
220         frSupportMaterial,
221         // The width parameter accepted by new_from_config_width is of type ConfigOptionFloatOrPercent, the Flow class takes care of the percent to value substitution.
222         (object->config().support_material_extrusion_width.value > 0) ? object->config().support_material_extrusion_width : object->config().extrusion_width,
223         // if object->config().support_material_extruder == 0 (which means to not trigger tool change, but use the current extruder instead), get_at will return the 0th component.
224         float(object->print()->config().nozzle_diameter.get_at(object->config().support_material_extruder-1)),
225         (layer_height > 0.f) ? layer_height : float(object->config().layer_height.value),
226         // bridge_flow_ratio
227         0.f);
228 }
229 
230 Flow support_material_1st_layer_flow(const PrintObject *object, float layer_height)
231 {
232     const auto &width = (object->print()->config().first_layer_extrusion_width.value > 0) ? object->print()->config().first_layer_extrusion_width : object->config().support_material_extrusion_width;
233     return Flow::new_from_config_width(
234         frSupportMaterial,
235         // The width parameter accepted by new_from_config_width is of type ConfigOptionFloatOrPercent, the Flow class takes care of the percent to value substitution.
236         (width.value > 0) ? width : object->config().extrusion_width,
237         float(object->print()->config().nozzle_diameter.get_at(object->config().support_material_extruder-1)),
238         (layer_height > 0.f) ? layer_height : float(object->config().first_layer_height.get_abs_value(object->config().layer_height.value)),
239         // bridge_flow_ratio
240         0.f);
241 }
242 
243 Flow support_material_interface_flow(const PrintObject *object, float layer_height)
244 {
245     return Flow::new_from_config_width(
246         frSupportMaterialInterface,
247         // The width parameter accepted by new_from_config_width is of type ConfigOptionFloatOrPercent, the Flow class takes care of the percent to value substitution.
248         (object->config().support_material_extrusion_width > 0) ? object->config().support_material_extrusion_width : object->config().extrusion_width,
249         // if object->config().support_material_interface_extruder == 0 (which means to not trigger tool change, but use the current extruder instead), get_at will return the 0th component.
250         float(object->print()->config().nozzle_diameter.get_at(object->config().support_material_interface_extruder-1)),
251         (layer_height > 0.f) ? layer_height : float(object->config().layer_height.value),
252         // bridge_flow_ratio
253         0.f);
254 }
255 
256 }
257