From 325add58dbd59e7a4626dcc53227d042e16c65a0 Mon Sep 17 00:00:00 2001 From: wmayer Date: Thu, 16 May 2019 16:16:03 +0200 Subject: [PATCH] add option to disable approximation of splines when creating curve on mesh --- src/Mod/MeshPart/Gui/CurveOnMesh.cpp | 62 ++++++++++++++++++++++-- src/Mod/MeshPart/Gui/CurveOnMesh.h | 5 ++ src/Mod/MeshPart/Gui/PreCompiled.h | 3 ++ src/Mod/MeshPart/Gui/TaskCurveOnMesh.cpp | 2 + src/Mod/MeshPart/Gui/TaskCurveOnMesh.ui | 3 ++ 5 files changed, 72 insertions(+), 3 deletions(-) diff --git a/src/Mod/MeshPart/Gui/CurveOnMesh.cpp b/src/Mod/MeshPart/Gui/CurveOnMesh.cpp index 78565ec17a..7bcaf079ab 100644 --- a/src/Mod/MeshPart/Gui/CurveOnMesh.cpp +++ b/src/Mod/MeshPart/Gui/CurveOnMesh.cpp @@ -40,6 +40,7 @@ # include # include # include +# include #endif #include "CurveOnMesh.h" @@ -77,6 +78,7 @@ #include #include #include +#include /* XPM */ static const char *cursor_curveonmesh[]={ @@ -241,6 +243,7 @@ public: : wireClosed(false) , distance(1.0) , cosAngle(0.7071) // 45 degree + , approximate(true) , curve(new ViewProviderCurveOnMesh) , mesh(0) , grid(0) @@ -321,6 +324,7 @@ public: bool wireClosed; double distance; double cosAngle; + bool approximate; ViewProviderCurveOnMesh* curve; Gui::ViewProviderDocumentObject* mesh; MeshCore::MeshFacetGrid* grid; @@ -340,6 +344,11 @@ CurveOnMeshHandler::~CurveOnMeshHandler() disableCallback(); } +void CurveOnMeshHandler::enableApproximation(bool on) +{ + d_ptr->approximate = on; +} + void CurveOnMeshHandler::setParameters(int maxDegree, GeomAbs_Shape cont, double tol3d, double angle) { d_ptr->par.maxDegree = maxDegree; @@ -364,9 +373,16 @@ void CurveOnMeshHandler::onCreate() { for (auto it = d_ptr->cutLines.begin(); it != d_ptr->cutLines.end(); ++it) { std::vector segm = d_ptr->convert(*it); - Handle(Geom_BSplineCurve) spline = approximateSpline(segm); - if (!spline.IsNull()) - displaySpline(spline); + if (d_ptr->approximate) { + Handle(Geom_BSplineCurve) spline = approximateSpline(segm); + if (!spline.IsNull()) + displaySpline(spline); + } + else { + TopoDS_Wire wire; + if (makePolyline(segm, wire)) + displayPolyline(wire); + } } d_ptr->curve->clearVertex(); @@ -509,8 +525,39 @@ void CurveOnMeshHandler::displaySpline(const Handle(Geom_BSplineCurve)& spline) Gui::View3DInventorViewer* view3d = d_ptr->viewer->getViewer(); App::Document* doc = view3d->getDocument()->getDocument(); + doc->openTransaction("Add spline"); Part::Feature* part = static_cast(doc->addObject("Part::Spline", "Spline")); part->Shape.setValue(edge); + doc->commitTransaction(); + } +} + +bool CurveOnMeshHandler::makePolyline(const std::vector& points, TopoDS_Wire& wire) +{ + BRepBuilderAPI_MakePolygon mkPoly; + for (std::vector::const_iterator it = points.begin(); it != points.end(); ++it) { + float x,y,z; + it->getValue(x,y,z); + mkPoly.Add(gp_Pnt(x,y,z)); + } + + if (mkPoly.IsDone()) { + wire = mkPoly.Wire(); + return true; + } + + return false; +} + +void CurveOnMeshHandler::displayPolyline(const TopoDS_Wire& wire) +{ + if (d_ptr->viewer) { + Gui::View3DInventorViewer* view3d = d_ptr->viewer->getViewer(); + App::Document* doc = view3d->getDocument()->getDocument(); + doc->openTransaction("Add polyline"); + Part::Feature* part = static_cast(doc->addObject("Part::Feature", "Polyline")); + part->Shape.setValue(wire); + doc->commitTransaction(); } } @@ -614,4 +661,13 @@ void CurveOnMeshHandler::Private::vertexCallback(void * ud, SoEventCallback * n) } } +void CurveOnMeshHandler::recomputeDocument() +{ + if (d_ptr->viewer) { + Gui::View3DInventorViewer* view3d = d_ptr->viewer->getViewer(); + App::Document* doc = view3d->getDocument()->getDocument(); + doc->recompute(); + } +} + #include "moc_CurveOnMesh.cpp" diff --git a/src/Mod/MeshPart/Gui/CurveOnMesh.h b/src/Mod/MeshPart/Gui/CurveOnMesh.h index d6b7c84237..b0dc032153 100644 --- a/src/Mod/MeshPart/Gui/CurveOnMesh.h +++ b/src/Mod/MeshPart/Gui/CurveOnMesh.h @@ -33,6 +33,7 @@ class SbVec3f; class SoCoordinate3; class SoDrawStyle; class TopoDS_Edge; +class TopoDS_Wire; namespace Gui { class View3DInventor; @@ -69,14 +70,18 @@ class CurveOnMeshHandler : public QObject public: CurveOnMeshHandler(QObject* parent = 0); ~CurveOnMeshHandler(); + void enableApproximation(bool); void setParameters(int maxDegree, GeomAbs_Shape cont, double tol3d, double angle); void enableCallback(Gui::View3DInventor* viewer); void disableCallback(); + void recomputeDocument(); private: Handle(Geom_BSplineCurve) approximateSpline(const std::vector& points); void approximateEdge(const TopoDS_Edge&, double tolerance); void displaySpline(const Handle(Geom_BSplineCurve)&); + bool makePolyline(const std::vector& points, TopoDS_Wire& wire); + void displayPolyline(const TopoDS_Wire& wire); std::vector getPoints() const; std::vector getVertexes() const; void closeWire(); diff --git a/src/Mod/MeshPart/Gui/PreCompiled.h b/src/Mod/MeshPart/Gui/PreCompiled.h index 10c7b54fd8..7cef9ef237 100644 --- a/src/Mod/MeshPart/Gui/PreCompiled.h +++ b/src/Mod/MeshPart/Gui/PreCompiled.h @@ -80,6 +80,9 @@ # include #endif +// OCCT +#include + #endif //_PreComp_ #endif // __PRECOMPILED_GUI__ diff --git a/src/Mod/MeshPart/Gui/TaskCurveOnMesh.cpp b/src/Mod/MeshPart/Gui/TaskCurveOnMesh.cpp index be2638360c..ae2f0e64bb 100644 --- a/src/Mod/MeshPart/Gui/TaskCurveOnMesh.cpp +++ b/src/Mod/MeshPart/Gui/TaskCurveOnMesh.cpp @@ -81,6 +81,7 @@ void CurveOnMeshWidget::changeEvent(QEvent *e) void CurveOnMeshWidget::on_startButton_clicked() { int cont = ui->continuity->itemData(ui->continuity->currentIndex()).toInt(); + myCurveHandler->enableApproximation(ui->groupBox_2->isChecked()); myCurveHandler->setParameters(ui->maxDegree->currentIndex(), static_cast(cont), ui->meshTolerance->value(), @@ -90,6 +91,7 @@ void CurveOnMeshWidget::on_startButton_clicked() void CurveOnMeshWidget::reject() { + myCurveHandler->recomputeDocument(); } // ---------------------------------------------------------------------------- diff --git a/src/Mod/MeshPart/Gui/TaskCurveOnMesh.ui b/src/Mod/MeshPart/Gui/TaskCurveOnMesh.ui index ae02a77ce8..3196bfb838 100644 --- a/src/Mod/MeshPart/Gui/TaskCurveOnMesh.ui +++ b/src/Mod/MeshPart/Gui/TaskCurveOnMesh.ui @@ -68,6 +68,9 @@ Spline Approximation + + true +