Skip to content

Commit

Permalink
Sketcher: Geometry Facade interface for Geometry and SketchGeometryEx…
Browse files Browse the repository at this point in the history
…tension
  • Loading branch information
abdullahtahiriyo committed Nov 3, 2020
1 parent 7bfec92 commit 0fd808d
Show file tree
Hide file tree
Showing 7 changed files with 847 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Mod/Sketcher/App/CMakeLists.txt
Expand Up @@ -38,6 +38,7 @@ generate_from_xml(SketchObjectSFPy)
generate_from_xml(SketchObjectPy)
generate_from_xml(SketchGeometryExtensionPy)
generate_from_xml(ExternalGeometryExtensionPy)
generate_from_xml(GeometryFacadePy)
generate_from_xml(ConstraintPy)
generate_from_xml(SketchPy)

Expand Down Expand Up @@ -69,6 +70,8 @@ SET(Datatypes_SRCS
Constraint.h
Sketch.cpp
Sketch.h
GeometryFacade.cpp
GeometryFacade.h
)
SOURCE_GROUP("Datatypes" FILES ${Datatypes_SRCS})

Expand All @@ -81,6 +84,8 @@ SET(Python_SRCS
SketchGeometryExtensionPyImp.cpp
ExternalGeometryExtensionPy.xml
ExternalGeometryExtensionPyImp.cpp
GeometryFacadePy.xml
GeometryFacadePyImp.cpp
ConstraintPyImp.cpp
ConstraintPy.xml
SketchPy.xml
Expand Down
107 changes: 107 additions & 0 deletions src/Mod/Sketcher/App/GeometryFacade.cpp
@@ -0,0 +1,107 @@
/***************************************************************************
* Copyright (c) 2020 Abdullah Tahiri <abdullah.tahiri.yo@gmail.com> *
* *
* 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_

#endif

#include "GeometryFacade.h"

#include <Base/Console.h> // Only for Debug - To be removed
#include <Base/Exception.h>
#include <boost/uuid/uuid_io.hpp>

#include "GeometryFacadePy.h"

using namespace Sketcher;

TYPESYSTEM_SOURCE(Sketcher::GeometryFacade,Part::GeometryExtension)

GeometryFacade::GeometryFacade(): Geo(nullptr), SketchGeoExtension(nullptr)
{

}

GeometryFacade::GeometryFacade(const Part::Geometry * geometry)
: Geo(geometry)
{
if(geometry != nullptr)
initExtension();
else
THROWM(Base::ValueError, "GeometryFacade initialized with Geometry null pointer");
}

std::unique_ptr<GeometryFacade> GeometryFacade::getFacade(Part::Geometry * geometry)
{
return std::unique_ptr<GeometryFacade>(new GeometryFacade(geometry));
//return std::make_unique<GeometryFacade>(geometry); // make_unique has no access to private constructor
}

std::unique_ptr<const GeometryFacade> GeometryFacade::getFacade(const Part::Geometry * geometry)
{
return std::unique_ptr<const GeometryFacade>(new GeometryFacade(geometry));
//return std::make_unique<const GeometryFacade>(geometry); // make_unique has no access to private constructor
}

void GeometryFacade::setGeometry(Part::Geometry *geometry)
{
Geo = geometry;

if(geometry != nullptr)
initExtension();
else
THROWM(Base::ValueError, "GeometryFacade initialized with Geometry null pointer");
}

void GeometryFacade::initExtension()
{
if(!Geo->hasExtension(SketchGeometryExtension::getClassTypeId())) {

getGeo()->setExtension(std::make_unique<SketchGeometryExtension>()); // Create getExtension

Base::Console().Warning("%s\nSketcher Geometry without Extension: %s \n", boost::uuids::to_string(Geo->getTag()).c_str());
}

SketchGeoExtension =
std::static_pointer_cast<const SketchGeometryExtension>(
(Geo->getExtension(SketchGeometryExtension::getClassTypeId())).lock()
);
}

void GeometryFacade::initExtension() const
{
if(!Geo->hasExtension(SketchGeometryExtension::getClassTypeId()))
THROWM(Base::ValueError, "GeometryConstFacade for const::Geometry without SketchGeometryExtension");

auto ext = std::static_pointer_cast<const SketchGeometryExtension>(Geo->getExtension(SketchGeometryExtension::getClassTypeId()).lock());

const_cast<GeometryFacade *>(this)->SketchGeoExtension = ext;
}



PyObject * GeometryFacade::getPyObject(void)
{
return new GeometryFacadePy(new GeometryFacade(this->Geo));
}
133 changes: 133 additions & 0 deletions src/Mod/Sketcher/App/GeometryFacade.h
@@ -0,0 +1,133 @@
/***************************************************************************
* Copyright (c) 2020 Abdullah Tahiri <abdullah.tahiri.yo@gmail.com> *
* *
* 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 *
* *
***************************************************************************/


#ifndef SKETCHER_GEOMETRYFACADE_H
#define SKETCHER_GEOMETRYFACADE_H

#include <Base/BaseClass.h>

#include <Base/Console.h> // Only for Debug - To be removed
#include <boost/uuid/uuid_io.hpp>

#include <Mod/Part/App/Geometry.h>
#include <Mod/Sketcher/App/SketchGeometryExtension.h>

namespace Sketcher
{

// This class is a Facade to handle geometry and sketcher geometry extensions with a single sketcher specific interface
//
//
//
// It is intended to have a separate type (not being a Geometry type).
// it is intended to have the relevant interface for the sketcher only
// It is intended to work on borrowed memory allocation.
class SketcherExport GeometryFacade : public Base::BaseClass, ISketchGeometryExtension
{
TYPESYSTEM_HEADER_WITH_OVERRIDE();

private:
GeometryFacade(const Part::Geometry * geometry);

public:
GeometryFacade(); // As TYPESYSTEM requirement for Python object construction

public: // Factory methods
static std::unique_ptr<GeometryFacade> getFacade(Part::Geometry * geometry);
static std::unique_ptr<const GeometryFacade> getFacade(const Part::Geometry * geometry);

public:
void setGeometry(Part::Geometry *geometry);

// Geometry Extension Interface
inline virtual long getId() const override {return getGeoExt()->getId();}
virtual void setId(long id) override {getGeoExt()->setId(id);}

// Geometry Extension Information
inline const std::string &getExtensionName () const {return SketchGeoExtension->getName();}

// Geometry Element
template < typename GeometryT = Part::Geometry,
typename = typename std::enable_if<
std::is_base_of<Part::Geometry, typename std::decay<GeometryT>::type>::value
>::type
>
GeometryT * getGeometry() {return dynamic_cast<GeometryT *>(const_cast<GeometryT *>(Geo));}

// Geometry Element
template < typename GeometryT = Part::Geometry,
typename = typename std::enable_if<
std::is_base_of<Part::Geometry, typename std::decay<GeometryT>::type>::value
>::type
>
GeometryT * getGeometry() const {return dynamic_cast<GeometryT *>(Geo);}

virtual PyObject *getPyObject(void) override;

// Geometry Interface
TopoDS_Shape toShape() const {return getGeo()->toShape();};
const Handle(Geom_Geometry)& handle() const {return getGeo()->handle();};
Part::Geometry *copy(void) const {return getGeo()->copy();};
Part::Geometry *clone(void) const {return getGeo()->clone();};
inline bool getConstruction(void) const {return getGeo()->getConstruction();};
inline void setConstruction(bool construction) {getGeo()->setConstruction(construction);};
boost::uuids::uuid getTag() const {return getGeo()->getTag();};

std::vector<std::weak_ptr<const Part::GeometryExtension>> getExtensions() const {return getGeo()->getExtensions();};
bool hasExtension(Base::Type type) const {return getGeo()->hasExtension(type);};
bool hasExtension(std::string name) const {return getGeo()->hasExtension(name);};
std::weak_ptr<const Part::GeometryExtension> getExtension(Base::Type type) const {return getGeo()->getExtension(type);};
std::weak_ptr<const Part::GeometryExtension> getExtension(std::string name) const {return getGeo()->getExtension(name);};
void setExtension(std::unique_ptr<Part::GeometryExtension> &&geo) {return getGeo()->setExtension(std::move(geo));};
void deleteExtension(Base::Type type) {return getGeo()->deleteExtension(type);};
void deleteExtension(std::string name) {return getGeo()->deleteExtension(name);};

void mirror(Base::Vector3d point) {return getGeo()->mirror(point);};
void mirror(Base::Vector3d point, Base::Vector3d dir) {return getGeo()->mirror(point, dir);};
void rotate(Base::Placement plm) {return getGeo()->rotate(plm);};
void scale(Base::Vector3d vec, double scale) {return getGeo()->scale(vec, scale);};
void transform(Base::Matrix4D mat) {return getGeo()->transform(mat);};
void translate(Base::Vector3d vec) {return getGeo()->translate(vec);};

private:
void initExtension(void);
void initExtension(void) const;

const Part::Geometry * getGeo(void) const {return Geo;}
Part::Geometry * getGeo(void) {return const_cast<Part::Geometry *>(Geo);}

std::shared_ptr<const SketchGeometryExtension> getGeoExt(void) const {return SketchGeoExtension;}
std::shared_ptr<SketchGeometryExtension> getGeoExt (void) {return std::const_pointer_cast<SketchGeometryExtension>(SketchGeoExtension);}

private:
const Part::Geometry * Geo;
std::shared_ptr<const SketchGeometryExtension> SketchGeoExtension;
};




} //namespace Sketcher


#endif // SKETCHER_GEOMETRYFACADE_H
111 changes: 111 additions & 0 deletions src/Mod/Sketcher/App/GeometryFacadePy.xml
@@ -0,0 +1,111 @@
<?xml version="1.0" encoding="UTF-8"?>
<GenerateModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="generateMetaModel_Module.xsd">
<PythonExport
Father="BaseClassPy"
Name="GeometryFacadePy"
PythonName="Sketcher.GeometryFacade"
Twin="GeometryFacade"
TwinPointer="GeometryFacade"
Include="Mod/Sketcher/App/GeometryFacade.h"
Namespace="Sketcher"
FatherInclude="Base/BaseClassPy.h"
FatherNamespace="Base"
Constructor="true">
<Documentation>
<Author Licence="LGPL" Name="Abdullah Tahiri" EMail="abdullah.tahiri.yo@gmail.com" />
<UserDocu>Describes a GeometryFacade</UserDocu>
</Documentation>
<Attribute Name="Id" ReadOnly="false">
<Documentation>
<UserDocu>
returns the Id of the SketchGeometryExtension.
</UserDocu>
</Documentation>
<Parameter Name="Id" Type="Long"/>
</Attribute>
<Methode Name="mirror">
<Documentation>
<UserDocu>Performs the symmetrical transformation of this geometric object</UserDocu>
</Documentation>
</Methode>
<Methode Name="rotate">
<Documentation>
<UserDocu>Rotates this geometric object at angle Ang (in radians) about axis</UserDocu>
</Documentation>
</Methode>
<Methode Name="scale">
<Documentation>
<UserDocu>Applies a scaling transformation on this geometric object with a center and scaling factor</UserDocu>
</Documentation>
</Methode>
<Methode Name="transform">
<Documentation>
<UserDocu>Applies a transformation to this geometric object</UserDocu>
</Documentation>
</Methode>
<Methode Name="translate">
<Documentation>
<UserDocu>Translates this geometric object</UserDocu>
</Documentation>
</Methode>
<Methode Name="hasExtensionOfType" Const="true">
<Documentation>
<UserDocu>Returns a boolean indicating whether a geometry extension of the type indicated as a string exists.</UserDocu>
</Documentation>
</Methode>
<Methode Name="hasExtensionOfName" Const="true">
<Documentation>
<UserDocu>Returns a boolean indicating whether a geometry extension with the name indicated as a string exists.</UserDocu>
</Documentation>
</Methode>
<Methode Name="getExtensionOfType" Const="true">
<Documentation>
<UserDocu>Gets the first geometry extension of the type indicated by the string.</UserDocu>
</Documentation>
</Methode>
<Methode Name="getExtensionOfName" Const="true">
<Documentation>
<UserDocu>Gets the first geometry extension of the name indicated by the string.</UserDocu>
</Documentation>
</Methode>
<Methode Name="setExtension" Const="false">
<Documentation>
<UserDocu>Sets a geometry extension of the indicated type.</UserDocu>
</Documentation>
</Methode>
<Methode Name="deleteExtensionOfType" Const="false">
<Documentation>
<UserDocu>Deletes all extensions of the indicated type.</UserDocu>
</Documentation>
</Methode>
<Methode Name="deleteExtensionOfName" Const="false">
<Documentation>
<UserDocu>Deletes all extensions of the indicated name.</UserDocu>
</Documentation>
</Methode>
<Methode Name="getExtensions" Const="true">
<Documentation>
<UserDocu>Returns a list with information about the geometry extensions.</UserDocu>
</Documentation>
</Methode>
<Attribute Name="Construction" ReadOnly="false">
<Documentation>
<UserDocu>Defines this geometry as a construction one which
means that it is not part of a later built shape.</UserDocu>
</Documentation>
<Parameter Name="Construction" Type="Boolean"/>
</Attribute>
<Attribute Name="Tag" ReadOnly="true">
<Documentation>
<UserDocu>Gives the tag of the geometry as string.</UserDocu>
</Documentation>
<Parameter Name="Tag" Type="String"/>
</Attribute>
<Attribute Name="Geometry" ReadOnly="false">
<Documentation>
<UserDocu>Gives the tag of the geometry as string.</UserDocu>
</Documentation>
<Parameter Name="Geometry" Type="Object"/>
</Attribute>
</PythonExport>
</GenerateModel>

0 comments on commit 0fd808d

Please sign in to comment.