Model.xsp 12 KB

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