Salome HOME
55cd9f2c7583fdd1e37b0e476daad817aeb60c92
[modules/shaper.git] / src / ModelHighAPI / ModelHighAPI_Dumper.cpp
1 // Copyright (C) 2016-20xx CEA/DEN, EDF R&D -->
2
3 // File:        ModelHighAPI_Dumper.cpp
4 // Created:     1 August 2016
5 // Author:      Artem ZHIDKOV
6
7 //--------------------------------------------------------------------------------------
8 #include "ModelHighAPI_Dumper.h"
9
10 #include <GeomAPI_Pnt.h>
11 #include <GeomAPI_Dir.h>
12
13 #include <GeomDataAPI_Dir.h>
14 #include <GeomDataAPI_Point.h>
15 #include <GeomDataAPI_Point2D.h>
16
17 #include <ModelAPI_AttributeBoolean.h>
18 #include <ModelAPI_AttributeDouble.h>
19 #include <ModelAPI_AttributeInteger.h>
20 #include <ModelAPI_AttributeRefAttr.h>
21 #include <ModelAPI_AttributeRefAttrList.h>
22 #include <ModelAPI_AttributeReference.h>
23 #include <ModelAPI_AttributeRefList.h>
24 #include <ModelAPI_AttributeSelection.h>
25 #include <ModelAPI_AttributeSelectionList.h>
26 #include <ModelAPI_AttributeString.h>
27 #include <ModelAPI_CompositeFeature.h>
28 #include <ModelAPI_Document.h>
29 #include <ModelAPI_Entity.h>
30 #include <ModelAPI_Feature.h>
31 #include <ModelAPI_Result.h>
32 #include <ModelAPI_ResultPart.h>
33
34 #include <PartSetPlugin_Part.h>
35
36 #include <SketchPlugin_SketchEntity.h>
37
38 #include <OSD_OpenFile.hxx>
39
40 #include <fstream>
41
42 #define DUMP_USER_DEFINED_NAMES
43
44 ModelHighAPI_Dumper* ModelHighAPI_Dumper::mySelf = 0;
45
46 ModelHighAPI_Dumper::ModelHighAPI_Dumper()
47 {
48   clear();
49 }
50
51 void ModelHighAPI_Dumper::setInstance(ModelHighAPI_Dumper* theDumper)
52 {
53   if (mySelf == 0)
54     mySelf = theDumper;
55 }
56
57 ModelHighAPI_Dumper* ModelHighAPI_Dumper::getInstance()
58 {
59   return mySelf;
60 }
61
62 void ModelHighAPI_Dumper::clear(bool bufferOnly)
63 {
64   myDumpBuffer = std::ostringstream();
65   myDumpBuffer << std::setprecision(16);
66
67   clearNotDumped();
68
69   if (!bufferOnly) {
70     myFullDump = std::ostringstream();
71     myFullDump << std::setprecision(16);
72
73     myNames.clear();
74     myModules.clear();
75     myFeatureCount.clear();
76     myLastEntityWithName = EntityPtr();
77   }
78 }
79
80 void ModelHighAPI_Dumper::clearNotDumped()
81 {
82   myNotDumpedEntities.clear();
83 }
84
85 const std::string& ModelHighAPI_Dumper::name(const EntityPtr& theEntity)
86 {
87   EntityNameMap::const_iterator aFound = myNames.find(theEntity);
88   if (aFound != myNames.end())
89     return aFound->second.first;
90
91   // entity is not found, store it
92   std::string aName;
93   bool isUserDefined = false;
94   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
95   if (aFeature) {
96     isUserDefined = true;
97     aName = aFeature->name();
98     const std::string& aKind = aFeature->getKind();
99     DocumentPtr aDoc = aFeature->document();
100     int& aNbFeatures = myFeatureCount[aDoc][aKind];
101
102     size_t anIndex = aName.find(aKind);
103     if (anIndex == 0 && aName[aKind.length()] == '_') { // name starts with "FeatureKind_"
104       std::string anIdStr = aName.substr(aKind.length() + 1, std::string::npos);
105       int anId = std::stoi(anIdStr);
106
107       // Check number of already registered objects of such kind. Index of current object
108       // should be greater than it to identify feature's name as automatically generated.
109       if (aNbFeatures < anId) {
110         isUserDefined = false;
111         aNbFeatures = anId - 1;
112       }
113     }
114
115     aNbFeatures += 1;
116   }
117
118   myNames[theEntity] = std::pair<std::string, bool>(aName, isUserDefined);
119   myNotDumpedEntities.insert(theEntity);
120   return myNames[theEntity].first;
121 }
122
123 const std::string& ModelHighAPI_Dumper::parentName(const FeaturePtr& theEntity)
124 {
125   const std::set<AttributePtr>& aRefs = theEntity->data()->refsToMe();
126   std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
127   for (; aRefIt != aRefs.end(); ++aRefIt) {
128     CompositeFeaturePtr anOwner = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(
129         ModelAPI_Feature::feature((*aRefIt)->owner()));
130     if (anOwner)
131       return name(anOwner);
132   }
133
134   static const std::string DUMMY;
135   return DUMMY;
136 }
137
138 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc,
139                                   const std::string& theFileName)
140 {
141   // dump top level document feature
142   static const std::string aDocName("partSet");
143   myNames[theDoc] = std::pair<std::string, bool>(aDocName, false);
144   *this << aDocName << " = model.moduleDocument()" << std::endl;
145
146   // dump subfeatures and store result to file
147   return process(theDoc) && exportTo(theFileName);
148 }
149
150 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc)
151 {
152   bool isOk = true;
153   // dump all features
154   std::list<FeaturePtr> aFeatures = theDoc->allFeatures();
155   std::list<FeaturePtr>::const_iterator aFeatIt = aFeatures.begin();
156   for (; aFeatIt != aFeatures.end(); ++aFeatIt) {
157     if (!isDumped(*aFeatIt))
158       dumpFeature(*aFeatIt);
159
160     // iteratively process composite features
161     CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFeatIt);
162     if (!aCompFeat)
163       continue;
164
165     // sub-part is processed independently, because it provides separate document
166     if ((*aFeatIt)->getKind() == PartSetPlugin_Part::ID()) {
167       ResultPartPtr aPartResult =
168           std::dynamic_pointer_cast<ModelAPI_ResultPart>((*aFeatIt)->lastResult());
169       if (!aPartResult)
170         continue;
171       DocumentPtr aSubDoc = aPartResult->partDoc();
172       // set name of document
173       const std::string& aPartName = myNames[*aFeatIt].first;
174       std::string aDocName = aPartName + "_doc";
175       myNames[aSubDoc] = std::pair<std::string, bool>(aDocName, false);
176
177       // dump document in a single line
178       *this << aDocName << " = " << aPartName << ".document()" << std::endl;
179
180       isOk = process(aSubDoc) && isOk;
181     } else
182       isOk = process(aCompFeat) && isOk;
183   }
184   return isOk;
185 }
186
187 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite)
188 {
189   // dump all sub-features;
190   int aNbSubs = theComposite->numberOfSubs();
191   for (int anIndex = 0; anIndex < aNbSubs; ++anIndex) {
192     FeaturePtr aFeature = theComposite->subFeature(anIndex);
193     if (isDumped(aFeature))
194       continue;
195     bool isForce = true;
196     // check the feature is a sketch entity and a copy of another entity
197     std::shared_ptr<SketchPlugin_SketchEntity> aSketchEntity =
198         std::dynamic_pointer_cast<SketchPlugin_SketchEntity>(aFeature);
199     if (aSketchEntity && aSketchEntity->isCopy())
200       isForce = false;
201     dumpFeature(aFeature, isForce);
202   }
203   // dump empty line for appearance
204   myDumpBuffer << std::endl;
205   return true;
206 }
207
208 bool ModelHighAPI_Dumper::exportTo(const std::string& theFileName)
209 {
210   std::ofstream aFile;
211   OSD_OpenStream(aFile, theFileName.c_str(), std::ofstream::out);
212   if (!aFile.is_open())
213     return false;
214
215   // standard header
216   for (ModulesMap::const_iterator aModIt = myModules.begin();
217        aModIt != myModules.end(); ++aModIt) {
218     aFile << "from " << aModIt->first << " import ";
219     if (aModIt->second.empty() || 
220         aModIt->second.find(std::string()) != aModIt->second.end())
221       aFile << "*"; // import whole module
222     else {
223       // import specific features
224       std::set<std::string>::const_iterator anObjIt = aModIt->second.begin();
225       aFile << *anObjIt;
226       for (++anObjIt; anObjIt != aModIt->second.end(); ++anObjIt)
227         aFile << ", " << *anObjIt;
228     }
229     aFile << std::endl;
230   }
231   if (!myModules.empty())
232     aFile << std::endl;
233
234   aFile << "import model" << std::endl << std::endl;
235   aFile << "model.begin()" << std::endl;
236
237   // dump collected data
238   aFile << myFullDump.str();
239
240   // standard footer
241   aFile << "model.end()" << std::endl;
242
243   aFile.close();
244   clear();
245
246   return true;
247 }
248
249 void ModelHighAPI_Dumper::importModule(const std::string& theModuleName,
250                                        const std::string& theObject)
251 {
252   myModules[theModuleName].insert(theObject);
253 }
254
255 void ModelHighAPI_Dumper::dumpEntitySetName()
256 {
257   if (!myLastEntityWithName)
258     return;
259
260 #ifdef DUMP_USER_DEFINED_NAMES
261   const std::string& aName = name(myLastEntityWithName);
262   myDumpBuffer << aName;
263   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(myLastEntityWithName);
264   if (aFeature)
265     myDumpBuffer << ".feature()";
266   myDumpBuffer << ".data().setName(\"" << aName << "\")" << std::endl;
267 #endif
268   myNames[myLastEntityWithName].second = false; // don't dump "setName" for the entity twice
269   myLastEntityWithName = EntityPtr();
270 }
271
272 bool ModelHighAPI_Dumper::isDumped(const EntityPtr& theEntity) const
273 {
274   EntityNameMap::const_iterator aFound = myNames.find(theEntity);
275   return aFound != myNames.end();
276 }
277
278 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char theChar)
279 {
280   myDumpBuffer << theChar;
281   return *this;
282 }
283
284 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char* theString)
285 {
286   myDumpBuffer << theString;
287   return *this;
288 }
289
290 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::string& theString)
291 {
292   myDumpBuffer << theString;
293   return *this;
294 }
295
296 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const bool theValue)
297 {
298   myDumpBuffer << (theValue ? "True" : "False");
299   return *this;
300 }
301
302 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const int theValue)
303 {
304   myDumpBuffer << theValue;
305   return *this;
306 }
307
308 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const double theValue)
309 {
310   myDumpBuffer << theValue;
311   return *this;
312 }
313
314 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
315 {
316   importModule("GeomAPI", "GeomAPI_Pnt");
317   myDumpBuffer << "GeomAPI_Pnt(" << thePoint->x() << ", "
318                << thePoint->y() << ", " << thePoint->z() << ")";
319   return *this;
320 }
321
322 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir)
323 {
324   importModule("GeomAPI", "GeomAPI_Dir");
325   myDumpBuffer << "GeomAPI_Dir(" << theDir->x() << ", "
326                << theDir->y() << ", " << theDir->z() << ")";
327   return *this;
328 }
329
330 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
331     const std::shared_ptr<GeomDataAPI_Dir>& theDir)
332 {
333   myDumpBuffer << theDir->x() << ", " << theDir->y() << ", " << theDir->z();
334   return *this;
335 }
336
337 static void dumpArray(std::ostringstream& theOutput, int theSize,
338                       double* theValues, std::string* theTexts)
339 {
340   for (int i = 0; i < theSize; ++i) {
341     if (i > 0)
342       theOutput << ", ";
343     if (theTexts[i].empty())
344       theOutput << theValues[i];
345     else
346       theOutput << "\"" << theTexts[i] << "\"";
347   }
348 }
349
350 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
351     const std::shared_ptr<GeomDataAPI_Point>& thePoint)
352 {
353   static const int aSize = 3;
354   double aValues[aSize] = {thePoint->x(), thePoint->y(), thePoint->z()};
355   std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY(), thePoint->textZ()};
356   dumpArray(myDumpBuffer, aSize, aValues, aTexts);
357   return *this;
358 }
359
360 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
361     const std::shared_ptr<GeomDataAPI_Point2D>& thePoint)
362 {
363   static const int aSize = 2;
364   double aValues[aSize] = {thePoint->x(), thePoint->y()};
365   std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY()};
366   dumpArray(myDumpBuffer, aSize, aValues, aTexts);
367   return *this;
368 }
369
370 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
371     const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool)
372 {
373   myDumpBuffer << (theAttrBool->value() ? "True" : "False");
374   return *this;
375 }
376
377 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
378     const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt)
379 {
380   std::string aText = theAttrInt->text();
381   if (aText.empty())
382     myDumpBuffer << theAttrInt->value();
383   else
384     myDumpBuffer << "\"" << aText << "\"";
385   return *this;
386 }
387
388 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
389     const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal)
390 {
391   std::string aText = theAttrReal->text();
392   if (aText.empty())
393     myDumpBuffer << theAttrReal->value();
394   else
395     myDumpBuffer << "\"" << aText << "\"";
396   return *this;
397 }
398
399 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
400     const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr)
401 {
402   myDumpBuffer << "\"" << theAttrStr->value() << "\"";
403   return *this;
404 }
405
406 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FeaturePtr& theEntity)
407 {
408   myDumpBuffer << name(theEntity);
409   if (myNames[theEntity].second)
410     myLastEntityWithName = theEntity;
411   myNotDumpedEntities.erase(theEntity);
412   return *this;
413 }
414
415 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ObjectPtr& theObject)
416 {
417   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
418   myDumpBuffer << name(aFeature);
419   return *this;
420 }
421
422 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const AttributePtr& theAttr)
423 {
424   FeaturePtr anOwner = ModelAPI_Feature::feature(theAttr->owner());
425   myDumpBuffer << name(anOwner) << "." << attributeGetter(anOwner, theAttr->id()) << "()";
426   return *this;
427 }
428
429 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
430     const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr)
431 {
432   if (theRefAttr->isObject())
433     *this << theRefAttr->object();
434   else
435     *this << theRefAttr->attr();
436   return *this;
437 }
438
439 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
440     const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList)
441 {
442   myDumpBuffer << "[";
443   std::list<std::pair<ObjectPtr, AttributePtr> > aList = theRefAttrList->list();
444   bool isAdded = false;
445   std::list<std::pair<ObjectPtr, AttributePtr> >::const_iterator anIt = aList.begin();
446   for (; anIt != aList.end(); ++anIt) {
447     if (isAdded)
448       myDumpBuffer << ", ";
449     else
450       isAdded = true;
451     if (anIt->first)
452       *this << anIt->first;
453     else if (anIt->second)
454       * this << anIt->second;
455   }
456   myDumpBuffer << "]";
457   return *this;
458 }
459
460 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
461     const std::shared_ptr<ModelAPI_AttributeReference>& theReference)
462 {
463   *this << theReference->value();
464   return *this;
465 }
466
467 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
468     const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList)
469 {
470   static const int aThreshold = 2;
471   // if number of elements in the list if greater than a threshold,
472   // dump it in a separate line with specific name
473   std::string aDumped = myDumpBuffer.str();
474   if (aDumped.empty() || theRefList->size() <= aThreshold) {
475     myDumpBuffer << "[";
476     std::list<ObjectPtr> aList = theRefList->list();
477     bool isAdded = false;
478     std::list<ObjectPtr>::const_iterator anIt = aList.begin();
479     for (; anIt != aList.end(); ++anIt) {
480       if (isAdded)
481         myDumpBuffer << ", ";
482       else
483         isAdded = true;
484
485       *this << *anIt;
486     }
487     myDumpBuffer << "]";
488   } else {
489     // clear buffer and store list "as is"
490     myDumpBuffer = std::ostringstream();
491     *this << theRefList;
492     // save buffer and clear it again
493     std::string aDumpedList = myDumpBuffer.str();
494     myDumpBuffer = std::ostringstream();
495     // obtain name of list
496     FeaturePtr anOwner = ModelAPI_Feature::feature(theRefList->owner());
497     std::string aListName = name(anOwner) + "_objects";
498     // store all previous data
499     myDumpBuffer << aListName << " = " << aDumpedList << std::endl
500                  << aDumped << aListName;
501   }
502   return *this;
503 }
504
505 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
506     const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect)
507 {
508   myDumpBuffer << "model.selection(";
509
510   if(!theAttrSelect->isInitialized()) {
511     myDumpBuffer << ")";
512     return *this;
513   }
514
515   GeomShapePtr aShape = theAttrSelect->value();
516   if(!aShape.get()) {
517     aShape = theAttrSelect->context()->shape();
518   }
519
520   if(!aShape.get()) {
521     myDumpBuffer << ")";
522     return *this;
523   }
524
525   myDumpBuffer << "\"" << aShape->shapeTypeStr() << "\", \"" << theAttrSelect->namingName() << "\")";
526   return *this;
527 }
528
529 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
530     const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList)
531 {
532   myDumpBuffer << "[";
533
534   GeomShapePtr aShape;
535   std::string aShapeTypeStr;
536
537   bool isAdded = false;
538
539   for(int anIndex = 0; anIndex < theAttrSelList->size(); ++anIndex) {
540     AttributeSelectionPtr anAttribute = theAttrSelList->value(anIndex);
541     aShape = anAttribute->value();
542     if(!aShape.get()) {
543       aShape = anAttribute->context()->shape();
544     }
545
546     if(!aShape.get()) {
547       continue;
548     }
549
550     if(isAdded) {
551       myDumpBuffer << ", ";
552     } else {
553       isAdded = true;
554     }
555     myDumpBuffer << "model.selection(\"" << aShape->shapeTypeStr() << "\", \"" << anAttribute->namingName() << "\")";
556   }
557
558   myDumpBuffer << "]";
559   return *this;
560 }
561
562 /// Dump std::endl
563 MODELHIGHAPI_EXPORT
564 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
565                                 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&))
566 {
567   theDumper.myDumpBuffer << theEndl;
568   theDumper.dumpEntitySetName();
569
570   // store all not-dumped entities first
571   std::set<EntityPtr> aNotDumped = theDumper.myNotDumpedEntities;
572   std::string aBufCopy = theDumper.myDumpBuffer.str();
573   theDumper.clear(true);
574   std::set<EntityPtr>::const_iterator anIt = aNotDumped.begin();
575   for (; anIt != aNotDumped.end(); ++anIt) {
576     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
577     theDumper.dumpFeature(aFeature, true);
578
579     // if the feature is composite, dump all its subs
580     CompositeFeaturePtr aCompFeat =
581         std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
582     if (aCompFeat)
583       theDumper.process(aCompFeat);
584   }
585
586   // then store currently dumped string
587   theDumper.myFullDump << aBufCopy;
588
589   return theDumper;
590 }