Model.xsp 10.0 KB

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