Salome HOME
5ec70a34b3a346f4618647d09a2963e5c51694c1
[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   bool isSubDumped = false;
258   int aNbSubs = theComposite->numberOfSubs();
259   for (int anIndex = 0; anIndex < aNbSubs; ++anIndex) {
260     FeaturePtr aFeature = theComposite->subFeature(anIndex);
261     if (isDumped(aFeature))
262       continue;
263
264     isSubDumped = true;
265     CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
266     if (aCompFeat) // iteratively process composite features
267       isOk = process(aCompFeat) && isOk;
268     else
269       dumpFeature(aFeature, true);
270   }
271
272   bool isDumpSetName = !myEntitiesStack.empty() &&
273       myEntitiesStack.top().myEntity == EntityPtr(theComposite);
274   bool isForceModelDo = isSubDumped && isDumpSetName &&
275       (myEntitiesStack.top().myUserName || !myEntitiesStack.top().myResults.empty());
276   // It is necessary for the sketch to create its result when complete (command "model.do()").
277   // This option is set by flat theDumpModelDo.
278   // However, nested sketches are rebuilt by parent feature, so, they do not need
279   // explicit call of "model.do()". This will be controlled by the depth of the stack.
280   if (isForceModelDo || (theDumpModelDo && gCompositeStackDepth <= 1))
281     *this << "model.do()" << std::endl;
282
283   // dump "setName" for composite feature
284   if (isDumpSetName)
285     dumpEntitySetName();
286   return isOk;
287 }
288
289 void ModelHighAPI_Dumper::dumpSubFeatureNameAndColor(const std::string theSubFeatureGet,
290                                                      const FeaturePtr& theSubFeature)
291 {
292   name(theSubFeature, false);
293   myNames[theSubFeature] = std::pair<std::string, std::string>(theSubFeatureGet, theSubFeature->name());
294
295   // store results if they have user-defined names or colors
296   std::list<ResultPtr> aResultsWithNameOrColor;
297   const std::list<ResultPtr>& aResults = theSubFeature->results();
298   std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
299   for (; aResIt != aResults.end(); ++aResIt) {
300     std::string aResName = (*aResIt)->data()->name();
301     myNames[*aResIt] = std::pair<std::string, std::string>(aResName, aResName);
302     aResultsWithNameOrColor.push_back(*aResIt);
303   }
304
305   // store just dumped entity to stack
306   myEntitiesStack.push(LastDumpedEntity(theSubFeature, true, aResultsWithNameOrColor));
307
308   dumpEntitySetName();
309 }
310
311 bool ModelHighAPI_Dumper::exportTo(const std::string& theFileName)
312 {
313   std::ofstream aFile;
314   OSD_OpenStream(aFile, theFileName.c_str(), std::ofstream::out);
315   if (!aFile.is_open())
316     return false;
317
318   // standard header
319   for (ModulesMap::const_iterator aModIt = myModules.begin();
320        aModIt != myModules.end(); ++aModIt) {
321     aFile << "from " << aModIt->first << " import ";
322     if (aModIt->second.empty() || 
323         aModIt->second.find(std::string()) != aModIt->second.end())
324       aFile << "*"; // import whole module
325     else {
326       // import specific features
327       std::set<std::string>::const_iterator anObjIt = aModIt->second.begin();
328       aFile << *anObjIt;
329       for (++anObjIt; anObjIt != aModIt->second.end(); ++anObjIt)
330         aFile << ", " << *anObjIt;
331     }
332     aFile << std::endl;
333   }
334   if (!myModules.empty())
335     aFile << std::endl;
336
337   aFile << "import model" << std::endl << std::endl;
338   aFile << "model.begin()" << std::endl;
339
340   // dump collected data
341   aFile << myFullDump.str();
342   aFile << myDumpBuffer.str();
343
344   // standard footer
345   aFile << "model.end()" << std::endl;
346
347   aFile.close();
348   clear();
349
350   return true;
351 }
352
353 void ModelHighAPI_Dumper::importModule(const std::string& theModuleName,
354                                        const std::string& theObject)
355 {
356   myModules[theModuleName].insert(theObject);
357 }
358
359 void ModelHighAPI_Dumper::dumpEntitySetName()
360 {
361   const LastDumpedEntity& aLastDumped = myEntitiesStack.top();
362
363   // dump "setName" for the entity
364   if (aLastDumped.myUserName) {
365     std::pair<std::string, std::string> anEntityNames = myNames[aLastDumped.myEntity];
366     if (!anEntityNames.second.empty())
367       myDumpBuffer << anEntityNames.first << ".setName(\"" << anEntityNames.second << "\")" << std::endl;
368     anEntityNames.second.clear(); // don't dump "setName" for the entity twice
369   }
370   // dump "setName" for results
371   std::list<ResultPtr>::const_iterator aResIt = aLastDumped.myResults.begin();
372   std::list<ResultPtr>::const_iterator aResEnd = aLastDumped.myResults.end();
373   for (; aResIt != aResEnd; ++aResIt) {
374     // set result name
375     std::pair<std::string, std::string> anEntityNames = myNames[*aResIt];
376     if (!anEntityNames.second.empty()) {
377       *this << *aResIt;
378       myDumpBuffer << ".setName(\"" << anEntityNames.second << "\")" << std::endl;
379       anEntityNames.second.clear(); // don't dump "setName" for the entity twice
380     }
381     // set result color
382     if (!isDefaultColor(*aResIt)) {
383       AttributeIntArrayPtr aColor = (*aResIt)->data()->intArray(ModelAPI_Result::COLOR_ID());
384       if (aColor && aColor->isInitialized()) {
385         *this << *aResIt;
386         myDumpBuffer << ".setColor(" << aColor->value(0) << ", " << aColor->value(1)
387                      << ", " << aColor->value(2) << ")" << std::endl;
388       }
389     }
390   }
391
392   myEntitiesStack.pop();
393 }
394
395 bool ModelHighAPI_Dumper::isDumped(const EntityPtr& theEntity) const
396 {
397   EntityNameMap::const_iterator aFound = myNames.find(theEntity);
398   return aFound != myNames.end();
399 }
400
401 bool ModelHighAPI_Dumper::isDefaultColor(const ResultPtr& theResult) const
402 {
403   AttributeIntArrayPtr aColor = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
404   if (!aColor || !aColor->isInitialized())
405     return true;
406
407   std::string aSection, aName, aDefault;
408   theResult->colorConfigInfo(aSection, aName, aDefault);
409
410   // dump current color
411   std::ostringstream aColorInfo;
412   aColorInfo << aColor->value(0) << "," << aColor->value(1) << "," << aColor->value(2);
413
414   return aDefault == aColorInfo.str();
415 }
416
417 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char theChar)
418 {
419   myDumpBuffer << theChar;
420   return *this;
421 }
422
423 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char* theString)
424 {
425   myDumpBuffer << theString;
426   return *this;
427 }
428
429 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::string& theString)
430 {
431   myDumpBuffer << theString;
432   return *this;
433 }
434
435 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const bool theValue)
436 {
437   myDumpBuffer << (theValue ? "True" : "False");
438   return *this;
439 }
440
441 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const int theValue)
442 {
443   myDumpBuffer << theValue;
444   return *this;
445 }
446
447 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const double theValue)
448 {
449   myDumpBuffer << theValue;
450   return *this;
451 }
452
453 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
454 {
455   importModule("GeomAPI", "GeomAPI_Pnt");
456   myDumpBuffer << "GeomAPI_Pnt(" << thePoint->x() << ", "
457                << thePoint->y() << ", " << thePoint->z() << ")";
458   return *this;
459 }
460
461 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir)
462 {
463   importModule("GeomAPI", "GeomAPI_Dir");
464   myDumpBuffer << "GeomAPI_Dir(" << theDir->x() << ", "
465                << theDir->y() << ", " << theDir->z() << ")";
466   return *this;
467 }
468
469 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
470     const std::shared_ptr<GeomDataAPI_Dir>& theDir)
471 {
472   myDumpBuffer << theDir->x() << ", " << theDir->y() << ", " << theDir->z();
473   return *this;
474 }
475
476 static void dumpArray(std::ostringstream& theOutput, int theSize,
477                       double* theValues, std::string* theTexts)
478 {
479   for (int i = 0; i < theSize; ++i) {
480     if (i > 0)
481       theOutput << ", ";
482     if (theTexts[i].empty())
483       theOutput << theValues[i];
484     else
485       theOutput << "\"" << theTexts[i] << "\"";
486   }
487 }
488
489 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
490     const std::shared_ptr<GeomDataAPI_Point>& thePoint)
491 {
492   static const int aSize = 3;
493   double aValues[aSize] = {thePoint->x(), thePoint->y(), thePoint->z()};
494   std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY(), thePoint->textZ()};
495   dumpArray(myDumpBuffer, aSize, aValues, aTexts);
496   return *this;
497 }
498
499 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
500     const std::shared_ptr<GeomDataAPI_Point2D>& thePoint)
501 {
502   static const int aSize = 2;
503   double aValues[aSize] = {thePoint->x(), thePoint->y()};
504   std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY()};
505   dumpArray(myDumpBuffer, aSize, aValues, aTexts);
506   return *this;
507 }
508
509 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
510     const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool)
511 {
512   myDumpBuffer << (theAttrBool->value() ? "True" : "False");
513   return *this;
514 }
515
516 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
517     const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt)
518 {
519   std::string aText = theAttrInt->text();
520   if (aText.empty())
521     myDumpBuffer << theAttrInt->value();
522   else
523     myDumpBuffer << "\"" << aText << "\"";
524   return *this;
525 }
526
527 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
528     const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal)
529 {
530   std::string aText = theAttrReal->text();
531   if (aText.empty())
532     myDumpBuffer << theAttrReal->value();
533   else
534     myDumpBuffer << "\"" << aText << "\"";
535   return *this;
536 }
537
538 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
539     const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr)
540 {
541   myDumpBuffer << "\"" << theAttrStr->value() << "\"";
542   return *this;
543 }
544
545 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FeaturePtr& theEntity)
546 {
547   myDumpBuffer << name(theEntity);
548
549   bool isUserDefinedName = !myNames[theEntity].second.empty();
550   // store results if they have user-defined names or colors
551   std::list<ResultPtr> aResultsWithNameOrColor;
552   const std::list<ResultPtr>& aResults = theEntity->results();
553   std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
554   for (; aResIt != aResults.end(); ++aResIt)
555     if (!myNames[*aResIt].second.empty() || !isDefaultColor(*aResIt))
556       aResultsWithNameOrColor.push_back(*aResIt);
557   // store just dumped entity to stack
558   myEntitiesStack.push(LastDumpedEntity(theEntity, isUserDefinedName, aResultsWithNameOrColor));
559
560   // remove entity from the list of not dumped items
561   myNotDumpedEntities.erase(theEntity);
562   return *this;
563 }
564
565 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ResultPtr& theResult)
566 {
567   FeaturePtr aFeature = ModelAPI_Feature::feature(theResult);
568   int anIndex = 0;
569   std::list<ResultPtr> aResults = aFeature->results();
570   for(std::list<ResultPtr>::const_iterator anIt = aResults.cbegin(); anIt != aResults.cend(); ++anIt, ++anIndex) {
571     if(theResult->isSame(*anIt)) {
572       break;
573     }
574   }
575   myDumpBuffer << name(aFeature) << ".result()[" << anIndex << "]";
576   return *this;
577 }
578
579 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ObjectPtr& theObject)
580 {
581   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
582   if(aFeature.get()) {
583     myDumpBuffer << name(aFeature);
584     return *this;
585   }
586
587   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
588   if(aResult.get()) {
589     *this << aResult;
590     return *this;
591   }
592
593   return *this;
594 }
595
596 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const AttributePtr& theAttr)
597 {
598   FeaturePtr anOwner = ModelAPI_Feature::feature(theAttr->owner());
599   myDumpBuffer << name(anOwner) << "." << attributeGetter(anOwner, theAttr->id()) << "()";
600   return *this;
601 }
602
603 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
604     const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr)
605 {
606   if (theRefAttr->isObject())
607     *this << theRefAttr->object();
608   else
609     *this << theRefAttr->attr();
610   return *this;
611 }
612
613 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
614     const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList)
615 {
616   myDumpBuffer << "[";
617   std::list<std::pair<ObjectPtr, AttributePtr> > aList = theRefAttrList->list();
618   bool isAdded = false;
619   std::list<std::pair<ObjectPtr, AttributePtr> >::const_iterator anIt = aList.begin();
620   for (; anIt != aList.end(); ++anIt) {
621     if (isAdded)
622       myDumpBuffer << ", ";
623     else
624       isAdded = true;
625     if (anIt->first)
626       *this << anIt->first;
627     else if (anIt->second)
628       * this << anIt->second;
629   }
630   myDumpBuffer << "]";
631   return *this;
632 }
633
634 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
635     const std::shared_ptr<ModelAPI_AttributeReference>& theReference)
636 {
637   *this << theReference->value();
638   return *this;
639 }
640
641 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
642     const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList)
643 {
644   static const int aThreshold = 2;
645   // if number of elements in the list if greater than a threshold,
646   // dump it in a separate line with specific name
647   std::string aDumped = myDumpBuffer.str();
648   if (aDumped.empty() || theRefList->size() <= aThreshold) {
649     myDumpBuffer << "[";
650     std::list<ObjectPtr> aList = theRefList->list();
651     bool isAdded = false;
652     std::list<ObjectPtr>::const_iterator anIt = aList.begin();
653     for (; anIt != aList.end(); ++anIt) {
654       if (isAdded)
655         myDumpBuffer << ", ";
656       else
657         isAdded = true;
658
659       *this << *anIt;
660     }
661     myDumpBuffer << "]";
662   } else {
663     // clear buffer and store list "as is"
664     myDumpBuffer.str("");
665     *this << theRefList;
666     // save buffer and clear it again
667     std::string aDumpedList = myDumpBuffer.str();
668     myDumpBuffer.str("");
669     // obtain name of list
670     FeaturePtr anOwner = ModelAPI_Feature::feature(theRefList->owner());
671     std::string aListName = name(anOwner) + "_objects";
672     // store all previous data
673     myDumpBuffer << aListName << " = " << aDumpedList << std::endl
674                  << aDumped << aListName;
675   }
676   return *this;
677 }
678
679 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
680     const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect)
681 {
682   myDumpBuffer << "model.selection(";
683
684   if(!theAttrSelect->isInitialized()) {
685     myDumpBuffer << ")";
686     return *this;
687   }
688
689   GeomShapePtr aShape = theAttrSelect->value();
690   if(!aShape.get()) {
691     aShape = theAttrSelect->context()->shape();
692   }
693
694   if(!aShape.get()) {
695     myDumpBuffer << ")";
696     return *this;
697   }
698
699   myDumpBuffer << "\"" << aShape->shapeTypeStr() << "\", \"" << theAttrSelect->namingName() << "\")";
700   return *this;
701 }
702
703 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
704     const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList)
705 {
706   myDumpBuffer << "[";
707
708   GeomShapePtr aShape;
709   std::string aShapeTypeStr;
710
711   bool isAdded = false;
712
713   for(int anIndex = 0; anIndex < theAttrSelList->size(); ++anIndex) {
714     AttributeSelectionPtr anAttribute = theAttrSelList->value(anIndex);
715     aShape = anAttribute->value();
716     if(!aShape.get()) {
717       aShape = anAttribute->context()->shape();
718     }
719
720     if(!aShape.get()) {
721       continue;
722     }
723
724     if(isAdded) {
725       myDumpBuffer << ", ";
726     } else {
727       isAdded = true;
728     }
729     myDumpBuffer << "model.selection(\"" << aShape->shapeTypeStr() << "\", \"" << anAttribute->namingName() << "\")";
730   }
731
732   myDumpBuffer << "]";
733   return *this;
734 }
735
736 /// Dump std::endl
737 MODELHIGHAPI_EXPORT
738 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
739                                 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&))
740 {
741   theDumper.myDumpBuffer << theEndl;
742
743   if (!theDumper.myEntitiesStack.empty()) {
744     // Name for composite feature is dumped when all sub-entities are dumped
745     // (see method ModelHighAPI_Dumper::processSubs).
746     const ModelHighAPI_Dumper::LastDumpedEntity& aLastDumped = theDumper.myEntitiesStack.top();
747     CompositeFeaturePtr aComposite =
748         std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aLastDumped.myEntity);
749     if (!aComposite)
750       theDumper.dumpEntitySetName();
751   }
752
753   // store all not-dumped entities first
754   std::set<EntityPtr> aNotDumped = theDumper.myNotDumpedEntities;
755   std::string aBufCopy = theDumper.myDumpBuffer.str();
756   theDumper.clear(true);
757   std::set<EntityPtr>::const_iterator anIt = aNotDumped.begin();
758   for (; anIt != aNotDumped.end(); ++anIt) {
759     // if the feature is composite, dump it with all subs
760     CompositeFeaturePtr aCompFeat =
761         std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anIt);
762     if (aCompFeat)
763       theDumper.process(aCompFeat, true);
764     else {
765       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
766       theDumper.dumpFeature(aFeature, true);
767     }
768   }
769
770   // avoid multiple empty lines
771   size_t anInd = std::string::npos;
772   while ((anInd = aBufCopy.find("\n\n\n")) != std::string::npos)
773     aBufCopy.erase(anInd, 1);
774   // then store currently dumped string
775   theDumper.myFullDump << aBufCopy;
776
777   return theDumper;
778 }