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