Skip to content

Commit

Permalink
Py binding for EdgeWalker
Browse files Browse the repository at this point in the history
refactor EdgeWalker code from DVP,DVS
  • Loading branch information
WandererFan committed Sep 1, 2016
1 parent 27ff991 commit 9a93185
Show file tree
Hide file tree
Showing 9 changed files with 1,820 additions and 1,572 deletions.
15 changes: 5 additions & 10 deletions src/Mod/TechDraw/App/AppTechDraw.cpp
Expand Up @@ -34,15 +34,12 @@
#include "DrawViewDraft.h"
#include "DrawViewSpreadsheet.h"

extern struct PyMethodDef TechDraw_methods[];

PyDoc_STRVAR(module_techdraw_doc,
"This module is the TechDraw module.");

namespace TechDraw {
extern PyObject* initModule();
}

/* Python entry */
extern "C" {
void TechDrawExport initTechDraw()
PyMODINIT_FUNC initTechDraw()
{
// load dependent module
try {
Expand All @@ -53,7 +50,7 @@ void TechDrawExport initTechDraw()
PyErr_SetString(PyExc_ImportError, e.what());
return;
}
Py_InitModule3("TechDraw", TechDraw_methods, module_techdraw_doc); /* mod name, table ptr */
(void)TechDraw::initModule();
Base::Console().Log("Loading TechDraw module... done\n");


Expand Down Expand Up @@ -87,5 +84,3 @@ void TechDrawExport initTechDraw()
TechDraw::DrawTemplatePython ::init();
TechDraw::DrawViewSymbolPython::init();
}

} // extern "C"
188 changes: 163 additions & 25 deletions src/Mod/TechDraw/App/AppTechDrawPy.cpp
@@ -1,6 +1,7 @@
/***************************************************************************
* Copyright (c) Jürgen Riegel (juergen.riegel@web.de) 2002 *
* *
* Copyright (c) WandererFan (wandererfan@gmail.com) 2016 *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
Expand All @@ -24,41 +25,178 @@
#include "PreCompiled.h"
#ifndef _PreComp_
# include <Python.h>
# include <TopoDS.hxx>
# include <TopoDS_Edge.hxx>
# include <TopoDS_Face.hxx>
# include <TopoDS_Wire.hxx>
#endif

#include <Mod/Part/App/TopoShapePy.h>
#include <CXX/Extensions.hxx>
#include <CXX/Objects.hxx>

#include <Base/Console.h>
#include <Base/VectorPy.h>
#include <boost/regex.hpp>
#include <Base/PyObjectBase.h>
#include <Base/Exception.h>
#include <Base/GeometryPyCXX.h>

#include <Mod/Part/App/TopoShape.h>
#include <Mod/Part/App/TopoShapePy.h>
#include <Mod/Part/App/TopoShapeEdgePy.h>
#include <Mod/Part/App/TopoShapeWirePy.h>

#include <Mod/Part/App/OCCError.h>

//using namespace TechDraw;
using namespace Part;
using namespace std;
#include "EdgeWalker.h"

static PyObject *
tdPlaceholder(PyObject *self, PyObject *args)
namespace TechDraw {
//module level static C++ functions go here
}

using Part::TopoShape;
using Part::TopoShapePy;
using Part::TopoShapeEdgePy;
using Part::TopoShapeWirePy;

namespace TechDraw {

class Module : public Py::ExtensionModule<Module>
{
PyObject *pcObjShape;
PyObject *pcObjDir=0;
public:
Module() : Py::ExtensionModule<Module>("TechDraw")
{
add_varargs_method("edgeWalker",&Module::edgeWalker,
"[wires] = edgeWalker(edgePile,inclBiggest) -- Planar graph traversal finds wires in edge pile."
);
add_varargs_method("findOuterWire",&Module::findOuterWire,
"wire = findOuterWire(edgeList) -- Planar graph traversal finds OuterWire in edge pile."
);
initialize("This is a module for making drawings"); // register with Python
}
virtual ~Module() {}

private:
virtual Py::Object invoke_method_varargs(void *method_def, const Py::Tuple &args)
{
try {
return Py::ExtensionModule<Module>::invoke_method_varargs(method_def, args);
}
catch (const Standard_Failure &e) {
std::string str;
Standard_CString msg = e.GetMessageString();
str += typeid(e).name();
str += " ";
if (msg) {str += msg;}
else {str += "No OCCT Exception Message";}
Base::Console().Error("%s\n", str.c_str());
throw Py::Exception(Part::PartExceptionOCCError, str);
}
catch (const Base::Exception &e) {
std::string str;
str += "FreeCAD exception thrown (";
str += e.what();
str += ")";
e.ReportException();
throw Py::RuntimeError(str);
}
catch (const std::exception &e) {
std::string str;
str += "C++ exception thrown (";
str += e.what();
str += ")";
Base::Console().Error("%s\n", str.c_str());
throw Py::RuntimeError(str);
}
}

if (!PyArg_ParseTuple(args, "O!|O!", &(TopoShapePy::Type), &pcObjShape,&(Base::VectorPy::Type), &pcObjDir)) // convert args: Python->C
return NULL; // NULL triggers exception
Py::Object edgeWalker(const Py::Tuple& args)
{
PyObject *pcObj;
PyObject *inclBig = Py_True;
if (!PyArg_ParseTuple(args.ptr(), "O|O", &pcObj,&inclBig)) {
throw Py::Exception();
}

PY_TRY {
TopoShapePy* pShape = static_cast<TopoShapePy*>(pcObjShape);
Py::List list;
list.append(Py::Object(pShape , true));
return Py::new_reference_to(list);
std::vector<TopoDS_Edge> edgeList;

} PY_CATCH_OCC;
try {
Py::Sequence list(pcObj);
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
if (PyObject_TypeCheck((*it).ptr(), &(Part::TopoShapeEdgePy::Type))) {
const TopoDS_Shape& sh = static_cast<TopoShapePy*>((*it).ptr())->
getTopoShapePtr()->getShape();
const TopoDS_Edge e = TopoDS::Edge(sh);
edgeList.push_back(e);
}
}
}
catch (Standard_Failure) {
Handle_Standard_Failure e = Standard_Failure::Caught();
throw Py::Exception(Part::PartExceptionOCCError, e->GetMessageString());
}

bool biggie;
if (inclBig == Py_True) {
biggie = true;
} else {
biggie = false;
}
PyObject* result = PyList_New(0);

EdgeWalker ew;
ew.loadEdges(edgeList);
ew.perform();
std::vector<TopoDS_Wire> rw = ew.getResultWires();
std::vector<TopoDS_Wire> sortedWires = ew.sortStrip(rw,biggie); //false==>do not include biggest wires
for (auto& w:sortedWires) {
PyList_Append(result,new TopoShapeWirePy(new TopoShape(w)));
}

return Py::asObject(result);
}

Py::Object findOuterWire(const Py::Tuple& args)
{
PyObject *pcObj;
if (!PyArg_ParseTuple(args.ptr(), "O", &pcObj)) {
throw Py::Exception();
}

std::vector<TopoDS_Edge> edgeList;

try {
Py::Sequence list(pcObj);
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
if (PyObject_TypeCheck((*it).ptr(), &(Part::TopoShapeEdgePy::Type))) {
const TopoDS_Shape& sh = static_cast<TopoShapePy*>((*it).ptr())->
getTopoShapePtr()->getShape();
const TopoDS_Edge e = TopoDS::Edge(sh);
edgeList.push_back(e);
}
}
}
catch (Standard_Failure) {
Handle_Standard_Failure e = Standard_Failure::Caught();
throw Py::Exception(Part::PartExceptionOCCError, e->GetMessageString());
}

PyObject* result = PyList_New(0);

EdgeWalker ew;
ew.loadEdges(edgeList);
ew.perform();
std::vector<TopoDS_Wire> rw = ew.getResultWires();
std::vector<TopoDS_Wire> sortedWires = ew.sortStrip(rw,true);
for (auto& w:sortedWires) {
PyList_Append(result,new TopoShapeWirePy(new TopoShape(w)));
}
PyObject* outerWire = PyList_GetItem(result, 0);
return Py::asObject(outerWire);
}
};

PyObject* initModule()
{
return (new Module)->module().ptr();
}

/* registration table */
struct PyMethodDef TechDraw_methods[] = {
{"tdPlaceholder" ,tdPlaceholder ,METH_VARARGS,
"[n/a] = tdPlaceholder(n/a) -- Temporary hack."},
{NULL, NULL} /* end of table marker */
};
} // namespace TechDraw
16 changes: 16 additions & 0 deletions src/Mod/TechDraw/App/DrawUtil.cpp
Expand Up @@ -32,6 +32,11 @@
# include <QString>
# include <QStringList>
# include <QRegExp>

