Skip to content

Commit

Permalink
implement Line2d
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Nov 26, 2016
1 parent 3105c4b commit 1348fe6
Show file tree
Hide file tree
Showing 6 changed files with 316 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/Mod/Part/App/AppPart.cpp
Expand Up @@ -109,6 +109,7 @@
#include <Mod/Part/App/Geom2d/Hyperbola2dPy.h>
#include <Mod/Part/App/Geom2d/Curve2dPy.h>
#include <Mod/Part/App/Geom2d/Line2dSegmentPy.h>
#include <Mod/Part/App/Geom2d/Line2dPy.h>
#include <Mod/Part/App/Geom2d/OffsetCurve2dPy.h>
#include <Mod/Part/App/Geom2d/Parabola2dPy.h>
#include "PropertyGeometryList.h"
Expand Down Expand Up @@ -262,6 +263,7 @@ PyMODINIT_FUNC initPart()
Base::Interpreter().addType(&Part::BezierCurve2dPy::Type,geom2dModule,"BezierCurve2d");
Base::Interpreter().addType(&Part::BSplineCurve2dPy::Type,geom2dModule,"BSplineCurve2d");
Base::Interpreter().addType(&Part::Line2dSegmentPy::Type,geom2dModule,"Line2dSegment");
Base::Interpreter().addType(&Part::Line2dPy::Type,geom2dModule,"Line2d");
Base::Interpreter().addType(&Part::OffsetCurve2dPy::Type,geom2dModule,"OffsetCurve2d");

