Surface: several improvements

* fix crash because of incomplete inheritance of BlendPoint and BlendCurve -> do not inherit from BaseClass
  as consequebce make destructor non-virtual and remove getPyObject
* pass std::vector by const reference
* remove unneeded includes
* use more suitable Python exception types
* when returning with null from Python handler then set an exception
* harmonize file guards
* several optimizations or simplifications
* use modern C++
* harmonize command name
* fix copyright note
This commit is contained in:
wmayer
2022-08-23 16:46:22 +02:00
parent 3b839551da
commit c2b9eceec1
17 changed files with 99 additions and 145 deletions

View File

@@ -26,7 +26,6 @@
#include <Base/PyObjectBase.h>
#include "Blending/BlendCurvePy.h"
#include "Blending/BlendPoint.h"
#include "Blending/BlendPointPy.h"
#include "Blending/FeatureBlendCurve.h"
#include "FeatureCut.h"
@@ -65,8 +64,6 @@ PyObject *initModule()
/* Python entry */
PyMOD_INIT_FUNC(Surface)
{
try {
Base::Interpreter().runString("import Part");
}
@@ -79,6 +76,7 @@ PyMOD_INIT_FUNC(Surface)
Base::Console().Log("Loading Surface module... done\n");
Base::Interpreter().addType(&Surface::BlendPointPy::Type, mod, "BlendPoint");
Base::Interpreter().addType(&Surface::BlendCurvePy::Type, mod, "BlendCurve");
// Add types to module
Surface::Filling ::init();
Surface::Sewing ::init();
@@ -87,8 +85,6 @@ PyMOD_INIT_FUNC(Surface)
Surface::Extend ::init();
Surface::FeatureBlendCurve ::init();
Surface::Sections ::init();
Surface::BlendPoint ::init();
Surface::BlendCurve ::init();
PyMOD_Return(mod);
}

View File

@@ -26,7 +26,6 @@
#include <BSplCLib.hxx>
#include <Geom_BezierCurve.hxx>
#include <Precision.hxx>
#include <Standard_Version.hxx>
#include <TColStd_Array1OfReal.hxx>
#include <TColgp_Array1OfPnt.hxx>
#include <gp_Pnt.hxx>
@@ -40,17 +39,13 @@
using namespace Surface;
BlendCurve::BlendCurve()
{
}
BlendCurve::BlendCurve(std::vector<BlendPoint> blendPointsList)
BlendCurve::BlendCurve(const std::vector<BlendPoint>& blendPointsList)
{
// Retrieve number of blendPoints and push them into blendPoints.
size_t nb_pts = blendPointsList.size();
if (nb_pts > 2) {
throw Base::ValueError("Not implemented");
throw Base::NotImplementedError("Not implemented");
}
else if (nb_pts < 2) {
throw Base::ValueError("Need two points for working");
@@ -58,10 +53,6 @@ BlendCurve::BlendCurve(std::vector<BlendPoint> blendPointsList)
blendPoints = blendPointsList;
}
BlendCurve::~BlendCurve()
{
}
Handle(Geom_BezierCurve) BlendCurve::compute()
{
size_t nb_pts = blendPoints.size();
@@ -127,9 +118,8 @@ Handle(Geom_BezierCurve) BlendCurve::compute()
Handle(Geom_BezierCurve) bezier = new Geom_BezierCurve(poles);
return bezier;
}
catch (Standard_Failure &e) {
PyErr_SetString(PyExc_Exception, "Failed to compute bezier curve");
catch (Standard_Failure &) {
PyErr_SetString(Base::PyExc_FC_CADKernelError, "Failed to compute bezier curve");
}
return nullptr;
}
@@ -146,6 +136,6 @@ void BlendCurve::setSize(int i, double f, bool relative)
blendPoints[i].setSize(size);
}
catch (Standard_Failure &e) {
PyErr_SetString(PyExc_Exception, e.GetMessageString());
PyErr_SetString(Base::PyExc_FC_CADKernelError, e.GetMessageString());
}
}

View File

