Work Out the Solid Model's Flat Face Cut-Off From Each Object's Layer Height and Walls

This commit is contained in:
Hanif Koh authored and HanifKoh committed 2026-11-29 03:10:00 +08:00
1 parent dac25fd5b0
commit 5131f4fc8e
7 files changed
+99 -32

No files matched your search

+53 -4
View File
@@ -74,6 +74,8 @@
#include <wx/string.h>
#include <wx/slider.h>
#include "libslic3r/PrintConfig.hpp"
#include "libslic3r/Flow.hpp"
#include "libslic3r/Exception.hpp"
#include "slic3r/GUI/Event.hpp"
#include <string_view>
#include <functional>
@@ -2021,6 +2023,7 @@ void GCodeViewer::set_shell_transparency(float alpha) { m_shells.volumes.set_tra
void GCodeViewer::reset_shell()
{
m_shells.volumes.clear();
m_shells.flat_face_normal_z.clear();
m_shells.print_id = -1;
m_shells.with_wipe_tower = false;
m_shell_bounding_box = BoundingBoxf3();
@@ -3432,6 +3435,46 @@ void GCodeViewer::export_toolpaths_to_obj(const char* filename) const
exporter.export_to(filename);
}
// The world normal z past which a face of the object counts as flat in the solid model. The strip a
// layer leaves uncovered by the next is the layer height over the slope's tangent wide, and it
// prints as a top or bottom surface only where it is wider than the walls that follow the outline.
// The thinnest layer and the widest walls are taken, so that under a variable layer height a slope
// that prints as walls on some of its layers keeps the walls' colour. Read from the configuration
// and the model, which the background slicing does not change, and loaded whatever the drag mode,
// so a configuration the slicer would reject falls back to the default rather than throwing.
static float flat_face_normal_z(const PrintObject& object)
{
const float fallback = GLVolumeCollection::FlatFaces().normal_z;
double layer_height = object.config().layer_height.value;
const ModelObject& model_object = *object.model_object();
// the profile holds pairs of z and layer height
const std::vector<double> profile = model_object.layer_height_profile.get();
for (size_t i = 1; i < profile.size(); i += 2)
if (profile[i] > 0.)
layer_height = std::min(layer_height, profile[i]);
for (const auto& [range, config] : model_object.layer_config_ranges)
if (const ConfigOption* height = config.option("layer_height"); height != nullptr && height->getFloat() > 0.)
layer_height = std::min(layer_height, height->getFloat());
if (layer_height <= 0.)
return fallback;
double walls = 0.;
try {
for (const PrintRegion& region : object.all_regions()) {
// only_one_wall_top is left out: it drops the inner walls only where the top surface is
// wider than min_width_top_surface, which a slope's strip seldom is, and never over a bridge
const int loops = std::max(1, region.config().wall_loops.value);
const double outer = region.flow(object, frExternalPerimeter, layer_height).width();
const double inner = region.flow(object, frPerimeter, layer_height).spacing();
walls = std::max(walls, outer + (loops - 1) * inner);
}
} catch (const InvalidArgument&) {
return fallback;
}
if (walls <= 0.)
return fallback;
return static_cast<float>(walls / std::hypot(walls, layer_height));
}
void GCodeViewer::load_shells(const Print& print, bool initialized, bool force_previewing)
{
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << boost::format(": initialized=%1%, force_previewing=%2%")%initialized %force_previewing;
@@ -3487,6 +3530,7 @@ void GCodeViewer::load_shells(const Print& print, bool initialized, bool force_p
size_t current_volumes_count = m_shells.volumes.volumes.size();
m_shells.volumes.load_object(model_obj, object_idx, instance_ids, "object", initialized, false);
m_shells.flat_face_normal_z.resize(m_shells.volumes.volumes.size(), flat_face_normal_z(*obj));
// adjust shells' z if raft is present
const SlicingParameters& slicing_parameters = obj->slicing_parameters();
@@ -3528,6 +3572,9 @@ void GCodeViewer::load_shells(const Print& print, bool initialized, bool force_p
while (true) {
GLVolumePtrs::iterator it = std::find_if(m_shells.volumes.volumes.begin(), m_shells.volumes.volumes.end(), [](GLVolume* volume) { return volume->is_modifier; });
if (it != m_shells.volumes.volumes.end()) {
// the cut-offs are kept by volume index, so each goes with its volume
if (const size_t index = std::distance(m_shells.volumes.volumes.begin(), it); index < m_shells.flat_face_normal_z.size())
m_shells.flat_face_normal_z.erase(m_shells.flat_face_normal_z.begin() + index);
delete (*it);
m_shells.volumes.volumes.erase(it);
}
@@ -3932,7 +3979,7 @@ void GCodeViewer::render_solid_model(int canvas_width, int canvas_height)
own_colors.reserve(m_shells.volumes.volumes.size());
// the tower draws itself in its own colours and leaves the volume's at the default; one list per volume
std::vector<std::vector<ColorRGBA>> own_tower_colors(m_shells.volumes.volumes.size());
std::vector<std::array<std::optional<ColorRGBA>, 2>> flat_face_colors(m_shells.volumes.volumes.size());
std::vector<GLVolumeCollection::FlatFaces> flat_faces(m_shells.volumes.volumes.size());
for (size_t k = 0; k < m_shells.volumes.volumes.size(); ++k) {
GLVolume* volume = m_shells.volumes.volumes[k];
own_colors.push_back(volume->color);
@@ -3946,10 +3993,12 @@ void GCodeViewer::render_solid_model(int canvas_width, int canvas_height)
volume->set_render_color();
for (size_t f = 0; f < 2; ++f) {
if (const std::optional<ColorRGBA>& face = colors.bodies[k].faces[f]; face.has_value())
flat_face_colors[k][f] = shade(*face);
flat_faces[k].colors[f] = shade(*face);
}
if (k < m_shells.flat_face_normal_z.size())
flat_faces[k].normal_z = m_shells.flat_face_normal_z[k];
}
m_shells.volumes.set_flat_face_colors(std::move(flat_face_colors));
m_shells.volumes.set_flat_faces(std::move(flat_faces));
m_shells.volumes.set_z_range(z_bottom, z_top);
// the section view cuts the solid as it cuts the toolpaths
m_shells.volumes.set_clipping_plane({ m_clipping_plane[0], m_clipping_plane[1], m_clipping_plane[2], m_clipping_plane[3] });
@@ -4034,7 +4083,7 @@ void GCodeViewer::render_solid_model(int canvas_width, int canvas_height)
}
m_shells.volumes.set_z_range(-FLT_MAX, FLT_MAX);
m_shells.volumes.set_flat_face_colors({});
m_shells.volumes.set_flat_faces({});
for (size_t k = 0; k < m_shells.volumes.volumes.size(); ++k) {
GLVolume* volume = m_shells.volumes.volumes[k];
if (auto* tower = dynamic_cast<GLWipeTowerVolume*>(volume); tower != nullptr)