Salome HOME
633984d81b7cc21d7924303c8ac6b7d0273633df
[modules/shaper.git] / src / ModelHighAPI / ModelHighAPI_Dumper.cpp
1 // Copyright (C) 2016-20xx CEA/DEN, EDF R&D -->
2
3 // File:        ModelHighAPI_Dumper.cpp
4 // Created:     1 August 2016
5 // Author:      Artem ZHIDKOV
6
7 //--------------------------------------------------------------------------------------
8 #include "ModelHighAPI_Dumper.h"
9
10 #include <GeomAPI_Pnt.h>
11 #include <GeomAPI_Dir.h>
12
13 #include <GeomDataAPI_Dir.h>
14 #include <GeomDataAPI_Point.h>
15 #include <GeomDataAPI_Point2D.h>
16
17 #include <ModelAPI_AttributeBoolean.h>
18 #include <ModelAPI_AttributeDouble.h>
19 #include <ModelAPI_AttributeIntArray.h>
20 #include <ModelAPI_AttributeInteger.h>
21 #include <ModelAPI_AttributeRefAttr.h>
22 #include <ModelAPI_AttributeRefAttrList.h>
23 #include <ModelAPI_AttributeReference.h>
24 #include <ModelAPI_AttributeRefList.h>
25 #include <ModelAPI_AttributeSelection.h>
26 #include <ModelAPI_AttributeSelectionList.h>
27 #include <ModelAPI_AttributeString.h>
28 #include <ModelAPI_CompositeFeature.h>
29 #include <ModelAPI_Document.h>
30 #include <ModelAPI_Entity.h>
31 #include <ModelAPI_Feature.h>
32 #include <ModelAPI_Result.h>
33 #include <ModelAPI_ResultPart.h>
34
35 #include <PartSetPlugin_Part.h>
36
37 #include <OSD_OpenFile.hxx>
38
39 #include <fstream>
40
41 static int gCompositeStackDepth = 0;
42
43 ModelHighAPI_Dumper* ModelHighAPI_Dumper::mySelf = 0;
44
45 ModelHighAPI_Dumper::ModelHighAPI_Dumper()
46 {
47   clear();
48 }
49
50 void ModelHighAPI_Dumper::setInstance(ModelHighAPI_Dumper* theDumper)
51 {
52   if (mySelf == 0)
53     mySelf = theDumper;
54 }
55
56 ModelHighAPI_Dumper* ModelHighAPI_Dumper::getInstance()
57 {
58   return mySelf;
59 }
60
61 void ModelHighAPI_Dumper::clear(bool bufferOnly)
62 {
63   myDumpBuffer.str("");
64   myDumpBuffer << std::setprecision(16);
65
66   clearNotDumped();
67
68   if (!bufferOnly) {
69     myFullDump.str("");
70     myFullDump << std::setprecision(16);
71
72     myNames.clear();
73     myModules.clear();
74     myFeatureCount.clear();
75     while (!myEntitiesStack.empty())
76       myEntitiesStack.pop();
77   }
78 }
79
80 void ModelHighAPI_Dumper::clearNotDumped()
81 {
82   myNotDumpedEntities.clear();
83 }
84
85 const std::string& ModelHighAPI_Dumper::name(const EntityPtr& theEntity,
86                                              bool theSaveNotDumped,
87                                              bool theUseEntityName)
88 {
89   EntityNameMap::const_iterator aFound = myNames.find(theEntity);
90   if (aFound != myNames.end())
91     return aFound->second.first;
92
93   // entity is not found, store it
94   std::string aName;
95   std::ostringstream aDefaultName;
96   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
97   if (aFeature) {
98     aName = aFeature->name();
99     const std::string& aKind = aFeature->getKind();
100     DocumentPtr aDoc = aFeature->document();
101     int& aNbFeatures = myFeatureCount[aDoc][aKind];
102     aNbFeatures += 1;
103
104     size_t anIndex = aName.find(aKind);
105     if (anIndex == 0 && aName[aKind.length()] == '_') { // name starts with "FeatureKind_"
106       std::string anIdStr = aName.substr(aKind.length() + 1);
107       int anId = std::stoi(anIdStr);
108
109       // Check number of already registered objects of such kind. Index of current object
110       // should be the same to identify feature's name as automatically generated.
111       if (aNbFeatures == anId) {
112         // name is not user-defined
113         aName.clear();
114       }
115     }
116
117     // obtain default name for the feature
118     if (theUseEntityName)
119       aDefaultName << aName;
120     else {
121       int aFullIndex = 0;
122       NbFeaturesMap::const_iterator aFIt = myFeatureCount.begin();
123       for (; aFIt != myFeatureCount.end(); ++aFIt) {
124         std::map<std::string, int>::const_iterator aFound = aFIt->second.find(aKind);
125         if (aFound != aFIt->second.end())
126           aFullIndex += aFound->second;
127       }
128       aDefaultName << aKind << "_" << aFullIndex;
129     }
130   }
131
132   myNames[theEntity] = std::pair<std::string, std::string>(aDefaultName.str(), aName);
133   if (theSaveNotDumped)
134     myNotDumpedEntities.insert(theEntity);
135
136   // store names of results
137   if (aFeature)
138     saveResultNames(aFeature);
139
140   return myNames[theEntity].first;
141 }
142
143 const std::string& ModelHighAPI_Dumper::parentName(const FeaturePtr& theEntity)
144 {
145   const std::set<AttributePtr>& aRefs = theEntity->data()->refsToMe();
146   std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
147   for (; aRefIt != aRefs.end(); ++aRefIt) {
148     CompositeFeaturePtr anOwner = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(
149         ModelAPI_Feature::feature((*aRefIt)->owner()));
150     if (anOwner)
151       return name(anOwner);
152   }
153
154   static const std::string DUMMY;
155   return DUMMY;
156 }
157
158 void ModelHighAPI_Dumper::saveResultNames(const FeaturePtr& theFeature)
159 {
160   const std::string& aFeatureName = myNames[theFeature].first;
161   const std::list<ResultPtr>& aResults = theFeature->results();
162   std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
163   for (int i = 1; aResIt != aResults.end(); ++aResIt, ++i) {
164     bool isUserDefined = true;
165     std::string aResName = (*aResIt)->data()->name();
166     size_t anIndex = aResName.find(aFeatureName);
167     if (anIndex == 0) {
168       std::string aSuffix = aResName.substr(aFeatureName.length());
169       if (aSuffix.empty() && i == 1) // first result may not constain index in the name
170         isUserDefined = false;
171       else {
172         if (aSuffix[0] == '_' && std::stoi(aSuffix.substr(1)) == i)
173           isUserDefined = false;
174       }
175     }
176
177     myNames[*aResIt] = std::pair<std::string, std::string>(aResName,
178         isUserDefined ? aResName : std::string());
179   }
180 }
181
182 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc,
183                                   const std::string& theFileName)
184 {
185   // dump top level document feature
186   static const std::string aDocName("partSet");
187   myNames[theDoc] = std::pair<std::string, std::string>(aDocName, std::string());
188   *this << aDocName << " = model.moduleDocument()" << std::endl;
189
190   // dump subfeatures and store result to file
191   return process(theDoc) && exportTo(theFileName);
192 }
193
194 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc)
195 {
196   bool isOk = true;
197   std::list<FeaturePtr> aFeatures = theDoc->allFeatures();
198   std::list<FeaturePtr>::const_iterator aFeatIt = aFeatures.begin();
199   // firstly, dump all parameters
200   for (; aFeatIt != aFeatures.end(); ++ aFeatIt)
201     dumpParameter(*aFeatIt);
202   // dump all other features
203   for (aFeatIt = aFeatures.begin(); aFeatIt != aFeatures.end(); ++aFeatIt) {
204     CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFeatIt);
205     if (aCompFeat) // iteratively process composite features
206       isOk = process(aCompFeat) && isOk;
207     else if (!isDumped(*aFeatIt)) // dump common feature 
208       dumpFeature(*aFeatIt);
209   }
210   return isOk;
211 }
212
213 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite, bool isForce)
214 {
215   // increase composite features stack
216   ++gCompositeStackDepth;
217   // dump composite itself
218   if (!isDumped(theComposite) || isForce)
219     dumpFeature(theComposite, isForce);
220
221   // sub-part is processed independently, because it provides separate document
222   if (theComposite->getKind() == PartSetPlugin_Part::ID()) {
223     // decrease composite features stack because we run into separate document
224     --gCompositeStackDepth;
225
226     ResultPartPtr aPartResult =
227         std::dynamic_pointer_cast<ModelAPI_ResultPart>(theComposite->lastResult());
228     if (!aPartResult)
229       return false;
230     DocumentPtr aSubDoc = aPartResult->partDoc();
231     if (!aSubDoc)
232       return false;
233     // set name of document
234     const std::string& aPartName = myNames[theComposite].first;
235     std::string aDocName = aPartName + "_doc";
236     myNames[aSubDoc] = std::pair<std::string, std::string>(aDocName, std::string());
237
238     // dump document in a separate line
239     *this << aDocName << " = " << aPartName << ".document()" << std::endl;
240     // dump features in the document
241     return process(aSubDoc);
242   }
243
244   // dump sub-features
245   bool isOk = processSubs(theComposite);
246   // decrease composite features stack
247   --gCompositeStackDepth;
248
249   return isOk;
250 }
251
252 bool ModelHighAPI_Dumper::processSubs(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
253                                       bool theDumpModelDo)
254 {
255   bool isOk = true;
256   // dump all sub-features;
257   int aNbSubs = theComposite->numberOfSubs();
258   for (int anIndex = 0; anIndex < aNbSubs; ++anIndex) {
259     FeaturePtr aFeature = theComposite->subFeature(anIndex);
260     if (isDumped(aFeature))
261       continue;
262
263     CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
264     if (aCompFeat) // iteratively process composite features
265       isOk = process(aCompFeat) && isOk;
266     else
267       dumpFeature(aFeature, true);
268   }
269
270   bool isDumpSetName = !myEntitiesStack.empty() &&
271       myEntitiesStack.top().myEntity == EntityPtr(theComposite);
272   bool isForceModelDo = isDumpSetName &&
273       (myEntitiesStack.top().myUserName || !myEntitiesStack.top().myResults.empty());
274   // It is necessary for the sketch to create its result when complete (command "model.do()").
275   // This option is set by flat theDumpModelDo.
276   // However, nested sketches are rebuilt by parent feature, so, they do not need
277   // explicit call of "model.do()". This will be controlled by the depth of the stack.
278   if (isForceModelDo || (theDumpModelDo && gCompositeStackDepth <= 1))
279     *this << "model.do()" << std::endl;
280
281   // dump "setName" for composite feature
282   if (isDumpSetName)
283     dumpEntitySetName();
284   return isOk;
285 }
286
287 void ModelHighAPI_Dumper::dumpSubFeatureNameAndColor(const std::string theSubFeatureGet,
288                                                      const FeaturePtr& theSubFeature)
289 {
290   name(theSubFeature, false);
291   myNames[theSubFeature] = std::pair<std::string, std::string>(theSubFeatureGet, theSubFeature->name());
292
293   // store results if they have user-defined names or colors
294   std::list<ResultPtr> aResultsWithNameOrColor;
295   const std::list<ResultPtr>& aResults = theSubFeature->results();
296   std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
297   for (; aResIt != aResults.end(); ++aResIt) {
298     std::string aResName = (*aResIt)->data()->name();
299     myNames[*aResIt] = std::pair<std::string, std::string>(aResName, aResName);
300     aResultsWithNameOrColor.push_back(*aResIt);
301   }
302
303   // store just dumped entity to stack
304   myEntitiesStack.push(LastDumpedEntity(theSubFeature, true, aResultsWithNameOrColor));
305
306   dumpEntitySetName();
307 }
308
309 bool ModelHighAPI_Dumper::exportTo(const std::string& theFileName)
310 {
311   std::ofstream aFile;
312   OSD_OpenStream(aFile, theFileName.c_str(), std::ofstream::out);
313   if (!aFile.is_open())
314     return false;
315
316   // standard header
317   for (ModulesMap::const_iterator aModIt = myModules.begin();
318        aModIt != myModules.end(); ++aModIt) {
319     aFile << "from " << aModIt->first << " import ";
320     if (aModIt->second.empty() || 
321         aModIt->second.find(std::string()) != aModIt->second.end())
322       aFile << "*"; // import whole module
323     else {
324       // import specific features
325       std::set<std::string>::const_iterator anObjIt = aModIt->second.begin();
326       aFile << *anObjIt;
327       for (++anObjIt; anObjIt != aModIt->second.end(); ++anObjIt)
328         aFile << ", " << *anObjIt;
329     }
330     aFile << std::endl;
331   }
332   if (!myModules.empty())
333     aFile << std::endl;
334
335   aFile << "import model" << std::endl << std::endl;
336   aFile << "model.begin()" << std::endl;
337
338   // dump collected data
339   aFile << myFullDump.str();
340   aFile << myDumpBuffer.str();
341
342   // standard footer
343   aFile << "model.end()" << std::endl;
344
345   aFile.close();
346   clear();
347
348   return true;
349 }
350
351 void ModelHighAPI_Dumper::importModule(const std::string& theModuleName,
352                                        const std::string& theObject)
353 {
354   myModules[theModuleName].insert(theObject);
355 }
356
357 void ModelHighAPI_Dumper::dumpEntitySetName()
358 {
359   const LastDumpedEntity& aLastDumped = myEntitiesStack.top();
360
361   // dump "setName" for the entity
362   if (aLastDumped.myUserName) {
363     std::pair<std::string, std::string> anEntityNames = myNames[aLastDumped.myEntity];
364     if (!anEntityNames.second.empty())
365       myDumpBuffer << anEntityNames.first << ".setName(\"" << anEntityNames.second << "\")" << std::endl;
366     anEntityNames.second.clear(); // don't dump "setName" for the entity twice
367   }
368   // dump "setName" for results
369   std::list<ResultPtr>::const_iterator aResIt = aLastDumped.myResults.begin();
370   std::list<ResultPtr>::const_iterator aResEnd = aLastDumped.myResults.end();
371   for (; aResIt != aResEnd; ++aResIt) {
372     // set result name
373     std::pair<std::string, std::string> anEntityNames = myNames[*aResIt];
374     if (!anEntityNames.second.empty()) {
375       *this << *aResIt;
376       myDumpBuffer << ".setName(\"" << anEntityNames.second << "\")" << std::endl;
377       anEntityNames.second.clear(); // don't dump "setName" for the entity twice
378     }
379     // set result color
380     if (!isDefaultColor(*aResIt)) {
381       AttributeIntArrayPtr aColor = (*aResIt)->data()->intArray(ModelAPI_Result::COLOR_ID());
382       if (aColor && aColor->isInitialized()) {
383         *this << *aResIt;
384         myDumpBuffer << ".setColor(" << aColor->value(0) << ", " << aColor->value(1)
385                      << ", " << aColor->value(2) << ")" << std::endl;
386       }
387     }
388   }
389
390   myEntitiesStack.pop();
391 }
392
393 bool ModelHighAPI_Dumper::isDumped(const EntityPtr& theEntity) const
394 {
395   EntityNameMap::const_iterator aFound = myNames.find(theEntity);
396   return aFound != myNames.end();
397 }
398
399 bool ModelHighAPI_Dumper::isDefaultColor(const ResultPtr& theResult) const
400 {
401   AttributeIntArrayPtr aColor = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
402   if (!aColor || !aColor->isInitialized())
403     return true;
404
405   std::string aSection, aName, aDefault;
406   theResult->colorConfigInfo(aSection, aName, aDefault);
407
408   // dump current color
409   std::ostringstream aColorInfo;
410   aColorInfo << aColor->value(0) << "," << aColor->value(1) << "," << aColor->value(2);
411
412   return aDefault == aColorInfo.str();
413 }
414
415 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char theChar)
416 {
417   myDumpBuffer << theChar;
418   return *this;
419 }
420
421 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char* theString)
422 {
423   myDumpBuffer << theString;
424   return *this;
425 }
426
427 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::string& theString)
428 {
429   myDumpBuffer << theString;
430   return *this;
431 }
432
433 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const bool theValue)
434 {
435   myDumpBuffer << (theValue ? "True" : "False");
436   return *this;
437 }
438
439 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const int theValue)
440 {
441   myDumpBuffer << theValue;
442   return *this;
443 }
444
445 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const double theValue)
446 {
447   myDumpBuffer << theValue;
448   return *this;
449 }
450
451 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
452 {
453   importModule("GeomAPI", "GeomAPI_Pnt");
454   myDumpBuffer << "GeomAPI_Pnt(" << thePoint->x() << ", "
455                << thePoint->y() << ", " << thePoint->z() << ")";
456   return *this;
457 }
458
459 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir)
460 {
461   importModule("GeomAPI", "GeomAPI_Dir");
462   myDumpBuffer << "GeomAPI_Dir(" << theDir->x() << ", "
463                << theDir->y() << ", " << theDir->z() << ")";
464   return *this;
465 }
466
467 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
468     const std::shared_ptr<GeomDataAPI_Dir>& theDir)
469 {
470   myDumpBuffer << theDir->x() << ", " << theDir->y() << ", " << theDir->z();
471   return *this;
472 }
473
474 static void dumpArray(std::ostringstream& theOutput, int theSize,
475                       double* theValues, std::string* theTexts)
476 {
477   for (int i = 0; i < theSize; ++i) {
478     if (i > 0)
479       theOutput << ", ";
480     if (theTexts[i].empty())
481       theOutput << theValues[i];
482     else
483       theOutput << "\"" << theTexts[i] << "\"";
484   }
485 }
486
487 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
488     const std::shared_ptr<GeomDataAPI_Point>& thePoint)
489 {
490   static const int aSize = 3;
491   double aValues[aSize] = {thePoint->x(), thePoint->y(), thePoint->z()};
492   std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY(), thePoint->textZ()};
493   dumpArray(myDumpBuffer, aSize, aValues, aTexts);
494   return *this;
495 }
496
497 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
498     const std::shared_ptr<GeomDataAPI_Point2D>& thePoint)
499 {
500   static const int aSize = 2;
501   double aValues[aSize] = {thePoint->x(), thePoint->y()};
502   std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY()};
503   dumpArray(myDumpBuffer, aSize, aValues, aTexts);
504   return *this;
505 }
506
507 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
508     const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool)
509 {
510   myDumpBuffer << (theAttrBool->value() ? "True" : "False");
511   return *this;
512 }
513
514 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
515     const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt)
516 {
517   std::string aText = theAttrInt->text();
518   if (aText.empty())
519     myDumpBuffer << theAttrInt->value();
520   else
521     myDumpBuffer << "\"" << aText << "\"";
522   return *this;
523 }
524
525 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
526     const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal)
527 {
528   std::string aText = theAttrReal->text();
529   if (aText.empty())
530     myDumpBuffer << theAttrReal->value();
531   else
532     myDumpBuffer << "\"" << aText << "\"";
533   return *this;
534 }
535
536 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
537     const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr)
538 {
539   myDumpBuffer << "\"" << theAttrStr->value() << "\"";
540   return *this;
541 }
542
543 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FeaturePtr& theEntity)
544 {
545   myDumpBuffer << name(theEntity);
546
547   bool isUserDefinedName = !myNames[theEntity].second.empty();
548   // store results if they have user-defined names or colors
549   std::list<ResultPtr> aResultsWithNameOrColor;
550   const std::list<ResultPtr>& aResults = theEntity->results();
551   std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
552   for (; aResIt != aResults.end(); ++aResIt)
553     if (!myNames[*aResIt].second.empty() || !isDefaultColor(*aResIt))
554       aResultsWithNameOrColor.push_back(*aResIt);
555   // store just dumped entity to stack
556   myEntitiesStack.push(LastDumpedEntity(theEntity, isUserDefinedName, aResultsWithNameOrColor));
557
558   // remove entity from the list of not dumped items
559   myNotDumpedEntities.erase(theEntity);
560   return *this;
561 }
562
563 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ResultPtr& theResult)
564 {
565   FeaturePtr aFeature = ModelAPI_Feature::feature(theResult);
566   int anIndex = 0;
567   std::list<ResultPtr> aResults = aFeature->results();
568   for(std::list<ResultPtr>::const_iterator anIt = aResults.cbegin(); anIt != aResults.cend(); ++anIt, ++anIndex) {
569     if(theResult->isSame(*anIt)) {
570       break;
571     }
572   }
573   myDumpBuffer << name(aFeature) << ".result()[" << anIndex << "]";
574   return *this;
575 }
576
577 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ObjectPtr& theObject)
578 {
579   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
580   if(aFeature.get()) {
581     myDumpBuffer << name(aFeature);
582     return *this;
583   }
584
585   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
586   if(aResult.get()) {
587     *this << aResult;
588     return *this;
589   }
590
591   return *this;
592 }
593
594 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const AttributePtr& theAttr)
595 {
596   FeaturePtr anOwner = ModelAPI_Feature::feature(theAttr->owner());
597   myDumpBuffer << name(anOwner) << "." << attributeGetter(anOwner, theAttr->id()) << "()";
598   return *this;
599 }
600
601 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
602     const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr)
603 {
604   if (theRefAttr->isObject())
605     *this << theRefAttr->object();
606   else
607     *this << theRefAttr->attr();
608   return *this;
609 }
610
611 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
612     const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList)
613 {
614   myDumpBuffer << "[";
615   std::list<std::pair<ObjectPtr, AttributePtr> > aList = theRefAttrList->list();
616   bool isAdded = false;
617   std::list<std::pair<ObjectPtr, AttributePtr> >::const_iterator anIt = aList.begin();
618   for (; anIt != aList.end(); ++anIt) {
619     if (isAdded)
620       myDumpBuffer << ", ";
621     else
622       isAdded = true;
623     if (anIt->first)
624       *this << anIt->first;
625     else if (anIt->second)
626       * this << anIt->second;
627   }
628   myDumpBuffer << "]";
629   return *this;
630 }
631
632 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
633     const std::shared_ptr<ModelAPI_AttributeReference>& theReference)
634 {
635   *this << theReference->value();
636   return *this;
637 }
638
639 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
640     const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList)
641 {
642   static const int aThreshold = 2;
643   // if number of elements in the list if greater than a threshold,
644   // dump it in a separate line with specific name
645   std::string aDumped = myDumpBuffer.str();
646   if (aDumped.empty() || theRefList->size() <= aThreshold) {
647     myDumpBuffer << "[";
648     std::list<ObjectPtr> aList = theRefList->list();
649     bool isAdded = false;
650     std::list<ObjectPtr>::const_iterator anIt = aList.begin();
651     for (; anIt != aList.end(); ++anIt) {
652       if (isAdded)
653         myDumpBuffer << ", ";
654       else
655         isAdded = true;
656
657       *this << *anIt;
658     }
659     myDumpBuffer << "]";
660   } else {
661     // clear buffer and store list "as is"
662     myDumpBuffer.str("");
663     *this << theRefList;
664     // save buffer and clear it again
665     std::string aDumpedList = myDumpBuffer.str();
666     myDumpBuffer.str("");
667     // obtain name of list
668     FeaturePtr anOwner = ModelAPI_Feature::feature(theRefList->owner());
669     std::string aListName = name(anOwner) + "_objects";
670     // store all previous data
671     myDumpBuffer << aListName << " = " << aDumpedList << std::endl
672                  << aDumped << aListName;
673   }
674   return *this;
675 }
676
677 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
678     const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect)
679 {
680   myDumpBuffer << "model.selection(";
681
682   if(!theAttrSelect->isInitialized()) {
683     myDumpBuffer << ")";
684     return *this;
685   }
686
687   GeomShapePtr aShape = theAttrSelect->value();
688   if(!aShape.get()) {
689     aShape = theAttrSelect->context()->shape();
690   }
691
692   if(!aShape.get()) {
693     myDumpBuffer << ")";
694     return *this;
695   }
696
697   myDumpBuffer << "\"" << aShape->shapeTypeStr() << "\", \"" << theAttrSelect->namingName() << "\")";
698   return *this;
699 }
700
701 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
702     const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList)
703 {
704   myDumpBuffer << "[";
705
706   GeomShapePtr aShape;
707   std::string aShapeTypeStr;
708
709   bool isAdded = false;
710
711   for(int anIndex = 0; anIndex < theAttrSelList->size(); ++anIndex) {
712     AttributeSelectionPtr anAttribute = theAttrSelList->value(anIndex);
713     aShape = anAttribute->value();
714     if(!aShape.get()) {
715       aShape = anAttribute->context()->shape();
716     }
717
718     if(!aShape.get()) {
719       continue;
720     }
721
722     if(isAdded) {
723       myDumpBuffer << ", ";
724     } else {
725       isAdded = true;
726     }
727     myDumpBuffer << "model.selection(\"" << aShape->shapeTypeStr() << "\", \"" << anAttribute->namingName() << "\")";
728   }
729
730   myDumpBuffer << "]";
731   return *this;
732 }
733
734 /// Dump std::endl
735 MODELHIGHAPI_EXPORT
736 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
737                                 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&))
738 {
739   theDumper.myDumpBuffer << theEndl;
740
741   if (!theDumper.myEntitiesStack.empty()) {
742     // Name for composite feature is dumped when all sub-entities are dumped
743     // (see method ModelHighAPI_Dumper::processSubs).
744     const ModelHighAPI_Dumper::LastDumpedEntity& aLastDumped = theDumper.myEntitiesStack.top();
745     CompositeFeaturePtr aComposite =
746         std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aLastDumped.myEntity);
747     if (!aComposite)
748       theDumper.dumpEntitySetName();
749   }
750
751   // store all not-dumped entities first
752   std::set<EntityPtr> aNotDumped = theDumper.myNotDumpedEntities;
753   std::string aBufCopy = theDumper.myDumpBuffer.str();
754   theDumper.clear(true);
755   std::set<EntityPtr>::const_iterator anIt = aNotDumped.begin();
756   for (; anIt != aNotDumped.end(); ++anIt) {
757     // if the feature is composite, dump it with all subs
758     CompositeFeaturePtr aCompFeat =
759         std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anIt);
760     if (aCompFeat)
761       theDumper.process(aCompFeat, true);
762     else {
763       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
764       theDumper.dumpFeature(aFeature, true);
765     }
766   }
767
768   // avoid multiple empty lines
769   size_t anInd = std::string::npos;
770   while ((anInd = aBufCopy.find("\n\n\n")) != std::string::npos)
771     aBufCopy.erase(anInd, 1);
772   // then store currently dumped string
773   theDumper.myFullDump << aBufCopy;
774
775   return theDumper;
776 }