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