diff --git a/src/Mod/Sketcher/App/SketchObject.cpp b/src/Mod/Sketcher/App/SketchObject.cpp index d5c641f4f..40d7cf07b 100644 --- a/src/Mod/Sketcher/App/SketchObject.cpp +++ b/src/Mod/Sketcher/App/SketchObject.cpp @@ -3766,6 +3766,43 @@ int SketchObject::DeleteUnusedInternalGeometry(int GeoId, bool delgeoid) } } +bool SketchObject::ConvertToNURBS(int GeoId) +{ + if (GeoId < 0 || GeoId > getHighestCurveIndex()) + return -1; + + const Part::Geometry *geo = getGeometry(GeoId); + + if(geo->getTypeId() == Part::GeomPoint::getClassTypeId()) + return -1; + + const Part::GeomCurve *geo1 = static_cast(geo); + + Part::GeomBSplineCurve* bspline; + + try { + bspline = geo1->toNurbs(geo1->getFirstParameter(), geo1->getLastParameter()); + } + catch (const Base::Exception& e) { + Base::Console().Error("%s\n", e.what()); + // revert to original values + return false; + } + + const std::vector< Part::Geometry * > &vals = getInternalGeometry(); + + std::vector< Part::Geometry * > newVals(vals); + + newVals[GeoId] = bspline; + + Geometry.setValues(newVals); + Constraints.acceptGeometry(getCompleteGeometry()); + rebuildVertexIndex(); + + return true; + +} + int SketchObject::addExternal(App::DocumentObject *Obj, const char* SubName) { // so far only externals to the support of the sketch and datum features diff --git a/src/Mod/Sketcher/App/SketchObject.h b/src/Mod/Sketcher/App/SketchObject.h index 612d701b6..09f20c754 100644 --- a/src/Mod/Sketcher/App/SketchObject.h +++ b/src/Mod/Sketcher/App/SketchObject.h @@ -196,6 +196,8 @@ public: \retval int - returns -1 on error, otherwise the number of deleted elements */ int DeleteUnusedInternalGeometry(int GeoId, bool delgeoid=false); + + bool ConvertToNURBS(int GeoId); /// retrieves for a Vertex number the corresponding GeoId and PosId void getGeoVertexIndex(int VertexId, int &GeoId, PointPos &PosId) const; diff --git a/src/Mod/Sketcher/Gui/CommandSketcherBSpline.cpp b/src/Mod/Sketcher/Gui/CommandSketcherBSpline.cpp index 0bec9be20..f7cf1c7e2 100644 --- a/src/Mod/Sketcher/Gui/CommandSketcherBSpline.cpp +++ b/src/Mod/Sketcher/Gui/CommandSketcherBSpline.cpp @@ -296,7 +296,63 @@ bool CmdSketcherCompBSplineShowHideGeometryInformation::isActive(void) return isSketcherBSplineActive( getActiveGuiDocument(), false ); } +// Convert to NURB +DEF_STD_CMD_A(CmdSketcherConvertToNURB); +CmdSketcherConvertToNURB::CmdSketcherConvertToNURB() +:Command("Sketcher_BSplineConvertToNURB") +{ + sAppModule = "Sketcher"; + sGroup = QT_TR_NOOP("Sketcher"); + sMenuText = QT_TR_NOOP("Convert Geometry to B-Spline"); + sToolTipText = QT_TR_NOOP("Converts the given Geometry to a B-Spline"); + sWhatsThis = "Sketcher_ConvertToNURB"; + sStatusTip = sToolTipText; + sPixmap = "Sketcher_ConvertToNURB"; + sAccel = ""; + eType = ForEdit; +} + +void CmdSketcherConvertToNURB::activated(int iMsg) +{ + Q_UNUSED(iMsg); + + // get the selection + std::vector selection = getSelection().getSelectionEx(); + + // only one sketch with its subelements are allowed to be selected + if (selection.size() != 1) { + return; + } + + // get the needed lists and objects + const std::vector &SubNames = selection[0].getSubNames(); + Sketcher::SketchObject* Obj = static_cast(selection[0].getObject()); + + for (unsigned int i=0; i 4 && SubNames[i].substr(0,4) == "Edge") { + + int GeoId = std::atoi(SubNames[i].substr(4,4000).c_str()) - 1; + + Obj->ConvertToNURBS(GeoId); + } + } + + ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Mod/Sketcher"); + bool autoRecompute = hGrp->GetBool("AutoRecompute",false); + + if (autoRecompute) + Gui::Command::updateActive(); + else + Obj->solve(); + +} + +bool CmdSketcherConvertToNURB::isActive(void) +{ + return isSketcherBSplineActive( getActiveGuiDocument(), false ); +} void CreateSketcherCommandsBSpline(void) @@ -307,4 +363,5 @@ void CreateSketcherCommandsBSpline(void) rcCmdMgr.addCommand(new CmdSketcherBSplinePolygon()); rcCmdMgr.addCommand(new CmdSketcherBSplineComb()); rcCmdMgr.addCommand(new CmdSketcherCompBSplineShowHideGeometryInformation()); + rcCmdMgr.addCommand(new CmdSketcherConvertToNURB()); } diff --git a/src/Mod/Sketcher/Gui/Workbench.cpp b/src/Mod/Sketcher/Gui/Workbench.cpp index 3cb6eddad..f36926558 100644 --- a/src/Mod/Sketcher/Gui/Workbench.cpp +++ b/src/Mod/Sketcher/Gui/Workbench.cpp @@ -285,12 +285,14 @@ template <> inline void SketcherAddWorkbenchBSplines(Gui::MenuItem& bspline){ bspline << "Sketcher_BSplineDegree" << "Sketcher_BSplinePolygon" - << "Sketcher_BSplineComb"; + << "Sketcher_BSplineComb" + << "Sketcher_BSplineConvertToNURB"; } template <> inline void SketcherAddWorkbenchBSplines(Gui::ToolBarItem& bspline){ - bspline << "Sketcher_CompBSplineShowHideGeometryInformation"; + bspline << "Sketcher_CompBSplineShowHideGeometryInformation" + << "Sketcher_BSplineConvertToNURB"; } template