1 // Copyright (C) 2016-20xx CEA/DEN, EDF R&D -->
3 // File: ModelHighAPI_Dumper.cpp
4 // Created: 1 August 2016
5 // Author: Artem ZHIDKOV
7 //--------------------------------------------------------------------------------------
8 #include "ModelHighAPI_Dumper.h"
10 #include <GeomAPI_Pnt.h>
11 #include <GeomAPI_Dir.h>
13 #include <GeomDataAPI_Dir.h>
14 #include <GeomDataAPI_Point.h>
15 #include <GeomDataAPI_Point2D.h>
17 #include <ModelAPI_AttributeBoolean.h>
18 #include <ModelAPI_AttributeDouble.h>
19 #include <ModelAPI_AttributeInteger.h>
20 #include <ModelAPI_AttributeRefAttr.h>
21 #include <ModelAPI_AttributeRefAttrList.h>
22 #include <ModelAPI_AttributeReference.h>
23 #include <ModelAPI_AttributeRefList.h>
24 #include <ModelAPI_AttributeSelection.h>
25 #include <ModelAPI_AttributeSelectionList.h>
26 #include <ModelAPI_AttributeString.h>
27 #include <ModelAPI_CompositeFeature.h>
28 #include <ModelAPI_Document.h>
29 #include <ModelAPI_Entity.h>
30 #include <ModelAPI_Feature.h>
31 #include <ModelAPI_Result.h>
32 #include <ModelAPI_ResultPart.h>
34 #include <PartSetPlugin_Part.h>
36 #include <OSD_OpenFile.hxx>
40 #define DUMP_USER_DEFINED_NAMES
42 ModelHighAPI_Dumper* ModelHighAPI_Dumper::mySelf = 0;
44 ModelHighAPI_Dumper::ModelHighAPI_Dumper()
49 void ModelHighAPI_Dumper::setInstance(ModelHighAPI_Dumper* theDumper)
55 ModelHighAPI_Dumper* ModelHighAPI_Dumper::getInstance()
60 void ModelHighAPI_Dumper::clear(bool bufferOnly)
62 myDumpBuffer = std::ostringstream();
63 myDumpBuffer << std::setprecision(16);
68 myFullDump = std::ostringstream();
69 myFullDump << std::setprecision(16);
73 myFeatureCount.clear();
74 myLastEntityWithName = EntityPtr();
78 void ModelHighAPI_Dumper::clearNotDumped()
80 myNotDumpedEntities.clear();
83 const std::string& ModelHighAPI_Dumper::name(const EntityPtr& theEntity)
85 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
86 if (aFound != myNames.end())
87 return aFound->second.first;
89 // entity is not found, store it
91 bool isUserDefined = false;
92 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
95 aName = aFeature->name();
96 const std::string& aKind = aFeature->getKind();
97 DocumentPtr aDoc = aFeature->document();
98 int& aNbFeatures = myFeatureCount[aDoc][aKind];
100 size_t anIndex = aName.find(aKind);
101 if (anIndex == 0 && aName[aKind.length()] == '_') { // name starts with "FeatureKind_"
102 std::string anIdStr = aName.substr(aKind.length() + 1, std::string::npos);
103 int anId = std::stoi(anIdStr);
105 // Check number of already registered objects of such kind. Index of current object
106 // should be greater than it to identify feature's name as automatically generated.
107 if (aNbFeatures < anId) {
108 isUserDefined = false;
109 aNbFeatures = anId - 1;
116 myNames[theEntity] = std::pair<std::string, bool>(aName, isUserDefined);
117 myNotDumpedEntities.insert(theEntity);
118 return myNames[theEntity].first;
121 const std::string& ModelHighAPI_Dumper::parentName(const FeaturePtr& theEntity)
123 const std::set<AttributePtr>& aRefs = theEntity->data()->refsToMe();
124 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
125 for (; aRefIt != aRefs.end(); ++aRefIt) {
126 CompositeFeaturePtr anOwner = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(
127 ModelAPI_Feature::feature((*aRefIt)->owner()));
129 return name(anOwner);
132 static const std::string DUMMY;
136 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc,
137 const std::string& theFileName)
139 // dump top level document feature
140 static const std::string aDocName("partSet");
141 myNames[theDoc] = std::pair<std::string, bool>(aDocName, false);
142 *this << aDocName << " = model.moduleDocument()" << std::endl;
144 // dump subfeatures and store result to file
145 return process(theDoc) && exportTo(theFileName);
148 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc)
152 std::list<FeaturePtr> aFeatures = theDoc->allFeatures();
153 std::list<FeaturePtr>::const_iterator aFeatIt = aFeatures.begin();
154 for (; aFeatIt != aFeatures.end(); ++aFeatIt) {
155 if (!isDumped(*aFeatIt))
156 dumpFeature(*aFeatIt);
158 // iteratively process composite features
159 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFeatIt);
163 // sub-part is processed independently, because it provides separate document
164 if ((*aFeatIt)->getKind() == PartSetPlugin_Part::ID()) {
165 ResultPartPtr aPartResult =
166 std::dynamic_pointer_cast<ModelAPI_ResultPart>((*aFeatIt)->lastResult());
169 DocumentPtr aSubDoc = aPartResult->partDoc();
170 // set name of document
171 const std::string& aPartName = myNames[*aFeatIt].first;
172 std::string aDocName = aPartName + "_doc";
173 myNames[aSubDoc] = std::pair<std::string, bool>(aDocName, false);
175 // dump document in a single line
176 *this << aDocName << " = " << aPartName << ".document()" << std::endl;
178 isOk = process(aSubDoc) && isOk;
180 isOk = process(aCompFeat) && isOk;
185 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite)
187 // dump all sub-features;
188 int aNbSubs = theComposite->numberOfSubs();
189 for (int anIndex = 0; anIndex < aNbSubs; ++anIndex) {
190 FeaturePtr aFeature = theComposite->subFeature(anIndex);
191 if (isDumped(aFeature))
193 dumpFeature(aFeature, true);
198 bool ModelHighAPI_Dumper::exportTo(const std::string& theFileName)
201 OSD_OpenStream(aFile, theFileName.c_str(), std::ofstream::out);
202 if (!aFile.is_open())
206 for (ModulesMap::const_iterator aModIt = myModules.begin();
207 aModIt != myModules.end(); ++aModIt) {
208 aFile << "from " << aModIt->first << " import ";
209 if (aModIt->second.empty() ||
210 aModIt->second.find(std::string()) != aModIt->second.end())
211 aFile << "*"; // import whole module
213 // import specific features
214 std::set<std::string>::const_iterator anObjIt = aModIt->second.begin();
216 for (++anObjIt; anObjIt != aModIt->second.end(); ++anObjIt)
217 aFile << ", " << *anObjIt;
221 if (!myModules.empty())
224 aFile << "import model" << std::endl << std::endl;
225 aFile << "model.begin()" << std::endl;
227 // dump collected data
228 aFile << myFullDump.str();
231 aFile << "model.end()" << std::endl;
239 void ModelHighAPI_Dumper::importModule(const std::string& theModuleName,
240 const std::string& theObject)
242 myModules[theModuleName].insert(theObject);
245 void ModelHighAPI_Dumper::dumpEntitySetName()
247 if (!myLastEntityWithName)
250 #ifdef DUMP_USER_DEFINED_NAMES
251 const std::string& aName = name(myLastEntityWithName);
252 myDumpBuffer << aName;
253 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(myLastEntityWithName);
255 myDumpBuffer << ".feature()";
256 myDumpBuffer << ".data().setName(\"" << aName << "\")" << std::endl;
258 myNames[myLastEntityWithName].second = false; // don't dump "setName" for the entity twice
259 myLastEntityWithName = EntityPtr();
262 bool ModelHighAPI_Dumper::isDumped(const EntityPtr& theEntity) const
264 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
265 return aFound != myNames.end();
268 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char theChar)
270 myDumpBuffer << theChar;
274 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char* theString)
276 myDumpBuffer << theString;
280 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::string& theString)
282 myDumpBuffer << theString;
286 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const bool theValue)
288 myDumpBuffer << (theValue ? "True" : "False");
292 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const int theValue)
294 myDumpBuffer << theValue;
298 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const double theValue)
300 myDumpBuffer << theValue;
304 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
306 importModule("GeomAPI", "GeomAPI_Pnt");
307 myDumpBuffer << "GeomAPI_Pnt(" << thePoint->x() << ", "
308 << thePoint->y() << ", " << thePoint->z() << ")";
312 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir)
314 importModule("GeomAPI", "GeomAPI_Dir");
315 myDumpBuffer << "GeomAPI_Dir(" << theDir->x() << ", "
316 << theDir->y() << ", " << theDir->z() << ")";
320 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
321 const std::shared_ptr<GeomDataAPI_Dir>& theDir)
323 myDumpBuffer << theDir->x() << ", " << theDir->y() << ", " << theDir->z();
327 static void dumpArray(std::ostringstream& theOutput, int theSize,
328 double* theValues, std::string* theTexts)
330 for (int i = 0; i < theSize; ++i) {
333 if (theTexts[i].empty())
334 theOutput << theValues[i];
336 theOutput << "\"" << theTexts[i] << "\"";
340 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
341 const std::shared_ptr<GeomDataAPI_Point>& thePoint)
343 static const int aSize = 3;
344 double aValues[aSize] = {thePoint->x(), thePoint->y(), thePoint->z()};
345 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY(), thePoint->textZ()};
346 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
350 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
351 const std::shared_ptr<GeomDataAPI_Point2D>& thePoint)
353 static const int aSize = 2;
354 double aValues[aSize] = {thePoint->x(), thePoint->y()};
355 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY()};
356 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
360 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
361 const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool)
363 myDumpBuffer << (theAttrBool->value() ? "True" : "False");
367 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
368 const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt)
370 std::string aText = theAttrInt->text();
372 myDumpBuffer << theAttrInt->value();
374 myDumpBuffer << "\"" << aText << "\"";
378 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
379 const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal)
381 std::string aText = theAttrReal->text();
383 myDumpBuffer << theAttrReal->value();
385 myDumpBuffer << "\"" << aText << "\"";
389 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
390 const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr)
392 myDumpBuffer << "\"" << theAttrStr->value() << "\"";
396 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FeaturePtr& theEntity)
398 myDumpBuffer << name(theEntity);
399 if (myNames[theEntity].second)
400 myLastEntityWithName = theEntity;
401 myNotDumpedEntities.erase(theEntity);
405 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ObjectPtr& theObject)
407 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
408 myDumpBuffer << name(aFeature);
412 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const AttributePtr& theAttr)
414 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttr->owner());
415 myDumpBuffer << name(anOwner) << "." << attributeGetter(anOwner, theAttr->id()) << "()";
419 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
420 const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr)
422 if (theRefAttr->isObject())
423 *this << theRefAttr->object();
425 *this << theRefAttr->attr();
429 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
430 const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList)
433 std::list<std::pair<ObjectPtr, AttributePtr> > aList = theRefAttrList->list();
434 bool isAdded = false;
435 std::list<std::pair<ObjectPtr, AttributePtr> >::const_iterator anIt = aList.begin();
436 for (; anIt != aList.end(); ++anIt) {
438 myDumpBuffer << ", ";
442 *this << anIt->first;
443 else if (anIt->second)
444 * this << anIt->second;
450 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
451 const std::shared_ptr<ModelAPI_AttributeReference>& theReference)
453 *this << theReference->value();
457 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
458 const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList)
460 static const int aThreshold = 2;
461 // if number of elements in the list if greater than a threshold,
462 // dump it in a separate line with specific name
463 std::string aDumped = myDumpBuffer.str();
464 if (aDumped.empty() || theRefList->size() <= aThreshold) {
466 std::list<ObjectPtr> aList = theRefList->list();
467 bool isAdded = false;
468 std::list<ObjectPtr>::const_iterator anIt = aList.begin();
469 for (; anIt != aList.end(); ++anIt) {
471 myDumpBuffer << ", ";
479 // clear buffer and store list "as is"
480 myDumpBuffer = std::ostringstream();
482 // save buffer and clear it again
483 std::string aDumpedList = myDumpBuffer.str();
484 myDumpBuffer = std::ostringstream();
485 // obtain name of list
486 FeaturePtr anOwner = ModelAPI_Feature::feature(theRefList->owner());
487 std::string aListName = name(anOwner) + "_objects";
488 // store all previous data
489 myDumpBuffer << aListName << " = " << aDumpedList << std::endl
490 << aDumped << aListName;
495 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
496 const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect)
498 myDumpBuffer << "model.selection(";
500 if(!theAttrSelect->isInitialized()) {
505 GeomShapePtr aShape = theAttrSelect->value();
507 aShape = theAttrSelect->context()->shape();
515 myDumpBuffer << "\"" << aShape->shapeTypeStr() << "\", \"" << theAttrSelect->namingName() << "\")";
519 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
520 const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList)
525 std::string aShapeTypeStr;
527 bool isAdded = false;
529 for(int anIndex = 0; anIndex < theAttrSelList->size(); ++anIndex) {
530 AttributeSelectionPtr anAttribute = theAttrSelList->value(anIndex);
531 aShape = anAttribute->value();
533 aShape = anAttribute->context()->shape();
541 myDumpBuffer << ", ";
545 myDumpBuffer << "model.selection(\"" << aShape->shapeTypeStr() << "\", \"" << anAttribute->namingName() << "\")";
554 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
555 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&))
557 theDumper.myDumpBuffer << theEndl;
558 theDumper.dumpEntitySetName();
560 // store all not-dumped entities first
561 std::set<EntityPtr> aNotDumped = theDumper.myNotDumpedEntities;
562 std::string aBufCopy = theDumper.myDumpBuffer.str();
563 theDumper.clear(true);
564 std::set<EntityPtr>::const_iterator anIt = aNotDumped.begin();
565 for (; anIt != aNotDumped.end(); ++anIt) {
566 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
567 theDumper.dumpFeature(aFeature, true);
569 // if the feature is composite, dump all its subs
570 CompositeFeaturePtr aCompFeat =
571 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
573 theDumper.process(aCompFeat);
576 // avoid multiple empty lines
577 size_t anInd = std::string::npos;
578 while ((anInd = aBufCopy.find("\n\n\n")) != std::string::npos)
579 aBufCopy.erase(anInd, 1);
580 // then store currently dumped string
581 theDumper.myFullDump << aBufCopy;