try {
Expand Down
11 changes: 7 additions & 4 deletions src/Mod/Part/App/CMakeLists.txt
Expand Up @@ -101,6 +101,7 @@ generate_from_xml(Geom2d/Geometry2dPy)
generate_from_xml(Geom2d/Hyperbola2dPy)
generate_from_xml(Geom2d/Curve2dPy)
generate_from_xml(Geom2d/Line2dSegmentPy)
generate_from_xml(Geom2d/Line2dPy)
generate_from_xml(Geom2d/OffsetCurve2dPy)
generate_from_xml(Geom2d/Parabola2dPy)

Expand Down Expand Up @@ -161,10 +162,10 @@ SET(Features_SRCS
FeatureGeometrySet.cpp
CustomFeature.cpp
CustomFeature.h
BodyBase.h
BodyBase.cpp
DatumFeature.cpp
DatumFeature.h
BodyBase.h
BodyBase.cpp
DatumFeature.cpp
DatumFeature.h
AttachableObject.h
AttachableObject.cpp
)
Expand Down Expand Up @@ -298,6 +299,8 @@ SET(Geom2dPy_SRCS
Geom2d/Curve2dPyImp.cpp
Geom2d/Hyperbola2dPy.xml
Geom2d/Hyperbola2dPyImp.cpp
Geom2d/Line2dPy.xml
Geom2d/Line2dPyImp.cpp
Geom2d/Line2dSegmentPy.xml
Geom2d/Line2dSegmentPyImp.cpp
Geom2d/OffsetCurve2dPy.xml
Expand Down
39 changes: 39 additions & 0 deletions src/Mod/Part/App/Geom2d/Line2dPy.xml
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<GenerateModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="generateMetaModel_Module.xsd">
<PythonExport
Father="Curve2dPy"
Name="Line2dPy"
Twin="Geom2dLine"
TwinPointer="Geom2dLine"
Include="Mod/Part/App/Geometry2d.h"
Namespace="Part"
FatherInclude="Mod/Part/App/Geom2d/Curve2dPy.h"
FatherNamespace="Part"
Constructor="true">
<Documentation>
<Author Licence="LGPL" Name="Werner Mayer" EMail="wmayer@users.sourceforge.net" />
<UserDocu>Describes an infinite line in 2D space
To create a line there are several ways:
Part.Geom2d.Line2d()
Creates a default line

Part.Geom2d.Line2d(Line)
Creates a copy of the given line

Part.Geom2d.Line2d(Point,Dir)
Creates a line that goes through two given points</UserDocu>
</Documentation>
<Attribute Name="Location" ReadOnly="false">
<Documentation>
<UserDocu>Returns the location of this line.</UserDocu>
</Documentation>
<Parameter Name="Location" Type="Object"/>
</Attribute>
<Attribute Name="Direction" ReadOnly="false">
<Documentation>
<UserDocu>Returns the direction of this line.</UserDocu>
</Documentation>
<Parameter Name="Direction" Type="Object"/>
</Attribute>
</PythonExport>
</GenerateModel>
242 changes: 242 additions & 0 deletions src/Mod/Part/App/Geom2d/Line2dPyImp.cpp
@@ -0,0 +1,242 @@
/***************************************************************************
* Copyright (c) 2016 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* 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 <gp.hxx>
# include <gp_Lin2d.hxx>
# include <Geom2d_Line.hxx>
# include <GCE2d_MakeLine.hxx>
# include <Precision.hxx>
#endif

#include <Base/GeometryPyCXX.h>

#include <Mod/Part/App/OCCError.h>
#include <Mod/Part/App/Geometry2d.h>
#include <Mod/Part/App/Geom2d/Line2dPy.h>
#include <Mod/Part/App/Geom2d/Line2dPy.cpp>

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 Line2dPy::representation(void) const
{
return "<Line2d object>";
}

PyObject *Line2dPy::PyMake(struct _typeobject *, PyObject *, PyObject *) // Python wrapper
{
// create a new instance of Line2dPy and the Twin object
return new Line2dPy(new Geom2dLine);
}

// constructor method
int Line2dPy::PyInit(PyObject* args, PyObject* /*kwd*/)
{
if (PyArg_ParseTuple(args, "")) {
// default line
return 0;
}

PyErr_Clear();
PyObject *pLine;
if (PyArg_ParseTuple(args, "O!", &(Line2dPy::Type), &pLine)) {
// Copy line
Line2dPy* pcLine = static_cast<Line2dPy*>(pLine);
// get Geom_Line of line
Handle_Geom2d_Line that_line = Handle_Geom2d_Line::DownCast
(pcLine->getGeom2dLinePtr()->handle());
// get Geom_Line of line
Handle_Geom2d_Line this_line = Handle_Geom2d_Line::DownCast
(this->getGeom2dLinePtr()->handle());

// Assign the lines
this_line->SetLin2d(that_line->Lin2d());
return 0;
}

PyErr_Clear();
PyObject *pV1, *pV2;
if (PyArg_ParseTuple(args, "O!O!", Base::Vector2dPy::type_object(), &pV1,
Base::Vector2dPy::type_object(), &pV2)) {
Base::Vector2d v1 = Py::Vector2d(pV1).getCxxObject()->value();
Base::Vector2d v2 = Py::Vector2d(pV2).getCxxObject()->value();
try {
// Create line out of two points
double distance = (v1-v2).Length();
if (distance < gp::Resolution())
Standard_Failure::Raise("Both points are equal");
GCE2d_MakeLine ms(gp_Pnt2d(v1.x,v1.y),
gp_Pnt2d(v2.x,v2.y));
if (!ms.IsDone()) {
PyErr_SetString(PartExceptionOCCError, gce_ErrorStatusText(ms.Status()));
return -1;
}

// get Geom_Line of line
Handle_Geom2d_Line this_line = Handle_Geom2d_Line::DownCast
(this->getGeom2dLinePtr()->handle());
Handle_Geom2d_Line that_line = ms.Value();
this_line->SetLin2d(that_line->Lin2d());
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"
"-- Line\n"
"-- Point, Point");
return -1;
}

Py::Object Line2dPy::getLocation(void) const
{
Handle_Geom2d_Line this_curve = Handle_Geom2d_Line::DownCast
(this->getGeom2dLinePtr()->handle());
gp_Pnt2d pnt = this_curve->Location();

Py::Module module("__FreeCADBase__");
Py::Callable method(module.getAttr("Vector2d"));
Py::Tuple arg(2);
arg.setItem(0, Py::Float(pnt.X()));
arg.setItem(1, Py::Float(pnt.Y()));
return method.apply(arg);
}

void Line2dPy::setLocation(Py::Object arg)
{
gp_Pnt2d pnt;
gp_Dir2d dir;
Handle_Geom2d_Line this_line = Handle_Geom2d_Line::DownCast
(this->getGeom2dLinePtr()->handle());
dir = this_line->Direction();

PyObject *p = arg.ptr();
if (PyObject_TypeCheck(p, Base::Vector2dPy::type_object())) {
Base::Vector2d v = Py::Vector2d(p).getCxxObject()->value();
pnt.SetX(v.x);
pnt.SetY(v.y);
}
else if (PyTuple_Check(p)) {
Py::Tuple tuple(arg);
pnt.SetX((double)Py::Float(tuple.getItem(0)));
pnt.SetY((double)Py::Float(tuple.getItem(1)));
}
else {
std::string error = std::string("type must be 'Vector2d' or tuple, not ");
error += p->ob_type->tp_name;
throw Py::TypeError(error);
}

try {
GCE2d_MakeLine ms(pnt, dir);
if (!ms.IsDone()) {
throw Py::Exception(gce_ErrorStatusText(ms.Status()));
}

// get Geom_Line of line
Handle_Geom2d_Line that_line = ms.Value();
this_line->SetLin2d(that_line->Lin2d());
}
catch (Standard_Failure) {
Handle_Standard_Failure e = Standard_Failure::Caught();
throw Py::Exception(e->GetMessageString());
}
}

Py::Object Line2dPy::getDirection(void) const
{
Handle_Geom2d_Line this_curve = Handle_Geom2d_Line::DownCast
(this->getGeom2dLinePtr()->handle());
gp_Dir2d dir = this_curve->Direction();

Py::Module module("__FreeCADBase__");
Py::Callable method(module.getAttr("Vector2d"));
Py::Tuple arg(2);
arg.setItem(0, Py::Float(dir.X()));
arg.setItem(1, Py::Float(dir.Y()));
return method.apply(arg);
}

void Line2dPy::setDirection(Py::Object arg)
{
gp_Pnt2d pnt;
gp_Dir2d dir;
Handle_Geom2d_Line this_line = Handle_Geom2d_Line::DownCast
(this->getGeom2dLinePtr()->handle());
pnt = this_line->Location();

PyObject *p = arg.ptr();
if (PyObject_TypeCheck(p, Base::Vector2dPy::type_object())) {
Base::Vector2d v = Py::Vector2d(p).getCxxObject()->value();
dir = gp_Dir2d(v.x,v.y);
}
else if (PyTuple_Check(p)) {
Py::Tuple tuple(arg);
double x = (double)Py::Float(tuple.getItem(0));
double y = (double)Py::Float(tuple.getItem(1));
dir = gp_Dir2d(x,y);
}
else {
std::string error = std::string("type must be 'Vector2d' or tuple, not ");
error += p->ob_type->tp_name;
throw Py::TypeError(error);
}

try {
GCE2d_MakeLine ms(pnt, dir);
if (!ms.IsDone()) {
throw Py::Exception(gce_ErrorStatusText(ms.Status()));
}

// get Geom_Line of line
Handle_Geom2d_Line that_line = ms.Value();
this_line->SetLin2d(that_line->Lin2d());
}
catch (Standard_Failure) {
Handle_Standard_Failure e = Standard_Failure::Caught();
throw Py::Exception(e->GetMessageString());
}
}

PyObject *Line2dPy::getCustomAttributes(const char* /*attr*/) const
{
return 0;
}

int Line2dPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
{
return 0;
}
27 changes: 24 additions & 3 deletions src/Mod/Part/App/Geom2d/Line2dSegmentPyImp.cpp
Expand Up @@ -36,6 +36,7 @@

#include <Mod/Part/App/OCCError.h>
#include <Mod/Part/App/Geometry2d.h>
#include <Mod/Part/App/Geom2d/Line2dPy.h>
#include <Mod/Part/App/Geom2d/Line2dSegmentPy.h>
#include <Mod/Part/App/Geom2d/Line2dSegmentPy.cpp>

Expand Down Expand Up @@ -107,6 +108,25 @@ int Line2dSegmentPy::PyInit(PyObject* args, PyObject* /*kwd*/)
return 0;
}

PyErr_Clear();
if (PyArg_ParseTuple(args, "O!dd", &(Line2dPy::Type), &pLine, &first, &last)) {
// Copy line
Line2dPy* pcLine = static_cast<Line2dPy*>(pLine);
// get Geom_Line of line
Handle_Geom2d_Line that_line = Handle_Geom2d_Line::DownCast
(pcLine->getGeom2dLinePtr()->handle());
// get Geom_Line of line segment
Handle_Geom2d_TrimmedCurve this_curv = Handle_Geom2d_TrimmedCurve::DownCast
(this->getGeom2dLineSegmentPtr()->handle());
Handle_Geom2d_Line this_line = Handle_Geom2d_Line::DownCast
(this_curv->BasisCurve());

// Assign the lines
this_line->SetLin2d(that_line->Lin2d());
this_curv->SetTrim(first, last);
return 0;
}

PyErr_Clear();
PyObject *pV1, *pV2;
if (PyArg_ParseTuple(args, "O!O!", Base::Vector2dPy::type_object(), &pV1,
Expand Down Expand Up @@ -147,10 +167,11 @@ int Line2dSegmentPy::PyInit(PyObject* args, PyObject* /*kwd*/)
}
}

PyErr_SetString(PyExc_TypeError, "Line constructor accepts:\n"
PyErr_SetString(PyExc_TypeError, "Line2dSegment constructor accepts:\n"
"-- empty parameter list\n"
"-- Line\n"
"-- Line, float, float\n"
"-- Line2dSegment\n"
"-- Line2dSegment, float, float\n"
"-- Line2d, float, float\n"
"-- Point, Point");
return -1;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Mod/Part/App/Geometry2d.cpp
Expand Up @@ -79,6 +79,7 @@
#include <Mod/Part/App/Geom2d/BezierCurve2dPy.h>
#include <Mod/Part/App/Geom2d/BSplineCurve2dPy.h>
#include <Mod/Part/App/Geom2d/Line2dSegmentPy.h>
#include <Mod/Part/App/Geom2d/Line2dPy.h>
#include <Mod/Part/App/Geom2d/OffsetCurve2dPy.h>

using namespace Part;
Expand Down Expand Up @@ -1980,7 +1981,7 @@ void Geom2dLine::Restore(Base::XMLReader &reader)

PyObject *Geom2dLine::getPyObject(void)
{
return 0;
return new Line2dPy(static_cast<Geom2dLine*>(this->clone()));
}

// -------------------------------------------------
Expand Down

0 comments on commit 1348fe6

Please sign in to comment.