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