Model.xsp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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/STL.hpp"
  13. #include "libslic3r/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, nullptr, only_if(add_default_instances, Model::LoadAttribute::AddDefaultInstances)));
  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) %code%{ ArrangeParams ap{scaled(dist)}; if (bb) arrange_objects(*THIS, scaled(*bb), ap); else arrange_objects(*THIS, InfiniteBed{}, ap); %};
  72. 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); %};
  73. 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); %};
  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. bool store_stl(char *path, bool binary)
  78. %code%{ TriangleMesh mesh = THIS->mesh(); RETVAL = Slic3r::store_stl(path, &mesh, binary); %};
  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. %}
  94. };
  95. %name{Slic3r::Model::Material} class ModelMaterial {
  96. Ref<Model> model()
  97. %code%{ RETVAL = THIS->get_model(); %};
  98. Ref<DynamicPrintConfig> config()
  99. %code%{ RETVAL = &const_cast<DynamicPrintConfig&>(THIS->config.get()); %};
  100. std::string get_attribute(std::string name)
  101. %code%{ if (THIS->attributes.find(name) != THIS->attributes.end()) RETVAL = THIS->attributes[name]; %};
  102. void set_attribute(std::string name, std::string value)
  103. %code%{ THIS->attributes[name] = value; %};
  104. %{
  105. SV*
  106. ModelMaterial::attributes()
  107. CODE:
  108. HV* hv = newHV();
  109. for (t_model_material_attributes::const_iterator attr = THIS->attributes.begin(); attr != THIS->attributes.end(); ++attr) {
  110. (void)hv_store( hv, attr->first.c_str(), attr->first.length(), newSVpv(attr->second.c_str(), attr->second.length()), 0 );
  111. }
  112. RETVAL = (SV*)newRV_noinc((SV*)hv);
  113. OUTPUT:
  114. RETVAL
  115. %}
  116. };
  117. %name{Slic3r::Model::Object} class ModelObject {
  118. ModelVolumePtrs* volumes()
  119. %code%{ RETVAL = &THIS->volumes; %};
  120. ModelInstancePtrs* instances()
  121. %code%{ RETVAL = &THIS->instances; %};
  122. void invalidate_bounding_box();
  123. Clone<TriangleMesh> mesh();
  124. Clone<TriangleMesh> raw_mesh();
  125. Clone<BoundingBoxf3> instance_bounding_box(int idx)
  126. %code%{ RETVAL = THIS->instance_bounding_box(idx, true); %};
  127. Clone<BoundingBoxf3> bounding_box();
  128. %name{_add_volume} Ref<ModelVolume> add_volume(TriangleMesh* mesh)
  129. %code%{ RETVAL = THIS->add_volume(*mesh); %};
  130. Ref<ModelVolume> _add_volume_clone(ModelVolume* other)
  131. %code%{ RETVAL = THIS->add_volume(*other); %};
  132. void delete_volume(size_t idx);
  133. void clear_volumes();
  134. int volumes_count()
  135. %code%{ RETVAL = THIS->volumes.size(); %};
  136. Ref<ModelVolume> get_volume(int idx)
  137. %code%{ RETVAL = THIS->volumes.at(idx); %};
  138. bool move_volume_up(int idx)
  139. %code%{
  140. if (idx > 0 && idx < int(THIS->volumes.size())) {
  141. std::swap(THIS->volumes[idx-1], THIS->volumes[idx]);
  142. RETVAL = true;
  143. } else
  144. RETVAL = false;
  145. %};
  146. bool move_volume_down(int idx)
  147. %code%{
  148. if (idx >= 0 && idx + 1 < int(THIS->volumes.size())) {
  149. std::swap(THIS->volumes[idx+1], THIS->volumes[idx]);
  150. RETVAL = true;
  151. } else
  152. RETVAL = false;
  153. %};
  154. %name{_add_instance} Ref<ModelInstance> add_instance();
  155. Ref<ModelInstance> _add_instance_clone(ModelInstance* other)
  156. %code%{ RETVAL = THIS->add_instance(*other); %};
  157. void delete_last_instance();
  158. void clear_instances();
  159. int instances_count()
  160. %code%{ RETVAL = THIS->instances.size(); %};
  161. std::string name()
  162. %code%{ RETVAL = THIS->name; %};
  163. void set_name(std::string value)
  164. %code%{ THIS->name = value; %};
  165. std::string input_file()
  166. %code%{ RETVAL = THIS->input_file; %};
  167. void set_input_file(std::string value)
  168. %code%{ THIS->input_file = value; %};
  169. Ref<DynamicPrintConfig> config()
  170. %code%{ RETVAL = &const_cast<DynamicPrintConfig&>(THIS->config.get()); %};
  171. Ref<Model> model()
  172. %code%{ RETVAL = THIS->get_model(); %};
  173. Ref<Vec3d> origin_translation()
  174. %code%{ RETVAL = &THIS->origin_translation; %};
  175. void set_origin_translation(Vec3d* point)
  176. %code%{ THIS->origin_translation = *point; %};
  177. void ensure_on_bed();
  178. int materials_count() const;
  179. int facets_count();
  180. void center_around_origin();
  181. void translate(double x, double y, double z);
  182. void scale_xyz(Vec3d* versor)
  183. %code{% THIS->scale(*versor); %};
  184. void rotate(float angle, Vec3d* axis)
  185. %code{% THIS->rotate(angle, *axis); %};
  186. void mirror(Axis axis);
  187. };
  188. %name{Slic3r::Model::Volume} class ModelVolume {
  189. Ref<ModelObject> object()
  190. %code%{ RETVAL = THIS->get_object(); %};
  191. std::string name()
  192. %code%{ RETVAL = THIS->name; %};
  193. void set_name(std::string value)
  194. %code%{ THIS->name = value; %};
  195. t_model_material_id material_id();
  196. void set_material_id(t_model_material_id material_id)
  197. %code%{ THIS->set_material_id(material_id); %};
  198. Ref<ModelMaterial> material();
  199. Ref<DynamicPrintConfig> config()
  200. %code%{ RETVAL = &const_cast<DynamicPrintConfig&>(THIS->config.get()); %};
  201. Ref<TriangleMesh> mesh()
  202. %code%{ RETVAL = &THIS->mesh(); %};
  203. bool modifier()
  204. %code%{ RETVAL = THIS->is_modifier(); %};
  205. void set_modifier(bool modifier)
  206. %code%{ THIS->set_type(modifier ? ModelVolumeType::PARAMETER_MODIFIER : ModelVolumeType::MODEL_PART); %};
  207. bool model_part()
  208. %code%{ RETVAL = THIS->is_model_part(); %};
  209. bool support_enforcer()
  210. %code%{ RETVAL = THIS->is_support_enforcer(); %};
  211. void set_support_enforcer()
  212. %code%{ THIS->set_type(ModelVolumeType::SUPPORT_ENFORCER); %};
  213. bool support_blocker()
  214. %code%{ RETVAL = THIS->is_support_blocker(); %};
  215. void set_support_blocker()
  216. %code%{ THIS->set_type(ModelVolumeType::SUPPORT_BLOCKER); %};
  217. size_t split(unsigned int max_extruders);
  218. };
  219. %name{Slic3r::Model::Instance} class ModelInstance {
  220. Ref<ModelObject> object()
  221. %code%{ RETVAL = THIS->get_object(); %};
  222. Vec3d* rotation()
  223. %code%{ RETVAL = new Vec3d(THIS->get_rotation(X), THIS->get_rotation(Y), THIS->get_rotation(Z)); %};
  224. Vec3d* scaling_factor()
  225. %code%{ RETVAL = new Vec3d(THIS->get_scaling_factor(X), THIS->get_scaling_factor(Y), THIS->get_scaling_factor(Z)); %};
  226. Vec2d* offset()
  227. %code%{ RETVAL = new Vec2d(THIS->get_offset(X), THIS->get_offset(Y)); %};
  228. void set_rotation(double val)
  229. %code%{ THIS->set_rotation(Z, val); THIS->get_object()->invalidate_bounding_box(); %};
  230. void set_rotations(Vec3d *rotation)
  231. %code%{ THIS->set_rotation(*rotation); THIS->get_object()->invalidate_bounding_box(); %};
  232. void set_scaling_factor(double val)
  233. %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(); %};
  234. void set_scaling_factors(Vec3d *scale)
  235. %code%{ THIS->set_scaling_factor(*scale); THIS->get_object()->invalidate_bounding_box(); %};
  236. void set_offset(Vec2d *offset)
  237. %code%{
  238. THIS->set_offset(X, (*offset)(0));
  239. THIS->set_offset(Y, (*offset)(1));
  240. %};
  241. void transform_mesh(TriangleMesh* mesh, bool dont_translate = false) const;
  242. void transform_polygon(Polygon* polygon) const;
  243. };