@@ -20,12 +20,11 @@
* *
***************************************************************************/
#ifndef BLEND_CURVE_H
#define BLEND_CURVE_H
#ifndef SURFACE_BLEND_CURVE_H
#define SURFACE_BLEND_CURVE_H
#include <Geom_BezierCurve.hxx>
#include <Mod/Surface/SurfaceGlobal.h>
#include <Base/BaseClass.h>
#include <Mod/Surface/App/Blending/BlendPoint.h>
namespace Surface
@@ -33,18 +32,18 @@ namespace Surface
/*!
* Create a BezierCurve interpolating a list of BlendPoints
*/
class SurfaceExport BlendCurve: public Base::BaseClass
class SurfaceExport BlendCurve
{
public:
std::vector<BlendPoint> blendPoints;
BlendCurve();
BlendCurve() = default;
/*!
* Constructor
*\param std::vector<BlendPoint>
*/
BlendCurve(std::vector<BlendPoint> blendPointsList);
virtual ~BlendCurve();
BlendCurve(const std::vector<BlendPoint>& blendPointsList);
~BlendCurve() = default;
/*!
* Perform the interpolate algorithm
*\return the BezierCurve
@@ -57,8 +56,6 @@ public:
*\param bool interpret new size relative to chordlength
*/
void setSize(int, double, bool);
virtual PyObject *getPyObject(void);
};
}// namespace Surface

View File

@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<GenerateModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="generateMetaModel_Module.xsd">
<PythonExport
Father="BaseClassPy"
Father="PyObjectBase"
FatherInclude="Base/BaseClassPy.h"
FatherNamespace="Base"
Name="BlendCurvePy"

View File

@@ -21,19 +21,16 @@
// ***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
#include <TColStd_Array1OfReal.hxx>
#include <TColgp_Array1OfPnt.hxx>
#endif
#include "Blending/BlendCurvePy.h"
#include "Blending/BlendPointPy.h"
#include "Blending/BlendCurvePy.cpp"
#include "Blending/BlendPointPy.h"
#include <Base/VectorPy.h>
#include <Mod/Part/App/BezierCurvePy.h>
using namespace Surface;
std::string BlendCurvePy::representation(void) const
std::string BlendCurvePy::representation() const
{
return "BlendCurve";
}
@@ -48,26 +45,29 @@ int BlendCurvePy::PyInit(PyObject *args, PyObject * /*kwds*/)
{
PyObject *b1;
PyObject *b2;
std::vector<BlendPoint> bpList;
if (PyArg_ParseTuple(args, "O!O!", &(Surface::BlendPointPy::Type), &b1, &(Surface::BlendPointPy::Type), &b2)) {
BlendPoint *geom1 = static_cast<BlendPointPy *>(b1)->getBlendPointPtr();
BlendPoint *geom2 = static_cast<BlendPointPy *>(b2)->getBlendPointPtr();
bpList.emplace_back(*geom1);
bpList.emplace_back(*geom2);
this->getBlendCurvePtr()->blendPoints = bpList;
return 0;
}
return -1;
if (!PyArg_ParseTuple(args, "O!O!", &(Surface::BlendPointPy::Type), &b1, &(Surface::BlendPointPy::Type), &b2))
return -1;
std::vector<BlendPoint> bpList;
BlendPoint *geom1 = static_cast<BlendPointPy *>(b1)->getBlendPointPtr();
BlendPoint *geom2 = static_cast<BlendPointPy *>(b2)->getBlendPointPtr();
bpList.emplace_back(*geom1);
bpList.emplace_back(*geom2);
this->getBlendCurvePtr()->blendPoints = bpList;
return 0;
}
PyObject *BlendCurvePy::compute(PyObject * /*args*/)
PyObject *BlendCurvePy::compute(PyObject * args)
{
if (!PyArg_ParseTuple(args, ""))
return nullptr;
BlendCurve *bc = getBlendCurvePtr();
Handle(Geom_BezierCurve) gc = bc->compute();
return new Part::BezierCurvePy(new Part::GeomBezierCurve(gc));
}
PyObject *BlendCurvePy::setSize(PyObject *args)
{
int i;
@@ -78,12 +78,12 @@ PyObject *BlendCurvePy::setSize(PyObject *args)
}
try {
getBlendCurvePtr()->setSize(i, size, Base::asBoolean(relative));
Py_Return;
}
catch (Standard_Failure &e) {
PyErr_SetString(PyExc_Exception, e.GetMessageString());
PyErr_SetString(Base::PyExc_FC_CADKernelError, e.GetMessageString());
return nullptr;
}
Py_Return;
}
PyObject *BlendCurvePy::getCustomAttributes(const char * /*attr*/) const

View File

