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