Salome HOME
c904de30e93d90e594f4b65b196c0233ac1e4f7d
[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 <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 (ModulesMap::const_iterator aModIt = myModules.begin();
444        aModIt != myModules.end(); ++aModIt) {
445     aFile << "from " << aModIt->first << " import ";
446     if (aModIt->second.empty() ||
447         aModIt->second.find(std::string()) != aModIt->second.end())
448       aFile << "*"; // import whole module
449     else {
450       // import specific features
451       std::set<std::string>::const_iterator anObjIt = aModIt->second.begin();
452       aFile << *anObjIt;
453       for (++anObjIt; anObjIt != aModIt->second.end(); ++anObjIt)
454         aFile << ", " << *anObjIt;
455     }
456     aFile << std::endl;
457   }
458   if (!myModules.empty())
459     aFile << std::endl;
460
461   aFile << "from salome.shaper import model" << std::endl << std::endl;
462   aFile << "model.begin()" << std::endl;
463
464   // dump collected data
465   aFile << myFullDump.str();
466   aFile << myDumpBuffer.str();
467
468   // standard footer
469   aFile << "model.end()" << std::endl;
470
471   aFile.close();
472   clear();
473
474   return true;
475 }
476
477 void ModelHighAPI_Dumper::importModule(const std::string& theModuleName,
478                                        const std::string& theObject)
479 {
480   myModules[theModuleName].insert(theObject);
481 }
482
483 void ModelHighAPI_Dumper::dumpEntitySetName()
484 {
485   const LastDumpedEntity& aLastDumped = myEntitiesStack.top();
486   bool isBufferEmpty = myDumpBuffer.str().empty();
487
488   // dump "setName" for the entity
489   if (aLastDumped.myUserName) {
490     EntityName& anEntityNames = myNames[aLastDumped.myEntity];
491     if (!anEntityNames.myIsDefault)
492       myDumpBuffer << anEntityNames.myCurrentName << ".setName(\""
493                    << anEntityNames.myUserName << "\")" << std::endl;
494     // don't dump "setName" for the entity twice
495     anEntityNames.myUserName.clear();
496     anEntityNames.myIsDefault = true;
497   }
498   // dump "setName" for results
499   std::list<ResultPtr>::const_iterator aResIt = aLastDumped.myResults.begin();
500   std::list<ResultPtr>::const_iterator aResEnd = aLastDumped.myResults.end();
501   for (; aResIt != aResEnd; ++aResIt) {
502     // set result name
503     EntityName& anEntityNames = myNames[*aResIt];
504     if (!anEntityNames.myIsDefault) {
505       *this << *aResIt;
506       myDumpBuffer << ".setName(\"" << anEntityNames.myUserName << "\")" << std::endl;
507       // don't dump "setName" for the entity twice
508       anEntityNames.myUserName.clear();
509       anEntityNames.myIsDefault = true;
510     }
511     // set result color
512     if (!isDefaultColor(*aResIt)) {
513       AttributeIntArrayPtr aColor = (*aResIt)->data()->intArray(ModelAPI_Result::COLOR_ID());
514       if (aColor && aColor->isInitialized()) {
515         *this << *aResIt;
516         myDumpBuffer << ".setColor(" << aColor->value(0) << ", " << aColor->value(1)
517                      << ", " << aColor->value(2) << ")" << std::endl;
518       }
519     }
520     // set result deflection
521     if (!isDefaultDeflection(*aResIt)) {
522       AttributeDoublePtr aDeflectionAttr =
523         (*aResIt)->data()->real(ModelAPI_Result::DEFLECTION_ID());
524       if(aDeflectionAttr.get() && aDeflectionAttr->isInitialized()) {
525         *this << *aResIt;
526         myDumpBuffer << ".setDeflection(" << aDeflectionAttr->value() << ")" << std::endl;
527       }
528     }
529     // set result transparency
530     if (!isDefaultTransparency(*aResIt)) {
531       AttributeDoublePtr aTransparencyAttr =
532         (*aResIt)->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
533       if(aTransparencyAttr.get() && aTransparencyAttr->isInitialized()) {
534         *this << *aResIt;
535         myDumpBuffer << ".setTransparency(" << aTransparencyAttr->value() << ")" << std::endl;
536       }
537     }
538   }
539
540   myNames[aLastDumped.myEntity].myIsDumped = true;
541   myEntitiesStack.pop();
542
543   // clean buffer if it was clear before
544   if (isBufferEmpty) {
545     myFullDump << myDumpBuffer.str();
546     myDumpBuffer.str("");
547   }
548 }
549
550 bool ModelHighAPI_Dumper::isDumped(const EntityPtr& theEntity) const
551 {
552   EntityNameMap::const_iterator aFound = myNames.find(theEntity);
553   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
554   return (aFound != myNames.end() && aFound->second.myIsDumped) ||
555          myFeaturesToSkip.find(aFeature) != myFeaturesToSkip.end();
556 }
557
558 bool ModelHighAPI_Dumper::isDumped(const AttributeRefAttrPtr& theRefAttr) const
559 {
560   FeaturePtr aFeature;
561   if (theRefAttr->isObject())
562     aFeature = ModelAPI_Feature::feature(theRefAttr->object());
563   else
564     aFeature = ModelAPI_Feature::feature(theRefAttr->attr()->owner());
565   return aFeature && isDumped(EntityPtr(aFeature));
566 }
567
568 bool ModelHighAPI_Dumper::isDumped(const AttributeRefListPtr& theRefList) const
569 {
570   std::list<ObjectPtr> aRefs = theRefList->list();
571   std::list<ObjectPtr>::iterator anIt = aRefs.begin();
572   for (; anIt != aRefs.end(); ++anIt) {
573     FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
574     if (aFeature && !isDumped(EntityPtr(aFeature)))
575       return false;
576   }
577   return true;
578 }
579
580 static bool isSketchSub(const FeaturePtr& theFeature)
581 {
582   static const std::string SKETCH("Sketch");
583   CompositeFeaturePtr anOwner = ModelAPI_Tools::compositeOwner(theFeature);
584   return anOwner && anOwner->getKind() == SKETCH;
585 }
586
587 bool ModelHighAPI_Dumper::isDefaultColor(const ResultPtr& theResult) const
588 {
589   AttributeIntArrayPtr aColor = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
590   if (!aColor || !aColor->isInitialized())
591     return true;
592
593   // check the result belongs to sketch entity, do not dump color in this way
594   ResultConstructionPtr aResConstr =
595       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theResult);
596   if (aResConstr) {
597     FeaturePtr aFeature = ModelAPI_Feature::feature(theResult->data()->owner());
598     if (isSketchSub(aFeature))
599       return true;
600   }
601
602   std::string aSection, aName, aDefault;
603   theResult->colorConfigInfo(aSection, aName, aDefault);
604
605   // dump current color
606   std::ostringstream aColorInfo;
607   aColorInfo << aColor->value(0) << "," << aColor->value(1) << "," << aColor->value(2);
608
609   return aDefault == aColorInfo.str();
610 }
611
612 bool ModelHighAPI_Dumper::isDefaultDeflection(const ResultPtr& theResult) const
613 {
614   AttributeDoublePtr aDeflectionAttr = theResult->data()->real(ModelAPI_Result::DEFLECTION_ID());
615   if(!aDeflectionAttr || !aDeflectionAttr->isInitialized()) {
616     return true;
617   }
618
619   double aCurrent = aDeflectionAttr->value();
620   double aDefault = -1;
621
622   bool isConstruction = false;
623   std::string aResultGroup = theResult->groupName();
624   if (aResultGroup == ModelAPI_ResultConstruction::group())
625     isConstruction = true;
626   else if (aResultGroup == ModelAPI_ResultBody::group()) {
627     GeomShapePtr aGeomShape = theResult->shape();
628     if (aGeomShape.get()) {
629       // if the shape could not be exploded on faces, it contains only wires, edges, and vertices
630       // correction of deviation for them should not influence to the application performance
631       GeomAPI_ShapeExplorer anExp(aGeomShape, GeomAPI_Shape::FACE);
632       isConstruction = !anExp.more();
633     }
634   }
635   if (isConstruction)
636     aDefault = Config_PropManager::real("Visualization", "construction_deflection");
637   else
638     aDefault = Config_PropManager::real("Visualization", "body_deflection");
639
640   return fabs(aCurrent - aDefault) < 1.e-12;
641 }
642
643 bool ModelHighAPI_Dumper::isDefaultTransparency(const ResultPtr& theResult) const
644 {
645   AttributeDoublePtr anAttribute = theResult->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
646   if(!anAttribute || !anAttribute->isInitialized()) {
647     return true;
648   }
649   return fabs(anAttribute->value()) < 1.e-12;
650 }
651
652 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char theChar)
653 {
654   myDumpBuffer << theChar;
655   return *this;
656 }
657
658 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char* theString)
659 {
660   myDumpBuffer << theString;
661   return *this;
662 }
663
664 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::string& theString)
665 {
666   myDumpBuffer << theString;
667   return *this;
668 }
669
670 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const bool theValue)
671 {
672   myDumpBuffer << (theValue ? "True" : "False");
673   return *this;
674 }
675
676 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const int theValue)
677 {
678   myDumpBuffer << theValue;
679   return *this;
680 }
681
682 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const double theValue)
683 {
684   myDumpBuffer << theValue;
685   return *this;
686 }
687
688 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
689 {
690   importModule("GeomAPI", "GeomAPI_Pnt");
691   myDumpBuffer << "GeomAPI_Pnt(" << thePoint->x() << ", "
692                << thePoint->y() << ", " << thePoint->z() << ")";
693   return *this;
694 }
695
696 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir)
697 {
698   importModule("GeomAPI", "GeomAPI_Dir");
699   myDumpBuffer << "GeomAPI_Dir(" << theDir->x() << ", "
700                << theDir->y() << ", " << theDir->z() << ")";
701   return *this;
702 }
703
704 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
705     const std::shared_ptr<GeomDataAPI_Dir>& theDir)
706 {
707   myDumpBuffer << theDir->x() << ", " << theDir->y() << ", " << theDir->z();
708   return *this;
709 }
710
711 static void dumpArray(std::ostringstream& theOutput, int theSize,
712                       double* theValues, std::string* theTexts)
713 {
714   for (int i = 0; i < theSize; ++i) {
715     if (i > 0)
716       theOutput << ", ";
717     if (theTexts[i].empty())
718       theOutput << theValues[i];
719     else
720       theOutput << "\"" << theTexts[i] << "\"";
721   }
722 }
723
724 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
725     const std::shared_ptr<GeomDataAPI_Point>& thePoint)
726 {
727   static const int aSize = 3;
728   double aValues[aSize] = {thePoint->x(), thePoint->y(), thePoint->z()};
729   std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY(), thePoint->textZ()};
730   dumpArray(myDumpBuffer, aSize, aValues, aTexts);
731   return *this;
732 }
733
734 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
735     const std::shared_ptr<GeomDataAPI_Point2D>& thePoint)
736 {
737   static const int aSize = 2;
738   double aValues[aSize] = {thePoint->x(), thePoint->y()};
739   std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY()};
740   dumpArray(myDumpBuffer, aSize, aValues, aTexts);
741   return *this;
742 }
743
744 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
745     const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool)
746 {
747   myDumpBuffer << (theAttrBool->value() ? "True" : "False");
748   return *this;
749 }
750
751 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
752     const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt)
753 {
754   std::string aText = theAttrInt->text();
755   if (aText.empty())
756     myDumpBuffer << theAttrInt->value();
757   else
758     myDumpBuffer << "\"" << aText << "\"";
759   return *this;
760 }
761
762 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
763     const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal)
764 {
765   std::string aText = theAttrReal->text();
766   if (aText.empty())
767     myDumpBuffer << theAttrReal->value();
768   else
769     myDumpBuffer << "\"" << aText << "\"";
770   return *this;
771 }
772
773 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
774     const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr)
775 {
776   myDumpBuffer << "\"" << theAttrStr->value() << "\"";
777   return *this;
778 }
779
780 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FolderPtr& theFolder)
781 {
782   myDumpBuffer << name(theFolder);
783
784   // add dumped folder to a stack
785   if (!myNames[theFolder].myIsDumped &&
786      (myEntitiesStack.empty() || myEntitiesStack.top().myEntity != theFolder))
787     myEntitiesStack.push(LastDumpedEntity(theFolder, !myNames[theFolder].myIsDefault));
788
789   return *this;
790 }
791
792 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FeaturePtr& theEntity)
793 {
794   myDumpBuffer << name(theEntity);
795
796   if (!myNames[theEntity].myIsDumped) {
797     bool isUserDefinedName = !myNames[theEntity].myIsDefault;
798     // store results if they have user-defined names or colors
799     std::list<ResultPtr> aResultsWithNameOrColor;
800     std::list<ResultPtr> allRes;
801     ModelAPI_Tools::allResults(theEntity, allRes);
802     for(std::list<ResultPtr>::iterator aRes = allRes.begin(); aRes != allRes.end(); aRes++) {
803       if(!myNames[*aRes].myIsDefault || !isDefaultColor(*aRes) ||
804          !isDefaultDeflection(*aRes) || !isDefaultTransparency(*aRes))
805         aResultsWithNameOrColor.push_back(*aRes);
806     }
807     // store just dumped entity to stack
808     if (myEntitiesStack.empty() || myEntitiesStack.top().myEntity != theEntity)
809       myEntitiesStack.push(
810           LastDumpedEntity(theEntity, isUserDefinedName, aResultsWithNameOrColor));
811   }
812
813   // remove entity from the list of not dumped items
814   myNotDumpedEntities.erase(theEntity);
815   return *this;
816 }
817
818 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ResultPtr& theResult)
819 {
820   // iterate in the structure of sub-results to the parent
821   ResultPtr aCurRes = theResult;
822   FeaturePtr aFeature = ModelAPI_Feature::feature(theResult);
823   std::list<int> anIndices; // indexes of results in the parent result, starting from topmost
824   while(aCurRes.get()) {
825     ResultBodyPtr aParent = ModelAPI_Tools::bodyOwner(aCurRes);
826     if (aParent) {
827       anIndices.push_front(ModelAPI_Tools::bodyIndex(aCurRes));
828     } else { // index of the result in the feature
829       std::list<ResultPtr>::const_iterator aRes = aFeature->results().cbegin();
830       for(int anIndex = 0; aRes != aFeature->results().cend(); aRes++, anIndex++) {
831         if (*aRes == aCurRes) {
832           anIndices.push_front(anIndex);
833           break;
834         }
835       }
836     }
837     aCurRes = aParent;
838   }
839
840   myDumpBuffer << name(aFeature);
841   for (std::list<int>::iterator anI = anIndices.begin(); anI != anIndices.end(); anI++) {
842     if (anI == anIndices.begin()) {
843       if(*anI == 0) {
844         myDumpBuffer << ".result()";
845       }
846       else {
847         myDumpBuffer << ".results()[" << *anI << "]";
848       }
849     } else {
850       myDumpBuffer << ".subResult(" << *anI << ")";
851     }
852   }
853
854   return *this;
855 }
856
857 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ObjectPtr& theObject)
858 {
859   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
860   if(aFeature.get()) {
861     myDumpBuffer << name(aFeature);
862     return *this;
863   }
864
865   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
866   if(aResult.get()) {
867     *this << aResult;
868     return *this;
869   }
870
871   return *this;
872 }
873
874 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const AttributePtr& theAttr)
875 {
876   FeaturePtr anOwner = ModelAPI_Feature::feature(theAttr->owner());
877
878   std::string aWrapperPrefix, aWrapperSuffix;
879   // Check the attribute belongs to copied (in multi-translation or multi-rotation) feature.
880   // In this case we need to cast explicitly feature to appropriate type.
881   AttributeBooleanPtr isCopy = anOwner->boolean("Copy");
882   if (isCopy.get() && isCopy->value()) {
883     aWrapperPrefix = featureWrapper(anOwner) + "(";
884     aWrapperSuffix = ")";
885     importModule("SketchAPI");
886   }
887
888   myDumpBuffer << aWrapperPrefix << name(anOwner) << aWrapperSuffix
889                << "." << attributeGetter(anOwner, theAttr->id()) << "()";
890   return *this;
891 }
892
893 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
894     const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr)
895 {
896   if (theRefAttr->isObject())
897     *this << theRefAttr->object();
898   else
899     *this << theRefAttr->attr();
900   return *this;
901 }
902
903 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
904     const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList)
905 {
906   myDumpBuffer << "[";
907   std::list<std::pair<ObjectPtr, AttributePtr> > aList = theRefAttrList->list();
908   bool isAdded = false;
909   std::list<std::pair<ObjectPtr, AttributePtr> >::const_iterator anIt = aList.begin();
910   for (; anIt != aList.end(); ++anIt) {
911     if (isAdded)
912       myDumpBuffer << ", ";
913     else
914       isAdded = true;
915     if (anIt->first)
916       *this << anIt->first;
917     else if (anIt->second)
918       * this << anIt->second;
919   }
920   myDumpBuffer << "]";
921   return *this;
922 }
923
924 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
925     const std::shared_ptr<ModelAPI_AttributeReference>& theReference)
926 {
927   *this << theReference->value();
928   return *this;
929 }
930
931 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
932     const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList)
933 {
934   static const int aThreshold = 2;
935   // if number of elements in the list if greater than a threshold,
936   // dump it in a separate line with specific name
937   std::string aDumped = myDumpBuffer.str();
938   if (aDumped.empty() || theRefList->size() <= aThreshold) {
939     myDumpBuffer << "[";
940     std::list<ObjectPtr> aList = theRefList->list();
941     bool isAdded = false;
942     std::list<ObjectPtr>::const_iterator anIt = aList.begin();
943     for (; anIt != aList.end(); ++anIt) {
944       if (isAdded)
945         myDumpBuffer << ", ";
946       else
947         isAdded = true;
948
949       *this << *anIt;
950     }
951     myDumpBuffer << "]";
952   } else {
953     // clear buffer and store list "as is"
954     myDumpBuffer.str("");
955     *this << theRefList;
956     // save buffer and clear it again
957     std::string aDumpedList = myDumpBuffer.str();
958     myDumpBuffer.str("");
959     // obtain name of list
960     FeaturePtr anOwner = ModelAPI_Feature::feature(theRefList->owner());
961     std::string aListName = name(anOwner) + "_objects";
962     // store all previous data
963     myDumpBuffer << aListName << " = " << aDumpedList << std::endl
964                  << aDumped << aListName;
965   }
966   return *this;
967 }
968
969 static int possibleSelectionsByPoint(const GeomPointPtr& thePoint,
970                                      const ResultPtr& theResult,
971                                      const GeomShapePtr& theShape,
972                                      const FeaturePtr& theStartFeature,
973                                      const FeaturePtr& theEndFeature)
974 {
975   DocumentPtr aDoc1 = theStartFeature->document();
976   DocumentPtr aDoc2 = theEndFeature->document();
977
978   std::list<FeaturePtr> aFeatures = aDoc1->allFeatures();
979   if (aDoc1 != aDoc2) {
980     std::list<FeaturePtr> anAdditionalFeatures = aDoc2->allFeatures();
981     aFeatures.insert(aFeatures.end(), anAdditionalFeatures.begin(), anAdditionalFeatures.end());
982   }
983
984   CompositeFeaturePtr aLastCompositeFeature;
985
986   std::list<FeaturePtr>::const_iterator aFIt = aFeatures.begin();
987   while (aFIt != aFeatures.end() && *aFIt != theStartFeature) {
988     CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFIt);
989     if (aCompFeat)
990       aLastCompositeFeature = aCompFeat;
991     ++aFIt;
992   }
993
994   // collect the list of composite features, containing the last feature;
995   // these features should be excluded from searching,
996   // because the feature cannot select sub-shapes from its parent
997   std::set<FeaturePtr> aEndFeatureParents = ModelAPI_Tools::getParents(theEndFeature);
998
999   int aNbPossibleSelections = 0;
1000   for (; aFIt != aFeatures.end() && *aFIt != theEndFeature; ++aFIt) {
1001     bool isSkipFeature = false;
1002     if (aLastCompositeFeature && aLastCompositeFeature->isSub(*aFIt))
1003       isSkipFeature = true;
1004     CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFIt);
1005     if (aCompFeat) {
1006       ResultPartPtr aPartRes =
1007           std::dynamic_pointer_cast<ModelAPI_ResultPart>(aCompFeat->firstResult());
1008       if (!aPartRes)
1009         aLastCompositeFeature = aCompFeat;
1010       if (aEndFeatureParents.find(aCompFeat) != aEndFeatureParents.end()) {
1011         // do not process the parent for the last feature,
1012         // because it cannot select objects from its parent
1013         isSkipFeature = true;
1014       }
1015     }
1016     if (isSkipFeature)
1017       continue;
1018
1019     std::list<ModelGeomAlgo_Shape::SubshapeOfResult> anApproproate;
1020     if (ModelGeomAlgo_Shape::findSubshapeByPoint(*aFIt, thePoint, theShape->shapeType(),
1021                                                  anApproproate)) {
1022       std::list<ModelGeomAlgo_Shape::SubshapeOfResult>::iterator anApIt = anApproproate.begin();
1023       for (; anApIt != anApproproate.end(); ++anApIt) {
1024         ++aNbPossibleSelections;
1025
1026         // stop if the target shape and result are found
1027         GeomShapePtr aCurShape = anApIt->mySubshape;
1028         if (!aCurShape)
1029           aCurShape = anApIt->myResult->shape();
1030
1031         if (anApIt->myResult->isSame(theResult) && aCurShape->isSame(theShape))
1032           break;
1033       }
1034     }
1035   }
1036   return aNbPossibleSelections;
1037 }
1038
1039 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1040     const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect)
1041 {
1042   myDumpBuffer << "model.selection(";
1043
1044   if(!theAttrSelect->isInitialized()) {
1045     myDumpBuffer << ")";
1046     return *this;
1047   }
1048
1049   GeomShapePtr aShape = theAttrSelect->value();
1050   if(!aShape.get()) {
1051     aShape = theAttrSelect->context()->shape();
1052   }
1053
1054   if(!aShape.get()) {
1055     myDumpBuffer << ")";
1056     return *this;
1057   }
1058
1059   // how to dump selection: construction features are dumped by name always
1060   bool isDumpByGeom = myGeometricalSelection;
1061   FeaturePtr aSelectedFeature;
1062   if (isDumpByGeom) {
1063     ResultPtr aRes = theAttrSelect->context();
1064     FeaturePtr aFeature = theAttrSelect->contextFeature();
1065     if (aRes && !aFeature)
1066       aSelectedFeature = ModelAPI_Feature::feature(aRes->data()->owner());
1067     isDumpByGeom = aSelectedFeature && aSelectedFeature->isInHistory();
1068   }
1069
1070   myDumpBuffer << "\"" << aShape->shapeTypeStr();
1071   bool aStandardDump = true;
1072   if (isDumpByGeom) {
1073     // check the selected item is a ResultPart;
1074     // in this case it is necessary to get shape with full transformation
1075     // for correct calculation of the middle point
1076     ResultPartPtr aResPart =
1077         std::dynamic_pointer_cast<ModelAPI_ResultPart>(theAttrSelect->context());
1078     if (aResPart && aShape->shapeType() == GeomAPI_Shape::COMPOUND)
1079       aShape = aResPart->shape();
1080     GeomPointPtr aMiddlePoint = aShape->middlePoint();
1081     // calculate number of features, which could be selected by the same point
1082     FeaturePtr anOwner = ModelAPI_Feature::feature(theAttrSelect->owner());
1083     int aNbPossibleSelections = possibleSelectionsByPoint(aMiddlePoint,
1084         theAttrSelect->context(), aShape, aSelectedFeature, anOwner);
1085
1086     // produce the index if the number of applicable features is greater than 1
1087     std::string anIndex;
1088     if (aNbPossibleSelections > 1) {
1089       std::ostringstream anOutput;
1090       anOutput << "_" << aNbPossibleSelections;
1091       anIndex = anOutput.str();
1092     }
1093
1094     myDumpBuffer << anIndex << "\", ("
1095                  << aMiddlePoint->x() << ", "
1096                  << aMiddlePoint->y() << ", "
1097                  << aMiddlePoint->z() << ")";
1098     aStandardDump = false;
1099   } else if (myWeakNamingSelection && aShape.get() && theAttrSelect->context().get() &&
1100        aShape != theAttrSelect->context()->shape()) { // weak naming for local selection only
1101     GeomAlgoAPI_NExplode aNExplode(theAttrSelect->context()->shape(), aShape->shapeType());
1102     int anIndex = aNExplode.index(aShape);
1103     if (anIndex != 0) { // found a week-naming index, so, export it
1104       myDumpBuffer<<"\", \""<<
1105         theAttrSelect->contextName(theAttrSelect->context())<<"\", "<<anIndex;
1106       aStandardDump = false;
1107     }
1108   }
1109   if (aStandardDump)
1110     myDumpBuffer << "\", \"" << theAttrSelect->namingName() << "\"";
1111   myDumpBuffer << ")";
1112   return *this;
1113 }
1114
1115 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1116     const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList)
1117 {
1118   static const int aThreshold = 2;
1119   // if number of elements in the list if greater than a threshold,
1120   // dump it in a separate line with specific name
1121   std::string aDumped = myDumpBuffer.str();
1122
1123   if (aDumped.empty() || theAttrSelList->size() <= aThreshold) {
1124     myDumpBuffer << "[";
1125
1126     GeomShapePtr aShape;
1127     std::string aShapeTypeStr;
1128
1129     bool isAdded = false;
1130
1131     for(int anIndex = 0; anIndex < theAttrSelList->size(); ++anIndex) {
1132       AttributeSelectionPtr anAttribute = theAttrSelList->value(anIndex);
1133       aShape = anAttribute->value();
1134       if(!aShape.get()) {
1135         ResultPtr aContext = anAttribute->context();
1136         if (aContext.get())
1137           aShape = aContext->shape();
1138       }
1139
1140       if(!aShape.get()) {
1141         continue;
1142       }
1143
1144       if(isAdded) {
1145         myDumpBuffer << ", ";
1146       } else {
1147         isAdded = true;
1148       }
1149       *this << anAttribute;
1150     }
1151
1152     myDumpBuffer << "]";
1153   } else {
1154     // clear buffer and store list "as is"
1155     myDumpBuffer.str("");
1156     *this << theAttrSelList;
1157     // save buffer and clear it again
1158     std::string aDumpedList = myDumpBuffer.str();
1159     myDumpBuffer.str("");
1160     // obtain name of list (the feature may contain several selection lists)
1161     FeaturePtr anOwner = ModelAPI_Feature::feature(theAttrSelList->owner());
1162     std::string aListName = name(anOwner) + "_objects";
1163     std::list<AttributePtr> aSelLists =
1164         anOwner->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
1165     if (aSelLists.size() > 1) {
1166       int anIndex = 1;
1167       for (std::list<AttributePtr>::iterator aSIt = aSelLists.begin();
1168            aSIt != aSelLists.end(); ++aSIt, ++anIndex)
1169         if ((*aSIt).get() == theAttrSelList.get())
1170           break;
1171       std::ostringstream aSStream;
1172       aSStream << aListName << "_" << anIndex;
1173       aListName = aSStream.str();
1174     }
1175     // store all previous data
1176     myDumpBuffer << aListName << " = " << aDumpedList << std::endl
1177                  << aDumped << aListName;
1178   }
1179   return *this;
1180 }
1181
1182 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1183   const std::shared_ptr<ModelAPI_AttributeStringArray>& theArray)
1184 {
1185   myDumpBuffer<<"[";
1186   for(int anIndex = 0; anIndex < theArray->size(); ++anIndex) {
1187     if (anIndex != 0)
1188       myDumpBuffer<<", ";
1189
1190     myDumpBuffer<<"\""<<theArray->value(anIndex)<<"\"";
1191   }
1192
1193   myDumpBuffer<<"]";
1194   return *this;
1195 }
1196
1197 /// Dump std::endl
1198 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
1199                                 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&))
1200 {
1201   theDumper.myDumpBuffer << theEndl;
1202
1203   if (!theDumper.myEntitiesStack.empty()) {
1204     bool isCopy;
1205     // all copies have been stored into stack, pop them all
1206     do {
1207       isCopy = false;
1208       // Name for composite feature is dumped when all sub-entities are dumped
1209       // (see method ModelHighAPI_Dumper::processSubs).
1210       const ModelHighAPI_Dumper::LastDumpedEntity& aLastDumped = theDumper.myEntitiesStack.top();
1211       CompositeFeaturePtr aComposite =
1212           std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aLastDumped.myEntity);
1213       if (!aComposite) {
1214         theDumper.dumpEntitySetName();
1215         FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aLastDumped.myEntity);
1216         if (aFeature) {
1217           AttributeBooleanPtr aCopyAttr = aFeature->boolean("Copy");
1218           isCopy = aCopyAttr.get() && aCopyAttr->value();
1219         }
1220       }
1221     } while (isCopy && !theDumper.myEntitiesStack.empty());
1222   }
1223
1224   // store all not-dumped entities first
1225   std::set<EntityPtr> aNotDumped = theDumper.myNotDumpedEntities;
1226   std::string aBufCopy = theDumper.myDumpBuffer.str();
1227   theDumper.clear(true);
1228   std::set<EntityPtr>::const_iterator anIt = aNotDumped.begin();
1229   for (; anIt != aNotDumped.end(); ++anIt) {
1230     // if the feature is composite, dump it with all subs
1231     CompositeFeaturePtr aCompFeat =
1232         std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anIt);
1233     if (aCompFeat)
1234       theDumper.process(aCompFeat, true);
1235     else {
1236       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
1237       theDumper.dumpFeature(aFeature, true);
1238       // dump the Projection feature which produces this "Copy" entity
1239       AttributeBooleanPtr aCopyAttr = aFeature->boolean("Copy");
1240       if (aCopyAttr.get() && aCopyAttr->value())
1241       {
1242         const std::set<AttributePtr>& aRefs = aFeature->data()->refsToMe();
1243         std::set<AttributePtr>::iterator aRefIt = aRefs.begin();
1244         for (; aRefIt != aRefs.end(); ++aRefIt)
1245           if ((*aRefIt)->id() == "ProjectedFeature")
1246           { // process projection only
1247             FeaturePtr anOwner = ModelAPI_Feature::feature((*aRefIt)->owner());
1248             if (anOwner && !theDumper.isDumped(EntityPtr(anOwner)))
1249               theDumper.dumpFeature(anOwner, true);
1250           }
1251       }
1252     }
1253   }
1254
1255   // avoid multiple empty lines
1256   size_t anInd = std::string::npos;
1257   while ((anInd = aBufCopy.find("\n\n\n")) != std::string::npos)
1258     aBufCopy.erase(anInd, 1);
1259   // then store currently dumped string
1260   theDumper.myFullDump << aBufCopy;
1261
1262   // now, store all postponed features
1263   theDumper.dumpPostponed();
1264
1265   return theDumper;
1266 }