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