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