Skip to content

Commit

Permalink
Part: start to expose ShapeFix framework to Python
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Jun 21, 2022
1 parent 6b2882e commit f3e941a
Show file tree
Hide file tree
Showing 7 changed files with 327 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Mod/Part/App/AppPart.cpp
Expand Up @@ -99,7 +99,9 @@
#include "Mod/Part/App/PlanePy.h"
#include "Mod/Part/App/PointPy.h"
#include <Mod/Part/App/PrecisionPy.h>
#include "Mod/Part/App/RectangularTrimmedSurfacePy.h"
#include <Mod/Part/App/RectangularTrimmedSurfacePy.h>
#include <Mod/Part/App/ShapeFix/ShapeFix_RootPy.h>
#include <Mod/Part/App/ShapeFix/ShapeFix_ShellPy.h>
#include <Mod/Part/App/ShapeUpgrade/UnifySameDomainPy.h>
#include "Mod/Part/App/SpherePy.h"
#include "Mod/Part/App/SurfaceOfExtrusionPy.h"
Expand Down Expand Up @@ -337,6 +339,11 @@ PyMOD_INIT_FUNC(Part)
Base::Interpreter().addType(&Part::CurveConstraintPy::Type, geomPlate, "CurveConstraint");
Base::Interpreter().addType(&Part::PointConstraintPy::Type, geomPlate, "PointConstraint");

// ShapeFix sub-module
PyObject* shapeFix(module.getAttr("ShapeFix").ptr());
Base::Interpreter().addType(&Part::ShapeFix_RootPy::Type, shapeFix, "Root");
Base::Interpreter().addType(&Part::ShapeFix_ShellPy::Type, shapeFix, "Shell");

// ShapeUpgrade sub-module
PyObject* shapeUpgrade(module.getAttr("ShapeUpgrade").ptr());
Base::Interpreter().addType(&Part::UnifySameDomainPy::Type, shapeUpgrade, "UnifySameDomain");
Expand Down
13 changes: 13 additions & 0 deletions src/Mod/Part/App/AppPartPy.cpp
Expand Up @@ -303,6 +303,17 @@ class HLRBRepModule : public Py::ExtensionModule<HLRBRepModule>
virtual ~HLRBRepModule() {}
};

class ShapeFixModule : public Py::ExtensionModule<ShapeFixModule>
{
public:
ShapeFixModule() : Py::ExtensionModule<ShapeFixModule>("ShapeFix")
{
initialize("This is a module working with the ShapeFix framework."); // register with Python
}

virtual ~ShapeFixModule() {}
};

class ShapeUpgradeModule : public Py::ExtensionModule<ShapeUpgradeModule>
{
public:
Expand Down Expand Up @@ -332,6 +343,7 @@ class Module : public Py::ExtensionModule<Module>
Geom2dModule geom2d;
GeomPlateModule geomPlate;
HLRBRepModule HLRBRep;
ShapeFixModule shapeFix;
ShapeUpgradeModule shapeUpgrade;
ChFi2dModule chFi2d;
public:
Expand Down Expand Up @@ -559,6 +571,7 @@ class Module : public Py::ExtensionModule<Module>
PyModule_AddObject(m_module, "Geom2d", geom2d.module().ptr());
PyModule_AddObject(m_module, "GeomPlate", geomPlate.module().ptr());
PyModule_AddObject(m_module, "HLRBRep", HLRBRep.module().ptr());
PyModule_AddObject(m_module, "ShapeFix", shapeFix.module().ptr());
PyModule_AddObject(m_module, "ShapeUpgrade", shapeUpgrade.module().ptr());
PyModule_AddObject(m_module, "ChFi2d", chFi2d.module().ptr());
}
Expand Down
15 changes: 15 additions & 0 deletions src/Mod/Part/App/CMakeLists.txt
Expand Up @@ -99,6 +99,7 @@ file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/ChFi2d)
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/Geom2d)
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/GeomPlate)
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/HLRBRep)
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/ShapeFix)
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/ShapeUpgrade)

generate_from_xml(BRepFeat/MakePrismPy)
Expand Down Expand Up @@ -135,6 +136,9 @@ generate_from_xml(HLRBRep/HLRToShapePy)
generate_from_xml(HLRBRep/HLRBRep_PolyAlgoPy)
generate_from_xml(HLRBRep/PolyHLRToShapePy)

generate_from_xml(ShapeFix/ShapeFix_RootPy)
generate_from_xml(ShapeFix/ShapeFix_ShellPy)

generate_from_xml(ShapeUpgrade/UnifySameDomainPy)