@@ -23,12 +23,8 @@
#include "PreCompiled.h"
#ifndef _PreComp_
#include <Base/Persistence.h>
#include <Base/Vector3D.h>
#include <Precision.hxx>
#include <Standard_Version.hxx>
#include <TopoDS.hxx>
#include <gp_Pnt.hxx>
#include <Standard_Real.hxx>
#endif
#include "Blending/BlendPoint.h"
#include "Blending/BlendPointPy.h"
@@ -36,11 +32,9 @@
using namespace Surface;
BlendPoint::BlendPoint(std::vector<Base::Vector3d> vectorList)
BlendPoint::BlendPoint(const std::vector<Base::Vector3d>& vectorList)
: vectors{vectorList}
{
for (size_t i = 0; i < vectorList.size(); i++) {
vectors.emplace_back(vectorList[i]);
}
}
BlendPoint::BlendPoint()
@@ -48,10 +42,6 @@ BlendPoint::BlendPoint()
vectors.emplace_back(Base::Vector3d(0, 0, 0));
}
BlendPoint::~BlendPoint()
{
}
void BlendPoint::multiply(double f)
{
for (int i = 0; i < nbVectors(); i++) {
@@ -78,8 +68,3 @@ int BlendPoint::nbVectors()
{
return vectors.size();
}
PyObject *BlendPoint::getPyObject(void)
{
return new BlendPointPy(new BlendPoint(vectors));
}

View File

@@ -20,13 +20,13 @@
* *
***************************************************************************/
#ifndef BLEND_POINT_H
#define BLEND_POINT_H
#ifndef SURFACE_BLEND_POINT_H
#define SURFACE_BLEND_POINT_H
#include <Mod/Surface/SurfaceGlobal.h>
#include <Base/BaseClass.h>
#include <Base/Vector3D.h>
#include <vector>
namespace Surface
@@ -36,7 +36,7 @@ namespace Surface
* Create a list of vectors formed by a point and some derivatives
* obtained from a curve or surface
*/
class SurfaceExport BlendPoint: public Base::BaseClass
class SurfaceExport BlendPoint
{
public:
std::vector<Base::Vector3d> vectors;
@@ -46,8 +46,8 @@ public:
* Constructor
*\param std::vector<Base::Vector3d>
*/
BlendPoint(std::vector<Base::Vector3d> vectorList);
virtual ~BlendPoint();
BlendPoint(const std::vector<Base::Vector3d>& vectorList);
~BlendPoint() = default;
/*!
* Scale the blendpoint vectors
*\param double scaling factor
@@ -67,7 +67,7 @@ public:
*\return Number of vectors of this BlendPoint
*/
int nbVectors();
virtual PyObject *getPyObject(void);
private:
};
}// namespace Surface

View File

@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<GenerateModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="generateMetaModel_Module.xsd">
<PythonExport
Father="BaseClassPy"
Father="PyObjectBase"
FatherInclude="Base/BaseClassPy.h"
FatherNamespace="Base"
Name="BlendPointPy"

View File

@@ -23,26 +23,27 @@
#include "PreCompiled.h"
#ifndef _PreComp_
#include <BRepAdaptor_Curve.hxx>
#include <TColStd_Array1OfReal.hxx>
#include <TColgp_Array1OfPnt.hxx>
#include <TopoDS.hxx>
#endif
#include "Blending/BlendPoint.h"
#include "Blending/BlendPointPy.h"
#include "Blending/BlendPointPy.cpp"
#include <Base/GeometryPyCXX.h>
#include <Base/VectorPy.h>
#include <Mod/Part/App/TopoShapePy.h>
#include "Blending/BlendPointPy.cpp"
using namespace Surface;
std::string BlendPointPy::representation(void) const
std::string BlendPointPy::representation() const
{
Base::Vector3d bp = getBlendPointPtr()->vectors[0];
std::stringstream str;
str << "G" << getBlendPointPtr()->getContinuity()
<< " BlendPoint at (" << bp.x << ", " << bp.y << ", " << bp.z << "), ";
str << "G" << getBlendPointPtr()->getContinuity() << " BlendPoint";
if (getBlendPointPtr()->vectors.empty()) {
Base::Vector3d bp = getBlendPointPtr()->vectors[0];
str << " at (" << bp.x << ", " << bp.y << ", " << bp.z << "), ";
}
return str.str();
}
@@ -109,10 +110,15 @@ int BlendPointPy::PyInit(PyObject *args, PyObject *)
return 0;
}
catch (const std::exception &e) {
std::cerr << e.what() << '\n';
PyErr_SetString(PyExc_RuntimeError, e.what());
return -1;
}
}
PyErr_SetString(PyExc_TypeError, "supported signatures:\n"
"BlendPoint()\n"
"BlendPoint(list of Vector)\n"
"BlendPoint(edge, parameter and continiuity)\n");
return -1;
}
@@ -127,7 +133,7 @@ PyObject *BlendPointPy::setSize(PyObject *args)
Py_Return;
}
catch (Standard_Failure &e) {
PyErr_SetString(PyExc_Exception, "Failed to set size");
PyErr_SetString(Base::PyExc_FC_CADKernelError, "Failed to set size");
return nullptr;
}
}
@@ -141,52 +147,41 @@ PyObject *BlendPointPy::getSize(PyObject *args)
double bpTangentLength = getBlendPointPtr()->vectors[1].Length();
return Py_BuildValue("d", bpTangentLength);
}
PyErr_SetString(PyExc_RuntimeError, "Cannot determine size");
return nullptr;
}
Py::List BlendPointPy::getVectors() const
{
try {
BlendPoint *bp = getBlendPointPtr();
std::vector<Base::Vector3d> p = bp->vectors;
Py::List vecs;
for (size_t i = 0; i < p.size(); i++) {
Base::VectorPy *vec = new Base::VectorPy(Base::Vector3d(
p[i].x, p[i].y, p[i].z));
vecs.append(Py::asObject(vec));
}
return vecs;
}
catch (Standard_Failure &e) {
PyErr_SetString(PyExc_RuntimeError,
e.GetMessageString());
return Py::List();
BlendPoint *bp = getBlendPointPtr();
Py::List vecs;
for (const auto& p : bp->vectors) {
Base::VectorPy *vec = new Base::VectorPy(p);
vecs.append(Py::asObject(vec));
}
return vecs;
}
PyObject *BlendPointPy::setvectors(PyObject *args)
{
BlendPoint *bp = getBlendPointPtr();
PyObject *plist;
if (!PyArg_ParseTuple(args, "O", &plist)) {
PyErr_SetString(PyExc_TypeError, "List of vectors required.");
return nullptr;
}
try {
Py::Sequence list(plist);
std::vector<Base::Vector3d> vecs;
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
Py::Vector v(*it);
Base::Vector3d pole = v.toVector();
vecs.emplace_back(pole);
}
bp->vectors = vecs;
Py_Return;
Py::Sequence list(plist);
std::vector<Base::Vector3d> vecs;
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
Py::Vector v(*it);
Base::Vector3d pole = v.toVector();
vecs.emplace_back(pole);
}
catch (Standard_Failure &e) {
PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
}
return nullptr;
BlendPoint *bp = getBlendPointPtr();
bp->vectors = vecs;
Py_Return;
}
PyObject *BlendPointPy::getCustomAttributes(const char * /*attr*/) const

