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