Model.xsp 10 KB

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