#include <OSD_OpenFile.hxx>
-#include <algorithm>
#include <fstream>
-//#define DUMP_USER_DEFINED_NAMES
+#define DUMP_USER_DEFINED_NAMES
ModelHighAPI_Dumper* ModelHighAPI_Dumper::mySelf = 0;
myNames.clear();
myModules.clear();
+ myFeatureCount.clear();
myLastEntityWithName = EntityPtr();
}
}
// entity is not found, store it
std::string aName;
- bool isNameDefined = false;
+ bool isUserDefined = false;
FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(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<std::string, bool>(aName, isNameDefined);
+ myNames[theEntity] = std::pair<std::string, bool>(aName, isUserDefined);
myNotDumpedEntities.insert(theEntity);
return myNames[theEntity].first;
}
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<std::string, bool>(aDocName, false);
+
+ // dump document in a single line
+ *this << aDocName << " = " << aPartName << ".document()" << std::endl;
isOk = process(aSubDoc) && isOk;
} else
class ModelAPI_Feature;
class ModelAPI_Object;
-typedef std::shared_ptr<ModelAPI_Entity> EntityPtr;
-typedef std::shared_ptr<ModelAPI_Feature> FeaturePtr;
+typedef std::shared_ptr<ModelAPI_Document> DocumentPtr;
+typedef std::shared_ptr<ModelAPI_Entity> EntityPtr;
+typedef std::shared_ptr<ModelAPI_Feature> FeaturePtr;
/**\class ModelHighAPI_Dumper
* \ingroup CPPHighAPI
private:
typedef std::map<EntityPtr, std::pair<std::string, bool> > EntityNameMap;
typedef std::map<std::string, std::set<std::string> > ModulesMap;
+ typedef std::map<DocumentPtr, std::map<std::string, int> > NbFeaturesMap;
static ModelHighAPI_Dumper* mySelf;
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<EntityPtr> myNotDumpedEntities; ///< list of entities, used by other features but not dumped yet
};