Skip to content

Commit

Permalink
Merge pull request FreeCAD#15500 from bgbsww/bgbsww-toponamingSaveRes…
Browse files Browse the repository at this point in the history
…tore3

Toponaming: refactor getExportElementName to separate method
  • Loading branch information
chennes authored Jul 20, 2024
2 parents 51789ae + 159c811 commit 608cb18
Show file tree
Hide file tree
Showing 13 changed files with 206 additions and 20 deletions.
1 change: 1 addition & 0 deletions src/Mod/Part/App/AppPart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@ PyMOD_INIT_FUNC(Part)
Part::GeometryBoolExtension ::init();
Part::GeometryDoubleExtension ::init();
Part::GeometryMigrationExtension ::init();
Part::GeometryMigrationPersistenceExtension ::init();
Part::Geometry ::init();
Part::GeomPoint ::init();
Part::GeomCurve ::init();
Expand Down
13 changes: 10 additions & 3 deletions src/Mod/Part/App/GeometryMigrationExtension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,21 @@

using namespace Part;

TYPESYSTEM_SOURCE_ABSTRACT(Part::GeometryMigrationPersistenceExtension, Part::GeometryPersistenceExtension)

//---------- Geometry Extension
TYPESYSTEM_SOURCE(Part::GeometryMigrationExtension,Part::GeometryExtension)

void GeometryMigrationExtension::copyAttributes(Part::GeometryExtension * cpy) const
{

void GeometryMigrationExtension::copyAttributes(Part::GeometryExtension *cpy) const {
Part::GeometryExtension::copyAttributes(cpy);
static_cast<GeometryMigrationExtension *>(cpy)->ConstructionState = this->ConstructionState;
static_cast<GeometryMigrationExtension *>(cpy)->GeometryMigrationFlags = this->GeometryMigrationFlags;
static_cast<GeometryMigrationExtension *>(cpy)->GeometryMigrationFlags = this->GeometryMigrationFlags;

static_cast<GeometryMigrationExtension *>(cpy)->Id = this->Id;
static_cast<GeometryMigrationExtension *>(cpy)->Flags = this->Flags;
static_cast<GeometryMigrationExtension *>(cpy)->Ref = this->Ref;
static_cast<GeometryMigrationExtension *>(cpy)->RefIndex = this->RefIndex;
}

std::unique_ptr<Part::GeometryExtension> GeometryMigrationExtension::copy() const
Expand Down
44 changes: 42 additions & 2 deletions src/Mod/Part/App/GeometryMigrationExtension.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class PartExport GeometryMigrationExtension : public Part::GeometryExtension
enum MigrationType {
None = 0,
Construction = 1,
GeometryId = 2,
ExternalReference = 3,
NumMigrationType // Must be the last
};

Expand All @@ -60,8 +62,32 @@ class PartExport GeometryMigrationExtension : public Part::GeometryExtension
PyObject *getPyObject() override;


virtual bool getConstruction() const {return ConstructionState;}
virtual void setConstruction(bool construction) {ConstructionState = construction;}
virtual bool getConstruction() const { return ConstructionState; }

virtual void setConstruction(bool construction) {
ConstructionState = construction;
setMigrationType(Construction);
}

long getId() const { return Id; }

void setId(long id) {
Id = id;
setMigrationType(GeometryId);
}

const std::string &getRef() const { return Ref; }

int getRefIndex() const { return RefIndex; }

unsigned long getFlags() const { return Flags; }

void setReference(const char *ref, int index, unsigned long flags) {
Ref = ref ? ref : "";
RefIndex = index;
Flags = flags;
setMigrationType(ExternalReference);
}

virtual bool testMigrationType(int flag) const { return GeometryMigrationFlags.test((size_t)(flag)); };
virtual void setMigrationType(int flag, bool v=true) { GeometryMigrationFlags.set((size_t)(flag), v); };
Expand All @@ -76,7 +102,21 @@ class PartExport GeometryMigrationExtension : public Part::GeometryExtension
using MigrationTypeFlagType = std::bitset<32>;
MigrationTypeFlagType GeometryMigrationFlags;
bool ConstructionState{false};
long Id = 0;
int RefIndex = -1;
unsigned long Flags = 0;
std::string Ref;
};


class PartExport GeometryMigrationPersistenceExtension : public Part::GeometryPersistenceExtension
{
TYPESYSTEM_HEADER();
public:
// Called to extend 'Geometry' XML tag with additional attributes (eg Id)
virtual void preSave(Base::Writer &/*writer*/) const {}
// Called to add additional tag after 'GeometryExtensions' XML elements (eg Construction flag)
virtual void postSave(Base::Writer &/*writer*/) const {}
};

} //namespace Part
Expand Down
15 changes: 8 additions & 7 deletions src/Mod/Part/App/PartFeature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,12 @@ std::pair<std::string, std::string> Feature::getElementName(const char* name,
if (!prop) {
return App::GeoFeature::getElementName(name, type);
}
return getExportElementName(prop->getShape(), name);
}

