Skip to content

Commit

Permalink
Hopefully a fix of prusa3d/PrusaSlicer#11
Browse files Browse the repository at this point in the history
Replaced eval { die } construct with a bool return value indicating
success or failure of an automatic arrangement of parts on the print bed.

Don't know exactly what is happening here, but throwing a "die" inside
a XS function and then catching it inside an eval {} block is suspcious.

Conflicts:

	xs/src/libslic3r/Geometry.cpp
	xs/src/libslic3r/Geometry.hpp
  • Loading branch information
bubnikv authored and alranel committed Nov 24, 2016
1 parent 954e2d3 commit a5135f4
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 327 deletions.
4 changes: 1 addition & 3 deletions lib/Slic3r/GUI/Plater.pm
Original file line number Diff line number Diff line change
Expand Up @@ -918,9 +918,7 @@ sub arrange {
$self->pause_background_process;

my $bb = Slic3r::Geometry::BoundingBoxf->new_from_points($self->{config}->bed_shape);
eval {
$self->{model}->arrange_objects($self->GetFrame->config->min_object_distance, $bb);
};
my $success = $self->{model}->arrange_objects($self->GetFrame->config->min_object_distance, $bb);
# ignore arrange failures on purpose: user has visual feedback and we don't need to warn him
# when parts don't fit in print bed

Expand Down
12 changes: 6 additions & 6 deletions xs/src/libslic3r/ExtrusionEntity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,22 +87,22 @@ class ExtrusionPath : public ExtrusionEntity
return this->role == erPerimeter
|| this->role == erExternalPerimeter
|| this->role == erOverhangPerimeter;
}
};
bool is_infill() const {
return this->role == erBridgeInfill
|| this->role == erInternalInfill
|| this->role == erSolidInfill
|| this->role == erTopSolidInfill;
}
};
bool is_solid_infill() const {
return this->role == erBridgeInfill
|| this->role == erSolidInfill
|| this->role == erTopSolidInfill;
}
};
bool is_bridge() const {
return this->role == erBridgeInfill
|| this->role == erOverhangPerimeter;
}
};
// Produce a list of 2D polygons covered by the extruded path.
Polygons grow() const;
// Minimum volumetric velocity of this extrusion entity. Used by the constant nozzle pressure algorithm.
Expand Down Expand Up @@ -148,13 +148,13 @@ class ExtrusionLoop : public ExtrusionEntity
return this->paths.front().role == erPerimeter
|| this->paths.front().role == erExternalPerimeter
|| this->paths.front().role == erOverhangPerimeter;
}
};
bool is_infill() const {
return this->paths.front().role == erBridgeInfill
|| this->paths.front().role == erInternalInfill
|| this->paths.front().role == erSolidInfill
|| this->paths.front().role == erTopSolidInfill;
}
};
bool is_solid_infill() const {
return this->paths.front().role == erBridgeInfill
|| this->paths.front().role == erSolidInfill
Expand Down
316 changes: 22 additions & 294 deletions xs/src/libslic3r/Geometry.cpp

Large diffs are not rendered by default.

18 changes: 5 additions & 13 deletions xs/src/libslic3r/Geometry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,12 @@ double rad2deg(double angle);
double rad2deg_dir(double angle);
double deg2rad(double angle);

class ArrangeItem {
public:
Pointf pos;
size_t index_x, index_y;
coordf_t dist;
};
class ArrangeItemIndex {
public:
coordf_t index;
ArrangeItem item;
ArrangeItemIndex(coordf_t _index, ArrangeItem _item) : index(_index), item(_item) {};
};
double linint(double value, double oldmin, double oldmax, double newmin, double newmax);
Pointfs arrange(size_t total_parts, Pointf part, coordf_t dist, const BoundingBoxf* bb);
bool arrange(
// input
size_t num_parts, const Pointf &part_size, coordf_t gap, const BoundingBoxf* bed_bounding_box,
// output
Pointfs &positions);

class MedialAxis {
public:
Expand Down
18 changes: 12 additions & 6 deletions xs/src/libslic3r/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,21 +224,22 @@ Model::raw_mesh() const
return mesh;
}

Pointfs
Model::_arrange(const Pointfs &sizes, coordf_t dist, const BoundingBoxf* bb) const
bool
Model::_arrange(const Pointfs &sizes, coordf_t dist, const BoundingBoxf* bb, Pointfs &out) const
{
// we supply unscaled data to arrange()
return Slic3r::Geometry::arrange(
sizes.size(), // number of parts
BoundingBoxf(sizes).max, // width and height of a single cell
dist, // distance between cells
bb // bounding box of the area to fill
bb, // bounding box of the area to fill
out // output positions
);
}

/* arrange objects preserving their instance count
but altering their instance positions */
void
bool
Model::arrange_objects(coordf_t dist, const BoundingBoxf* bb)
{
// get the (transformed) size of each instance so that we take
Expand All @@ -250,7 +251,9 @@ Model::arrange_objects(coordf_t dist, const BoundingBoxf* bb)
}
}

