Model.xsp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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, nullptr, 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. Clone<Model> clone()
  27. %code%{ RETVAL = THIS; %};
  28. %name{_add_object} Ref<ModelObject> add_object();
  29. Ref<ModelObject> _add_object_clone(ModelObject* other, bool copy_volumes = true)
  30. %code%{ auto ptr = THIS->add_object(*other); if (! copy_volumes) ptr->clear_volumes(); RETVAL = ptr; %};
  31. void delete_object(size_t idx);
  32. void clear_objects();
  33. size_t objects_count()
  34. %code%{ RETVAL = THIS->objects.size(); %};
  35. Ref<ModelObject> get_object(int idx)
  36. %code%{ RETVAL = THIS->objects.at(idx); %};
  37. Ref<ModelMaterial> get_material(t_model_material_id material_id)
  38. %code%{
  39. RETVAL = THIS->get_material(material_id);
  40. if (RETVAL == NULL) {
  41. XSRETURN_UNDEF;
  42. }
  43. %};
  44. %name{add_material} Ref<ModelMaterial> add_material(t_model_material_id material_id);
  45. Ref<ModelMaterial> add_material_clone(t_model_material_id material_id, ModelMaterial* other)
  46. %code%{ RETVAL = THIS->add_material(material_id, *other); %};
  47. bool has_material(t_model_material_id material_id) const
  48. %code%{
  49. RETVAL = (THIS->get_material(material_id) != NULL);
  50. %};
  51. void delete_material(t_model_material_id material_id);
  52. void clear_materials();
  53. std::vector<std::string> material_names() const
  54. %code%{
  55. for (ModelMaterialMap::iterator i = THIS->materials.begin();
  56. i != THIS->materials.end(); ++i)
  57. {
  58. RETVAL.push_back(i->first);
  59. }
  60. %};
  61. size_t material_count() const
  62. %code%{ RETVAL = THIS->materials.size(); %};
  63. bool add_default_instances();
  64. Clone<BoundingBoxf3> bounding_box();
  65. void center_instances_around_point(Vec2d* point)
  66. %code%{ THIS->center_instances_around_point(*point); %};
  67. void translate(double x, double y, double z);
  68. Clone<TriangleMesh> mesh();
  69. ModelObjectPtrs* objects()
  70. %code%{ RETVAL = &THIS->objects; %};
  71. bool arrange_objects(double dist, BoundingBoxf* bb = NULL);
  72. void duplicate(unsigned int copies_num, double dist, BoundingBoxf* bb = NULL);
  73. void duplicate_objects(unsigned int copies_num, double dist, BoundingBoxf* bb = NULL);
  74. void duplicate_objects_grid(unsigned int x, unsigned int y, double dist);
  75. bool looks_like_multipart_object() const;
  76. void convert_multipart_object(unsigned int max_extruders);
  77. void print_info() const;
  78. bool store_stl(char *path, bool binary)
  79. %code%{ TriangleMesh mesh = THIS->mesh(); RETVAL = Slic3r::store_stl(path, &mesh, binary); %};
  80. %{
  81. Model*
  82. load_stl(CLASS, path, object_name)
  83. char* CLASS;
  84. char* path;
  85. char* object_name;
  86. CODE:
  87. RETVAL = new Model();
  88. if (! load_stl(path, RETVAL, object_name)) {
  89. delete RETVAL;
  90. RETVAL = NULL;
  91. }
  92. OUTPUT:
  93. RETVAL
  94. %}
  95. };
  96. %name{Slic3r::Model::Material} class ModelMaterial {
  97. Ref<Model> model()
  98. %code%{ RETVAL = THIS->get_model(); %};
  99. Ref<DynamicPrintConfig> config()
  100. %code%{ RETVAL = &THIS->config; %};
  101. std::string get_attribute(std::string name)
  102. %code%{ if (THIS->attributes.find(name) != THIS->attributes.end()) RETVAL = THIS->attributes[name]; %};
  103. void set_attribute(std::string name, std::string value)
  104. %code%{ THIS->attributes[name] = value; %};
  105. %{
  106. SV*
  107. ModelMaterial::attributes()
  108. CODE:
  109. HV* hv = newHV();
  110. for (t_model_material_attributes::const_iterator attr = THIS->attributes.begin(); attr != THIS->attributes.end(); ++attr) {
  111. (void)hv_store( hv, attr->first.c_str(), attr->first.length(), newSVpv(attr->second.c_str(), attr->second.length()), 0 );
  112. }
  113. RETVAL = (SV*)newRV_noinc((SV*)hv);
  114. OUTPUT:
  115. RETVAL
  116. %}
  117. };
  118. %name{Slic3r::Model::Object} class ModelObject {
  119. ModelVolumePtrs* volumes()
  120. %code%{ RETVAL = &THIS->volumes; %};
  121. ModelInstancePtrs* instances()
  122. %code%{ RETVAL = &THIS->instances; %};
  123. void invalidate_bounding_box();
  124. Clone<TriangleMesh> mesh();
  125. Clone<TriangleMesh> raw_mesh();
  126. Clone<BoundingBoxf3> instance_bounding_box(int idx)
  127. %code%{ RETVAL = THIS->instance_bounding_box(idx, true); %};
  128. Clone<BoundingBoxf3> bounding_box();
  129. %name{_add_volume} Ref<ModelVolume> add_volume(TriangleMesh* mesh)
  130. %code%{ RETVAL = THIS->add_volume(*mesh); %};
  131. Ref<ModelVolume> _add_volume_clone(ModelVolume* other)
  132. %code%{ RETVAL = THIS->add_volume(*other); %};
  133. void delete_volume(size_t idx);
  134. void clear_volumes();
  135. int volumes_count()
  136. %code%{ RETVAL = THIS->volumes.size(); %};
  137. Ref<ModelVolume> get_volume(int idx)
  138. %code%{ RETVAL = THIS->volumes.at(idx); %};
  139. bool move_volume_up(int idx)
  140. %code%{
  141. if (idx > 0 && idx < int(THIS->volumes.size())) {
  142. std::swap(THIS->volumes[idx-1], THIS->volumes[idx]);
  143. RETVAL = true;
  144. } else
  145. RETVAL = false;
  146. %};
  147. bool move_volume_down(int idx)
  148. %code%{
  149. if (idx >= 0 && idx + 1 < int(THIS->volumes.size())) {
  150. std::swap(THIS->volumes[idx+1], THIS->volumes[idx]);
  151. RETVAL = true;
  152. } else
  153. RETVAL = false;
  154. %};
  155. %name{_add_instance} Ref<ModelInstance> add_instance();
  156. Ref<ModelInstance> _add_instance_clone(ModelInstance* other)
  157. %code%{ RETVAL = THIS->add_instance(*other); %};
  158. void delete_last_instance();
  159. void clear_instances();
  160. int instances_count()
  161. %code%{ RETVAL = THIS->instances.size(); %};
  162. std::string name()
  163. %code%{ RETVAL = THIS->name; %};
  164. void set_name(std::string value)
  165. %code%{ THIS->name = value; %};
  166. std::string input_file()
  167. %code%{ RETVAL = THIS->input_file; %};
  168. void set_input_file(std::string value)
  169. %code%{ THIS->input_file = value; %};
  170. Ref<DynamicPrintConfig> config()
  171. %code%{ RETVAL = &THIS->config; %};
  172. Ref<Model> model()
  173. %code%{ RETVAL = THIS->get_model(); %};
  174. Ref<Vec3d> origin_translation()
  175. %code%{ RETVAL = &THIS->origin_translation; %};
  176. void set_origin_translation(Vec3d* point)
  177. %code%{ THIS->origin_translation = *point; %};
  178. void ensure_on_bed();
  179. bool needed_repair() const;
  180. int materials_count() const;
  181. int facets_count();
  182. void center_around_origin();
  183. void translate(double x, double y, double z);
  184. void scale_xyz(Vec3d* versor)
  185. %code{% THIS->scale(*versor); %};
  186. void rotate(float angle, Vec3d* axis)
  187. %code{% THIS->rotate(angle, *axis); %};
  188. void mirror(Axis axis);
  189. ModelObjectPtrs* split_object()
  190. %code%{
  191. RETVAL = new ModelObjectPtrs(); // leak?
  192. THIS->split(RETVAL);
  193. %};
  194. void print_info() const;
  195. };
  196. %name{Slic3r::Model::Volume} class ModelVolume {
  197. Ref<ModelObject> object()
  198. %code%{ RETVAL = THIS->get_object(); %};
  199. std::string name()
  200. %code%{ RETVAL = THIS->name; %};
  201. void set_name(std::string value)
  202. %code%{ THIS->name = value; %};
  203. t_model_material_id material_id();
  204. void set_material_id(t_model_material_id material_id)
  205. %code%{ THIS->set_material_id(material_id); %};
  206. Ref<ModelMaterial> material();
  207. Ref<DynamicPrintConfig> config()
  208. %code%{ RETVAL = &THIS->config; %};
  209. Ref<TriangleMesh> mesh()
  210. %code%{ RETVAL = &THIS->mesh(); %};
  211. bool modifier()
  212. %code%{ RETVAL = THIS->is_modifier(); %};
  213. void set_modifier(bool modifier)
  214. %code%{ THIS->set_type(modifier ? ModelVolumeType::PARAMETER_MODIFIER : ModelVolumeType::MODEL_PART); %};
  215. bool model_part()
  216. %code%{ RETVAL = THIS->is_model_part(); %};
  217. bool support_enforcer()
  218. %code%{ RETVAL = THIS->is_support_enforcer(); %};
  219. void set_support_enforcer()
  220. %code%{ THIS->set_type(ModelVolumeType::SUPPORT_ENFORCER); %};
  221. bool support_blocker()
  222. %code%{ RETVAL = THIS->is_support_blocker(); %};
  223. void set_support_blocker()
  224. %code%{ THIS->set_type(ModelVolumeType::SUPPORT_BLOCKER); %};
  225. size_t split(unsigned int max_extruders);
  226. };
  227. %name{Slic3r::Model::Instance} class ModelInstance {
  228. Ref<ModelObject> object()
  229. %code%{ RETVAL = THIS->get_object(); %};
  230. Vec3d* rotation()
  231. %code%{ RETVAL = new Vec3d(THIS->get_rotation(X), THIS->get_rotation(Y), THIS->get_rotation(Z)); %};
  232. Vec3d* scaling_factor()
  233. %code%{ RETVAL = new Vec3d(THIS->get_scaling_factor(X), THIS->get_scaling_factor(Y), THIS->get_scaling_factor(Z)); %};
  234. Vec2d* offset()
  235. %code%{ RETVAL = new Vec2d(THIS->get_offset(X), THIS->get_offset(Y)); %};
  236. void set_rotation(double val)
  237. %code%{ THIS->set_rotation(Z, val); THIS->get_object()->invalidate_bounding_box(); %};
  238. void set_rotations(Vec3d *rotation)
  239. %code%{ THIS->set_rotation(*rotation); THIS->get_object()->invalidate_bounding_box(); %};
  240. void set_scaling_factor(double val)
  241. %code%{ THIS->set_scaling_factor(X, val); THIS->set_scaling_factor(Y, val); THIS->set_scaling_factor(Z, val); THIS->get_object()->invalidate_bounding_box(); %};
  242. void set_scaling_factors(Vec3d *scale)
  243. %code%{ THIS->set_scaling_factor(*scale); THIS->get_object()->invalidate_bounding_box(); %};
  244. void set_offset(Vec2d *offset)
  245. %code%{
  246. THIS->set_offset(X, (*offset)(0));
  247. THIS->set_offset(Y, (*offset)(1));
  248. %};
  249. void transform_mesh(TriangleMesh* mesh, bool dont_translate = false) const;
  250. void transform_polygon(Polygon* polygon) const;
  251. };