View File

@@ -128,7 +128,6 @@ BlendPoint FeatureBlendCurve::GetBlendPoint(App::PropertyLinkSub &link, App::Pro
App::DocumentObjectExecReturn *FeatureBlendCurve::execute(void)
{
BlendPoint bp1 = GetBlendPoint(StartEdge, StartParameter, StartContinuity);
BlendPoint bp2 = GetBlendPoint(EndEdge, EndParameter, EndContinuity);
@@ -149,10 +148,6 @@ App::DocumentObjectExecReturn *FeatureBlendCurve::execute(void)
return StdReturn;
}
PyObject* Surface::BlendCurve::getPyObject(){
return nullptr;
}
double FeatureBlendCurve::RelativeToRealParameters(double relativeValue, double fp, double lp)
{
return fp + relativeValue * (lp - fp);

View File

@@ -53,9 +53,9 @@ public:
Standard_Integer maxDegree;
App::DocumentObjectExecReturn *execute(void) override;
App::DocumentObjectExecReturn *execute() override;
short mustExecute() const override;
const char *getViewProviderName(void) const override
const char *getViewProviderName() const override
{
return "SurfaceGui::ViewProviderBlendCurve";
}
@@ -66,7 +66,7 @@ private:
bool lockOnChangeMutex;
protected:
virtual void onChanged(const App::Property *prop) override;
void onChanged(const App::Property *prop) override;
};
}//Namespace Surface

View File