TopoShape shape = prop->getShape();
std::pair<std::string, std::string> Feature::getExportElementName(TopoShape shape,
const char* name) const
{
Data::MappedElement mapped = shape.getElementName(name);
auto res = shape.shapeTypeAndIndex(mapped.index);
static const int MinLowerTopoNames = 3;
Expand Down Expand Up @@ -259,10 +263,10 @@ std::pair<std::string, std::string> Feature::getElementName(const char* name,
// disambiguation.
auto it = std::find(ancestors.begin(), ancestors.end(), res.second);
if (it == ancestors.end()) {
assert(0 && "ancestor not found"); // this shouldn't happened
assert(0 && "ancestor not found"); // this shouldn't happen
}
else {
op = Data::POSTFIX_TAG + std::to_string(it - ancestors.begin());
op = Data::POSTFIX_INDEX + std::to_string(it - ancestors.begin());
}
}

Expand All @@ -284,10 +288,8 @@ std::pair<std::string, std::string> Feature::getElementName(const char* name,
}
}
}
return App::GeoFeature::_getElementName(name, mapped);
}

if (!res.second && mapped.name) {
else if (!res.second && mapped.name) {
const char* dot = strchr(name, '.');
if (dot) {
++dot;
Expand Down Expand Up @@ -350,7 +352,6 @@ std::pair<std::string, std::string> Feature::getElementName(const char* name,
if (ancestors.size() == 1) {
idxName.setIndex(ancestors.front());
mapped.index = idxName;
return App::GeoFeature::_getElementName(name, mapped);
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/Mod/Part/App/PartFeature.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,17 @@ class PartExport Feature : public App::GeoFeature

void registerElementCache(const std::string &prefix, PropertyPartShape *prop);

/** Helper function to obtain mapped and indexed element name from a shape
* @params shape: source shape
* @param name: the input name, can be either mapped or indexed name
* @return Returns both the indexed and mapped name
*
* If the 'name' referencing a non-primary shape type, i.e. not
* Vertex/Edge/Face, this function will auto generate a name from primary
* sub-shapes.
*/
std::pair<std::string,std::string> getExportElementName(TopoShape shape, const char *name) const;

/**
* Build a history of changes
* MakeShape: The operation that created the changes, e.g. BRepAlgoAPI_Common
Expand Down
26 changes: 22 additions & 4 deletions src/Mod/Sketcher/App/ExternalGeometryExtension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,47 @@ using namespace Sketcher;
constexpr std::array<const char*, ExternalGeometryExtension::NumFlags>
ExternalGeometryExtension::flag2str;

TYPESYSTEM_SOURCE(Sketcher::ExternalGeometryExtension, Part::GeometryPersistenceExtension)
TYPESYSTEM_SOURCE(Sketcher::ExternalGeometryExtension, Part::GeometryMigrationPersistenceExtension)

void ExternalGeometryExtension::copyAttributes(Part::GeometryExtension* cpy) const
{
Part::GeometryPersistenceExtension::copyAttributes(cpy);

static_cast<ExternalGeometryExtension*>(cpy)->Ref = this->Ref;
static_cast<ExternalGeometryExtension*>(cpy)->RefIndex = this->RefIndex;
static_cast<ExternalGeometryExtension*>(cpy)->Flags = this->Flags;
}

void ExternalGeometryExtension::restoreAttributes(Base::XMLReader& reader)
{
Part::GeometryPersistenceExtension::restoreAttributes(reader);

Ref = reader.getAttribute("Ref");
Flags = FlagType(reader.getAttribute("Flags"));
Ref = reader.getAttribute("Ref", "");
RefIndex = reader.getAttributeAsInteger("RefIndex", "-1");
Flags = FlagType(reader.getAttributeAsUnsigned("Flags", "0"));
}

void ExternalGeometryExtension::saveAttributes(Base::Writer& writer) const
{
Part::GeometryPersistenceExtension::saveAttributes(writer);
writer.Stream() << "\" Ref=\"" << Base::Persistence::encodeAttribute(Ref);
writer.Stream() << "\" Flags=\"" << Flags.to_ulong();
if (RefIndex >= 0) {
writer.Stream() << "\" RefIndex=\"" << RefIndex;
}
}

writer.Stream() << "\" Ref=\"" << Ref << "\" Flags=\"" << Flags.to_string();
void ExternalGeometryExtension::preSave(Base::Writer& writer) const
{
if (Ref.size()) {
writer.Stream() << " ref=\"" << Base::Persistence::encodeAttribute(Ref) << "\"";
}
if (RefIndex >= 0) {
writer.Stream() << " refIndex=\"" << RefIndex << "\"";
}
if (Flags.any()) {
writer.Stream() << " flags=\"" << Flags.to_ulong() << "\"";
}
}

std::unique_ptr<Part::GeometryExtension> ExternalGeometryExtension::copy() const
Expand Down
43 changes: 42 additions & 1 deletion src/Mod/Sketcher/App/ExternalGeometryExtension.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <bitset>

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


Expand All @@ -40,18 +41,29 @@ class ISketchExternalGeometryExtension
// START_CREDIT_BLOCK: Credit under LGPL for this block to Zheng, Lei (realthunder)
// <[email protected]>
virtual bool testFlag(int flag) const = 0;

virtual void setFlag(int flag, bool v = true) = 0;

virtual unsigned long getFlags() const = 0;

virtual void setFlags(unsigned long flags) = 0;
// END_CREDIT_BLOCK: Credit under LGPL for this block to Zheng, Lei (realthunder)
// <[email protected]>

virtual bool isClear() const = 0;

virtual size_t flagSize() const = 0;

virtual const std::string& getRef() const = 0;

virtual void setRef(const std::string& ref) = 0;

virtual int getRefIndex() const = 0;

virtual void setRefIndex(int index) = 0;
};

class SketcherExport ExternalGeometryExtension: public Part::GeometryPersistenceExtension,
class SketcherExport ExternalGeometryExtension: public Part::GeometryMigrationPersistenceExtension,
private ISketchExternalGeometryExtension
{
TYPESYSTEM_HEADER_WITH_OVERRIDE();
Expand All @@ -76,6 +88,7 @@ class SketcherExport ExternalGeometryExtension: public Part::GeometryPersistence

public:
ExternalGeometryExtension() = default;

~ExternalGeometryExtension() override = default;

std::unique_ptr<Part::GeometryExtension> copy() const override;
Expand All @@ -88,17 +101,29 @@ class SketcherExport ExternalGeometryExtension: public Part::GeometryPersistence
{
return Flags.test((size_t)(flag));
}

void setFlag(int flag, bool v = true) override
{
Flags.set((size_t)(flag), v);
}

unsigned long getFlags() const override
{
return Flags.to_ulong();
}

void setFlags(unsigned long flags) override
{
Flags = flags;
}
// END_CREDIT_BLOCK: Credit under LGPL for this block to Zheng, Lei (realthunder)
// <[email protected]>

bool isClear() const override
{
return Flags.none();
}

size_t flagSize() const override
{
return Flags.size();
Expand All @@ -108,18 +133,33 @@ class SketcherExport ExternalGeometryExtension: public Part::GeometryPersistence
{
return Ref;
}

void setRef(const std::string& ref) override
{
Ref = ref;
}

int getRefIndex() const override
{
return RefIndex;
}

void setRefIndex(int index) override
{
RefIndex = index;
}

static bool getFlagsFromName(std::string str, ExternalGeometryExtension::Flag& flag);

protected:
void copyAttributes(Part::GeometryExtension* cpy) const override;

void restoreAttributes(Base::XMLReader& reader) override;

void saveAttributes(Base::Writer& writer) const override;

void preSave(Base::Writer& writer) const override;

private:
ExternalGeometryExtension(const ExternalGeometryExtension&) = default;

Expand All @@ -128,6 +168,7 @@ class SketcherExport ExternalGeometryExtension: public Part::GeometryPersistence
// START_CREDIT_BLOCK: Credit under LGPL for this block to Zheng, Lei (realthunder)
// <[email protected]>
std::string Ref;
int RefIndex = -1;
FlagType Flags;
// END_CREDIT_BLOCK: Credit under LGPL for this block to Zheng, Lei (realthunder)
// <[email protected]>
Expand Down
18 changes: 18 additions & 0 deletions src/Mod/Sketcher/App/ExternalGeometryFacade.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "ExternalGeometryFacade.h"
#include "ExternalGeometryFacadePy.h"

FC_LOG_LEVEL_INIT("Sketch", true, true);

Check warning on line 33 in src/Mod/Sketcher/App/ExternalGeometryFacade.cpp

View workflow job for this annotation

GitHub Actions / Lint / Lint

non-POD static (LogLevel) [-Wclazy-non-pod-global-static]

using namespace Sketcher;

Expand Down Expand Up @@ -160,6 +161,23 @@ void ExternalGeometryFacade::copyId(const Part::Geometry* src, Part::Geometry* d
gfdst->setId(gfsrc->getId());
}

void ExternalGeometryFacade::copyFlags(const Part::Geometry* src, Part::Geometry* dst)
{
auto gfsrc = ExternalGeometryFacade::getFacade(src);
auto gfdst = ExternalGeometryFacade::getFacade(dst);
gfdst->setFlags(gfsrc->getFlags());
}

void ExternalGeometryFacade::setRef(const std::string& ref)
{
if (ref.empty() && getId() < 0) {
FC_ERR("Cannot set reference on root geometries");
}
else {
getExternalGeoExt()->setRef(ref);
}
}

PyObject* ExternalGeometryFacade::getPyObject()
{
return new ExternalGeometryFacadePy(new ExternalGeometryFacade(this->Geo));
Expand Down
Loading

0 comments on commit 608cb18

Please sign in to comment.