Salome HOME
Merge remote-tracking branch 'origin/Toolbars_Management'
[modules/shaper.git] / src / ModelHighAPI / ModelHighAPI_Dumper.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "ModelHighAPI_Dumper.h"
22
23 #include <Config_PropManager.h>
24
25 #include <GeomAPI_Pnt.h>
26 #include <GeomAPI_Dir.h>
27 #include <GeomAPI_ShapeExplorer.h>
28 #include <GeomAPI_ShapeIterator.h>
29 #include <GeomAlgoAPI_NExplode.h>
30
31 #include <GeomDataAPI_Dir.h>
32 #include <GeomDataAPI_Point.h>
33 #include <GeomDataAPI_Point2D.h>
34
35 #include <ModelAPI_AttributeBoolean.h>
36 #include <ModelAPI_AttributeDouble.h>
37 #include <ModelAPI_AttributeIntArray.h>
38 #include <ModelAPI_AttributeInteger.h>
39 #include <ModelAPI_AttributeRefAttr.h>
40 #include <ModelAPI_AttributeRefAttrList.h>
41 #include <ModelAPI_AttributeReference.h>
42 #include <ModelAPI_AttributeRefList.h>
43 #include <ModelAPI_AttributeSelection.h>
44 #include <ModelAPI_AttributeSelectionList.h>
45 #include <ModelAPI_AttributeString.h>
46 #include <ModelAPI_AttributeStringArray.h>
47 #include <ModelAPI_CompositeFeature.h>
48 #include <ModelAPI_Document.h>
49 #include <ModelAPI_Entity.h>
50 #include <ModelAPI_Feature.h>
51 #include <ModelAPI_Folder.h>
52 #include <ModelAPI_Result.h>
53 #include <ModelAPI_ResultBody.h>
54 #include <ModelAPI_ResultConstruction.h>
55 #include <ModelAPI_ResultPart.h>
56 #include <ModelAPI_Session.h>
57 #include <ModelAPI_Tools.h>
58
59 #include <ModelGeomAlgo_Shape.h>
60
61 #include <PartSetPlugin_Part.h>
62
63 #include <OSD_OpenFile.hxx>
64
65 #include <fstream>
66
67 static int gCompositeStackDepth = 0;
68
69 ModelHighAPI_Dumper* ModelHighAPI_Dumper::mySelf = 0;
70
71 ModelHighAPI_Dumper::ModelHighAPI_Dumper()
72   : myGeometricalSelection(false)
73 {
74   clear();
75 }
76
77 void ModelHighAPI_Dumper::setInstance(ModelHighAPI_Dumper* theDumper)
78 {
79   if (mySelf == 0)
80     mySelf = theDumper;
81 }
82
83 ModelHighAPI_Dumper* ModelHighAPI_Dumper::getInstance()
84 {
85   return mySelf;
86 }
87
88 void ModelHighAPI_Dumper::clear(bool bufferOnly)
89 {
90   myDumpBuffer.str("");
91   myDumpBuffer << std::setprecision(16);
92
93   clearNotDumped();
94
95   if (!bufferOnly) {
96     myFullDump.str("");
97     myFullDump << std::setprecision(16);
98
99     myNames.clear();
100     myModules.clear();
101     myFeatureCount.clear();
102     while (!myEntitiesStack.empty())
103       myEntitiesStack.pop();
104
105     myPostponed.clear();
106     myDumpPostponedInProgress = false;
107   }
108 }
109
110 void ModelHighAPI_Dumper::clearNotDumped()
111 {
112   myNotDumpedEntities.clear();
113 }
114
115 // Convert string to integer. If the string is not a number, return -1
116 static int toInt(const std::string& theString)
117 {
118   std::string::const_iterator aChar = theString.begin();
119   for (; aChar != theString.end(); ++aChar)
120     if (!std::isdigit(*aChar))
121       break;
122   if (aChar != theString.end())
123     return -1; // not a number
124   return std::stoi(theString);
125 }
126
127 const std::string& ModelHighAPI_Dumper::name(const EntityPtr& theEntity,
128                                              bool theSaveNotDumped,
129                                              bool theUseEntityName)
130 {
131   EntityNameMap::const_iterator aFound = myNames.find(theEntity);
132   if (aFound != myNames.end())
133     return aFound->second.myCurrentName;
134
135   // entity is not found, store it
136   std::string aName, aKind;
137   bool isDefaultName = false;
138   bool isSaveNotDumped = theSaveNotDumped;
139   std::ostringstream aDefaultName;
140   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
141   if (aFeature) {
142     aName = aFeature->name();
143     aKind = aFeature->getKind();
144   } else {
145     FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(theEntity);
146     if (aFolder) {
147       aName = aFolder->data()->name();
148       aKind = ModelAPI_Folder::ID();
149       isSaveNotDumped = false;
150     }
151   }
152
153   ObjectPtr anObject = std::dynamic_pointer_cast<ModelAPI_Object>(theEntity);
154   if (anObject) {
155     DocumentPtr aDoc = anObject->document();
156     std::pair<int, int>& aNbFeatures = myFeatureCount[aDoc][aKind];
157     aNbFeatures.first += 1;
158
159     size_t anIndex = aName.find(aKind);
160     if (anIndex == 0 && aName[aKind.length()] == '_') { // name starts with "FeatureKind_"
161       std::string anIdStr = aName.substr(aKind.length() + 1);
162       int anId = toInt(anIdStr);
163
164       // Check number of already registered objects of such kind. Index of current object
165       // should be the same to identify feature's name as automatically generated.
166       if (aNbFeatures.first == anId && aNbFeatures.second < anId) {
167         // name is not user-defined
168         isDefaultName = true;
169
170         // check there are postponed features of this kind,
171         // dump their names, because the sequence of features may be changed
172         for (std::list<EntityPtr>::const_iterator aPpIt = myPostponed.begin();
173             aPpIt != myPostponed.end(); ++aPpIt) {
174           FeaturePtr aCurFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*aPpIt);
175           if (aCurFeature && aCurFeature->getKind() == aKind) {
176             myNames[*aPpIt].myIsDefault = false;
177             isDefaultName = false;
178           }
179         }
180       }
181
182       if (anId > aNbFeatures.second)
183         aNbFeatures.second = anId;
184     }
185
186     // obtain default name for the feature
187     if (theUseEntityName)
188       aDefaultName << aName;
189     else {
190       int aFullIndex = 0;
191       NbFeaturesMap::const_iterator aFIt = myFeatureCount.begin();
192       for (; aFIt != myFeatureCount.end(); ++aFIt) {
193         std::map<std::string, std::pair<int, int> >::const_iterator aFound =
194           aFIt->second.find(aKind);
195         if (aFound != aFIt->second.end())
196           aFullIndex += aFound->second.first;
197       }
198       aDefaultName << aKind << "_" << aFullIndex;
199     }
200   }
201
202   myNames[theEntity] = EntityName(aDefaultName.str(), aName, isDefaultName);
203   if (isSaveNotDumped)
204     myNotDumpedEntities.insert(theEntity);
205
206   // store names of results
207   if (aFeature)
208     saveResultNames(aFeature);
209
210   return myNames[theEntity].myCurrentName;
211 }
212
213 const std::string& ModelHighAPI_Dumper::parentName(const FeaturePtr& theEntity)
214 {
215   const std::set<AttributePtr>& aRefs = theEntity->data()->refsToMe();
216   std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
217   for (; aRefIt != aRefs.end(); ++aRefIt) {
218     CompositeFeaturePtr anOwner = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(
219         ModelAPI_Feature::feature((*aRefIt)->owner()));
220     if (anOwner)
221       return name(anOwner);
222   }
223
224   static const std::string DUMMY;
225   return DUMMY;
226 }
227
228 void ModelHighAPI_Dumper::saveResultNames(const FeaturePtr& theFeature)
229 {
230   // Default name of the feature
231   bool isFeatureDefaultName = myNames[theFeature].myIsDefault;
232
233   // Save only names of results which is not correspond to default feature name
234   const std::list<ResultPtr>& aResults = theFeature->results();
235   std::list<ResultPtr> allRes;
236   ModelAPI_Tools::allResults(theFeature, allRes);
237   for(std::list<ResultPtr>::iterator aRes = allRes.begin(); aRes != allRes.end(); aRes++) {
238     std::pair<std::string, bool> aName = ModelAPI_Tools::getDefaultName(*aRes);
239     std::string aDefaultName = aName.first;
240     std::string aResName = (*aRes)->data()->name();
241     bool isUserDefined = !(isFeatureDefaultName && aDefaultName == aResName);
242     myNames[*aRes] =
243       EntityName(aResName, (isUserDefined ? aResName : std::string()), !isUserDefined);
244   }
245 }
246
247 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc,
248                                   const std::string& theFileName)
249 {
250   // dump top level document feature
251   static const std::string aDocName("partSet");
252   myNames[theDoc] = EntityName(aDocName, std::string(), true);
253   *this << aDocName << " = model.moduleDocument()" << std::endl;
254
255   // dump subfeatures and store result to file
256   return process(theDoc) && exportTo(theFileName);
257 }
258
259 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc)
260 {
261   bool isOk = true;
262   std::list<ObjectPtr> anObjects = theDoc->allObjects();
263   std::list<ObjectPtr>::const_iterator anObjIt = anObjects.begin();
264   // firstly, dump all parameters
265   for (; anObjIt != anObjects.end(); ++ anObjIt) {
266     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIt);
267     if (aFeature)
268       dumpParameter(aFeature);
269   }
270   // dump all other features
271   for (anObjIt = anObjects.begin(); anObjIt != anObjects.end(); ++anObjIt) {
272     CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anObjIt);
273     if (aCompFeat) // iteratively process composite features
274       isOk = process(aCompFeat) && isOk;
275     else if (!isDumped(EntityPtr(*anObjIt))) {
276       // dump folder
277       FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(*anObjIt);
278       if (aFolder)
279         dumpFolder(aFolder);
280       else {
281         FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIt);
282         if (aFeature) // dump common feature
283           dumpFeature(aFeature);
284       }
285     }
286   }
287   // dump folders if any
288   dumpPostponed(true);
289   return isOk;
290 }
291
292 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
293                                   bool isForce)
294 {
295   // increase composite features stack
296   ++gCompositeStackDepth;
297   // dump composite itself
298   if (!isDumped(EntityPtr(theComposite)) || isForce)
299     dumpFeature(FeaturePtr(theComposite), isForce);
300
301   // sub-part is processed independently, because it provides separate document
302   if (theComposite->getKind() == PartSetPlugin_Part::ID()) {
303     // dump name of the part if it is different from default
304     if (!myEntitiesStack.empty())
305       dumpEntitySetName();
306
307     // decrease composite features stack because we run into separate document
308     --gCompositeStackDepth;
309
310     ResultPartPtr aPartResult =
311         std::dynamic_pointer_cast<ModelAPI_ResultPart>(theComposite->lastResult());
312     if (!aPartResult)
313       return false;
314     DocumentPtr aSubDoc = aPartResult->partDoc();
315     if (!aSubDoc)
316       return false;
317     // set name of document
318     const std::string& aPartName = myNames[theComposite].myCurrentName;
319     std::string aDocName = aPartName + "_doc";
320     myNames[aSubDoc] = EntityName(aDocName, std::string(), true);
321
322     // dump document in a separate line
323     *this << aDocName << " = " << aPartName << ".document()" << std::endl;
324     // dump features in the document
325     bool aRes = process(aSubDoc);
326     *this << "model.do()" << std::endl;
327     return aRes;
328   }
329
330   // dump sub-features
331   bool isOk = processSubs(theComposite);
332   // decrease composite features stack
333   --gCompositeStackDepth;
334
335   return isOk;
336 }
337
338 bool ModelHighAPI_Dumper::processSubs(
339   const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
340   bool theDumpModelDo)
341 {
342   bool isOk = true;
343   // dump all sub-features;
344   bool isSubDumped = false;
345   int aNbSubs = theComposite->numberOfSubs();
346   for (int anIndex = 0; anIndex < aNbSubs; ++anIndex) {
347     FeaturePtr aFeature = theComposite->subFeature(anIndex);
348     if (isDumped(EntityPtr(aFeature)))
349       continue;
350
351     isSubDumped = true;
352     CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
353     if (aCompFeat) // iteratively process composite features
354       isOk = process(aCompFeat) && isOk;
355     else
356       dumpFeature(aFeature, true);
357   }
358
359   bool isDumpSetName = !myEntitiesStack.empty() &&
360       myEntitiesStack.top().myEntity == EntityPtr(theComposite);
361   bool isForceModelDo = isSubDumped && isDumpSetName &&
362       (myEntitiesStack.top().myUserName || !myEntitiesStack.top().myResults.empty());
363   // It is necessary for the sketch to create its result when complete (command "model.do()").
364   // This option is set by flat theDumpModelDo.
365   // However, nested sketches are rebuilt by parent feature, so, they do not need
366   // explicit call of "model.do()". This will be controlled by the depth of the stack.
367   if (isForceModelDo || (theDumpModelDo && gCompositeStackDepth <= 1))
368     *this << "model.do()" << std::endl;
369
370   // dump "setName" for composite feature
371   if (isDumpSetName)
372     dumpEntitySetName();
373   return isOk;
374 }
375
376 void ModelHighAPI_Dumper::postpone(const EntityPtr& theEntity)
377 {
378   // keep the name
379   name(theEntity, false);
380   myPostponed.push_back(theEntity);
381 }
382
383 void ModelHighAPI_Dumper::dumpPostponed(bool theDumpFolders)
384 {
385   if (myDumpPostponedInProgress)
386     return;
387
388   myDumpPostponedInProgress = true;
389   // make a copy of postponed entities, because the list will be updated
390   // if some features are not able to be dumped
391   std::list<EntityPtr> aPostponedCopy = myPostponed;
392   myPostponed.clear();
393
394   // iterate over postponed entities and try to dump them
395   std::list<EntityPtr>::const_iterator anIt = aPostponedCopy.begin();
396   for (; anIt != aPostponedCopy.end(); ++anIt) {
397     FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(*anIt);
398     if (aFolder) {
399       if (theDumpFolders)
400         dumpFolder(aFolder);
401       else
402         myPostponed.push_back(*anIt);
403     }
404     else {
405       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
406       if (aFeature)
407         dumpFeature(aFeature, true);
408     }
409   }
410   myDumpPostponedInProgress = false;
411 }
412
413 void ModelHighAPI_Dumper::dumpSubFeatureNameAndColor(const std::string theSubFeatureGet,
414                                                      const FeaturePtr& theSubFeature)
415 {
416   name(theSubFeature, false);
417   myNames[theSubFeature] = EntityName(theSubFeatureGet, theSubFeature->name(), false);
418
419   // store results if they have user-defined names or colors
420   std::list<ResultPtr> aResultsWithNameOrColor;
421   const std::list<ResultPtr>& aResults = theSubFeature->results();
422   std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
423   for (; aResIt != aResults.end(); ++aResIt) {
424     std::string aResName = (*aResIt)->data()->name();
425     myNames[*aResIt] = EntityName(aResName, aResName, false);
426     aResultsWithNameOrColor.push_back(*aResIt);
427   }
428
429   // store just dumped entity to stack
430   myEntitiesStack.push(LastDumpedEntity(theSubFeature, true, aResultsWithNameOrColor));
431
432   dumpEntitySetName();
433 }
434
435 bool ModelHighAPI_Dumper::exportTo(const std::string& theFileName)
436 {
437   std::ofstream aFile;
438   OSD_OpenStream(aFile, theFileName.c_str(), std::ofstream::out);
439   if (!aFile.is_open())
440     return false;
441
442   // standard header (encoding + imported modules)
443   aFile << "# -*- coding: utf-8 -*-" << std::endl << std::endl;
444   for (ModulesSet::const_iterator aModIt = myModules.begin();
445        aModIt != myModules.end(); ++aModIt) {
446     aFile << "from " << *aModIt << " import *" << std::endl;
447   }
448   if (!myModules.empty())
449     aFile << std::endl;
450
451   aFile << "from salome.shaper import model" << std::endl << std::endl;
452   aFile << "model.begin()" << std::endl;
453
454   // dump collected data
455   aFile << myFullDump.str();
456   aFile << myDumpBuffer.str();
457
458   // standard footer
459   aFile << "model.end()" << std::endl;
460
461   aFile.close();
462   clear();
463
464   return true;
465 }
466
467 void ModelHighAPI_Dumper::importModule(const std::string& theModuleName)
468 {
469   myModules.insert(theModuleName);
470 }
471
472 void ModelHighAPI_Dumper::dumpEntitySetName()
473 {
474   const LastDumpedEntity& aLastDumped = myEntitiesStack.top();
475   bool isBufferEmpty = myDumpBuffer.str().empty();
476
477   // dump "setName" for the entity
478   if (aLastDumped.myUserName) {
479     EntityName& anEntityNames = myNames[aLastDumped.myEntity];
480     if (!anEntityNames.myIsDefault)
481       myDumpBuffer << anEntityNames.myCurrentName << ".setName(\""
482                    << anEntityNames.myUserName << "\")" << std::endl;
483     // don't dump "setName" for the entity twice
484     anEntityNames.myUserName.clear();
485     anEntityNames.myIsDefault = true;
486   }
487   // dump "setName" for results
488   std::list<ResultPtr>::const_iterator aResIt = aLastDumped.myResults.begin();
489   std::list<ResultPtr>::const_iterator aResEnd = aLastDumped.myResults.end();
490   for (; aResIt != aResEnd; ++aResIt) {
491     // set result name
492     EntityName& anEntityNames = myNames[*aResIt];
493     if (!anEntityNames.myIsDefault) {
494       *this << *aResIt;
495       myDumpBuffer << ".setName(\"" << anEntityNames.myUserName << "\")" << std::endl;
496       // don't dump "setName" for the entity twice
497       anEntityNames.myUserName.clear();
498       anEntityNames.myIsDefault = true;
499     }
500     // set result color
501     if (!isDefaultColor(*aResIt)) {
502       AttributeIntArrayPtr aColor = (*aResIt)->data()->intArray(ModelAPI_Result::COLOR_ID());
503       if (aColor && aColor->isInitialized()) {
504         *this << *aResIt;
505         myDumpBuffer << ".setColor(" << aColor->value(0) << ", " << aColor->value(1)
506                      << ", " << aColor->value(2) << ")" << std::endl;
507       }
508     }
509     // set result deflection
510     if (!isDefaultDeflection(*aResIt)) {
511       AttributeDoublePtr aDeflectionAttr =
512         (*aResIt)->data()->real(ModelAPI_Result::DEFLECTION_ID());
513       if(aDeflectionAttr.get() && aDeflectionAttr->isInitialized()) {
514         *this << *aResIt;
515         myDumpBuffer << ".setDeflection(" << aDeflectionAttr->value() << ")" << std::endl;
516       }
517     }
518     // set result transparency
519     if (!isDefaultTransparency(*aResIt)) {
520       AttributeDoublePtr aTransparencyAttr =
521         (*aResIt)->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
522       if(aTransparencyAttr.get() && aTransparencyAttr->isInitialized()) {
523         *this << *aResIt;
524         myDumpBuffer << ".setTransparency(" << aTransparencyAttr->value() << ")" << std::endl;
525       }
526     }
527   }
528
529   myNames[aLastDumped.myEntity].myIsDumped = true;
530   myEntitiesStack.pop();
531
532   // clean buffer if it was clear before
533   if (isBufferEmpty) {
534     myFullDump << myDumpBuffer.str();
535     myDumpBuffer.str("");
536   }
537 }
538
539 bool ModelHighAPI_Dumper::isDumped(const EntityPtr& theEntity) const
540 {
541   EntityNameMap::const_iterator aFound = myNames.find(theEntity);
542   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
543   return (aFound != myNames.end() && aFound->second.myIsDumped) ||
544          myFeaturesToSkip.find(aFeature) != myFeaturesToSkip.end();
545 }
546
547 bool ModelHighAPI_Dumper::isDumped(const AttributeRefAttrPtr& theRefAttr) const
548 {
549   FeaturePtr aFeature;
550   if (theRefAttr->isObject())
551     aFeature = ModelAPI_Feature::feature(theRefAttr->object());
552   else
553     aFeature = ModelAPI_Feature::feature(theRefAttr->attr()->owner());
554   return aFeature && isDumped(EntityPtr(aFeature));
555 }
556
557 bool ModelHighAPI_Dumper::isDumped(const AttributeRefListPtr& theRefList) const
558 {
559   std::list<ObjectPtr> aRefs = theRefList->list();
560   std::list<ObjectPtr>::iterator anIt = aRefs.begin();
561   for (; anIt != aRefs.end(); ++anIt) {
562     FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
563     if (aFeature && !isDumped(EntityPtr(aFeature)))
564       return false;
565   }
566   return true;
567 }
568
569 static bool isSketchSub(const FeaturePtr& theFeature)
570 {
571   static const std::string SKETCH("Sketch");
572   CompositeFeaturePtr anOwner = ModelAPI_Tools::compositeOwner(theFeature);
573   return anOwner && anOwner->getKind() == SKETCH;
574 }
575
576 bool ModelHighAPI_Dumper::isDefaultColor(const ResultPtr& theResult) const
577 {
578   AttributeIntArrayPtr aColor = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
579   if (!aColor || !aColor->isInitialized())
580     return true;
581
582   // check the result belongs to sketch entity, do not dump color in this way
583   ResultConstructionPtr aResConstr =
584       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theResult);
585   if (aResConstr) {
586     FeaturePtr aFeature = ModelAPI_Feature::feature(theResult->data()->owner());
587     if (isSketchSub(aFeature))
588       return true;
589   }
590
591   std::string aSection, aName, aDefault;
592   theResult->colorConfigInfo(aSection, aName, aDefault);
593
594   // dump current color
595   std::ostringstream aColorInfo;
596   aColorInfo << aColor->value(0) << "," << aColor->value(1) << "," << aColor->value(2);
597
598   return aDefault == aColorInfo.str();
599 }
600
601 bool ModelHighAPI_Dumper::isDefaultDeflection(const ResultPtr& theResult) const
602 {
603   AttributeDoublePtr aDeflectionAttr = theResult->data()->real(ModelAPI_Result::DEFLECTION_ID());
604   if(!aDeflectionAttr || !aDeflectionAttr->isInitialized()) {
605     return true;
606   }
607
608   double aCurrent = aDeflectionAttr->value();
609   double aDefault = -1;
610
611   bool isConstruction = false;
612   std::string aResultGroup = theResult->groupName();
613   if (aResultGroup == ModelAPI_ResultConstruction::group())
614     isConstruction = true;
615   else if (aResultGroup == ModelAPI_ResultBody::group()) {
616     GeomShapePtr aGeomShape = theResult->shape();
617     if (aGeomShape.get()) {
618       // if the shape could not be exploded on faces, it contains only wires, edges, and vertices
619       // correction of deviation for them should not influence to the application performance
620       GeomAPI_ShapeExplorer anExp(aGeomShape, GeomAPI_Shape::FACE);
621       isConstruction = !anExp.more();
622     }
623   }
624   if (isConstruction)
625     aDefault = Config_PropManager::real("Visualization", "construction_deflection");
626   else
627     aDefault = Config_PropManager::real("Visualization", "body_deflection");
628
629   return fabs(aCurrent - aDefault) < 1.e-12;
630 }
631
632 bool ModelHighAPI_Dumper::isDefaultTransparency(const ResultPtr& theResult) const
633 {
634   AttributeDoublePtr anAttribute = theResult->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
635   if(!anAttribute || !anAttribute->isInitialized()) {
636     return true;
637   }
638   return fabs(anAttribute->value()) < 1.e-12;
639 }
640
641 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char theChar)
642 {
643   myDumpBuffer << theChar;
644   return *this;
645 }
646
647 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char* theString)
648 {
649   myDumpBuffer << theString;
650   return *this;
651 }
652
653 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::string& theString)
654 {
655   myDumpBuffer << theString;
656   return *this;
657 }
658
659 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const bool theValue)
660 {
661   myDumpBuffer << (theValue ? "True" : "False");
662   return *this;
663 }
664
665 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const int theValue)
666 {
667   myDumpBuffer << theValue;
668   return *this;
669 }
670
671 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const double theValue)
672 {
673   myDumpBuffer << theValue;
674   return *this;
675 }
676
677 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
678 {
679   importModule("GeomAPI");
680   myDumpBuffer << "GeomAPI_Pnt(" << thePoint->x() << ", "
681                << thePoint->y() << ", " << thePoint->z() << ")";
682   return *this;
683 }
684
685 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir)
686 {
687   importModule("GeomAPI");
688   myDumpBuffer << "GeomAPI_Dir(" << theDir->x() << ", "
689                << theDir->y() << ", " << theDir->z() << ")";
690   return *this;
691 }
692
693 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
694     const std::shared_ptr<GeomDataAPI_Dir>& theDir)
695 {
696   myDumpBuffer << theDir->x() << ", " << theDir->y() << ", " << theDir->z();
697   return *this;
698 }
699
700 static void dumpArray(std::ostringstream& theOutput, int theSize,
701                       double* theValues, std::string* theTexts)
702 {
703   for (int i = 0; i < theSize; ++i) {
704     if (i > 0)
705       theOutput << ", ";
706     if (theTexts[i].empty())
707       theOutput << theValues[i];
708     else
709       theOutput << "\"" << theTexts[i] << "\"";
710   }
711 }
712
713 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
714     const std::shared_ptr<GeomDataAPI_Point>& thePoint)
715 {
716   static const int aSize = 3;
717   double aValues[aSize] = {thePoint->x(), thePoint->y(), thePoint->z()};
718   std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY(), thePoint->textZ()};
719   dumpArray(myDumpBuffer, aSize, aValues, aTexts);
720   return *this;
721 }
722
723 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
724     const std::shared_ptr<GeomDataAPI_Point2D>& thePoint)
725 {
726   static const int aSize = 2;
727   double aValues[aSize] = {thePoint->x(), thePoint->y()};
728   std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY()};
729   dumpArray(myDumpBuffer, aSize, aValues, aTexts);
730   return *this;
731 }
732
733 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
734     const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool)
735 {
736   myDumpBuffer << (theAttrBool->value() ? "True" : "False");
737   return *this;
738 }
739
740 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
741     const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt)
742 {
743   std::string aText = theAttrInt->text();
744   if (aText.empty())
745     myDumpBuffer << theAttrInt->value();
746   else
747     myDumpBuffer << "\"" << aText << "\"";
748   return *this;
749 }
750
751 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
752     const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal)
753 {
754   std::string aText = theAttrReal->text();
755   if (aText.empty())
756     myDumpBuffer << theAttrReal->value();
757   else
758     myDumpBuffer << "\"" << aText << "\"";
759   return *this;
760 }
761
762 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
763     const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr)
764 {
765   myDumpBuffer << "\"" << theAttrStr->value() << "\"";
766   return *this;
767 }
768
769 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FolderPtr& theFolder)
770 {
771   myDumpBuffer << name(theFolder);
772
773   // add dumped folder to a stack
774   if (!myNames[theFolder].myIsDumped &&
775      (myEntitiesStack.empty() || myEntitiesStack.top().myEntity != theFolder))
776     myEntitiesStack.push(LastDumpedEntity(theFolder, !myNames[theFolder].myIsDefault));
777
778   return *this;
779 }
780
781 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FeaturePtr& theEntity)
782 {
783   myDumpBuffer << name(theEntity);
784
785   if (!myNames[theEntity].myIsDumped) {
786     bool isUserDefinedName = !myNames[theEntity].myIsDefault;
787     // store results if they have user-defined names or colors
788     std::list<ResultPtr> aResultsWithNameOrColor;
789     std::list<ResultPtr> allRes;
790     ModelAPI_Tools::allResults(theEntity, allRes);
791     for(std::list<ResultPtr>::iterator aRes = allRes.begin(); aRes != allRes.end(); aRes++) {
792       if(!myNames[*aRes].myIsDefault || !isDefaultColor(*aRes) ||
793          !isDefaultDeflection(*aRes) || !isDefaultTransparency(*aRes))
794         aResultsWithNameOrColor.push_back(*aRes);
795     }
796     // store just dumped entity to stack
797     if (myEntitiesStack.empty() || myEntitiesStack.top().myEntity != theEntity)
798       myEntitiesStack.push(
799           LastDumpedEntity(theEntity, isUserDefinedName, aResultsWithNameOrColor));
800   }
801
802   // remove entity from the list of not dumped items
803   myNotDumpedEntities.erase(theEntity);
804   return *this;
805 }
806
807 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ResultPtr& theResult)
808 {
809   // iterate in the structure of sub-results to the parent
810   ResultPtr aCurRes = theResult;
811   FeaturePtr aFeature = ModelAPI_Feature::feature(theResult);
812   std::list<int> anIndices; // indexes of results in the parent result, starting from topmost
813   while(aCurRes.get()) {
814     ResultBodyPtr aParent = ModelAPI_Tools::bodyOwner(aCurRes);
815     if (aParent) {
816       anIndices.push_front(ModelAPI_Tools::bodyIndex(aCurRes));
817     } else { // index of the result in the feature
818       std::list<ResultPtr>::const_iterator aRes = aFeature->results().cbegin();
819       for(int anIndex = 0; aRes != aFeature->results().cend(); aRes++, anIndex++) {
820         if (*aRes == aCurRes) {
821           anIndices.push_front(anIndex);
822           break;
823         }
824       }
825     }
826     aCurRes = aParent;
827   }
828
829   myDumpBuffer << name(aFeature);
830   for (std::list<int>::iterator anI = anIndices.begin(); anI != anIndices.end(); anI++) {
831     if (anI == anIndices.begin()) {
832       if(*anI == 0) {
833         myDumpBuffer << ".result()";
834       }
835       else {
836         myDumpBuffer << ".results()[" << *anI << "]";
837       }
838     } else {
839       myDumpBuffer << ".subResult(" << *anI << ")";
840     }
841   }
842
843   return *this;
844 }
845
846 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ObjectPtr& theObject)
847 {
848   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
849   if(aFeature.get()) {
850     myDumpBuffer << name(aFeature);
851     return *this;
852   }
853
854   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
855   if(aResult.get()) {
856     *this << aResult;
857     return *this;
858   }
859
860   return *this;
861 }
862
863 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const AttributePtr& theAttr)
864 {
865   FeaturePtr anOwner = ModelAPI_Feature::feature(theAttr->owner());
866
867   std::string aWrapperPrefix, aWrapperSuffix;
868   // Check the attribute belongs to copied (in multi-translation or multi-rotation) feature.
869   // In this case we need to cast explicitly feature to appropriate type.
870   AttributeBooleanPtr isCopy = anOwner->boolean("Copy");
871   if (isCopy.get() && isCopy->value()) {
872     aWrapperPrefix = featureWrapper(anOwner) + "(";
873     aWrapperSuffix = ")";
874     importModule("SketchAPI");
875   }
876
877   myDumpBuffer << aWrapperPrefix << name(anOwner) << aWrapperSuffix
878                << "." << attributeGetter(anOwner, theAttr->id()) << "()";
879   return *this;
880 }
881
882 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
883     const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr)
884 {
885   if (theRefAttr->isObject())
886     *this << theRefAttr->object();
887   else
888     *this << theRefAttr->attr();
889   return *this;
890 }
891
892 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
893     const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList)
894 {
895   myDumpBuffer << "[";
896   std::list<std::pair<ObjectPtr, AttributePtr> > aList = theRefAttrList->list();
897   bool isAdded = false;
898   std::list<std::pair<ObjectPtr, AttributePtr> >::const_iterator anIt = aList.begin();
899   for (; anIt != aList.end(); ++anIt) {
900     if (isAdded)
901       myDumpBuffer << ", ";
902     else
903       isAdded = true;
904     if (anIt->first)
905       *this << anIt->first;
906     else if (anIt->second)
907       * this << anIt->second;
908   }
909   myDumpBuffer << "]";
910   return *this;
911 }
912
913 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
914     const std::shared_ptr<ModelAPI_AttributeReference>& theReference)
915 {
916   *this << theReference->value();
917   return *this;
918 }
919
920 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
921     const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList)
922 {
923   static const int aThreshold = 2;
924   // if number of elements in the list if greater than a threshold,
925   // dump it in a separate line with specific name
926   std::string aDumped = myDumpBuffer.str();
927   if (aDumped.empty() || theRefList->size() <= aThreshold) {
928     myDumpBuffer << "[";
929     std::list<ObjectPtr> aList = theRefList->list();
930     bool isAdded = false;
931     std::list<ObjectPtr>::const_iterator anIt = aList.begin();
932     for (; anIt != aList.end(); ++anIt) {
933       if (isAdded)
934         myDumpBuffer << ", ";
935       else
936         isAdded = true;
937
938       *this << *anIt;
939     }
940     myDumpBuffer << "]";
941   } else {
942     // clear buffer and store list "as is"
943     myDumpBuffer.str("");
944     *this << theRefList;
945     // save buffer and clear it again
946     std::string aDumpedList = myDumpBuffer.str();
947     myDumpBuffer.str("");
948     // obtain name of list
949     FeaturePtr anOwner = ModelAPI_Feature::feature(theRefList->owner());
950     std::string aListName = name(anOwner) + "_objects";
951     // store all previous data
952     myDumpBuffer << aListName << " = " << aDumpedList << std::endl
953                  << aDumped << aListName;
954   }
955   return *this;
956 }
957
958 static int possibleSelectionsByPoint(const GeomPointPtr& thePoint,
959                                      const ResultPtr& theResult,
960                                      const GeomShapePtr& theShape,
961                                      const FeaturePtr& theStartFeature,
962                                      const FeaturePtr& theEndFeature)
963 {
964   DocumentPtr aDoc1 = theStartFeature->document();
965   DocumentPtr aDoc2 = theEndFeature->document();
966
967   std::list<FeaturePtr> aFeatures = aDoc1->allFeatures();
968   if (aDoc1 != aDoc2) {
969     std::list<FeaturePtr> anAdditionalFeatures = aDoc2->allFeatures();
970     aFeatures.insert(aFeatures.end(), anAdditionalFeatures.begin(), anAdditionalFeatures.end());
971   }
972
973   CompositeFeaturePtr aLastCompositeFeature;
974
975   std::list<FeaturePtr>::const_iterator aFIt = aFeatures.begin();
976   while (aFIt != aFeatures.end() && *aFIt != theStartFeature) {
977     CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFIt);
978     if (aCompFeat)
979       aLastCompositeFeature = aCompFeat;
980     ++aFIt;
981   }
982
983   // collect the list of composite features, containing the last feature;
984   // these features should be excluded from searching,
985   // because the feature cannot select sub-shapes from its parent
986   std::set<FeaturePtr> aEndFeatureParents = ModelAPI_Tools::getParents(theEndFeature);
987
988   int aNbPossibleSelections = 0;
989   for (; aFIt != aFeatures.end() && *aFIt != theEndFeature; ++aFIt) {
990     bool isSkipFeature = false;
991     if (aLastCompositeFeature && aLastCompositeFeature->isSub(*aFIt))
992       isSkipFeature = true;
993     CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFIt);
994     if (aCompFeat) {
995       ResultPartPtr aPartRes =
996           std::dynamic_pointer_cast<ModelAPI_ResultPart>(aCompFeat->firstResult());
997       if (!aPartRes)
998         aLastCompositeFeature = aCompFeat;
999       if (aEndFeatureParents.find(aCompFeat) != aEndFeatureParents.end()) {
1000         // do not process the parent for the last feature,
1001         // because it cannot select objects from its parent
1002         isSkipFeature = true;
1003       }
1004     }
1005     if (isSkipFeature)
1006       continue;
1007
1008     std::list<ModelGeomAlgo_Shape::SubshapeOfResult> anApproproate;
1009     if (ModelGeomAlgo_Shape::findSubshapeByPoint(*aFIt, thePoint, theShape->shapeType(),
1010                                                  anApproproate)) {
1011       std::list<ModelGeomAlgo_Shape::SubshapeOfResult>::iterator anApIt = anApproproate.begin();
1012       for (; anApIt != anApproproate.end(); ++anApIt) {
1013         ++aNbPossibleSelections;
1014
1015         // stop if the target shape and result are found
1016         GeomShapePtr aCurShape = anApIt->mySubshape;
1017         if (!aCurShape)
1018           aCurShape = anApIt->myResult->shape();
1019
1020         if (anApIt->myResult->isSame(theResult) && aCurShape->isSame(theShape))
1021           break;
1022       }
1023     }
1024   }
1025   return aNbPossibleSelections;
1026 }
1027
1028 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1029     const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect)
1030 {
1031   myDumpBuffer << "model.selection(";
1032
1033   if(!theAttrSelect->isInitialized()) {
1034     myDumpBuffer << ")";
1035     return *this;
1036   }
1037
1038   GeomShapePtr aShape = theAttrSelect->value();
1039   if(!aShape.get()) {
1040     aShape = theAttrSelect->context()->shape();
1041   }
1042
1043   if(!aShape.get()) {
1044     myDumpBuffer << ")";
1045     return *this;
1046   }
1047
1048   // how to dump selection: construction features are dumped by name always
1049   bool isDumpByGeom = myGeometricalSelection;
1050   FeaturePtr aSelectedFeature;
1051   if (isDumpByGeom) {
1052     ResultPtr aRes = theAttrSelect->context();
1053     FeaturePtr aFeature = theAttrSelect->contextFeature();
1054     if (aRes && !aFeature)
1055       aSelectedFeature = ModelAPI_Feature::feature(aRes->data()->owner());
1056     isDumpByGeom = aSelectedFeature && aSelectedFeature->isInHistory();
1057   }
1058
1059   if (theAttrSelect->isGeometricalSelection() && aShape->shapeType() == GeomAPI_Shape::COMPOUND
1060     && theAttrSelect->context().get() && !aShape->isEqual(theAttrSelect->context()->shape())
1061     && theAttrSelect->context()->groupName() != ModelAPI_ResultPart::group()) {
1062     GeomAPI_ShapeIterator anIt(aShape);
1063     aShape = anIt.current();
1064   }
1065
1066   myDumpBuffer << "\"" << aShape->shapeTypeStr();
1067   bool aStandardDump = true;
1068   if (isDumpByGeom) {
1069     // check the selected item is a ResultPart;
1070     // in this case it is necessary to get shape with full transformation
1071     // for correct calculation of the middle point
1072     ResultPartPtr aResPart =
1073         std::dynamic_pointer_cast<ModelAPI_ResultPart>(theAttrSelect->context());
1074     if (aResPart && aShape->shapeType() == GeomAPI_Shape::COMPOUND)
1075       aShape = aResPart->shape();
1076     GeomPointPtr aMiddlePoint = aShape->middlePoint();
1077     // calculate number of features, which could be selected by the same point
1078     FeaturePtr anOwner = ModelAPI_Feature::feature(theAttrSelect->owner());
1079     int aNbPossibleSelections = possibleSelectionsByPoint(aMiddlePoint,
1080         theAttrSelect->context(), aShape, aSelectedFeature, anOwner);
1081
1082     // produce the index if the number of applicable features is greater than 1
1083     std::string anIndex;
1084     if (aNbPossibleSelections > 1) {
1085       std::ostringstream anOutput;
1086       anOutput << "_" << aNbPossibleSelections;
1087       anIndex = anOutput.str();
1088     }
1089
1090     myDumpBuffer << anIndex << "\", ("
1091                  << aMiddlePoint->x() << ", "
1092                  << aMiddlePoint->y() << ", "
1093                  << aMiddlePoint->z() << ")";
1094     aStandardDump = false;
1095   } else if (myWeakNamingSelection && aShape.get() && theAttrSelect->context().get() &&
1096        aShape != theAttrSelect->context()->shape()) { // weak naming for local selection only
1097     GeomAlgoAPI_NExplode aNExplode(theAttrSelect->context()->shape(), aShape->shapeType());
1098     int anIndex = aNExplode.index(aShape);
1099     if (anIndex != 0) { // found a week-naming index, so, export it
1100       myDumpBuffer<<"\", \""<<
1101         theAttrSelect->contextName(theAttrSelect->context())<<"\", "<<anIndex;
1102       aStandardDump = false;
1103     }
1104   }
1105   if (aStandardDump)
1106     myDumpBuffer << "\", \"" << theAttrSelect->namingName() << "\"";
1107   myDumpBuffer << ")";
1108   return *this;
1109 }
1110
1111 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1112     const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList)
1113 {
1114   static const int aThreshold = 2;
1115   // if number of elements in the list if greater than a threshold,
1116   // dump it in a separate line with specific name
1117   std::string aDumped = myDumpBuffer.str();
1118
1119   if (aDumped.empty() || theAttrSelList->size() <= aThreshold) {
1120     myDumpBuffer << "[";
1121
1122     GeomShapePtr aShape;
1123     std::string aShapeTypeStr;
1124
1125     bool isAdded = false;
1126
1127     for(int anIndex = 0; anIndex < theAttrSelList->size(); ++anIndex) {
1128       AttributeSelectionPtr anAttribute = theAttrSelList->value(anIndex);
1129       aShape = anAttribute->value();
1130       if(!aShape.get()) {
1131         ResultPtr aContext = anAttribute->context();
1132         if (aContext.get())
1133           aShape = aContext->shape();
1134       }
1135
1136       if(!aShape.get()) {
1137         continue;
1138       }
1139
1140       if(isAdded) {
1141         myDumpBuffer << ", ";
1142       } else {
1143         isAdded = true;
1144       }
1145       *this << anAttribute;
1146     }
1147
1148     myDumpBuffer << "]";
1149   } else {
1150     // clear buffer and store list "as is"
1151     myDumpBuffer.str("");
1152     *this << theAttrSelList;
1153     // save buffer and clear it again
1154     std::string aDumpedList = myDumpBuffer.str();
1155     myDumpBuffer.str("");
1156     // obtain name of list (the feature may contain several selection lists)
1157     FeaturePtr anOwner = ModelAPI_Feature::feature(theAttrSelList->owner());
1158     std::string aListName = name(anOwner) + "_objects";
1159     std::list<AttributePtr> aSelLists =
1160         anOwner->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
1161     if (aSelLists.size() > 1) {
1162       int anIndex = 1;
1163       for (std::list<AttributePtr>::iterator aSIt = aSelLists.begin();
1164            aSIt != aSelLists.end(); ++aSIt, ++anIndex)
1165         if ((*aSIt).get() == theAttrSelList.get())
1166           break;
1167       std::ostringstream aSStream;
1168       aSStream << aListName << "_" << anIndex;
1169       aListName = aSStream.str();
1170     }
1171     // store all previous data
1172     myDumpBuffer << aListName << " = " << aDumpedList << std::endl
1173                  << aDumped << aListName;
1174   }
1175   return *this;
1176 }
1177
1178 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1179   const std::shared_ptr<ModelAPI_AttributeStringArray>& theArray)
1180 {
1181   myDumpBuffer<<"[";
1182   for(int anIndex = 0; anIndex < theArray->size(); ++anIndex) {
1183     if (anIndex != 0)
1184       myDumpBuffer<<", ";
1185
1186     myDumpBuffer<<"\""<<theArray->value(anIndex)<<"\"";
1187   }
1188
1189   myDumpBuffer<<"]";
1190   return *this;
1191 }
1192
1193 /// Dump std::endl
1194 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
1195                                 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&))
1196 {
1197   theDumper.myDumpBuffer << theEndl;
1198
1199   if (!theDumper.myEntitiesStack.empty()) {
1200     bool isCopy;
1201     // all copies have been stored into stack, pop them all
1202     do {
1203       isCopy = false;
1204       // Name for composite feature is dumped when all sub-entities are dumped
1205       // (see method ModelHighAPI_Dumper::processSubs).
1206       const ModelHighAPI_Dumper::LastDumpedEntity& aLastDumped = theDumper.myEntitiesStack.top();
1207       CompositeFeaturePtr aComposite =
1208           std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aLastDumped.myEntity);
1209       if (!aComposite) {
1210         theDumper.dumpEntitySetName();
1211         FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aLastDumped.myEntity);
1212         if (aFeature) {
1213           AttributeBooleanPtr aCopyAttr = aFeature->boolean("Copy");
1214           isCopy = aCopyAttr.get() && aCopyAttr->value();
1215         }
1216       }
1217     } while (isCopy && !theDumper.myEntitiesStack.empty());
1218   }
1219
1220   // store all not-dumped entities first
1221   std::set<EntityPtr> aNotDumped = theDumper.myNotDumpedEntities;
1222   std::string aBufCopy = theDumper.myDumpBuffer.str();
1223   theDumper.clear(true);
1224   std::set<EntityPtr>::const_iterator anIt = aNotDumped.begin();
1225   for (; anIt != aNotDumped.end(); ++anIt) {
1226     // if the feature is composite, dump it with all subs
1227     CompositeFeaturePtr aCompFeat =
1228         std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anIt);
1229     if (aCompFeat)
1230       theDumper.process(aCompFeat, true);
1231     else {
1232       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
1233       theDumper.dumpFeature(aFeature, true);
1234       // dump the Projection feature which produces this "Copy" entity
1235       AttributeBooleanPtr aCopyAttr = aFeature->boolean("Copy");
1236       if (aCopyAttr.get() && aCopyAttr->value())
1237       {
1238         const std::set<AttributePtr>& aRefs = aFeature->data()->refsToMe();
1239         std::set<AttributePtr>::iterator aRefIt = aRefs.begin();
1240         for (; aRefIt != aRefs.end(); ++aRefIt)
1241           if ((*aRefIt)->id() == "ProjectedFeature")
1242           { // process projection only
1243             FeaturePtr anOwner = ModelAPI_Feature::feature((*aRefIt)->owner());
1244             if (anOwner && !theDumper.isDumped(EntityPtr(anOwner)))
1245               theDumper.dumpFeature(anOwner, true);
1246           }
1247       }
1248     }
1249   }
1250
1251   // avoid multiple empty lines
1252   size_t anInd = std::string::npos;
1253   while ((anInd = aBufCopy.find("\n\n\n")) != std::string::npos)
1254     aBufCopy.erase(anInd, 1);
1255   // then store currently dumped string
1256   theDumper.myFullDump << aBufCopy;
1257
1258   // now, store all postponed features
1259   theDumper.dumpPostponed();
1260
1261   return theDumper;
1262 }