Skip to content

Commit

Permalink
Part: Expose BRepOffsetAPI_MakeOffset to Python
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Jan 3, 2025
1 parent e977eb4 commit 2f11eb1
Show file tree
Hide file tree
Showing 4 changed files with 289 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Mod/Part/App/AppPart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
#include "PropertyTopoShapeList.h"

#include <BRepFeat/MakePrismPy.h>
#include <BRepOffsetAPI/MakeOffsetPy.h>

#include <ChFi2d/ChFi2d_AnaFilletAlgoPy.h>
#include <ChFi2d/ChFi2d_ChamferAPIPy.h>
Expand Down Expand Up @@ -341,6 +342,7 @@ PyMOD_INIT_FUNC(Part)
PyObject* brepOffsetApiModule(module.getAttr("BRepOffsetAPI").ptr());
Base::Interpreter().addType(&Part::BRepOffsetAPI_MakePipeShellPy::Type,brepOffsetApiModule,"MakePipeShell");
Base::Interpreter().addType(&Part::BRepOffsetAPI_MakeFillingPy::Type,brepOffsetApiModule,"MakeFilling");
Base::Interpreter().addType(&Part::MakeOffsetPy::Type,brepOffsetApiModule,"MakeOffset");

// HLRBRep package
PyObject* hlrfeatModule(module.getAttr("HLRBRep").ptr());
Expand Down
72 changes: 72 additions & 0 deletions src/Mod/Part/App/BRepOffsetAPI/MakeOffsetPy.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?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="MakeOffsetPy"
PythonName="Part.BRepOffsetAPI.MakeOffset"
Twin="BRepOffsetAPI_MakeOffsetFix"
TwinPointer="BRepOffsetAPI_MakeOffsetFix"
Include="Mod/Part/App/BRepOffsetAPI_MakeOffsetFix.h"
Namespace="Part"
FatherInclude="Base/PyObjectBase.h"
FatherNamespace="Base"
Constructor="true"
Delete="true">
<Documentation>
<Author Licence="LGPL" Name="Werner Mayer" EMail="wmayer[at]users.sourceforge.net" />
<UserDocu>Describes algorithms for offsetting wires</UserDocu>
</Documentation>
<Methode Name="init" Keyword="true">
<Documentation>
<UserDocu>
Initializes this algorithm to construct parallels to the spine.
Join defines the type of parallel generated by the
salient vertices of the spine.
The default type is GeomAbs_Arc where the vertices generate
sections of a circle.

If join type is GeomAbs_Intersection, the edges that
intersect in a salient vertex generate the edges
prolonged until intersection.
</UserDocu>
</Documentation>
</Methode>
<Methode Name="addWire">
<Documentation>
<UserDocu>
addWire(Spine:Wire)
Adds a wire.
</UserDocu>
</Documentation>
</Methode>
<Methode Name="build">
<Documentation>
<UserDocu>Builds the resulting shape.</UserDocu>
</Documentation>
</Methode>
<Methode Name="perform" Keyword="true">
<Documentation>
<UserDocu>
perform(Offset:float, Alt:float = 0.0)

Computes a parallel to the spine at distance Offset and
at an altitude Alt from the plane of the spine in relation
to the normal to the spine.
</UserDocu>
</Documentation>
</Methode>
<Methode Name="isDone">
<Documentation>
<UserDocu>Tests whether computation of the offset has been completed.</UserDocu>
</Documentation>
</Methode>
<Methode Name="shape">
<Documentation>
<UserDocu>
shape()
Returns the resulting shape.
</UserDocu>
</Documentation>
</Methode>
</PythonExport>
</GenerateModel>
205 changes: 205 additions & 0 deletions src/Mod/Part/App/BRepOffsetAPI/MakeOffsetPyImp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
// SPDX-License-Identifier: LGPL-2.1-or-later

/***************************************************************************
* Copyright (c) 2025 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* This file is part of FreeCAD. *
* *
* FreeCAD is free software: you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 2.1 of the *
* License, or (at your option) any later version. *
* *
* FreeCAD 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 *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with FreeCAD. If not, see *
* <https://www.gnu.org/licenses/>. *
* *
**************************************************************************/

#include "PreCompiled.h"
#ifndef _PreComp_
# include <memory>
# include <GeomAbs_Shape.hxx>
# include <gp_Pnt.hxx>
# include <TopoDS.hxx>
# include <TopoDS_Face.hxx>
#endif

#include <Base/PyWrapParseTupleAndKeywords.h>
#include <Base/VectorPy.h>

#include "BRepOffsetAPI/MakeOffsetPy.h"
#include "BRepOffsetAPI/MakeOffsetPy.cpp"
#include "TopoShapeWirePy.h"
#include "TopoShapeFacePy.h"


using namespace Part;

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

// constructor method
int MakeOffsetPy::PyInit(PyObject* args, PyObject* kwds)
{
int jointype {};
PyObject* openres {};

static const std::array<const char *, 3> keywords{"Join", "IsOpenResult", nullptr};
if (Base::Wrapped_ParseTupleAndKeywords(args, kwds, "iO!", keywords,
&jointype, &PyBool_Type, &openres)) {
std::unique_ptr<BRepOffsetAPI_MakeOffsetFix> ptr(new BRepOffsetAPI_MakeOffsetFix(
static_cast<GeomAbs_JoinType>(jointype),
Base::asBoolean(openres)));
setTwinPointer(ptr.release());
return 0;
}

PyErr_Clear();
if (PyArg_ParseTuple(args, "")) {
std::unique_ptr<BRepOffsetAPI_MakeOffsetFix> ptr(new BRepOffsetAPI_MakeOffsetFix());
setTwinPointer(ptr.release());
return 0;
}

PyErr_SetString(PyExc_TypeError, "supported signatures:\n"
"MakeOffset()\n"
"MakeOffset(Join [GeomAbs_JoinType], IsOpenResult [bool])\n");
return -1;
}

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