SET(Features_SRCS
Expand Down Expand Up @@ -417,6 +421,16 @@ SET(HLRBRepPy_SRCS
)
SOURCE_GROUP("HLRBRep" FILES ${HLRBRepPy_SRCS})

# ShapeFix wrappers
SET(ShapeFixPy_SRCS
ShapeFix/ShapeFix_RootPy.xml
ShapeFix/ShapeFix_RootPyImp.cpp
ShapeFix/ShapeFix_ShellPy.xml
ShapeFix/ShapeFix_ShellPyImp.cpp
)

SOURCE_GROUP("ShapeFix" FILES ${ShapeFixPy_SRCS})

# ShapeUpgrade wrappers
SET(ShapeUpgradePy_SRCS
ShapeUpgrade/UnifySameDomainPy.xml
Expand All @@ -434,6 +448,7 @@ SET(Part_SRCS
${Geom2dPy_SRCS}
${GeomPlatePy_SRCS}
${HLRBRepPy_SRCS}
${ShapeFixPy_SRCS}
${ShapeUpgradePy_SRCS}
Attacher.cpp
Attacher.h
Expand Down
43 changes: 43 additions & 0 deletions src/Mod/Part/App/ShapeFix/ShapeFix_RootPy.xml
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<GenerateModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="generateMetaModel_Module.xsd">
<PythonExport
Father="PyObjectBase"
Name="ShapeFix_RootPy"
PythonName="Part.ShapeFix.Root"
Twin="ShapeFix_Root"
TwinPointer="ShapeFix_Root"
Include="ShapeFix_Root.hxx"
Namespace="Part"
FatherInclude="Base/PyObjectBase.h"
FatherNamespace="Base"
Constructor="true"
Delete="true">
<Documentation>
<Author Licence="LGPL" Name="Werner Mayer" EMail="wmayer@users.sourceforge.net" />
<UserDocu>Root class for fixing operations</UserDocu>
</Documentation>
<Methode Name="limitTolerance" Const="true">
<Documentation>
<UserDocu>Returns tolerance limited by [MinTolerance,MaxTolerance]</UserDocu>
</Documentation>
</Methode>
<Attribute Name="Precision" ReadOnly="false">
<Documentation>
<UserDocu>Basic precision value</UserDocu>
</Documentation>
<Parameter Name="Precision" Type="Float"/>
</Attribute>
<Attribute Name="MinTolerance" ReadOnly="false">
<Documentation>
<UserDocu>Minimal allowed tolerance</UserDocu>
</Documentation>
<Parameter Name="MinTolerance" Type="Float"/>
</Attribute>
<Attribute Name="MaxTolerance" ReadOnly="false">
<Documentation>
<UserDocu>Maximal allowed tolerance</UserDocu>
</Documentation>
<Parameter Name="MaxTolerance" Type="Float"/>
</Attribute>
</PythonExport>
</GenerateModel>
101 changes: 101 additions & 0 deletions src/Mod/Part/App/ShapeFix/ShapeFix_RootPyImp.cpp
@@ -0,0 +1,101 @@
/***************************************************************************
* Copyright (c) 2022 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 <Precision.hxx>
# include <Standard_Failure.hxx>
#endif

#include "ShapeFix/ShapeFix_RootPy.h"
#include "ShapeFix/ShapeFix_RootPy.cpp"

using namespace Part;

// returns a string which represents the object e.g. when printed in python
std::string ShapeFix_RootPy::representation() const
{
return "<ShapeFix_Root object>";
}

PyObject *ShapeFix_RootPy::PyMake(struct _typeobject *, PyObject *, PyObject *) // Python wrapper
{
// create a new instance of ShapeFix_RootPy
return new ShapeFix_RootPy(new ShapeFix_Root);
}

// constructor method
int ShapeFix_RootPy::PyInit(PyObject* /*args*/, PyObject* /*kwds*/)
{
return 0;
}

PyObject* ShapeFix_RootPy::limitTolerance(PyObject *args)
{
double tol;
if (!PyArg_ParseTuple(args, "d", &tol))
return nullptr;

tol = getShapeFix_RootPtr()->LimitTolerance(tol);
return Py::new_reference_to(Py::Float(tol));
}

Py::Float ShapeFix_RootPy::getPrecision() const
{
return Py::Float(getShapeFix_RootPtr()->Precision());
}

void ShapeFix_RootPy::setPrecision(Py::Float arg)
{
getShapeFix_RootPtr()->SetPrecision(arg);
}

Py::Float ShapeFix_RootPy::getMinTolerance() const
{
return Py::Float(getShapeFix_RootPtr()->MinTolerance());
}

void ShapeFix_RootPy::setMinTolerance(Py::Float arg)
{
getShapeFix_RootPtr()->SetMinTolerance(arg);
}

Py::Float ShapeFix_RootPy::getMaxTolerance() const
{
return Py::Float(getShapeFix_RootPtr()->MaxTolerance());
}

void ShapeFix_RootPy::setMaxTolerance(Py::Float arg)
{
getShapeFix_RootPtr()->SetMaxTolerance(arg);
}

PyObject *ShapeFix_RootPy::getCustomAttributes(const char* /*attr*/) const
{
return nullptr;
}

int ShapeFix_RootPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
{
return 0;
}
39 changes: 39 additions & 0 deletions src/Mod/Part/App/ShapeFix/ShapeFix_ShellPy.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="ShapeFix_RootPy"
Name="ShapeFix_ShellPy"
PythonName="Part.ShapeFix.Shell"
Twin="ShapeFix_Shell"
TwinPointer="ShapeFix_Shell"
Include="ShapeFix_Shell.hxx"
Namespace="Part"
FatherInclude="Mod/Part/App/ShapeFix/ShapeFix_RootPy.h"
FatherNamespace="Part"
Constructor="true">
<Documentation>
<Author Licence="LGPL" Name="Werner Mayer" EMail="wmayer@users.sourceforge.net" />
<UserDocu>Root class for fixing operations</UserDocu>
</Documentation>
<Methode Name="init">
<Documentation>
<UserDocu>Initializes by shell</UserDocu>
</Documentation>
</Methode>
<Methode Name="perform">
<Documentation>
<UserDocu>Iterates on subshapes and performs fixes</UserDocu>
</Documentation>
</Methode>
<Methode Name="shell">
<Documentation>
<UserDocu>Returns fixed shell (or subset of oriented faces)</UserDocu>
</Documentation>
</Methode>
<Methode Name="shape">
<Documentation>
<UserDocu>In case of multiconnexity returns compound of fixed shells and one shell otherwise</UserDocu>
</Documentation>
</Methode>
</PythonExport>
</GenerateModel>
108 changes: 108 additions & 0 deletions src/Mod/Part/App/ShapeFix/ShapeFix_ShellPyImp.cpp
@@ -0,0 +1,108 @@
/***************************************************************************
* Copyright (c) 2022 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 <Precision.hxx>
# include <Standard_Failure.hxx>
# include <TopoDS.hxx>
#endif

#include "ShapeFix/ShapeFix_ShellPy.h"
#include "ShapeFix/ShapeFix_ShellPy.cpp"
#include <Mod/Part/App/TopoShapeShellPy.h>

using namespace Part;

// returns a string which represents the object e.g. when printed in python
std::string ShapeFix_ShellPy::representation() const
{
return "<ShapeFix_Shell object>";
}

PyObject *ShapeFix_ShellPy::PyMake(struct _typeobject *, PyObject *, PyObject *) // Python wrapper
{
// create a new instance of ShapeFix_RootPy
return new ShapeFix_ShellPy(new ShapeFix_Shell);
}

// constructor method
int ShapeFix_ShellPy::PyInit(PyObject* args, PyObject* /*kwds*/)
{
PyObject* shell = nullptr;
if (!PyArg_ParseTuple(args, "|O!", &TopoShapeShellPy::Type, &shell))
return -1;

if (shell) {
getShapeFix_ShellPtr()->Init(TopoDS::Shell(static_cast<TopoShapePy*>(shell)->getTopoShapePtr()->getShape()));
}

return 0;
}

PyObject* ShapeFix_ShellPy::init(PyObject *args)
{
PyObject* shell;
if (!PyArg_ParseTuple(args, "O!", &TopoShapeShellPy::Type, &shell))
return nullptr;

getShapeFix_ShellPtr()->Init(TopoDS::Shell(static_cast<TopoShapePy*>(shell)->getTopoShapePtr()->getShape()));
Py_Return;
}

PyObject* ShapeFix_ShellPy::perform(PyObject *args)
{
if (!PyArg_ParseTuple(args, ""))
return nullptr;

Standard_Boolean ok = getShapeFix_ShellPtr()->Perform();
return Py::new_reference_to(Py::Boolean(ok ? true : false));
}

PyObject* ShapeFix_ShellPy::shell(PyObject *args)
{
if (!PyArg_ParseTuple(args, ""))
return nullptr;

TopoShape shape = getShapeFix_ShellPtr()->Shell();
return shape.getPyObject();
}

PyObject* ShapeFix_ShellPy::shape(PyObject *args)
{
if (!PyArg_ParseTuple(args, ""))
return nullptr;

TopoShape shape = getShapeFix_ShellPtr()->Shape();
return shape.getPyObject();
}

PyObject *ShapeFix_ShellPy::getCustomAttributes(const char* /*attr*/) const
{
return nullptr;
}

int ShapeFix_ShellPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
{
return 0;
}

0 comments on commit f3e941a

Please sign in to comment.