From 61deef8a7c2f39b92d8cd7eb3ac8b3cef75e2bdc Mon Sep 17 00:00:00 2001 From: azv Date: Thu, 11 Aug 2016 15:16:28 +0300 Subject: [PATCH] Dump Python in the High Level Parameterized Geometry API (issue #1648) * Dump user-defined names --- src/ModelHighAPI/ModelHighAPI_Dumper.cpp | 44 ++++++++++++++++-------- src/ModelHighAPI/ModelHighAPI_Dumper.h | 8 +++-- src/PartSetAPI/PartSetAPI_Part.cpp | 2 +- 3 files changed, 36 insertions(+), 18 deletions(-) diff --git a/src/ModelHighAPI/ModelHighAPI_Dumper.cpp b/src/ModelHighAPI/ModelHighAPI_Dumper.cpp index efff25927..55cd9f2c7 100644 --- a/src/ModelHighAPI/ModelHighAPI_Dumper.cpp +++ b/src/ModelHighAPI/ModelHighAPI_Dumper.cpp @@ -37,10 +37,9 @@ #include -#include #include -//#define DUMP_USER_DEFINED_NAMES +#define DUMP_USER_DEFINED_NAMES ModelHighAPI_Dumper* ModelHighAPI_Dumper::mySelf = 0; @@ -73,6 +72,7 @@ void ModelHighAPI_Dumper::clear(bool bufferOnly) myNames.clear(); myModules.clear(); + myFeatureCount.clear(); myLastEntityWithName = EntityPtr(); } } @@ -90,23 +90,32 @@ const std::string& ModelHighAPI_Dumper::name(const EntityPtr& theEntity) // entity is not found, store it std::string aName; - bool isNameDefined = false; + bool isUserDefined = false; FeaturePtr aFeature = std::dynamic_pointer_cast(theEntity); if (aFeature) { + isUserDefined = true; aName = aFeature->name(); - isNameDefined = !aName.empty(); - - if (!isNameDefined) { - static long anIndex = 0; - // set default name: feature ID + index - std::ostringstream aConverter; - aConverter << aFeature->getKind() << "_" << ++anIndex; - aName = aConverter.str(); - std::transform(aName.begin(), aName.end(), aName.begin(), ::tolower); + const std::string& aKind = aFeature->getKind(); + DocumentPtr aDoc = aFeature->document(); + int& aNbFeatures = myFeatureCount[aDoc][aKind]; + + size_t anIndex = aName.find(aKind); + if (anIndex == 0 && aName[aKind.length()] == '_') { // name starts with "FeatureKind_" + std::string anIdStr = aName.substr(aKind.length() + 1, std::string::npos); + int anId = std::stoi(anIdStr); + + // Check number of already registered objects of such kind. Index of current object + // should be greater than it to identify feature's name as automatically generated. + if (aNbFeatures < anId) { + isUserDefined = false; + aNbFeatures = anId - 1; + } } + + aNbFeatures += 1; } - myNames[theEntity] = std::pair(aName, isNameDefined); + myNames[theEntity] = std::pair(aName, isUserDefined); myNotDumpedEntities.insert(theEntity); return myNames[theEntity].first; } @@ -160,8 +169,13 @@ bool ModelHighAPI_Dumper::process(const std::shared_ptr& theD if (!aPartResult) continue; DocumentPtr aSubDoc = aPartResult->partDoc(); - // set name of document equal to part name - myNames[aSubDoc] = myNames[*aFeatIt]; + // set name of document + const std::string& aPartName = myNames[*aFeatIt].first; + std::string aDocName = aPartName + "_doc"; + myNames[aSubDoc] = std::pair(aDocName, false); + + // dump document in a single line + *this << aDocName << " = " << aPartName << ".document()" << std::endl; isOk = process(aSubDoc) && isOk; } else diff --git a/src/ModelHighAPI/ModelHighAPI_Dumper.h b/src/ModelHighAPI/ModelHighAPI_Dumper.h index 2c9edffe4..2bab94d9c 100644 --- a/src/ModelHighAPI/ModelHighAPI_Dumper.h +++ b/src/ModelHighAPI/ModelHighAPI_Dumper.h @@ -39,8 +39,9 @@ class ModelAPI_Entity; class ModelAPI_Feature; class ModelAPI_Object; -typedef std::shared_ptr EntityPtr; -typedef std::shared_ptr FeaturePtr; +typedef std::shared_ptr DocumentPtr; +typedef std::shared_ptr EntityPtr; +typedef std::shared_ptr FeaturePtr; /**\class ModelHighAPI_Dumper * \ingroup CPPHighAPI @@ -211,6 +212,7 @@ private: private: typedef std::map > EntityNameMap; typedef std::map > ModulesMap; + typedef std::map > NbFeaturesMap; static ModelHighAPI_Dumper* mySelf; @@ -221,6 +223,8 @@ private: EntityNameMap myNames; ///< names of the entities EntityPtr myLastEntityWithName; ///< not null, if last dumped entity had user defined name + NbFeaturesMap myFeatureCount; ///< number of features of each kind + protected: std::set myNotDumpedEntities; ///< list of entities, used by other features but not dumped yet }; diff --git a/src/PartSetAPI/PartSetAPI_Part.cpp b/src/PartSetAPI/PartSetAPI_Part.cpp index acda463d3..9460819c6 100644 --- a/src/PartSetAPI/PartSetAPI_Part.cpp +++ b/src/PartSetAPI/PartSetAPI_Part.cpp @@ -35,7 +35,7 @@ void PartSetAPI_Part::dump(ModelHighAPI_Dumper& theDumper) const FeaturePtr aBase = feature(); const std::string& aDocName = theDumper.name(aBase->document()); - theDumper << aBase << " = model.addPart(" << aDocName << ").document()" << std::endl; + theDumper << aBase << " = model.addPart(" << aDocName << ")" << std::endl; } //-------------------------------------------------------------------------------------- -- 2.39.2