Py binding for EdgeWalker
refactor EdgeWalker code from DVP,DVS
This commit is contained in:
@@ -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 *
|
||||
@@ -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;
|
||||
|
||||
static PyObject *
|
||||
tdPlaceholder(PyObject *self, PyObject *args)
|
||||
{
|
||||
PyObject *pcObjShape;
|
||||
PyObject *pcObjDir=0;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "O!|O!", &(TopoShapePy::Type), &pcObjShape,&(Base::VectorPy::Type), &pcObjDir)) // convert args: Python->C
|
||||
return NULL; // NULL triggers exception
|
||||
|
||||
PY_TRY {
|
||||
TopoShapePy* pShape = static_cast<TopoShapePy*>(pcObjShape);
|
||||
Py::List list;
|
||||
list.append(Py::Object(pShape , true));
|
||||
return Py::new_reference_to(list);
|
||||
|
||||
} PY_CATCH_OCC;
|
||||
#include "EdgeWalker.h"
|
||||
|
||||
namespace TechDraw {
|
||||
//module level static C++ functions go here
|
||||
}
|
||||
|
||||
/* registration table */
|
||||
struct PyMethodDef TechDraw_methods[] = {
|
||||
{"tdPlaceholder" ,tdPlaceholder ,METH_VARARGS,
|
||||
"[n/a] = tdPlaceholder(n/a) -- Temporary hack."},
|
||||
{NULL, NULL} /* end of table marker */
|
||||
};
|
||||
using Part::TopoShape;
|
||||
using Part::TopoShapePy;
|
||||
using Part::TopoShapeEdgePy;
|
||||
using Part::TopoShapeWirePy;
|
||||
|
||||
namespace TechDraw {
|
||||
|
||||
class Module : public Py::ExtensionModule<Module>
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
} // namespace TechDraw
|
||||
|
||||
Reference in New Issue
Block a user