PyObject* MakeOffsetPy::init(PyObject *args, PyObject* kwds)
{
int jointype {};
PyObject* spine {};
PyObject* openres = Py_False;

static const std::array<const char *, 4> keywords_s{"Spine", "Join", "IsOpenResult", nullptr};
if (Base::Wrapped_ParseTupleAndKeywords(args, kwds, "O!|iO!", keywords_s,
&TopoShapeFacePy::Type, &spine,
&jointype,
&PyBool_Type, &openres)) {
const auto& shape = static_cast<TopoShapeFacePy*>(spine)->getTopoShapePtr()->getShape();
getBRepOffsetAPI_MakeOffsetFixPtr()->Init(TopoDS::Face(shape),
static_cast<GeomAbs_JoinType>(jointype),
Base::asBoolean(openres));
Py_Return;
}

PyErr_Clear();
static const std::array<const char *, 3> keywords{"Join", "IsOpenResult", nullptr};
if (Base::Wrapped_ParseTupleAndKeywords(args, kwds, "|iO!", keywords,
&jointype, &PyBool_Type, &openres)) {
getBRepOffsetAPI_MakeOffsetFixPtr()->Init(static_cast<GeomAbs_JoinType>(jointype),
Base::asBoolean(openres));
Py_Return;
}

PyErr_SetString(PyExc_TypeError, "supported signatures:\n"
"init(Spine:Face, Join:GeomAbs_JoinType = GeomAbs_Arc, IsOpenResult:bool = False)\n"
"init(Join:GeomAbs_JoinType = GeomAbs_Arc, IsOpenResult:bool = False)\n");
return nullptr;
}

PyObject* MakeOffsetPy::addWire(PyObject *args)
{
PyObject* wire {};
if (!PyArg_ParseTuple(args, "O!", &Part::TopoShapeWirePy::Type, &wire)) {
return nullptr;
}

try {
const auto& shape = static_cast<TopoShapeWirePy*>(wire)->getTopoShapePtr()->getShape();
getBRepOffsetAPI_MakeOffsetFixPtr()->AddWire(TopoDS::Wire(shape));
Py_Return;
}
catch (const Standard_Failure& e) {
PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
return nullptr;
}
}

PyObject* MakeOffsetPy::perform(PyObject *args, PyObject* kwds)
{
double offset {};
double alt {};
static const std::array<const char *, 3> keywords{"Offset", "Alt", nullptr};
if (!Base::Wrapped_ParseTupleAndKeywords(args, kwds, "d|d", keywords, &offset, &alt)) {
return nullptr;
}

try {
getBRepOffsetAPI_MakeOffsetFixPtr()->Perform(offset, alt);
Py_Return;
}
catch (const Standard_Failure& e) {
PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
return nullptr;
}
}

PyObject* MakeOffsetPy::build(PyObject *args)
{
if (!PyArg_ParseTuple(args, "")) {
return nullptr;
}

try {
getBRepOffsetAPI_MakeOffsetFixPtr()->Build();
Py_Return;
}
catch (const Standard_Failure& e) {
PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
return nullptr;
}
}

PyObject* MakeOffsetPy::isDone(PyObject *args)
{
if (!PyArg_ParseTuple(args, "")) {
return nullptr;
}

Py::Boolean res(getBRepOffsetAPI_MakeOffsetFixPtr()->IsDone());
return Py::new_reference_to(res);
}

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

try {
TopoShape shape(getBRepOffsetAPI_MakeOffsetFixPtr()->Shape());
return shape.getPyObject();
}
catch (const Standard_Failure& e) {
PyErr_SetString(PyExc_RuntimeError, e.GetMessageString());
return nullptr;
}
}

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

int MakeOffsetPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
{
return 0;
}
10 changes: 10 additions & 0 deletions src/Mod/Part/App/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ generate_from_xml(BRepOffsetAPI_MakeFillingPy)

# make sure to create the directory at configure time
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/BRepFeat)
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/BRepOffsetAPI)
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/ChFi2d)
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/Geom2d)
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/GeomPlate)
Expand All @@ -97,6 +98,7 @@ file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/ShapeFix)
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/ShapeUpgrade)

generate_from_xml(BRepFeat/MakePrismPy)
generate_from_xml(BRepOffsetAPI/MakeOffsetPy)

generate_from_xml(ChFi2d/ChFi2d_AnaFilletAlgoPy)
generate_from_xml(ChFi2d/ChFi2d_FilletAlgoPy)
Expand Down Expand Up @@ -373,6 +375,13 @@ SET(BRepFeatPy_SRCS
)
SOURCE_GROUP("BRepFeat" FILES ${BRepFeatPy_SRCS})

# BRepOffsetAPI wrappers
SET(BRepOffsetAPIPy_SRCS
BRepOffsetAPI/MakeOffsetPy.xml
BRepOffsetAPI/MakeOffsetPyImp.cpp
)
SOURCE_GROUP("BRepOffsetAPI" FILES ${BRepOffsetAPIPy_SRCS})

# ChFi2d wrappers
SET(ChFi2dPy_SRCS
ChFi2d/ChFi2d_AnaFilletAlgoPy.xml
Expand Down Expand Up @@ -505,6 +514,7 @@ SET(Part_SRCS
${Properties_SRCS}
${Python_SRCS}
${BRepFeatPy_SRCS}
${BRepOffsetAPIPy_SRCS}
${ChFi2dPy_SRCS}
${Geom2dPy_SRCS}
${GeomPlatePy_SRCS}
Expand Down

0 comments on commit 2f11eb1

Please sign in to comment.