@@ -54,7 +54,7 @@ public:
private:
};
PyObject *initModule(){
PyObject *initModule() {
return Base::Interpreter().addModule(new Module);
}
@@ -76,10 +76,10 @@ PyMOD_INIT_FUNC(SurfaceGui)
SurfaceGui::Workbench::init();
SurfaceGui::ViewProviderGeomFillSurface ::init();
SurfaceGui::ViewProviderFilling ::init();
SurfaceGui::ViewProviderSections ::init();
SurfaceGui::ViewProviderExtend ::init();
SurfaceGui::ViewProviderBlendCurve ::init();
SurfaceGui::ViewProviderFilling ::init();
SurfaceGui::ViewProviderSections ::init();
SurfaceGui::ViewProviderExtend ::init();
SurfaceGui::ViewProviderBlendCurve ::init();
// SurfaceGui::ViewProviderCut::init();
PyObject *mod = SurfaceGui::initModule();

View File

@@ -1,5 +1,5 @@
/***************************************************************************
* Copyright (c) 2020 Eliud Cabrera Castillo <matteogrellier@gmail.com> *
* Copyright (c) 2022 Matteo Grellier <matteogrellier@gmail.com> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
@@ -20,21 +20,17 @@
* *
***************************************************************************/
#include "../PreCompiled.h"
#include "PreCompiled.h"
#include <Gui/BitmapFactory.h>
#include <Mod/Part/Gui/ViewProvider.h>
#include "ViewProviderBlendCurve.h"
using namespace SurfaceGui;
PROPERTY_SOURCE(SurfaceGui::ViewProviderBlendCurve, PartGui::ViewProviderSpline)
namespace SurfaceGui
{
QIcon ViewProviderBlendCurve::getIcon(void) const
QIcon ViewProviderBlendCurve::getIcon() const
{
return Gui::BitmapFactory().pixmap("BlendCurve");
}

View File

@@ -1,5 +1,5 @@
/***************************************************************************
* Copyright (c) 2020 Eliud Cabrera Castillo <e.cabrera-castillo@tum.de> *
* Copyright (c) 2022 Matteo Grellier <matteogrellier@gmail.com> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
@@ -24,7 +24,6 @@
#define SURFACEGUI_VIEWPROVIDERBLENDCURVE_H
#include <Mod/Part/Gui/ViewProviderSpline.h>
#include <Mod/Surface/App/Blending/FeatureBlendCurve.h>
namespace SurfaceGui
{
@@ -34,7 +33,7 @@ class ViewProviderBlendCurve: public PartGui::ViewProviderSpline
PROPERTY_HEADER(SurfaceGui::ViewProviderBlendCurve);
public:
QIcon getIcon(void) const;
QIcon getIcon() const;
};
}//namespace SurfaceGui

View File

@@ -9,6 +9,7 @@ include_directories(
${CMAKE_SOURCE_DIR}/src
${CMAKE_BINARY_DIR}/src
${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_CURRENT_SOURCE_DIR}
${Boost_INCLUDE_DIRS}
${COIN3D_INCLUDE_DIRS}
${OCC_INCLUDE_DIR}

View File

@@ -236,7 +236,7 @@ bool CmdSurfaceCurveOnMesh::isActive()
DEF_STD_CMD_A(CmdBlendSurface)
CmdBlendSurface::CmdBlendSurface()
: Command("BlendSurface")
: Command("Surface_BlendSurface")
{
sAppModule = "Surface";
sGroup = QT_TR_NOOP("Surface");
@@ -249,10 +249,10 @@ CmdBlendSurface::CmdBlendSurface()
void CmdBlendSurface::activated(int)
{
}
bool CmdBlendSurface::isActive(void)
bool CmdBlendSurface::isActive()
{
return true;
}
@@ -267,7 +267,7 @@ bool CmdBlendSurface::isActive(void)
DEF_STD_CMD_A(CmdBlendCurve)
CmdBlendCurve::CmdBlendCurve()
: Command("BlendCurve")
: Command("Surface_BlendCurve")
{
sAppModule = "Surface";
sGroup = QT_TR_NOOP("Surface");
@@ -332,7 +332,7 @@ void CmdBlendCurve::activated(int)
commitCommand();
}
bool CmdBlendCurve::isActive(void)
bool CmdBlendCurve::isActive()
{
Gui::SelectionFilter edgeFilter("SELECT Part::Feature SUBELEMENT Edge COUNT 2");
return edgeFilter.match();

View File

@@ -57,7 +57,7 @@ Gui::MenuItem *Workbench::setupMenuBar() const
<< "Surface_Sections"
<< "Surface_ExtendFace"
<< "Surface_CurveOnMesh"
<< "BlendCurve";
<< "Surface_BlendCurve";
/*
*surface << "Surface_Cut";
*/
@@ -76,7 +76,7 @@ Gui::ToolBarItem *Workbench::setupToolBars() const
<< "Surface_Sections"
<< "Surface_ExtendFace"
<< "Surface_CurveOnMesh"
<< "BlendCurve";
<< "Surface_BlendCurve";
/*
*surface << "Surface_Cut";
*/