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