From 13e2b24ddf0fae8be2223539bb8677ba0e6a7cf4 Mon Sep 17 00:00:00 2001 From: wmayer Date: Thu, 19 Nov 2015 15:27:35 +0100 Subject: [PATCH] + add Python binding to Plate surface --- src/Mod/Part/App/AppPart.cpp | 3 + src/Mod/Part/App/CMakeLists.txt | 5 +- src/Mod/Part/App/Geometry.cpp | 75 ++++++++- src/Mod/Part/App/Geometry.h | 28 ++++ src/Mod/Part/App/PlateSurfacePy.xml | 33 ++++ src/Mod/Part/App/PlateSurfacePyImp.cpp | 208 +++++++++++++++++++++++++ 6 files changed, 347 insertions(+), 5 deletions(-) create mode 100644 src/Mod/Part/App/PlateSurfacePy.xml create mode 100644 src/Mod/Part/App/PlateSurfacePyImp.cpp diff --git a/src/Mod/Part/App/AppPart.cpp b/src/Mod/Part/App/AppPart.cpp index d4dd38731ac4..736c2bba6ff3 100644 --- a/src/Mod/Part/App/AppPart.cpp +++ b/src/Mod/Part/App/AppPart.cpp @@ -80,6 +80,7 @@ #include "ConePy.h" #include "CylinderPy.h" #include "OffsetSurfacePy.h" +#include "PlateSurfacePy.h" #include "PlanePy.h" #include "RectangularTrimmedSurfacePy.h" #include "SpherePy.h" @@ -201,6 +202,7 @@ void PartExport initPart() Base::Interpreter().addType(&Part::BezierSurfacePy ::Type,partModule,"BezierSurface"); Base::Interpreter().addType(&Part::BSplineSurfacePy ::Type,partModule,"BSplineSurface"); Base::Interpreter().addType(&Part::OffsetSurfacePy ::Type,partModule,"OffsetSurface"); + Base::Interpreter().addType(&Part::PlateSurfacePy ::Type,partModule,"PlateSurface"); Base::Interpreter().addType(&Part::SurfaceOfExtrusionPy ::Type,partModule,"SurfaceOfExtrusion"); Base::Interpreter().addType(&Part::SurfaceOfRevolutionPy::Type,partModule,"SurfaceOfRevolution"); Base::Interpreter().addType(&Part::RectangularTrimmedSurfacePy @@ -298,6 +300,7 @@ void PartExport initPart() Part::GeomToroid ::init(); Part::GeomPlane ::init(); Part::GeomOffsetSurface ::init(); + Part::GeomPlateSurface ::init(); Part::GeomTrimmedSurface ::init(); Part::GeomSurfaceOfRevolution ::init(); Part::GeomSurfaceOfExtrusion ::init(); diff --git a/src/Mod/Part/App/CMakeLists.txt b/src/Mod/Part/App/CMakeLists.txt index 00bd1c9f24e0..98dac14be8ae 100644 --- a/src/Mod/Part/App/CMakeLists.txt +++ b/src/Mod/Part/App/CMakeLists.txt @@ -62,13 +62,12 @@ generate_from_xml(ToroidPy) generate_from_xml(BezierSurfacePy) generate_from_xml(BSplineSurfacePy) generate_from_xml(OffsetSurfacePy) +generate_from_xml(PlateSurfacePy) generate_from_xml(RectangularTrimmedSurfacePy) generate_from_xml(SurfaceOfExtrusionPy) generate_from_xml(SurfaceOfRevolutionPy) generate_from_xml(PartFeaturePy) generate_from_xml(Part2DObjectPy) -generate_from_xml(OffsetSurfacePy) -generate_from_xml(OffsetSurfacePy) generate_from_xml(TopoShapePy) generate_from_xml(TopoShapeCompoundPy) generate_from_xml(TopoShapeCompSolidPy) @@ -197,6 +196,8 @@ SET(Python_SRCS BSplineSurfacePyImp.cpp OffsetSurfacePy.xml OffsetSurfacePyImp.cpp + PlateSurfacePy.xml + PlateSurfacePyImp.cpp RectangularTrimmedSurfacePy.xml RectangularTrimmedSurfacePyImp.cpp SurfaceOfExtrusionPy.xml diff --git a/src/Mod/Part/App/Geometry.cpp b/src/Mod/Part/App/Geometry.cpp index 5543682c7cf0..dbdcbcac2d71 100644 --- a/src/Mod/Part/App/Geometry.cpp +++ b/src/Mod/Part/App/Geometry.cpp @@ -46,6 +46,7 @@ # include # include # include +# include # include # include # include @@ -184,13 +185,13 @@ unsigned int Geometry::getMemSize (void) const return 1; } -void Geometry::Save (Base::Writer &writer) const +void Geometry::Save(Base::Writer &writer) const { const char c = Construction?'1':'0'; writer.Stream() << writer.ind() << "" << endl; } -void Geometry::Restore (Base::XMLReader &reader) +void Geometry::Restore(Base::XMLReader &reader) { // read my Element reader.readElement("Construction"); @@ -249,7 +250,7 @@ void GeomPoint::setPoint(const Base::Vector3d& p) } // Persistence implementer -unsigned int GeomPoint::getMemSize (void) const +unsigned int GeomPoint::getMemSize (void) const { return sizeof(Geom_CartesianPoint); } @@ -3214,6 +3215,7 @@ PyObject *GeomPlane::getPyObject(void) { return new PlanePy((GeomPlane*)this->clone()); } + // ------------------------------------------------- TYPESYSTEM_SOURCE(Part::GeomOffsetSurface,Part::GeomSurface); @@ -3265,6 +3267,73 @@ PyObject *GeomOffsetSurface::getPyObject(void) // ------------------------------------------------- +TYPESYSTEM_SOURCE(Part::GeomPlateSurface,Part::GeomSurface); + +GeomPlateSurface::GeomPlateSurface() +{ +} + +GeomPlateSurface::GeomPlateSurface(const Handle_Geom_Surface& s, const Plate_Plate& plate) +{ + this->mySurface = new GeomPlate_Surface(s, plate); +} + +GeomPlateSurface::GeomPlateSurface(const GeomPlate_BuildPlateSurface& buildPlate) +{ + Handle_GeomPlate_Surface s = buildPlate.Surface(); + this->mySurface = Handle_GeomPlate_Surface::DownCast(s->Copy()); +} + +GeomPlateSurface::GeomPlateSurface(const Handle_GeomPlate_Surface& s) +{ + this->mySurface = Handle_GeomPlate_Surface::DownCast(s->Copy()); +} + +GeomPlateSurface::~GeomPlateSurface() +{ +} + +void GeomPlateSurface::setHandle(const Handle_GeomPlate_Surface& s) +{ + mySurface = Handle_GeomPlate_Surface::DownCast(s->Copy()); +} + +const Handle_Geom_Geometry& GeomPlateSurface::handle() const +{ + return mySurface; +} + +Geometry *GeomPlateSurface::clone(void) const +{ + GeomPlateSurface *newSurf = new GeomPlateSurface(mySurface); + newSurf->Construction = this->Construction; + return newSurf; +} + +// Persistence implementer +unsigned int GeomPlateSurface::getMemSize (void) const +{ + throw Base::NotImplementedError("GeomPlateSurface::getMemSize"); +} + +void GeomPlateSurface::Save(Base::Writer &/*writer*/) const +{ + throw Base::NotImplementedError("GeomPlateSurface::Save"); +} + +void GeomPlateSurface::Restore(Base::XMLReader &/*reader*/) +{ + throw Base::NotImplementedError("GeomPlateSurface::Restore"); +} + +PyObject *GeomPlateSurface::getPyObject(void) +{ + throw Base::NotImplementedError("GeomPlateSurface::getPyObject"); +// return new PlateSurfacePy(static_cast(this->clone())); +} + +// ------------------------------------------------- + TYPESYSTEM_SOURCE(Part::GeomTrimmedSurface,Part::GeomSurface); GeomTrimmedSurface::GeomTrimmedSurface() diff --git a/src/Mod/Part/App/Geometry.h b/src/Mod/Part/App/Geometry.h index 5ec2a71c3dbd..19f0c12bb60e 100644 --- a/src/Mod/Part/App/Geometry.h +++ b/src/Mod/Part/App/Geometry.h @@ -43,9 +43,12 @@ #include #include #include +#include #include #include #include +#include +#include #include #include #include @@ -746,6 +749,31 @@ class PartExport GeomOffsetSurface : public GeomSurface Handle_Geom_OffsetSurface mySurface; }; +class PartExport GeomPlateSurface : public GeomSurface +{ + TYPESYSTEM_HEADER(); +public: + GeomPlateSurface(); + GeomPlateSurface(const Handle_Geom_Surface&, const Plate_Plate&); + GeomPlateSurface(const GeomPlate_BuildPlateSurface&); + GeomPlateSurface(const Handle_GeomPlate_Surface&); + virtual ~GeomPlateSurface(); + virtual Geometry *clone(void) const; + + // Persistence implementer --------------------- + virtual unsigned int getMemSize(void) const; + virtual void Save(Base::Writer &/*writer*/) const; + virtual void Restore(Base::XMLReader &/*reader*/); + // Base implementer ---------------------------- + virtual PyObject *getPyObject(void); + + void setHandle(const Handle_GeomPlate_Surface& s); + const Handle_Geom_Geometry& handle() const; + +private: + Handle_GeomPlate_Surface mySurface; +}; + class PartExport GeomTrimmedSurface : public GeomSurface { TYPESYSTEM_HEADER(); diff --git a/src/Mod/Part/App/PlateSurfacePy.xml b/src/Mod/Part/App/PlateSurfacePy.xml new file mode 100644 index 000000000000..dc6c2d4ca7ae --- /dev/null +++ b/src/Mod/Part/App/PlateSurfacePy.xml @@ -0,0 +1,33 @@ + + + + + + + + + + Builds the U isoparametric line of this surface + + + + + Builds the V isoparametric line of this surface + + + + + Approximate the plate surface to a B-Spline surface + + + + diff --git a/src/Mod/Part/App/PlateSurfacePyImp.cpp b/src/Mod/Part/App/PlateSurfacePyImp.cpp new file mode 100644 index 000000000000..99de264b9117 --- /dev/null +++ b/src/Mod/Part/App/PlateSurfacePyImp.cpp @@ -0,0 +1,208 @@ +/*************************************************************************** + * Copyright (c) 2015 Werner Mayer * + * * + * This file is part of the FreeCAD CAx development system. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Library General Public * + * License as published by the Free Software Foundation; either * + * version 2 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Library General Public License for more details. * + * * + * You should have received a copy of the GNU Library General Public * + * License along with this library; see the file COPYING.LIB. If not, * + * write to the Free Software Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307, USA * + * * + ***************************************************************************/ + + +#include "PreCompiled.h" +#ifndef _PreComp_ +# include +# include +# include +# include +# include +#endif + +#include +#include +#include + +#include "OCCError.h" +#include "Geometry.h" +#include "PlateSurfacePy.h" +#include "PlateSurfacePy.cpp" +#include "BSplineSurfacePy.h" + +using namespace Part; + +// returns a string which represents the object e.g. when printed in python +std::string PlateSurfacePy::representation(void) const +{ + return ""; +} + +PyObject *PlateSurfacePy::PyMake(struct _typeobject *, PyObject *, PyObject *) // Python wrapper +{ + // create a new instance of PlateSurfacePy and the Twin object + return new PlateSurfacePy(new GeomPlateSurface); +} + +// constructor method +int PlateSurfacePy::PyInit(PyObject* args, PyObject* kwds) +{ + static char* kwds_Parameter[] = {"Surface","Points","Curves","Degree", + "NbPtsOnCur","NbIter","Tol2d","Tol3d","TolAng","TolCurv","Anisotropie",NULL}; + + PyObject* surface = 0; + PyObject* points = 0; + PyObject* curves = 0; + int Degree = 3; + int NbPtsOnCur = 10; + int NbIter = 3; + double Tol2d = 0.00001; + double Tol3d = 0.0001; + double TolAng = 0.01; + double TolCurv = 0.1; + PyObject* Anisotropie = Py_False; + + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O!OOiiiddddO!", kwds_Parameter, + &(GeometryPy::Type), &surface, &points, &curves, + &Degree, &NbPtsOnCur, &NbIter, &Tol2d, &Tol3d, &TolAng, &TolCurv, + &PyBool_Type,&Anisotropie)) + return -1; + + if (!surface && !points && !curves) { + PyErr_SetString(PyExc_ValueError, "set points or curves as constraints"); + return -1; + } + + Handle_Geom_Surface surf; + if (surface) { + GeometryPy* pcGeo = static_cast(surface); + surf = Handle_Geom_Surface::DownCast + (pcGeo->getGeometryPtr()->handle()); + if (surf.IsNull()) { + PyErr_SetString(PyExc_TypeError, "geometry is not a surface"); + return -1; + } + } + + try { + GeomPlate_BuildPlateSurface buildPlate(Degree, NbPtsOnCur, NbIter, Tol2d, Tol3d, TolAng, TolCurv, + PyObject_IsTrue(Anisotropie) ? Standard_True : Standard_False); + if (!surf.IsNull()) { + buildPlate.LoadInitSurface(surf); + + if (!points && !curves) { + Standard_Real U1,U2,V1,V2; + surf->Bounds(U1,U2,V1,V2); + buildPlate.Add(new GeomPlate_PointConstraint(surf->Value(U1,V1),0)); + buildPlate.Add(new GeomPlate_PointConstraint(surf->Value(U1,V2),0)); + buildPlate.Add(new GeomPlate_PointConstraint(surf->Value(U2,V1),0)); + buildPlate.Add(new GeomPlate_PointConstraint(surf->Value(U2,V2),0)); + } + } + + if (points) { + Py::Sequence list(points); + for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) { + Base::Vector3d vec = Py::Vector(*it).toVector(); + Handle(GeomPlate_PointConstraint) PCont = new GeomPlate_PointConstraint(gp_Pnt(vec.x,vec.y,vec.z),0); + buildPlate.Add(PCont); + } + } + + if (curves) { + Py::Sequence list(curves); + for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) { + //TODO + } + } + + buildPlate.Perform(); + getGeomPlateSurfacePtr()->setHandle(buildPlate.Surface()); + return 0; + } + catch (Standard_Failure) { + Handle_Standard_Failure e = Standard_Failure::Caught(); + PyErr_SetString(PartExceptionOCCError, e->GetMessageString()); + return -1; + } +} + +PyObject* PlateSurfacePy::makeApprox(PyObject *args, PyObject* kwds) +{ + static char* kwds_Parameter[] = {"Tol3d","MaxSegments","MaxDegree","MaxDistance", + "CritOrder","Continuity","EnlargeCoeff",NULL}; + + double tol3d=0.01; + int maxSeg=9; + int maxDegree=3; + double dmax = 0.0001; + int critOrder=0; + char* cont = "C1"; + double enlargeCoeff = 1.1; + + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|diidisd", kwds_Parameter, + &tol3d, &maxSeg, &maxDegree, &dmax, &critOrder, &cont, &enlargeCoeff)) + return 0; + + GeomAbs_Shape continuity; + std::string uc = cont; + if (uc == "C0") + continuity = GeomAbs_C0; + else if (uc == "C1") + continuity = GeomAbs_C1; + else if (uc == "C2") + continuity = GeomAbs_C2; + else if (uc == "C3") + continuity = GeomAbs_C3; + else if (uc == "CN") + continuity = GeomAbs_CN; + else if (uc == "G1") + continuity = GeomAbs_G1; + else + continuity = GeomAbs_C1; + + PY_TRY { + GeomPlate_MakeApprox approx(Handle_GeomPlate_Surface::DownCast(getGeomPlateSurfacePtr()->handle()), + tol3d, maxSeg, maxDegree, dmax, critOrder, continuity, enlargeCoeff); + Handle_Geom_BSplineSurface hSurf = approx.Surface(); + + if (!hSurf.IsNull()) { + return new Part::BSplineSurfacePy(new Part::GeomBSplineSurface(hSurf)); + } + + PyErr_SetString(PyExc_RuntimeError, "Approximation of B-Spline surface failed"); + return 0; + } PY_CATCH_OCC; +} + +PyObject* PlateSurfacePy::uIso(PyObject *args) +{ + PyErr_SetString(PyExc_NotImplementedError, "Not yet implemented"); + return 0; +} + +PyObject* PlateSurfacePy::vIso(PyObject *args) +{ + PyErr_SetString(PyExc_NotImplementedError, "Not yet implemented"); + return 0; +} + +PyObject *PlateSurfacePy::getCustomAttributes(const char* /*attr*/) const +{ + return 0; +} + +int PlateSurfacePy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/) +{ + return 0; +}