Skip to content

Commit

Permalink
Sketcher: Geometry Extension for Internal Alignment Geometry
Browse files Browse the repository at this point in the history
============================================================

This commit extends SketchGeometryExtension to store within a Geometry if it is an internal aligment geometry
and which type.
  • Loading branch information
abdullahtahiriyo committed Dec 10, 2020
1 parent 2b35a7e commit 858d29f
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 4 deletions.
11 changes: 8 additions & 3 deletions src/Mod/Sketcher/App/SketchGeometryExtension.cpp
Expand Up @@ -31,19 +31,21 @@

using namespace Sketcher;


//---------- Geometry Extension
constexpr std::array<const char *, InternalType::NumInternalGeometryType> SketchGeometryExtension::internaltype2str;

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

// scoped within the class, multithread ready
std::atomic<long> SketchGeometryExtension::_GeometryID;

SketchGeometryExtension::SketchGeometryExtension():Id(++SketchGeometryExtension::_GeometryID)
SketchGeometryExtension::SketchGeometryExtension():Id(++SketchGeometryExtension::_GeometryID),InternalGeometryType(InternalType::None)
{

}

SketchGeometryExtension::SketchGeometryExtension(long cid):Id(cid)
SketchGeometryExtension::SketchGeometryExtension(long cid):Id(cid),InternalGeometryType(InternalType::None)
{

}
Expand All @@ -63,21 +65,24 @@ void SketchGeometryExtension::Save(Base::Writer &writer) const
if(name.size() > 0)
writer.Stream() << "\" name=\"" << name;

writer.Stream() << "\" id=\"" << Id << "\"/>" << std::endl;
writer.Stream() << "\" id=\"" << Id
<< "\" internalGeometryType=\"" << (int) InternalGeometryType << "\"/>" << std::endl;
}

void SketchGeometryExtension::Restore(Base::XMLReader &reader)
{
restoreNameAttribute(reader);

Id = reader.getAttributeAsInteger("id");
InternalGeometryType = (InternalType::InternalType) reader.getAttributeAsInteger("internalGeometryType");
}

std::unique_ptr<Part::GeometryExtension> SketchGeometryExtension::copy(void) const
{
auto cpy = std::make_unique<SketchGeometryExtension>();

cpy->Id = this->Id;
cpy->InternalGeometryType = this->InternalGeometryType;

cpy->setName(this->getName()); // Base Class

Expand Down
31 changes: 30 additions & 1 deletion src/Mod/Sketcher/App/SketchGeometryExtension.h
Expand Up @@ -25,23 +25,46 @@

#include <Mod/Part/App/Geometry.h>
#include <atomic>
#include <bitset>
#include <array>

namespace Sketcher
{

namespace InternalType {
enum InternalType {
None = 0,
EllipseMajorDiameter = 1,
EllipseMinorDiameter = 2,
EllipseFocus1 = 3,
EllipseFocus2 = 4,
HyperbolaMajor = 5,
HyperbolaMinor = 6,
HyperbolaFocus = 7,
ParabolaFocus = 8,
BSplineControlPoint = 9,
BSplineKnotPoint = 10,
NumInternalGeometryType // Must be the last
};
}

class ISketchGeometryExtension
{

public:
// Identification information
virtual long getId() const = 0;
virtual void setId(long id) = 0;

virtual InternalType::InternalType getInternalType() const = 0;
virtual void setInternalType(InternalType::InternalType type) = 0;
};

class SketcherExport SketchGeometryExtension : public Part::GeometryExtension, private ISketchGeometryExtension
{
TYPESYSTEM_HEADER_WITH_OVERRIDE();
public:

SketchGeometryExtension();
SketchGeometryExtension(long cid);
virtual ~SketchGeometryExtension() override = default;
Expand All @@ -58,11 +81,17 @@ class SketcherExport SketchGeometryExtension : public Part::GeometryExtension, p
virtual long getId() const override {return Id;}
virtual void setId(long id) override {Id = id;}

virtual InternalType::InternalType getInternalType() const override {return InternalGeometryType;}
virtual void setInternalType(InternalType::InternalType type) override {InternalGeometryType = type;}

constexpr static std::array<const char *,InternalType::NumInternalGeometryType> internaltype2str {{ "None", "EllipseMajorDiameter", "EllipseMinorDiameter","EllipseFocus1", "EllipseFocus2", "HyperbolaMajor", "HyperbolaMinor", "HyperbolaFocus", "ParabolaFocus", "BSplineControlPoint", "BSplineKnotPoint" }};

private:
SketchGeometryExtension(const SketchGeometryExtension&) = default;

private:
long Id;
long Id;
InternalType::InternalType InternalGeometryType;

private:
static std::atomic<long> _GeometryID;
Expand Down
8 changes: 8 additions & 0 deletions src/Mod/Sketcher/App/SketchGeometryExtensionPy.xml
Expand Up @@ -23,5 +23,13 @@
</Documentation>
<Parameter Name="Id" Type="Long"/>
</Attribute>
<Attribute Name="InternalType" ReadOnly="false">
<Documentation>
<UserDocu>
returns the Id of the SketchGeometryExtension.
</UserDocu>
</Documentation>
<Parameter Name="InternalType" Type="String"/>
</Attribute>
</PythonExport>
</GenerateModel>
33 changes: 33 additions & 0 deletions src/Mod/Sketcher/App/SketchGeometryExtensionPyImp.cpp
Expand Up @@ -89,6 +89,39 @@ void SketchGeometryExtensionPy::setId(Py::Long Id)
this->getSketchGeometryExtensionPtr()->setId(long(Id));
}

Py::String SketchGeometryExtensionPy::getInternalType(void) const
{
int internaltypeindex = (int)this->getSketchGeometryExtensionPtr()->getInternalType();

if(internaltypeindex >= InternalType::NumInternalGeometryType)
throw Py::NotImplementedError("String name of enum not implemented");

std::string typestr = this->getSketchGeometryExtensionPtr()->internaltype2str[internaltypeindex];

return Py::String(typestr);
}

void SketchGeometryExtensionPy::setInternalType(Py::String arg)
{
std::string argstr = arg;

auto pos = std::find_if(this->getSketchGeometryExtensionPtr()->internaltype2str.begin(),
getSketchGeometryExtensionPtr()->internaltype2str.end(),
[argstr](const char * val) {
return strcmp(val,argstr.c_str())==0;}
);

if( pos != getSketchGeometryExtensionPtr()->internaltype2str.end()) {
int index = std::distance( getSketchGeometryExtensionPtr()->internaltype2str.begin(), pos );

this->getSketchGeometryExtensionPtr()->setInternalType((InternalType::InternalType)index);
return;
}

throw Py::ValueError("Argument is not a valid internal geometry type.");
}


PyObject *SketchGeometryExtensionPy::getCustomAttributes(const char* /*attr*/) const
{
return 0;
Expand Down

0 comments on commit 858d29f

Please sign in to comment.