Config.xsp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. %module{Slic3r::XS};
  2. %{
  3. #include <xsinit.h>
  4. #include "libslic3r/PrintConfig.hpp"
  5. %}
  6. %name{Slic3r::Config} class DynamicPrintConfig {
  7. DynamicPrintConfig();
  8. ~DynamicPrintConfig();
  9. bool has(t_config_option_key opt_key);
  10. SV* as_hash()
  11. %code{% RETVAL = ConfigBase__as_hash(THIS); %};
  12. SV* get(t_config_option_key opt_key)
  13. %code{% RETVAL = ConfigBase__get(THIS, opt_key); %};
  14. SV* get_at(t_config_option_key opt_key, int i)
  15. %code{% RETVAL = ConfigBase__get_at(THIS, opt_key, i); %};
  16. bool set(t_config_option_key opt_key, SV* value)
  17. %code{% RETVAL = ConfigBase__set(THIS, opt_key, value); %};
  18. bool set_deserialize(t_config_option_key opt_key, SV* str)
  19. %code{% RETVAL = ConfigBase__set_deserialize(THIS, opt_key, str); %};
  20. void set_ifndef(t_config_option_key opt_key, SV* value, bool deserialize = false)
  21. %code{% ConfigBase__set_ifndef(THIS, opt_key, value, deserialize); %};
  22. std::string serialize(t_config_option_key opt_key);
  23. double get_abs_value(t_config_option_key opt_key);
  24. %name{get_abs_value_over}
  25. double get_abs_value(t_config_option_key opt_key, double ratio_over);
  26. void apply(DynamicPrintConfig* other)
  27. %code{% THIS->apply(*other, true); %};
  28. std::vector<std::string> diff(DynamicPrintConfig* other)
  29. %code{% RETVAL = THIS->diff(*other); %};
  30. bool equals(DynamicPrintConfig* other)
  31. %code{% RETVAL = THIS->equals(*other); %};
  32. void apply_static(StaticPrintConfig* other)
  33. %code{% THIS->apply(*other, true); %};
  34. %name{get_keys} std::vector<std::string> keys();
  35. void erase(t_config_option_key opt_key);
  36. void normalize();
  37. %name{setenv} void setenv_();
  38. double min_object_distance();
  39. %name{_load} void load(std::string file);
  40. %name{_load_from_gcode} void load_from_gcode(std::string input_file)
  41. %code%{
  42. try {
  43. THIS->load_from_gcode(input_file);
  44. } catch (std::exception& e) {
  45. croak("Error extracting configuration from a g-code %s:\n%s\n", input_file.c_str(), e.what());
  46. }
  47. %};
  48. %name{_save} void save(std::string file);
  49. };
  50. %name{Slic3r::Config::Static} class StaticPrintConfig {
  51. static StaticPrintConfig* new_GCodeConfig()
  52. %code{% RETVAL = new GCodeConfig (); %};
  53. static StaticPrintConfig* new_PrintConfig()
  54. %code{% RETVAL = new PrintConfig (); %};
  55. static StaticPrintConfig* new_PrintObjectConfig()
  56. %code{% RETVAL = new PrintObjectConfig (); %};
  57. static StaticPrintConfig* new_PrintRegionConfig()
  58. %code{% RETVAL = new PrintRegionConfig (); %};
  59. static StaticPrintConfig* new_FullPrintConfig()
  60. %code{% RETVAL = new FullPrintConfig (); %};
  61. ~StaticPrintConfig();
  62. bool has(t_config_option_key opt_key);
  63. SV* as_hash()
  64. %code{% RETVAL = ConfigBase__as_hash(THIS); %};
  65. SV* get(t_config_option_key opt_key)
  66. %code{% RETVAL = ConfigBase__get(THIS, opt_key); %};
  67. SV* get_at(t_config_option_key opt_key, int i)
  68. %code{% RETVAL = ConfigBase__get_at(THIS, opt_key, i); %};
  69. bool set(t_config_option_key opt_key, SV* value)
  70. %code{% RETVAL = StaticConfig__set(THIS, opt_key, value); %};
  71. bool set_deserialize(t_config_option_key opt_key, SV* str)
  72. %code{% RETVAL = ConfigBase__set_deserialize(THIS, opt_key, str); %};
  73. void set_ifndef(t_config_option_key opt_key, SV* value, bool deserialize = false)
  74. %code{% ConfigBase__set_ifndef(THIS, opt_key, value, deserialize); %};
  75. std::string serialize(t_config_option_key opt_key);
  76. double get_abs_value(t_config_option_key opt_key);
  77. %name{get_abs_value_over}
  78. double get_abs_value(t_config_option_key opt_key, double ratio_over);
  79. void apply_static(StaticPrintConfig* other)
  80. %code{% THIS->apply(*other, true); %};
  81. void apply_dynamic(DynamicPrintConfig* other)
  82. %code{% THIS->apply(*other, true); %};
  83. %name{get_keys} std::vector<std::string> keys();
  84. std::string get_extrusion_axis()
  85. %code{%
  86. if (GCodeConfig* config = dynamic_cast<GCodeConfig*>(THIS)) {
  87. RETVAL = config->get_extrusion_axis();
  88. } else {
  89. CONFESS("This StaticConfig object does not provide get_extrusion_axis()");
  90. }
  91. %};
  92. %name{setenv} void setenv_();
  93. double min_object_distance();
  94. %name{_load} void load(std::string file);
  95. %name{_load_from_gcode} void load_from_gcode(std::string file);
  96. %name{_save} void save(std::string file);
  97. };
  98. %package{Slic3r::Config};
  99. %{
  100. PROTOTYPES: DISABLE
  101. SV*
  102. print_config_def()
  103. CODE:
  104. t_optiondef_map &def = Slic3r::print_config_def.options;
  105. HV* options_hv = newHV();
  106. for (t_optiondef_map::iterator oit = def.begin(); oit != def.end(); ++oit) {
  107. HV* hv = newHV();
  108. t_config_option_key opt_key = oit->first;
  109. ConfigOptionDef* optdef = &oit->second;
  110. const char* opt_type;
  111. if (optdef->type == coFloat || optdef->type == coFloats || optdef->type == coFloatOrPercent) {
  112. opt_type = "f";
  113. } else if (optdef->type == coPercent || optdef->type == coPercents) {
  114. opt_type = "percent";
  115. } else if (optdef->type == coInt || optdef->type == coInts) {
  116. opt_type = "i";
  117. } else if (optdef->type == coString) {
  118. opt_type = "s";
  119. } else if (optdef->type == coStrings) {
  120. opt_type = "s@";
  121. } else if (optdef->type == coPoint || optdef->type == coPoints) {
  122. opt_type = "point";
  123. } else if (optdef->type == coBool || optdef->type == coBools) {
  124. opt_type = "bool";
  125. } else if (optdef->type == coEnum) {
  126. opt_type = "select";
  127. } else {
  128. throw "Unknown option type";
  129. }
  130. (void)hv_stores( hv, "type", newSVpv(opt_type, 0) );
  131. (void)hv_stores( hv, "gui_type", newSVpvn(optdef->gui_type.c_str(), optdef->gui_type.length()) );
  132. (void)hv_stores( hv, "gui_flags", newSVpvn(optdef->gui_flags.c_str(), optdef->gui_flags.length()) );
  133. (void)hv_stores( hv, "label", newSVpvn_utf8(optdef->label.c_str(), optdef->label.length(), true) );
  134. if (!optdef->full_label.empty())
  135. (void)hv_stores( hv, "full_label", newSVpvn_utf8(optdef->full_label.c_str(), optdef->full_label.length(), true) );
  136. (void)hv_stores( hv, "category", newSVpvn_utf8(optdef->category.c_str(), optdef->category.length(), true) );
  137. (void)hv_stores( hv, "tooltip", newSVpvn_utf8(optdef->tooltip.c_str(), optdef->tooltip.length(), true) );
  138. (void)hv_stores( hv, "sidetext", newSVpvn_utf8(optdef->sidetext.c_str(), optdef->sidetext.length(), true) );
  139. (void)hv_stores( hv, "cli", newSVpvn(optdef->cli.c_str(), optdef->cli.length()) );
  140. (void)hv_stores( hv, "ratio_over", newSVpvn(optdef->ratio_over.c_str(), optdef->ratio_over.length()) );
  141. (void)hv_stores( hv, "multiline", newSViv(optdef->multiline ? 1 : 0) );
  142. (void)hv_stores( hv, "full_width", newSViv(optdef->full_width ? 1 : 0) );
  143. (void)hv_stores( hv, "readonly", newSViv(optdef->readonly ? 1 : 0) );
  144. (void)hv_stores( hv, "height", newSViv(optdef->height) );
  145. (void)hv_stores( hv, "width", newSViv(optdef->width) );
  146. (void)hv_stores( hv, "min", newSViv(optdef->min) );
  147. (void)hv_stores( hv, "max", newSViv(optdef->max) );
  148. // aliases
  149. if (!optdef->aliases.empty()) {
  150. AV* av = newAV();
  151. av_fill(av, optdef->aliases.size()-1);
  152. for (std::vector<t_config_option_key>::iterator it = optdef->aliases.begin(); it != optdef->aliases.end(); ++it)
  153. av_store(av, it - optdef->aliases.begin(), newSVpvn(it->c_str(), it->length()));
  154. (void)hv_stores( hv, "aliases", newRV_noinc((SV*)av) );
  155. }
  156. // shortcut
  157. if (!optdef->shortcut.empty()) {
  158. AV* av = newAV();
  159. av_fill(av, optdef->shortcut.size()-1);
  160. for (std::vector<t_config_option_key>::iterator it = optdef->shortcut.begin(); it != optdef->shortcut.end(); ++it)
  161. av_store(av, it - optdef->shortcut.begin(), newSVpvn(it->c_str(), it->length()));
  162. (void)hv_stores( hv, "shortcut", newRV_noinc((SV*)av) );
  163. }
  164. // enum_values
  165. if (!optdef->enum_values.empty()) {
  166. AV* av = newAV();
  167. av_fill(av, optdef->enum_values.size()-1);
  168. for (std::vector<std::string>::iterator it = optdef->enum_values.begin(); it != optdef->enum_values.end(); ++it)
  169. av_store(av, it - optdef->enum_values.begin(), newSVpvn(it->c_str(), it->length()));
  170. (void)hv_stores( hv, "values", newRV_noinc((SV*)av) );
  171. }
  172. // enum_labels
  173. if (!optdef->enum_labels.empty()) {
  174. AV* av = newAV();
  175. av_fill(av, optdef->enum_labels.size()-1);
  176. for (std::vector<std::string>::iterator it = optdef->enum_labels.begin(); it != optdef->enum_labels.end(); ++it)
  177. av_store(av, it - optdef->enum_labels.begin(), newSVpvn_utf8(it->c_str(), it->length(), true));
  178. (void)hv_stores( hv, "labels", newRV_noinc((SV*)av) );
  179. }
  180. if (optdef->default_value != NULL)
  181. (void)hv_stores( hv, "default", ConfigOption_to_SV(*optdef->default_value, *optdef) );
  182. (void)hv_store( options_hv, opt_key.c_str(), opt_key.length(), newRV_noinc((SV*)hv), 0 );
  183. }
  184. RETVAL = newRV_noinc((SV*)options_hv);
  185. OUTPUT:
  186. RETVAL
  187. %}