Model.xsp 11 KB

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