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