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