From e96370f820a3c4b429b3caec13a442aa12aa56c6 Mon Sep 17 00:00:00 2001 From: wmayer Date: Wed, 30 Nov 2016 16:26:21 +0100 Subject: [PATCH] add class LineSegment --- src/Mod/Part/App/AppPart.cpp | 2 + src/Mod/Part/App/CMakeLists.txt | 3 + src/Mod/Part/App/Geometry.cpp | 7 +- src/Mod/Part/App/LineSegmentPy.xml | 44 ++++ src/Mod/Part/App/LineSegmentPyImp.cpp | 308 ++++++++++++++++++++++++++ 5 files changed, 361 insertions(+), 3 deletions(-) create mode 100644 src/Mod/Part/App/LineSegmentPy.xml create mode 100644 src/Mod/Part/App/LineSegmentPyImp.cpp diff --git a/src/Mod/Part/App/AppPart.cpp b/src/Mod/Part/App/AppPart.cpp index 49c9d17be..26b1aa9e7 100644 --- a/src/Mod/Part/App/AppPart.cpp +++ b/src/Mod/Part/App/AppPart.cpp @@ -67,6 +67,7 @@ #include "Mod/Part/App/TopoShapeCompSolidPy.h" #include "Mod/Part/App/TopoShapeShellPy.h" #include "Mod/Part/App/LinePy.h" +#include "Mod/Part/App/LineSegmentPy.h" #include "Mod/Part/App/PointPy.h" #include "Mod/Part/App/CirclePy.h" #include "Mod/Part/App/EllipsePy.h" @@ -207,6 +208,7 @@ PyMODINIT_FUNC initPart() Base::Interpreter().addType(&Part::TopoShapeShellPy ::Type,partModule,"Shell"); Base::Interpreter().addType(&Part::LinePy ::Type,partModule,"Line"); + Base::Interpreter().addType(&Part::LineSegmentPy ::Type,partModule,"LineSegment"); Base::Interpreter().addType(&Part::PointPy ::Type,partModule,"Point"); Base::Interpreter().addType(&Part::CirclePy ::Type,partModule,"Circle"); Base::Interpreter().addType(&Part::EllipsePy ::Type,partModule,"Ellipse"); diff --git a/src/Mod/Part/App/CMakeLists.txt b/src/Mod/Part/App/CMakeLists.txt index 50c307865..04e7278a0 100644 --- a/src/Mod/Part/App/CMakeLists.txt +++ b/src/Mod/Part/App/CMakeLists.txt @@ -54,6 +54,7 @@ generate_from_xml(GeometryPy) generate_from_xml(GeometryCurvePy) generate_from_xml(GeometrySurfacePy) generate_from_xml(LinePy) +generate_from_xml(LineSegmentPy) generate_from_xml(PointPy) generate_from_xml(BezierCurvePy) generate_from_xml(BSplineCurvePy) @@ -210,6 +211,8 @@ SET(Python_SRCS GeometrySurfacePyImp.cpp LinePy.xml LinePyImp.cpp + LineSegmentPy.xml + LineSegmentPyImp.cpp PointPy.xml PointPyImp.cpp BezierCurvePy.xml diff --git a/src/Mod/Part/App/Geometry.cpp b/src/Mod/Part/App/Geometry.cpp index 2217b13ca..7efd2e1a8 100644 --- a/src/Mod/Part/App/Geometry.cpp +++ b/src/Mod/Part/App/Geometry.cpp @@ -100,6 +100,7 @@ #include #include +#include #include #include #include @@ -2720,7 +2721,7 @@ PyObject *GeomArcOfParabola::getPyObject(void) // ------------------------------------------------- -TYPESYSTEM_SOURCE(Part::GeomLine,Part::GeomCurve); +TYPESYSTEM_SOURCE(Part::GeomLine,Part::GeomCurve) GeomLine::GeomLine() { @@ -2825,7 +2826,7 @@ PyObject *GeomLine::getPyObject(void) // ------------------------------------------------- -TYPESYSTEM_SOURCE(Part::GeomLineSegment,Part::GeomCurve); +TYPESYSTEM_SOURCE(Part::GeomLineSegment,Part::GeomCurve) GeomLineSegment::GeomLineSegment() { @@ -2951,7 +2952,7 @@ void GeomLineSegment::Restore (Base::XMLReader &reader) PyObject *GeomLineSegment::getPyObject(void) { - return new LinePy((GeomLineSegment*)this->clone()); + return new LineSegmentPy(static_cast(this->clone())); } // ------------------------------------------------- diff --git a/src/Mod/Part/App/LineSegmentPy.xml b/src/Mod/Part/App/LineSegmentPy.xml new file mode 100644 index 000000000..37975a1ee --- /dev/null +++ b/src/Mod/Part/App/LineSegmentPy.xml @@ -0,0 +1,44 @@ + + + + + + Describes a line segment +To create a line segment there are several ways: +Part.LineSegment() + Creates a default line segment + +Part.LineSegment(LineSegment) + Creates a copy of the given line segment + +Part.LineSegment(Point1,Point2) + Creates a line segment that goes through two given points + + + + Set the parameter range of the underlying line geometry + + + + + Returns the start point of this line. + + + + + + Returns the end point point of this line. + + + + + diff --git a/src/Mod/Part/App/LineSegmentPyImp.cpp b/src/Mod/Part/App/LineSegmentPyImp.cpp new file mode 100644 index 000000000..975acb179 --- /dev/null +++ b/src/Mod/Part/App/LineSegmentPyImp.cpp @@ -0,0 +1,308 @@ +/*************************************************************************** + * Copyright (c) 2008 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 +# include +# include +#endif + +#include +#include + +#include "OCCError.h" +#include "Geometry.h" +#include +#include +#include + +using namespace Part; + +extern const char* gce_ErrorStatusText(gce_ErrorType et); + +// returns a string which represents the object e.g. when printed in python +std::string LineSegmentPy::representation(void) const +{ + std::stringstream str; + Base::Vector3d start = getGeomLineSegmentPtr()->getStartPoint(); + Base::Vector3d end = getGeomLineSegmentPtr()->getEndPoint(); + str << ""; + + return str.str(); +} + +PyObject *LineSegmentPy::PyMake(struct _typeobject *, PyObject *, PyObject *) // Python wrapper +{ + // create a new instance of LineSegmentPy and the Twin object + return new LineSegmentPy(new GeomLineSegment); +} + +// constructor method +int LineSegmentPy::PyInit(PyObject* args, PyObject* /*kwd*/) +{ + if (PyArg_ParseTuple(args, "")) { + return 0; + } + + PyErr_Clear(); + PyObject *pLine; + if (PyArg_ParseTuple(args, "O!", &(LineSegmentPy::Type), &pLine)) { + // Copy line + LineSegmentPy* pcLine = static_cast(pLine); + // get Geom_Line of line segment + Handle_Geom_TrimmedCurve that_curv = Handle_Geom_TrimmedCurve::DownCast + (pcLine->getGeomLineSegmentPtr()->handle()); + Handle_Geom_Line that_line = Handle_Geom_Line::DownCast + (that_curv->BasisCurve()); + // get Geom_Line of line segment + Handle_Geom_TrimmedCurve this_curv = Handle_Geom_TrimmedCurve::DownCast + (this->getGeomLineSegmentPtr()->handle()); + Handle_Geom_Line this_line = Handle_Geom_Line::DownCast + (this_curv->BasisCurve()); + + // Assign the lines + this_line->SetLin(that_line->Lin()); + this_curv->SetTrim(that_curv->FirstParameter(), that_curv->LastParameter()); + return 0; + } + + PyErr_Clear(); + double first, last; + if (PyArg_ParseTuple(args, "O!dd", &(LineSegmentPy::Type), &pLine, &first, &last)) { + // Copy line + LineSegmentPy* pcLine = static_cast(pLine); + // get Geom_Line of line segment + Handle_Geom_TrimmedCurve that_curv = Handle_Geom_TrimmedCurve::DownCast + (pcLine->getGeomLineSegmentPtr()->handle()); + Handle_Geom_Line that_line = Handle_Geom_Line::DownCast + (that_curv->BasisCurve()); + // get Geom_Line of line segment + Handle_Geom_TrimmedCurve this_curv = Handle_Geom_TrimmedCurve::DownCast + (this->getGeomLineSegmentPtr()->handle()); + Handle_Geom_Line this_line = Handle_Geom_Line::DownCast + (this_curv->BasisCurve()); + + // Assign the lines + this_line->SetLin(that_line->Lin()); + this_curv->SetTrim(first, last); + return 0; + } + + PyErr_Clear(); + PyObject *pV1, *pV2; + if (PyArg_ParseTuple(args, "O!O!", &(Base::VectorPy::Type), &pV1, + &(Base::VectorPy::Type), &pV2)) { + Base::Vector3d v1 = static_cast(pV1)->value(); + Base::Vector3d v2 = static_cast(pV2)->value(); + try { + // Create line out of two points + double distance = Base::Distance(v1, v2); + if (distance < gp::Resolution()) + Standard_Failure::Raise("Both points are equal"); + GC_MakeSegment ms(gp_Pnt(v1.x,v1.y,v1.z), + gp_Pnt(v2.x,v2.y,v2.z)); + if (!ms.IsDone()) { + PyErr_SetString(PartExceptionOCCError, gce_ErrorStatusText(ms.Status())); + return -1; + } + + // get Geom_Line of line segment + Handle_Geom_TrimmedCurve this_curv = Handle_Geom_TrimmedCurve::DownCast + (this->getGeomLineSegmentPtr()->handle()); + Handle_Geom_Line this_line = Handle_Geom_Line::DownCast + (this_curv->BasisCurve()); + Handle_Geom_TrimmedCurve that_curv = ms.Value(); + Handle_Geom_Line that_line = Handle_Geom_Line::DownCast(that_curv->BasisCurve()); + this_line->SetLin(that_line->Lin()); + this_curv->SetTrim(that_curv->FirstParameter(), that_curv->LastParameter()); + + return 0; + } + catch (Standard_Failure) { + Handle_Standard_Failure e = Standard_Failure::Caught(); + PyErr_SetString(PartExceptionOCCError, e->GetMessageString()); + return -1; + } + catch (...) { + PyErr_SetString(PartExceptionOCCError, "creation of line failed"); + return -1; + } + } + + PyErr_SetString(PyExc_TypeError, "Line constructor accepts:\n" + "-- empty parameter list\n" + "-- LineSegment\n" + "-- Point, Point"); + return -1; +} + +PyObject* LineSegmentPy::setParameterRange(PyObject *args) +{ + double first, last; + if (!PyArg_ParseTuple(args, "dd", &first, &last)) + return NULL; + + try { + Handle_Geom_TrimmedCurve this_curve = Handle_Geom_TrimmedCurve::DownCast + (this->getGeomLineSegmentPtr()->handle()); + this_curve->SetTrim(first, last); + } + catch (Standard_Failure) { + Handle_Standard_Failure e = Standard_Failure::Caught(); + PyErr_SetString(PartExceptionOCCError, e->GetMessageString()); + return NULL; + } + + Py_Return; +} + +Py::Object LineSegmentPy::getStartPoint(void) const +{ + Handle_Geom_TrimmedCurve this_curve = Handle_Geom_TrimmedCurve::DownCast + (this->getGeomLineSegmentPtr()->handle()); + gp_Pnt pnt = this_curve->StartPoint(); + return Py::Vector(Base::Vector3d(pnt.X(), pnt.Y(), pnt.Z())); +} + +void LineSegmentPy::setStartPoint(Py::Object arg) +{ + gp_Pnt p1, p2; + Handle_Geom_TrimmedCurve this_curv = Handle_Geom_TrimmedCurve::DownCast + (this->getGeomLineSegmentPtr()->handle()); + p2 = this_curv->EndPoint(); + + PyObject *p = arg.ptr(); + if (PyObject_TypeCheck(p, &(Base::VectorPy::Type))) { + Base::Vector3d v = static_cast(p)->value(); + p1.SetX(v.x); + p1.SetY(v.y); + p1.SetZ(v.z); + } + else if (PyTuple_Check(p)) { + Py::Tuple tuple(arg); + p1.SetX((double)Py::Float(tuple.getItem(0))); + p1.SetY((double)Py::Float(tuple.getItem(1))); + p1.SetZ((double)Py::Float(tuple.getItem(2))); + } + else { + std::string error = std::string("type must be 'Vector' or tuple, not "); + error += p->ob_type->tp_name; + throw Py::TypeError(error); + } + + try { + // Create line out of two points + if (p1.Distance(p2) < gp::Resolution()) + Standard_Failure::Raise("Both points are equal"); + GC_MakeSegment ms(p1, p2); + if (!ms.IsDone()) { + throw Py::Exception(gce_ErrorStatusText(ms.Status())); + } + + // get Geom_Line of line segment + Handle_Geom_Line this_line = Handle_Geom_Line::DownCast + (this_curv->BasisCurve()); + Handle_Geom_TrimmedCurve that_curv = ms.Value(); + Handle_Geom_Line that_line = Handle_Geom_Line::DownCast(that_curv->BasisCurve()); + this_line->SetLin(that_line->Lin()); + this_curv->SetTrim(that_curv->FirstParameter(), that_curv->LastParameter()); + } + catch (Standard_Failure) { + Handle_Standard_Failure e = Standard_Failure::Caught(); + throw Py::Exception(e->GetMessageString()); + } +} + +Py::Object LineSegmentPy::getEndPoint(void) const +{ + Handle_Geom_TrimmedCurve this_curve = Handle_Geom_TrimmedCurve::DownCast + (this->getGeomLineSegmentPtr()->handle()); + gp_Pnt pnt = this_curve->EndPoint(); + return Py::Vector(Base::Vector3d(pnt.X(), pnt.Y(), pnt.Z())); +} + +void LineSegmentPy::setEndPoint(Py::Object arg) +{ + gp_Pnt p1, p2; + Handle_Geom_TrimmedCurve this_curv = Handle_Geom_TrimmedCurve::DownCast + (this->getGeomLineSegmentPtr()->handle()); + p1 = this_curv->StartPoint(); + + PyObject *p = arg.ptr(); + if (PyObject_TypeCheck(p, &(Base::VectorPy::Type))) { + Base::Vector3d v = static_cast(p)->value(); + p2.SetX(v.x); + p2.SetY(v.y); + p2.SetZ(v.z); + } + else if (PyTuple_Check(p)) { + Py::Tuple tuple(arg); + p2.SetX((double)Py::Float(tuple.getItem(0))); + p2.SetY((double)Py::Float(tuple.getItem(1))); + p2.SetZ((double)Py::Float(tuple.getItem(2))); + } + else { + std::string error = std::string("type must be 'Vector' or tuple, not "); + error += p->ob_type->tp_name; + throw Py::TypeError(error); + } + + try { + // Create line out of two points + if (p1.Distance(p2) < gp::Resolution()) + Standard_Failure::Raise("Both points are equal"); + GC_MakeSegment ms(p1, p2); + if (!ms.IsDone()) { + throw Py::Exception(gce_ErrorStatusText(ms.Status())); + } + + // get Geom_Line of line segment + Handle_Geom_Line this_line = Handle_Geom_Line::DownCast + (this_curv->BasisCurve()); + Handle_Geom_TrimmedCurve that_curv = ms.Value(); + Handle_Geom_Line that_line = Handle_Geom_Line::DownCast(that_curv->BasisCurve()); + this_line->SetLin(that_line->Lin()); + this_curv->SetTrim(that_curv->FirstParameter(), that_curv->LastParameter()); + } + catch (Standard_Failure) { + Handle_Standard_Failure e = Standard_Failure::Caught(); + throw Py::Exception(e->GetMessageString()); + } +} + +PyObject *LineSegmentPy::getCustomAttributes(const char* /*attr*/) const +{ + return 0; +} + +int LineSegmentPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/) +{ + return 0; +}