Config.xsp 10 KB

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