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