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