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