Config.xsp 11 KB

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