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