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