Config.xsp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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_FullPrintConfig()
  77. %code{% RETVAL = static_cast<GCodeConfig*>(new FullPrintConfig()); %};
  78. ~StaticPrintConfig();
  79. bool has(t_config_option_key opt_key);
  80. SV* as_hash()
  81. %code{% RETVAL = ConfigBase__as_hash(THIS); %};
  82. SV* get(t_config_option_key opt_key)
  83. %code{% RETVAL = ConfigBase__get(THIS, opt_key); %};
  84. SV* get_at(t_config_option_key opt_key, int i)
  85. %code{% RETVAL = ConfigBase__get_at(THIS, opt_key, i); %};
  86. bool set(t_config_option_key opt_key, SV* value)
  87. %code{% RETVAL = StaticConfig__set(THIS, opt_key, value); %};
  88. bool set_deserialize(t_config_option_key opt_key, SV* str)
  89. %code{% RETVAL = ConfigBase__set_deserialize(THIS, opt_key, str); %};
  90. void set_ifndef(t_config_option_key opt_key, SV* value, bool deserialize = false)
  91. %code{% ConfigBase__set_ifndef(THIS, opt_key, value, deserialize); %};
  92. std::string opt_serialize(t_config_option_key opt_key);
  93. double get_abs_value(t_config_option_key opt_key);
  94. %name{get_abs_value_over}
  95. double get_abs_value(t_config_option_key opt_key, double ratio_over);
  96. void apply_static(StaticPrintConfig* other)
  97. %code{% THIS->apply(*other, true); %};
  98. void apply_dynamic(DynamicPrintConfig* other)
  99. %code{% THIS->apply(*other, true); %};
  100. %name{get_keys} std::vector<std::string> keys();
  101. std::string get_extrusion_axis()
  102. %code{%
  103. if (GCodeConfig* config = dynamic_cast<GCodeConfig*>(THIS)) {
  104. RETVAL = get_extrusion_axis(*config);
  105. } else {
  106. CONFESS("This StaticConfig object does not provide get_extrusion_axis()");
  107. }
  108. %};
  109. %name{setenv} void setenv_();
  110. double min_object_distance() %code{% RETVAL = Slic3r::min_object_distance(*THIS); %};
  111. static StaticPrintConfig* load(char *path)
  112. %code%{
  113. auto config = new FullPrintConfig();
  114. try {
  115. config->load(path, ForwardCompatibilitySubstitutionRule::Disable);
  116. RETVAL = static_cast<GCodeConfig*>(config);
  117. } catch (std::exception& e) {
  118. delete config;
  119. croak("Error extracting configuration from %s:\n%s\n", path, e.what());
  120. }
  121. %};
  122. void save(std::string file);
  123. };
  124. %package{Slic3r::Config};
  125. %{
  126. PROTOTYPES: DISABLE
  127. SV*
  128. print_config_def()
  129. CODE:
  130. t_optiondef_map &def = *const_cast<t_optiondef_map*>(&Slic3r::print_config_def.options);
  131. HV* options_hv = newHV();
  132. for (t_optiondef_map::iterator oit = def.begin(); oit != def.end(); ++oit) {
  133. HV* hv = newHV();
  134. t_config_option_key opt_key = oit->first;
  135. ConfigOptionDef* optdef = &oit->second;
  136. const char* opt_type;
  137. if (optdef->type == coFloat || optdef->type == coFloats || optdef->type == coFloatOrPercent || optdef->type == coFloatsOrPercents) {
  138. opt_type = "f";
  139. } else if (optdef->type == coPercent || optdef->type == coPercents) {
  140. opt_type = "percent";
  141. } else if (optdef->type == coInt || optdef->type == coInts) {
  142. opt_type = "i";
  143. } else if (optdef->type == coString) {
  144. opt_type = "s";
  145. } else if (optdef->type == coStrings) {
  146. opt_type = "s@";
  147. } else if (optdef->type == coPoint || optdef->type == coPoints) {
  148. opt_type = "point";
  149. } else if (optdef->type == coPoint3) {
  150. opt_type = "point3";
  151. } else if (optdef->type == coBool || optdef->type == coBools) {
  152. opt_type = "bool";
  153. } else if (optdef->type == coEnum) {
  154. opt_type = "select";
  155. } else {
  156. throw "Unknown option type";
  157. }
  158. (void)hv_stores( hv, "type", newSVpv(opt_type, 0) );
  159. (void)hv_stores( hv, "height", newSViv(optdef->height) );
  160. (void)hv_stores( hv, "width", newSViv(optdef->width) );
  161. (void)hv_stores( hv, "min", newSViv(optdef->min) );
  162. (void)hv_stores( hv, "max", newSViv(optdef->max) );
  163. // aliases
  164. if (!optdef->aliases.empty()) {
  165. AV* av = newAV();
  166. av_fill(av, optdef->aliases.size()-1);
  167. for (std::vector<t_config_option_key>::iterator it = optdef->aliases.begin(); it != optdef->aliases.end(); ++it)
  168. av_store(av, it - optdef->aliases.begin(), newSVpvn(it->c_str(), it->length()));
  169. (void)hv_stores( hv, "aliases", newRV_noinc((SV*)av) );
  170. }
  171. // shortcut
  172. if (!optdef->shortcut.empty()) {
  173. AV* av = newAV();
  174. av_fill(av, optdef->shortcut.size()-1);
  175. for (std::vector<t_config_option_key>::iterator it = optdef->shortcut.begin(); it != optdef->shortcut.end(); ++it)
  176. av_store(av, it - optdef->shortcut.begin(), newSVpvn(it->c_str(), it->length()));
  177. (void)hv_stores( hv, "shortcut", newRV_noinc((SV*)av) );
  178. }
  179. // enum_values
  180. if (optdef->enum_def && !optdef->enum_def->values().empty()) {
  181. AV* av = newAV();
  182. av_fill(av, optdef->enum_def->values().size()-1);
  183. for (std::vector<std::string>::const_iterator it = optdef->enum_def->values().begin(); it != optdef->enum_def->values().end(); ++it)
  184. av_store(av, it - optdef->enum_def->values().begin(), newSVpvn(it->c_str(), it->length()));
  185. (void)hv_stores( hv, "values", newRV_noinc((SV*)av) );
  186. }
  187. // enum_labels
  188. if (optdef->enum_def && !optdef->enum_def->labels().empty()) {
  189. AV* av = newAV();
  190. av_fill(av, optdef->enum_def->labels().size()-1);
  191. for (std::vector<std::string>::const_iterator it = optdef->enum_def->labels().begin(); it != optdef->enum_def->labels().end(); ++it)
  192. av_store(av, it - optdef->enum_def->labels().begin(), newSVpvn_utf8(it->c_str(), it->length(), true));
  193. (void)hv_stores( hv, "labels", newRV_noinc((SV*)av) );
  194. }
  195. if (optdef->default_value)
  196. (void)hv_stores( hv, "default", ConfigOption_to_SV(*optdef->default_value.get(), *optdef) );
  197. (void)hv_store( options_hv, opt_key.c_str(), opt_key.length(), newRV_noinc((SV*)hv), 0 );
  198. }
  199. RETVAL = newRV_noinc((SV*)options_hv);
  200. OUTPUT:
  201. RETVAL
  202. %}