Merge pull request #12804 from bgbsww/bgbsww-toponamingPartFeature

Toponaming/Part: methods in part feature and dependencies for correct elementMaps
This commit is contained in:
Chris Hennes
2024-03-11 13:35:59 -05:00
committed by GitHub
10 changed files with 1023 additions and 243 deletions

View File

@@ -28,6 +28,7 @@
# include <unordered_set>
#endif
#include "DocumentObject.h"
#include "MappedElement.h"
using namespace Data;
@@ -161,4 +162,11 @@ bool ElementNameComparator::operator()(const MappedName& leftName,
}
}
return leftName.size() < rightName.size();
}
}
HistoryItem::HistoryItem(App::DocumentObject *obj, const Data::MappedName &name)
:obj(obj),tag(0),element(name)
{
if(obj)
tag = obj->getID();
}

View File

@@ -99,6 +99,15 @@ struct AppExport MappedElement
}
};
struct AppExport HistoryItem {
App::DocumentObject *obj;
long tag;
Data::MappedName element;
Data::IndexedName index;
std::vector<Data::MappedName> intermediates;
HistoryItem(App::DocumentObject *obj, const Data::MappedName &name);
};
struct AppExport ElementNameComparator {
/** Comparison function to make topo name more stable
*

File diff suppressed because it is too large Load Diff

View File

@@ -35,6 +35,11 @@
class gp_Dir;
class BRepBuilderAPI_MakeShape;
namespace Data
{
struct HistoryItem;
}
namespace Part
{
@@ -67,6 +72,34 @@ public:
std::pair<std::string,std::string> getElementName(
const char *name, ElementNameType type=Normal) const override;
static std::list<Data::HistoryItem> getElementHistory(App::DocumentObject *obj,
const char *name, bool recursive=true, bool sameType=false);
static QVector<Data::MappedElement>
getRelatedElements(App::DocumentObject* obj,
const char* name,
HistoryTraceType sameType = HistoryTraceType::followTypeChange,
bool withCache = true);
/** Obtain the element name from a feature based of the element name of its source feature
*
* @param obj: current feature
* @param subname: sub-object/element reference
* @param src: source feature
* @param srcSub: sub-object/element reference of the source
* @param single: if true, then return upon first match is found, or else
* return all matches. Multiple matches are possible for
* compound of multiple instances of the same source shape.
*
* @return Return a vector of pair of new style and old style element names.
*/
static QVector<Data::MappedElement>
getElementFromSource(App::DocumentObject *obj,
const char *subname,
App::DocumentObject *src,
const char *srcSub,
bool single = false);
TopLoc_Location getLocation() const;
DocumentObject *getSubObject(const char *subname, PyObject **pyObj,
@@ -113,6 +146,8 @@ public:
static Feature*
create(const TopoShape& shape, const char* name = nullptr, App::Document* document = nullptr);
static bool isElementMappingDisabled(App::PropertyContainer *container);
protected:
/// recompute only this object
App::DocumentObjectExecReturn *recompute() override;

View File

@@ -50,6 +50,7 @@
#include "PartPyCXX.h"
#include "PropertyTopoShape.h"
#include "TopoShapePy.h"
#include "PartFeature.h"
FC_LOG_LEVEL_INIT("App", true, true)
@@ -103,14 +104,15 @@ TopoShape PropertyPartShape::getShape() const
{
_Shape.initCache(-1);
auto res = _Shape;
// March, 2024 Toponaming project: There was originally an unused feature to disable elementMapping
// that has not been kept:
// March, 2024 Toponaming project: There was originally an unused feature to disable
// elementMapping that has not been kept:
// if (Feature::isElementMappingDisabled(getContainer()))
if ( false )
res.Tag = -1;
else if (!res.Tag) {
if (auto parent = Base::freecad_dynamic_cast<App::DocumentObject>(getContainer()))
// res.Tag = -1;
// else if (!res.Tag) {
if (!res.Tag) {
if (auto parent = Base::freecad_dynamic_cast<App::DocumentObject>(getContainer())) {
res.Tag = parent->getID();
}
}
return res;
}

View File

@@ -557,44 +557,44 @@ PyObject * TopoShape::getPyObject()
{
Base::PyObjectBase* prop = nullptr;
if (_Shape.IsNull()) {
prop = new TopoShapePy(new TopoShape(_Shape));
prop = new TopoShapePy(new TopoShape(*this));
}
else {
TopAbs_ShapeEnum type = _Shape.ShapeType();
switch (type)
{
case TopAbs_COMPOUND:
prop = new TopoShapeCompoundPy(new TopoShape(_Shape));
prop = new TopoShapeCompoundPy(new TopoShape(*this));
break;
case TopAbs_COMPSOLID:
prop = new TopoShapeCompSolidPy(new TopoShape(_Shape));
prop = new TopoShapeCompSolidPy(new TopoShape(*this));
break;
case TopAbs_SOLID:
prop = new TopoShapeSolidPy(new TopoShape(_Shape));
prop = new TopoShapeSolidPy(new TopoShape(*this));
break;
case TopAbs_SHELL:
prop = new TopoShapeShellPy(new TopoShape(_Shape));
prop = new TopoShapeShellPy(new TopoShape(*this));
break;
case TopAbs_FACE:
prop = new TopoShapeFacePy(new TopoShape(_Shape));
prop = new TopoShapeFacePy(new TopoShape(*this));
break;
case TopAbs_WIRE:
prop = new TopoShapeWirePy(new TopoShape(_Shape));
prop = new TopoShapeWirePy(new TopoShape(*this));
break;
case TopAbs_EDGE:
prop = new TopoShapeEdgePy(new TopoShape(_Shape));
prop = new TopoShapeEdgePy(new TopoShape(*this));
break;
case TopAbs_VERTEX:
prop = new TopoShapeVertexPy(new TopoShape(_Shape));
prop = new TopoShapeVertexPy(new TopoShape(*this));
break;
case TopAbs_SHAPE:
default:
prop = new TopoShapePy(new TopoShape(_Shape));
prop = new TopoShapePy(new TopoShape(*this));
break;
}
}
prop->setNotTracking();
prop->setNotTracking(); // TODO: Does this still belong here?
return prop;
}

View File

@@ -67,6 +67,7 @@
#endif
#include <App/PropertyStandard.h>
#include <App/StringHasherPy.h>
#include <Base/FileInfo.h>
#include <Base/GeometryPyCXX.h>
#include <Base/MatrixPy.h>
@@ -85,6 +86,7 @@
#include <Mod/Part/App/TopoShapeCompSolidPy.h>
#include <Mod/Part/App/TopoShapeEdgePy.h>
#include <Mod/Part/App/TopoShapeFacePy.h>
#include <Mod/Part/App/TopoShapeOpCode.h>
#include <Mod/Part/App/TopoShapeShellPy.h>
#include <Mod/Part/App/TopoShapeSolidPy.h>
#include <Mod/Part/App/TopoShapeVertexPy.h>
@@ -105,6 +107,26 @@ using namespace Part;
#define M_PI_2 1.57079632679489661923 /* pi/2 */
#endif
#ifdef FC_USE_TNP_FIX
static Py_hash_t _TopoShapeHash(PyObject *self) {
if (!self) {
PyErr_SetString(PyExc_TypeError, "descriptor 'hash' of 'Part.TopoShape' object needs an argument");
return 0;
}
if (!static_cast<Base::PyObjectBase*>(self)->isValid()) {
PyErr_SetString(PyExc_ReferenceError, "This object is already deleted most likely through closing a document. This reference is no longer valid!");
return 0;
}
return static_cast<TopoShapePy*>(self)->getTopoShapePtr()->getShape().HashCode(INT_MAX);
}
struct TopoShapePyInit {
TopoShapePyInit() {
TopoShapePy::Type.tp_hash = _TopoShapeHash;
}
} _TopoShapePyInit;
#endif
// returns a string which represents the object e.g. when printed in python
std::string TopoShapePy::representation() const
{
@@ -120,18 +142,68 @@ PyObject *TopoShapePy::PyMake(struct _typeobject *, PyObject *, PyObject *) //
return new TopoShapePy(new TopoShape);
}
int TopoShapePy::PyInit(PyObject* args, PyObject*)
int TopoShapePy::PyInit(PyObject* args, PyObject* keywds)
{
PyObject *pcObj=nullptr;
if (!PyArg_ParseTuple(args, "|O", &pcObj))
#ifdef FC_USE_TNP_FIX
static char* kwlist[] = {"shape", "op", "tag", "hasher", nullptr};
long tag = 0;
PyObject* pyHasher = nullptr;
const char* op = nullptr;
PyObject* pcObj = nullptr;
if (!PyArg_ParseTupleAndKeywords(args,
keywds,
"|OsiO!",
kwlist,
&pcObj,
&op,
&tag,
&App::StringHasherPy::Type,
&pyHasher)) {
return -1;
}
auto& self = *getTopoShapePtr();
self.Tag = tag;
if (pyHasher) {
self.Hasher = static_cast<App::StringHasherPy*>(pyHasher)->getStringHasherPtr();
}
auto shapes = getPyShapes(pcObj);
PY_TRY
{
if (shapes.size() == 1 && !op) {
auto s = shapes.front();
if (self.Tag) {
if ((s.Tag && self.Tag != s.Tag)
|| (self.Hasher && s.getElementMapSize() && self.Hasher != s.Hasher)) {
s.reTagElementMap(self.Tag, self.Hasher);
}
else {
s.Tag = self.Tag;
s.Hasher = self.Hasher;
}
}
self = s;
}
else if (shapes.size()) {
if (!op) {
op = Part::OpCodes::Fuse;
}
self.makeElementBoolean(op, shapes);
}
}
_PY_CATCH_OCC(return (-1))
#else
PyObject* pcObj = nullptr;
if (!PyArg_ParseTuple(args, "|O", &pcObj)) {
return -1;
}
auto shapes = getPyShapes(pcObj);
if (pcObj) {
TopoShape shape;
PY_TRY {
if (PyObject_TypeCheck(pcObj,&TopoShapePy::Type)) {
PY_TRY
{
if (PyObject_TypeCheck(pcObj, &TopoShapePy::Type)) {
shape = *static_cast<TopoShapePy*>(pcObj)->getTopoShapePtr();
}
else {
@@ -139,8 +211,8 @@ int TopoShapePy::PyInit(PyObject* args, PyObject*)
bool first = true;
for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
if (PyObject_TypeCheck((*it).ptr(), &(Part::GeometryPy::Type))) {
TopoDS_Shape sh = static_cast<GeometryPy*>((*it).ptr())->
getGeometryPtr()->toShape();
TopoDS_Shape sh =
static_cast<GeometryPy*>((*it).ptr())->getGeometryPtr()->toShape();
if (first) {
first = false;
shape.setShape(sh);
@@ -152,11 +224,11 @@ int TopoShapePy::PyInit(PyObject* args, PyObject*)
}
}
}
_PY_CATCH_OCC(return(-1))
_PY_CATCH_OCC(return (-1))
getTopoShapePtr()->setShape(shape.getShape());
}
#endif
return 0;
}

View File

@@ -218,6 +218,6 @@ TEST_F(FeaturePartCommonTest, testMapping)
#ifndef FC_USE_TNP_FIX
EXPECT_EQ(ts1.getElementMap().size(), 0);
#else
EXPECT_EQ(ts1.getElementMap().size(), 26); // TODO: This should be 26.
EXPECT_EQ(ts1.getElementMap().size(), 26);
#endif
}

View File

@@ -6,6 +6,7 @@
#include <src/App/InitApplication.h>
#include <BRepBuilderAPI_MakeVertex.hxx>
#include "PartTestHelpers.h"
#include "App/MappedElement.h"
using namespace Part;
using namespace PartTestHelpers;
@@ -54,7 +55,7 @@ TEST_F(FeaturePartTest, testGetElementName)
#ifndef FC_USE_TNP_FIX
EXPECT_EQ(ts.getElementMap().size(), 0);
#else
EXPECT_EQ(ts.getElementMap().size(), 0); // TODO: Value and code TBD
EXPECT_EQ(ts.getElementMap().size(), 26);
#endif
// TBD
}
@@ -127,3 +128,88 @@ TEST_F(FeaturePartTest, create)
// will have only 1 feature
EXPECT_EQ(otherDoc->getObjects().size(), 1);
}
TEST_F(FeaturePartTest, getElementHistory)
{
// Arrange
const char* name = "Part__Box";
const char* name2 = "Edge2"; // Edge, Vertex, or Face. will work here.
// Act
auto result = Feature::getElementHistory(_boxes[0], name2, true, false);
Data::HistoryItem histItem = result.front();
// Assert
EXPECT_EQ(result.size(), 1);
EXPECT_NE(histItem.tag, 0); // Make sure we have one. It will vary.
EXPECT_EQ(histItem.index.getIndex(), 2);
EXPECT_STREQ(histItem.index.getType(), "Edge");
EXPECT_STREQ(histItem.element.toString().c_str(), name2);
EXPECT_EQ(histItem.obj, _boxes[0]);
}
TEST_F(FeaturePartTest, getRelatedElements)
{
// Arrange
_common->Base.setValue(_boxes[0]);
_common->Tool.setValue(_boxes[1]);
// Act
_common->execute();
auto label1 = _common->Label.getValue();
auto label2 = _boxes[1]->Label.getValue();
const TopoShape& ts = _common->Shape.getShape();
auto result = Feature::getRelatedElements(_doc->getObject(label1),
"Edge2",
HistoryTraceType::followTypeChange,
true);
auto result2 = Feature::getRelatedElements(_doc->getObject(label2),
"Edge1",
HistoryTraceType::followTypeChange,
true);
// Assert
#ifdef FC_USE_TNP_FIX
EXPECT_EQ(result.size(), 1); // Found the one.
EXPECT_EQ(result2.size(), 0); // No element map, so no related elements
// The results are always going to vary, so we can't test for specific values:
// EXPECT_STREQ(result.front().name.toString().c_str(),"Edge3;:M;CMN;:H38d:7,E");
#else
EXPECT_EQ(result.size(), 0);
#endif
}
// Note that this test is pretty trivial and useless .. but the method in question is never
// called in the codebase.
TEST_F(FeaturePartTest, getElementFromSource)
{
// Arrange
_common->Base.setValue(_boxes[0]);
_common->Tool.setValue(_boxes[1]);
App::DocumentObject sourceObject;
// const char *sourceSubElement;
// Act
_common->execute();
auto label1 = _common->Label.getValue();
auto label2 = _boxes[1]->Label.getValue();
const TopoShape& ts = _common->Shape.getShape();
auto element = Feature::getElementFromSource(_common,
"Part__Box001", // "Edge1",
_boxes[0],
"Face1", // "Edge1",
false);
// Assert
EXPECT_EQ(element.size(), 0);
}
TEST_F(FeaturePartTest, getSubObject)
{
// Arrange
_common->Base.setValue(_boxes[0]);
_common->Tool.setValue(_boxes[1]);
App::DocumentObject sourceObject;
const char* sourceSubElement;
PyObject* pyObj;
// Act
_common->execute();
auto result = _boxes[1]->getSubObject("Face5", &pyObj, nullptr, false, 10);
// Assert
ASSERT_NE(result, nullptr);
EXPECT_STREQ(result->getNameInDocument(), "Part__Box001");
}

View File

@@ -2028,17 +2028,16 @@ TEST_F(TopoShapeExpansionTest, makeElementSlice)
{
"Face1;SLC;:H1:4,F;:G2;SLC;:H1:8,V;SLC;:H1:4,V;MAK;:H1:4,V",
"Face1;SLC;:H1:4,F;:G3;SLC;:H1:8,V;SLC;:H1:4,V;MAK;:H1:4,V",
// TODO: Prove that this difference is not a problem.
// The next element varies according to platform / OCCT version and thus can't be
// absolutely tested.
// "Face1;SLC;:H1:4,F;:G4;SLC;:H1:8,V;D1;:H1:3,V;SLC;:H1:4,V;MAK;:H1:4,V",
// MacOSX difference:
// "Face1;SLC;:H1:4,F;:G4;SLC;:H1:8,V;D25fd;:H1:6,V;SLC;:H1:4,V;MAK;:H1:4,V",
// "Face1;SLC;:H1:4,F;:G4;SLC;:H1:8,V;D1;:H1:3,V;SLC;:H1:4,V;MAK;:H1:4,V",
"Face1;SLC;:H1:4,F;:G4;SLC;:H1:8,V;SLC;:H1:4,V;MAK;:H1:4,V",
"Face1;SLC;:H1:4,F;:G5;SLC;:H1:8,E;SLC;:H1:4,E;MAK;:H1:4,E",
"Face1;SLC;:H1:4,F;:G6;SLC;:H1:8,E;SLC;:H1:4,E;MAK;:H1:4,E",
"Face1;SLC;:H1:4,F;:G7;SLC;:H1:8,E;SLC;:H1:4,E;MAK;:H1:4,E",
"Face1;SLC;:H1:4,F;:G8;SLC;:H1:8,E;SLC;:H1:4,E;MAK;:H1:4,E",
})); // Changed with PR#12471. Probably will change again after importing
// other TopoNaming logics
})); // Changed with PR#12471. Probably will change again after
// importing other TopoNaming logics
}
TEST_F(TopoShapeExpansionTest, makeElementSlices)