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