]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModelHighAPI/ModelHighAPI_Dumper.cpp
Salome HOME
Removed Debug for deflection dump
[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.get() && 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   return fabs(aCurrent - aDefault) < 1.e-12;
475 }
476
477 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char theChar)
478 {
479   myDumpBuffer << theChar;
480   return *this;
481 }
482
483 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char* theString)
484 {
485   myDumpBuffer << theString;
486   return *this;
487 }
488
489 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::string& theString)
490 {
491   myDumpBuffer << theString;
492   return *this;
493 }
494
495 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const bool theValue)
496 {
497   myDumpBuffer << (theValue ? "True" : "False");
498   return *this;
499 }
500
501 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const int theValue)
502 {
503   myDumpBuffer << theValue;
504   return *this;
505 }
506
507 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const double theValue)
508 {
509   myDumpBuffer << theValue;
510   return *this;
511 }
512
513 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
514 {
515   importModule("GeomAPI", "GeomAPI_Pnt");
516   myDumpBuffer << "GeomAPI_Pnt(" << thePoint->x() << ", "
517                << thePoint->y() << ", " << thePoint->z() << ")";
518   return *this;
519 }
520
521 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir)
522 {
523   importModule("GeomAPI", "GeomAPI_Dir");
524   myDumpBuffer << "GeomAPI_Dir(" << theDir->x() << ", "
525                << theDir->y() << ", " << theDir->z() << ")";
526   return *this;
527 }
528
529 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
530     const std::shared_ptr<GeomDataAPI_Dir>& theDir)
531 {
532   myDumpBuffer << theDir->x() << ", " << theDir->y() << ", " << theDir->z();
533   return *this;
534 }
535
536 static void dumpArray(std::ostringstream& theOutput, int theSize,
537                       double* theValues, std::string* theTexts)
538 {
539   for (int i = 0; i < theSize; ++i) {
540     if (i > 0)
541       theOutput << ", ";
542     if (theTexts[i].empty())
543       theOutput << theValues[i];
544     else
545       theOutput << "\"" << theTexts[i] << "\"";
546   }
547 }
548
549 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
550     const std::shared_ptr<GeomDataAPI_Point>& thePoint)
551 {
552   static const int aSize = 3;
553   double aValues[aSize] = {thePoint->x(), thePoint->y(), thePoint->z()};
554   std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY(), thePoint->textZ()};
555   dumpArray(myDumpBuffer, aSize, aValues, aTexts);
556   return *this;
557 }
558
559 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
560     const std::shared_ptr<GeomDataAPI_Point2D>& thePoint)
561 {
562   static const int aSize = 2;
563   double aValues[aSize] = {thePoint->x(), thePoint->y()};
564   std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY()};
565   dumpArray(myDumpBuffer, aSize, aValues, aTexts);
566   return *this;
567 }
568
569 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
570     const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool)
571 {
572   myDumpBuffer << (theAttrBool->value() ? "True" : "False");
573   return *this;
574 }
575
576 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
577     const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt)
578 {
579   std::string aText = theAttrInt->text();
580   if (aText.empty())
581     myDumpBuffer << theAttrInt->value();
582   else
583     myDumpBuffer << "\"" << aText << "\"";
584   return *this;
585 }
586
587 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
588     const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal)
589 {
590   std::string aText = theAttrReal->text();
591   if (aText.empty())
592     myDumpBuffer << theAttrReal->value();
593   else
594     myDumpBuffer << "\"" << aText << "\"";
595   return *this;
596 }
597
598 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
599     const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr)
600 {
601   myDumpBuffer << "\"" << theAttrStr->value() << "\"";
602   return *this;
603 }
604
605 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FeaturePtr& theEntity)
606 {
607   myDumpBuffer << name(theEntity);
608
609   bool isUserDefinedName = !myNames[theEntity].myIsDefault;
610   // store results if they have user-defined names or colors
611   std::list<ResultPtr> aResultsWithNameOrColor;
612   const std::list<ResultPtr>& aResults = theEntity->results();
613   std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
614   for (; aResIt != aResults.end(); ++aResIt)
615     if (!myNames[*aResIt].myIsDefault || !isDefaultColor(*aResIt) || !isDefaultDeflection(*aResIt))
616       aResultsWithNameOrColor.push_back(*aResIt);
617   // store just dumped entity to stack
618   myEntitiesStack.push(LastDumpedEntity(theEntity, isUserDefinedName, aResultsWithNameOrColor));
619
620   // remove entity from the list of not dumped items
621   myNotDumpedEntities.erase(theEntity);
622   return *this;
623 }
624
625 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ResultPtr& theResult)
626 {
627   FeaturePtr aFeature = ModelAPI_Feature::feature(theResult);
628   int anIndex = 0;
629   std::list<ResultPtr> aResults = aFeature->results();
630   for(std::list<ResultPtr>::const_iterator anIt = aResults.cbegin(); anIt != aResults.cend(); ++anIt, ++anIndex) {
631     if(theResult->isSame(*anIt)) {
632       break;
633     }
634   }
635   myDumpBuffer << name(aFeature) << ".result()[" << anIndex << "]";
636   return *this;
637 }
638
639 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ObjectPtr& theObject)
640 {
641   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
642   if(aFeature.get()) {
643     myDumpBuffer << name(aFeature);
644     return *this;
645   }
646
647   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
648   if(aResult.get()) {
649     *this << aResult;
650     return *this;
651   }
652
653   return *this;
654 }
655
656 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const AttributePtr& theAttr)
657 {
658   FeaturePtr anOwner = ModelAPI_Feature::feature(theAttr->owner());
659
660   std::string aWrapperPrefix, aWrapperSuffix;
661   // Check the attribute belongs to copied (in multi-translation or multi-rotation) feature.
662   // In this case we need to cast explicitly feature to appropriate type.
663   AttributeBooleanPtr isCopy = anOwner->boolean("Copy");
664   if (isCopy.get() && isCopy->value()) {
665     aWrapperPrefix = featureWrapper(anOwner) + "(";
666     aWrapperSuffix = ")";
667     importModule("SketchAPI");
668   }
669
670   myDumpBuffer << aWrapperPrefix << name(anOwner) << aWrapperSuffix
671                << "." << attributeGetter(anOwner, theAttr->id()) << "()";
672   return *this;
673 }
674
675 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
676     const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr)
677 {
678   if (theRefAttr->isObject())
679     *this << theRefAttr->object();
680   else
681     *this << theRefAttr->attr();
682   return *this;
683 }
684
685 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
686     const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList)
687 {
688   myDumpBuffer << "[";
689   std::list<std::pair<ObjectPtr, AttributePtr> > aList = theRefAttrList->list();
690   bool isAdded = false;
691   std::list<std::pair<ObjectPtr, AttributePtr> >::const_iterator anIt = aList.begin();
692   for (; anIt != aList.end(); ++anIt) {
693     if (isAdded)
694       myDumpBuffer << ", ";
695     else
696       isAdded = true;
697     if (anIt->first)
698       *this << anIt->first;
699     else if (anIt->second)
700       * this << anIt->second;
701   }
702   myDumpBuffer << "]";
703   return *this;
704 }
705
706 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
707     const std::shared_ptr<ModelAPI_AttributeReference>& theReference)
708 {
709   *this << theReference->value();
710   return *this;
711 }
712
713 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
714     const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList)
715 {
716   static const int aThreshold = 2;
717   // if number of elements in the list if greater than a threshold,
718   // dump it in a separate line with specific name
719   std::string aDumped = myDumpBuffer.str();
720   if (aDumped.empty() || theRefList->size() <= aThreshold) {
721     myDumpBuffer << "[";
722     std::list<ObjectPtr> aList = theRefList->list();
723     bool isAdded = false;
724     std::list<ObjectPtr>::const_iterator anIt = aList.begin();
725     for (; anIt != aList.end(); ++anIt) {
726       if (isAdded)
727         myDumpBuffer << ", ";
728       else
729         isAdded = true;
730
731       *this << *anIt;
732     }
733     myDumpBuffer << "]";
734   } else {
735     // clear buffer and store list "as is"
736     myDumpBuffer.str("");
737     *this << theRefList;
738     // save buffer and clear it again
739     std::string aDumpedList = myDumpBuffer.str();
740     myDumpBuffer.str("");
741     // obtain name of list
742     FeaturePtr anOwner = ModelAPI_Feature::feature(theRefList->owner());
743     std::string aListName = name(anOwner) + "_objects";
744     // store all previous data
745     myDumpBuffer << aListName << " = " << aDumpedList << std::endl
746                  << aDumped << aListName;
747   }
748   return *this;
749 }
750
751 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
752     const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect)
753 {
754   myDumpBuffer << "model.selection(";
755
756   if(!theAttrSelect->isInitialized()) {
757     myDumpBuffer << ")";
758     return *this;
759   }
760
761   GeomShapePtr aShape = theAttrSelect->value();
762   if(!aShape.get()) {
763     aShape = theAttrSelect->context()->shape();
764   }
765
766   if(!aShape.get()) {
767     myDumpBuffer << ")";
768     return *this;
769   }
770
771   myDumpBuffer << "\"" << aShape->shapeTypeStr() << "\", \"" << theAttrSelect->namingName() << "\")";
772   return *this;
773 }
774
775 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
776     const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList)
777 {
778   myDumpBuffer << "[";
779
780   GeomShapePtr aShape;
781   std::string aShapeTypeStr;
782
783   bool isAdded = false;
784
785   for(int anIndex = 0; anIndex < theAttrSelList->size(); ++anIndex) {
786     AttributeSelectionPtr anAttribute = theAttrSelList->value(anIndex);
787     aShape = anAttribute->value();
788     if(!aShape.get()) {
789       aShape = anAttribute->context()->shape();
790     }
791
792     if(!aShape.get()) {
793       continue;
794     }
795
796     if(isAdded) {
797       myDumpBuffer << ", ";
798     } else {
799       isAdded = true;
800     }
801     myDumpBuffer << "model.selection(\"" << aShape->shapeTypeStr() << "\", \"" << anAttribute->namingName() << "\")";
802   }
803
804   myDumpBuffer << "]";
805   return *this;
806 }
807
808 /// Dump std::endl
809 MODELHIGHAPI_EXPORT
810 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
811                                 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&))
812 {
813   theDumper.myDumpBuffer << theEndl;
814
815   if (!theDumper.myEntitiesStack.empty()) {
816     // Name for composite feature is dumped when all sub-entities are dumped
817     // (see method ModelHighAPI_Dumper::processSubs).
818     const ModelHighAPI_Dumper::LastDumpedEntity& aLastDumped = theDumper.myEntitiesStack.top();
819     CompositeFeaturePtr aComposite =
820         std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aLastDumped.myEntity);
821     if (!aComposite)
822       theDumper.dumpEntitySetName();
823   }
824
825   // store all not-dumped entities first
826   std::set<EntityPtr> aNotDumped = theDumper.myNotDumpedEntities;
827   std::string aBufCopy = theDumper.myDumpBuffer.str();
828   theDumper.clear(true);
829   std::set<EntityPtr>::const_iterator anIt = aNotDumped.begin();
830   for (; anIt != aNotDumped.end(); ++anIt) {
831     // if the feature is composite, dump it with all subs
832     CompositeFeaturePtr aCompFeat =
833         std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anIt);
834     if (aCompFeat)
835       theDumper.process(aCompFeat, true);
836     else {
837       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
838       theDumper.dumpFeature(aFeature, true);
839     }
840   }
841
842   // avoid multiple empty lines
843   size_t anInd = std::string::npos;
844   while ((anInd = aBufCopy.find("\n\n\n")) != std::string::npos)
845     aBufCopy.erase(anInd, 1);
846   // then store currently dumped string
847   theDumper.myFullDump << aBufCopy;
848
849   return theDumper;
850 }