//#include <TopoDS_Vertex.hxx>
#include <BRep_Tool.hxx>
#include <gp_Pnt.hxx>
#include <Precision.hxx>
#endif

#include <App/Application.h>
Expand Down Expand Up @@ -93,3 +98,14 @@ std::string DrawUtil::makeGeomName(std::string geomType, int index)
return newName.str();
}


bool DrawUtil::isSamePoint(TopoDS_Vertex v1, TopoDS_Vertex v2)
{
bool result = false;
gp_Pnt p1 = BRep_Tool::Pnt(v1);
gp_Pnt p2 = BRep_Tool::Pnt(v2);
if (p1.IsEqual(p2,Precision::Confusion())) {
result = true;
}
return result;
}
2 changes: 2 additions & 0 deletions src/Mod/TechDraw/App/DrawUtil.h
Expand Up @@ -24,6 +24,7 @@
#define _DrawUtil_h_

#include <string>
#include <TopoDS_Vertex.hxx>

namespace TechDraw
{
Expand All @@ -34,6 +35,7 @@ class TechDrawExport DrawUtil {
static int getIndexFromName(std::string geomName);
static std::string getGeomTypeFromName(std::string geomName);
static std::string makeGeomName(std::string geomType, int index);
static bool isSamePoint(TopoDS_Vertex v1, TopoDS_Vertex v2);
};

} //end namespace TechDraw
Expand Down

0 comments on commit 9a93185

Please sign in to comment.