Model.xsp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. %module{Slic3r::XS};
  2. %{
  3. #include <xsinit.h>
  4. #include "libslic3r/Model.hpp"
  5. #include "libslic3r/Print.hpp"
  6. #include "libslic3r/PrintConfig.hpp"
  7. #include "libslic3r/Slicing.hpp"
  8. #include "libslic3r/Format/AMF.hpp"
  9. #include "libslic3r/Format/3mf.hpp"
  10. #include "libslic3r/Format/OBJ.hpp"
  11. #include "libslic3r/Format/PRUS.hpp"
  12. #include "libslic3r/Format/STL.hpp"
  13. #include "slic3r/GUI/PresetBundle.hpp"
  14. %}
  15. %name{Slic3r::Model} class Model {
  16. Model();
  17. ~Model();
  18. %name{read_from_file} Model(std::string input_file, bool add_default_instances = true)
  19. %code%{
  20. try {
  21. RETVAL = new Model(Model::read_from_file(input_file, add_default_instances));
  22. } catch (std::exception& e) {
  23. croak("Error while opening %s: %s\n", input_file.c_str(), e.what());
  24. }
  25. %};
  26. %name{read_from_archive} Model(std::string input_file, PresetBundle* bundle, bool add_default_instances = true)
  27. %code%{
  28. try {
  29. RETVAL = new Model(Model::read_from_archive(input_file, bundle, add_default_instances));
  30. } catch (std::exception& e) {
  31. croak("Error while opening %s: %s\n", input_file.c_str(), e.what());
  32. }
  33. %};
  34. Clone<Model> clone()
  35. %code%{ RETVAL = THIS; %};
  36. %name{_add_object} Ref<ModelObject> add_object();
  37. Ref<ModelObject> _add_object_clone(ModelObject* other, bool copy_volumes = true)
  38. %code%{ RETVAL = THIS->add_object(*other, copy_volumes); %};
  39. void delete_object(size_t idx);
  40. void clear_objects();
  41. size_t objects_count()
  42. %code%{ RETVAL = THIS->objects.size(); %};
  43. Ref<ModelObject> get_object(int idx)
  44. %code%{ RETVAL = THIS->objects.at(idx); %};
  45. Ref<ModelMaterial> get_material(t_model_material_id material_id)
  46. %code%{
  47. RETVAL = THIS->get_material(material_id);
  48. if (RETVAL == NULL) {
  49. XSRETURN_UNDEF;
  50. }
  51. %};
  52. %name{add_material} Ref<ModelMaterial> add_material(t_model_material_id material_id);
  53. Ref<ModelMaterial> add_material_clone(t_model_material_id material_id, ModelMaterial* other)
  54. %code%{ RETVAL = THIS->add_material(material_id, *other); %};
  55. bool has_material(t_model_material_id material_id) const
  56. %code%{
  57. RETVAL = (THIS->get_material(material_id) != NULL);
  58. %};
  59. void delete_material(t_model_material_id material_id);
  60. void clear_materials();
  61. std::vector<std::string> material_names() const
  62. %code%{
  63. for (ModelMaterialMap::iterator i = THIS->materials.begin();
  64. i != THIS->materials.end(); ++i)
  65. {
  66. RETVAL.push_back(i->first);
  67. }
  68. %};
  69. size_t material_count() const
  70. %code%{ RETVAL = THIS->materials.size(); %};
  71. bool add_default_instances();
  72. Clone<BoundingBoxf3> bounding_box();
  73. void center_instances_around_point(Pointf* point)
  74. %code%{ THIS->center_instances_around_point(*point); %};
  75. void translate(double x, double y, double z);
  76. Clone<TriangleMesh> mesh();
  77. ModelObjectPtrs* objects()
  78. %code%{ RETVAL = &THIS->objects; %};
  79. bool arrange_objects(double dist, BoundingBoxf* bb = NULL);
  80. void duplicate(unsigned int copies_num, double dist, BoundingBoxf* bb = NULL);
  81. void duplicate_objects(unsigned int copies_num, double dist, BoundingBoxf* bb = NULL);
  82. void duplicate_objects_grid(unsigned int x, unsigned int y, double dist);
  83. bool looks_like_multipart_object() const;
  84. void convert_multipart_object(unsigned int max_extruders);
  85. void print_info() const;
  86. bool store_stl(char *path, bool binary)
  87. %code%{ TriangleMesh mesh = THIS->mesh(); RETVAL = Slic3r::store_stl(path, &mesh, binary); %};
  88. bool store_amf(char *path, Print* print, bool export_print_config)
  89. %code%{ RETVAL = Slic3r::store_amf(path, THIS, print, export_print_config); %};
  90. bool store_3mf(char *path, Print* print, bool export_print_config)
  91. %code%{ RETVAL = Slic3r::store_3mf(path, THIS, print, export_print_config); %};
  92. %{
  93. Model*
  94. load_stl(CLASS, path, object_name)
  95. char* CLASS;
  96. char* path;
  97. char* object_name;
  98. CODE:
  99. RETVAL = new Model();
  100. if (! load_stl(path, RETVAL, object_name)) {
  101. delete RETVAL;
  102. RETVAL = NULL;
  103. }
  104. OUTPUT:
  105. RETVAL
  106. Model*
  107. load_obj(CLASS, path, object_name)
  108. char* CLASS;
  109. char* path;
  110. char* object_name;
  111. CODE:
  112. RETVAL = new Model();
  113. if (! load_obj(path, RETVAL, object_name)) {
  114. delete RETVAL;
  115. RETVAL = NULL;
  116. }
  117. OUTPUT:
  118. RETVAL
  119. Model*
  120. load_amf(CLASS, bundle, path)
  121. char* CLASS;
  122. PresetBundle* bundle;
  123. char* path;
  124. CODE:
  125. RETVAL = new Model();
  126. if (! load_amf(path, bundle, RETVAL)) {
  127. delete RETVAL;
  128. RETVAL = NULL;
  129. }
  130. OUTPUT:
  131. RETVAL
  132. Model*
  133. load_3mf(CLASS, bundle, path)
  134. char* CLASS;
  135. PresetBundle* bundle;
  136. char* path;
  137. CODE:
  138. RETVAL = new Model();
  139. if (! load_3mf(path, bundle, RETVAL)) {
  140. delete RETVAL;
  141. RETVAL = NULL;
  142. }
  143. OUTPUT:
  144. RETVAL
  145. Model*
  146. load_prus(CLASS, path)
  147. char* CLASS;
  148. char* path;
  149. CODE:
  150. #ifdef SLIC3R_PRUS
  151. RETVAL = new Model();
  152. if (! load_prus(path, RETVAL)) {
  153. delete RETVAL;
  154. RETVAL = NULL;
  155. }
  156. #else
  157. RETVAL = nullptr;
  158. #endif
  159. OUTPUT:
  160. RETVAL
  161. %}
  162. };
  163. %name{Slic3r::Model::Material} class ModelMaterial {
  164. Ref<Model> model()
  165. %code%{ RETVAL = THIS->get_model(); %};
  166. Ref<DynamicPrintConfig> config()
  167. %code%{ RETVAL = &THIS->config; %};
  168. std::string get_attribute(std::string name)
  169. %code%{ if (THIS->attributes.find(name) != THIS->attributes.end()) RETVAL = THIS->attributes[name]; %};
  170. void set_attribute(std::string name, std::string value)
  171. %code%{ THIS->attributes[name] = value; %};
  172. %{
  173. SV*
  174. ModelMaterial::attributes()
  175. CODE:
  176. HV* hv = newHV();
  177. for (t_model_material_attributes::const_iterator attr = THIS->attributes.begin(); attr != THIS->attributes.end(); ++attr) {
  178. (void)hv_store( hv, attr->first.c_str(), attr->first.length(), newSVpv(attr->second.c_str(), attr->second.length()), 0 );
  179. }
  180. RETVAL = (SV*)newRV_noinc((SV*)hv);
  181. OUTPUT:
  182. RETVAL
  183. %}
  184. };
  185. %name{Slic3r::Model::Object} class ModelObject {
  186. ModelVolumePtrs* volumes()
  187. %code%{ RETVAL = &THIS->volumes; %};
  188. ModelInstancePtrs* instances()
  189. %code%{ RETVAL = &THIS->instances; %};
  190. void invalidate_bounding_box();
  191. Clone<TriangleMesh> mesh();
  192. Clone<TriangleMesh> raw_mesh();
  193. Clone<BoundingBoxf3> instance_bounding_box(int idx)
  194. %code%{ RETVAL = THIS->instance_bounding_box(idx, true); %};
  195. Clone<BoundingBoxf3> bounding_box();
  196. %name{_add_volume} Ref<ModelVolume> add_volume(TriangleMesh* mesh)
  197. %code%{ RETVAL = THIS->add_volume(*mesh); %};
  198. Ref<ModelVolume> _add_volume_clone(ModelVolume* other)
  199. %code%{ RETVAL = THIS->add_volume(*other); %};
  200. void delete_volume(size_t idx);
  201. void clear_volumes();
  202. int volumes_count()
  203. %code%{ RETVAL = THIS->volumes.size(); %};
  204. Ref<ModelVolume> get_volume(int idx)
  205. %code%{ RETVAL = THIS->volumes.at(idx); %};
  206. bool move_volume_up(int idx)
  207. %code%{
  208. if (idx > 0 && idx < int(THIS->volumes.size())) {
  209. std::swap(THIS->volumes[idx-1], THIS->volumes[idx]);
  210. RETVAL = true;
  211. } else
  212. RETVAL = false;
  213. %};
  214. bool move_volume_down(int idx)
  215. %code%{
  216. if (idx >= 0 && idx + 1 < int(THIS->volumes.size())) {
  217. std::swap(THIS->volumes[idx+1], THIS->volumes[idx]);
  218. RETVAL = true;
  219. } else
  220. RETVAL = false;
  221. %};
  222. %name{_add_instance} Ref<ModelInstance> add_instance();
  223. Ref<ModelInstance> _add_instance_clone(ModelInstance* other)
  224. %code%{ RETVAL = THIS->add_instance(*other); %};
  225. void delete_last_instance();
  226. void clear_instances();
  227. int instances_count()
  228. %code%{ RETVAL = THIS->instances.size(); %};
  229. std::string name()
  230. %code%{ RETVAL = THIS->name; %};
  231. void set_name(std::string value)
  232. %code%{ THIS->name = value; %};
  233. std::string input_file()
  234. %code%{ RETVAL = THIS->input_file; %};
  235. void set_input_file(std::string value)
  236. %code%{ THIS->input_file = value; %};
  237. Ref<DynamicPrintConfig> config()
  238. %code%{ RETVAL = &THIS->config; %};
  239. Ref<Model> model()
  240. %code%{ RETVAL = THIS->get_model(); %};
  241. t_layer_height_ranges layer_height_ranges()
  242. %code%{ RETVAL = THIS->layer_height_ranges; %};
  243. void set_layer_height_ranges(t_layer_height_ranges ranges)
  244. %code%{ THIS->layer_height_ranges = ranges; %};
  245. std::vector<double> layer_height_profile()
  246. %code%{ RETVAL = THIS->layer_height_profile_valid ? THIS->layer_height_profile : std::vector<double>(); %};
  247. void set_layer_height_profile(std::vector<double> profile)
  248. %code%{ THIS->layer_height_profile = profile; THIS->layer_height_profile_valid = true; %};
  249. Ref<Pointf3> origin_translation()
  250. %code%{ RETVAL = &THIS->origin_translation; %};
  251. void set_origin_translation(Pointf3* point)
  252. %code%{ THIS->origin_translation = *point; %};
  253. bool needed_repair() const;
  254. int materials_count() const;
  255. int facets_count();
  256. void center_around_origin();
  257. void translate(double x, double y, double z);
  258. void scale_xyz(Pointf3* versor)
  259. %code{% THIS->scale(*versor); %};
  260. void rotate(float angle, Axis axis);
  261. void mirror(Axis axis);
  262. Model* cut(double z)
  263. %code%{
  264. RETVAL = new Model();
  265. THIS->cut(z, RETVAL);
  266. %};
  267. ModelObjectPtrs* split_object()
  268. %code%{
  269. RETVAL = new ModelObjectPtrs(); // leak?
  270. THIS->split(RETVAL);
  271. %};
  272. void print_info() const;
  273. };
  274. %name{Slic3r::Model::Volume} class ModelVolume {
  275. Ref<ModelObject> object()
  276. %code%{ RETVAL = THIS->get_object(); %};
  277. std::string name()
  278. %code%{ RETVAL = THIS->name; %};
  279. void set_name(std::string value)
  280. %code%{ THIS->name = value; %};
  281. t_model_material_id material_id();
  282. void set_material_id(t_model_material_id material_id)
  283. %code%{ THIS->material_id(material_id); %};
  284. Ref<ModelMaterial> material();
  285. Ref<DynamicPrintConfig> config()
  286. %code%{ RETVAL = &THIS->config; %};
  287. Ref<TriangleMesh> mesh()
  288. %code%{ RETVAL = &THIS->mesh; %};
  289. bool modifier()
  290. %code%{ RETVAL = THIS->modifier; %};
  291. void set_modifier(bool modifier)
  292. %code%{ THIS->modifier = modifier; %};
  293. size_t split(unsigned int max_extruders);
  294. ModelMaterial* assign_unique_material();
  295. };
  296. %name{Slic3r::Model::Instance} class ModelInstance {
  297. Ref<ModelObject> object()
  298. %code%{ RETVAL = THIS->get_object(); %};
  299. double rotation()
  300. %code%{ RETVAL = THIS->rotation; %};
  301. double scaling_factor()
  302. %code%{ RETVAL = THIS->scaling_factor; %};
  303. Ref<Pointf> offset()
  304. %code%{ RETVAL = &THIS->offset; %};
  305. void set_rotation(double val)
  306. %code%{ THIS->rotation = val; THIS->get_object()->invalidate_bounding_box(); %};
  307. void set_scaling_factor(double val)
  308. %code%{ THIS->scaling_factor = val; THIS->get_object()->invalidate_bounding_box(); %};
  309. void set_offset(Pointf *offset)
  310. %code%{ THIS->offset = *offset; %};
  311. void transform_mesh(TriangleMesh* mesh, bool dont_translate = false) const;
  312. void transform_polygon(Polygon* polygon) const;
  313. };