Pointfs positions = this->_arrange(instance_sizes, dist, bb);
Pointfs positions;
if (! this->_arrange(instance_sizes, dist, bb, positions))
return false;

for (ModelObjectPtrs::const_iterator o = this->objects.begin(); o != this->objects.end(); ++o) {
for (ModelInstancePtrs::const_iterator i = (*o)->instances.begin(); i != (*o)->instances.end(); ++i) {
Expand All @@ -259,14 +262,17 @@ Model::arrange_objects(coordf_t dist, const BoundingBoxf* bb)
}
(*o)->invalidate_bounding_box();
}
return true;
}

/* duplicate the entire model preserving instance relative positions */
void
Model::duplicate(size_t copies_num, coordf_t dist, const BoundingBoxf* bb)
{
Pointfs model_sizes(copies_num-1, this->bounding_box().size());
Pointfs positions = this->_arrange(model_sizes, dist, bb);
Pointfs positions;
if (! this->_arrange(model_sizes, dist, bb, positions))
CONFESS("Cannot duplicate part as the resulting objects would not fit on the print bed.\n");

// note that this will leave the object count unaltered

Expand Down
5 changes: 3 additions & 2 deletions xs/src/libslic3r/Model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ class Model
void translate(coordf_t x, coordf_t y, coordf_t z);
TriangleMesh mesh() const;
TriangleMesh raw_mesh() const;
Pointfs _arrange(const Pointfs &sizes, coordf_t dist, const BoundingBoxf* bb = NULL) const;
void arrange_objects(coordf_t dist, const BoundingBoxf* bb = NULL);
bool _arrange(const Pointfs &sizes, coordf_t dist, const BoundingBoxf* bb, Pointfs &out) const;
bool arrange_objects(coordf_t dist, const BoundingBoxf* bb = NULL);
// Croaks if the duplicated objects do not fit the print bed.
void duplicate(size_t copies_num, coordf_t dist, const BoundingBoxf* bb = NULL);
void duplicate_objects(size_t copies_num, coordf_t dist, const BoundingBoxf* bb = NULL);
void duplicate_objects_grid(size_t x, size_t y, coordf_t dist);
Expand Down
7 changes: 6 additions & 1 deletion xs/xsp/Geometry.xsp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
%package{Slic3r::Geometry};

Pointfs arrange(size_t total_parts, Pointf* part, coordf_t dist, BoundingBoxf* bb = NULL)
%code{% RETVAL = Slic3r::Geometry::arrange(total_parts, *part, dist, bb); %};
%code{%
Pointfs points;
if (! Slic3r::Geometry::arrange(total_parts, *part, dist, bb, points))
CONFESS("%zu parts won't fit in your print area!\n", total_parts);
RETVAL = points;
%};

%{

Expand Down
3 changes: 1 addition & 2 deletions xs/xsp/Model.xsp
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@
ModelObjectPtrs* objects()
%code%{ RETVAL = &THIS->objects; %};

Pointfs _arrange(Pointfs sizes, double dist, BoundingBoxf* bb = NULL);
void arrange_objects(double dist, BoundingBoxf* bb = NULL);
bool arrange_objects(double dist, BoundingBoxf* bb = NULL);
void duplicate(unsigned int copies_num, double dist, BoundingBoxf* bb = NULL);
void duplicate_objects(unsigned int copies_num, double dist, BoundingBoxf* bb = NULL);
void duplicate_objects_grid(unsigned int x, unsigned int y, double dist);
Expand Down

0 comments on commit a5135f4

Please sign in to comment.