Prepare Drag&drop svg file

This commit is contained in:
Filip Sykala - NTB T15p 2023-06-13 14:33:00 +02:00
parent 1c80d4358a
commit 4db49f7882
2 changed files with 44 additions and 9 deletions

View File

@ -68,15 +68,17 @@ std::string choose_svg_file();
/// Let user to choose file with (S)calable (V)ector (G)raphics - SVG. /// Let user to choose file with (S)calable (V)ector (G)raphics - SVG.
/// Than let select contour /// Than let select contour
/// </summary> /// </summary>
/// <param name="filepath">SVG file path</param>
/// <returns>EmbossShape to create</returns> /// <returns>EmbossShape to create</returns>
EmbossShape select_shape(); EmbossShape select_shape(std::string_view filepath = "");
/// <summary> /// <summary>
/// Create new embos data /// Create new embos data
/// </summary> /// </summary>
/// <param name="cancel">Cancel for previous job</param> /// <param name="cancel">Cancel for previous job</param>
/// <param name="filepath">SVG file path</param>
/// <returns>Base data for emboss SVG</returns> /// <returns>Base data for emboss SVG</returns>
DataBasePtr create_emboss_data_base(std::shared_ptr<std::atomic<bool>> &cancel); DataBasePtr create_emboss_data_base(std::shared_ptr<std::atomic<bool>> &cancel, std::string_view filepath = "");
/// <summary> /// <summary>
/// Create symbol '?' as default shape /// Create symbol '?' as default shape
@ -171,6 +173,13 @@ bool GLGizmoSVG::create_volume(ModelVolumeType volume_type)
return start_create_volume_without_position(input, std::move(base)); return start_create_volume_without_position(input, std::move(base));
} }
bool GLGizmoSVG::create_volume(std::string_view svg_file, ModelVolumeType volume_type, const Vec2d &mouse_pos)
{
CreateVolumeParams input = create_input(m_parent, m_raycast_manager, volume_type);
DataBasePtr base = create_emboss_data_base(m_job_cancel, svg_file);
return start_create_volume(input, std::move(base), mouse_pos);
}
bool GLGizmoSVG::is_svg(const ModelVolume &volume) { bool GLGizmoSVG::is_svg(const ModelVolume &volume) {
return volume.emboss_shape.has_value(); return volume.emboss_shape.has_value();
} }
@ -656,6 +665,7 @@ void GLGizmoSVG::draw_window()
if (ImGui::Button("change file")) { if (ImGui::Button("change file")) {
m_volume_shape.shapes_with_ids = select_shape().shapes_with_ids; m_volume_shape.shapes_with_ids = select_shape().shapes_with_ids;
init_texture(m_texture, *m_volume);
// TODO: use setted scale // TODO: use setted scale
process(); process();
} }
@ -1090,15 +1100,26 @@ ExPolygons default_shape()
return shape; return shape;
} }
EmbossShape select_shape() namespace {
void translate(ExPolygons &expolys, const Point &p) {
for (ExPolygon &expoly : expolys)
expoly.translate(p);
}
}
EmbossShape select_shape(std::string_view filepath)
{ {
EmbossShape shape; EmbossShape shape;
shape.projection.depth = 10.; shape.projection.depth = 10.;
shape.projection.use_surface = false; shape.projection.use_surface = false;
shape.svg_file_path = choose_svg_file(); if (filepath.empty()) {
if (shape.svg_file_path.empty()) shape.svg_file_path = choose_svg_file();
return {}; if (shape.svg_file_path.empty())
return {};
} else {
shape.svg_file_path = filepath; // copy
}
const char *unit_mm{"mm"}; const char *unit_mm{"mm"};
// common used DPI is 96 or 72 // common used DPI is 96 or 72
@ -1113,20 +1134,25 @@ EmbossShape select_shape()
float scale = static_cast<float>(1 / shape.scale); float scale = static_cast<float>(1 / shape.scale);
bool is_y_negative = true; bool is_y_negative = true;
ExPolygons expoly = to_expolygons(image, tesselation_tolerance, max_level, scale, is_y_negative); ExPolygons expoly = to_expolygons(image, tesselation_tolerance, max_level, scale, is_y_negative);
// Must contain some shapes !!! // Must contain some shapes !!!
if (expoly.empty()) if (expoly.empty())
expoly = default_shape(); expoly = default_shape();
// SVG is used as centered
// Do not disturb user by settings of pivot position
BoundingBox bb = get_extents(expoly);
translate(expoly, -bb.center());
unsigned id = 0; unsigned id = 0;
shape.shapes_with_ids = {{id, expoly}}; shape.shapes_with_ids = {{id, expoly}};
return shape; return shape;
} }
DataBasePtr create_emboss_data_base(std::shared_ptr<std::atomic<bool>> &cancel) DataBasePtr create_emboss_data_base(std::shared_ptr<std::atomic<bool>> &cancel, std::string_view filepath)
{ {
EmbossShape shape = select_shape(); EmbossShape shape = select_shape(filepath);
if (shape.shapes_with_ids.empty()) if (shape.shapes_with_ids.empty())
// canceled selection of SVG file // canceled selection of SVG file

View File

@ -55,6 +55,15 @@ public:
/// <returns>True on succesfull start creation otherwise False</returns> /// <returns>True on succesfull start creation otherwise False</returns>
bool create_volume(ModelVolumeType volume_type); // first open file dialog bool create_volume(ModelVolumeType volume_type); // first open file dialog
/// <summary>
/// Create volume from already selected svg file
/// </summary>
/// <param name="svg_file">File path</param>
/// <param name="volume_type">Object part / Negative volume / Modifier</param>
/// <param name="mouse_pos">Position on screen where to create volume</param>
/// <returns>True on succesfull start creation otherwise False</returns>
bool create_volume(std::string_view svg_file, ModelVolumeType volume_type = ModelVolumeType::MODEL_PART, const Vec2d &mouse_pos = Vec2d(nan, nan));
/// <summary> /// <summary>
/// Check whether volume is object containing only emboss volume /// Check whether volume is object containing only emboss volume
/// </summary> /// </summary>