1 // Copyright (C) 2016-20xx CEA/DEN, EDF R&D -->
3 // File: ModelHighAPI_Dumper.cpp
4 // Created: 1 August 2016
5 // Author: Artem ZHIDKOV
7 //--------------------------------------------------------------------------------------
8 #include "ModelHighAPI_Dumper.h"
10 #include <GeomAPI_Pnt.h>
11 #include <GeomAPI_Dir.h>
13 #include <GeomDataAPI_Dir.h>
14 #include <GeomDataAPI_Point.h>
15 #include <GeomDataAPI_Point2D.h>
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>
34 #include <PartSetPlugin_Part.h>
36 #include <OSD_OpenFile.hxx>
40 #define DUMP_USER_DEFINED_NAMES
42 static int gCompositeStackDepth = 0;
44 ModelHighAPI_Dumper* ModelHighAPI_Dumper::mySelf = 0;
46 ModelHighAPI_Dumper::ModelHighAPI_Dumper()
51 void ModelHighAPI_Dumper::setInstance(ModelHighAPI_Dumper* theDumper)
57 ModelHighAPI_Dumper* ModelHighAPI_Dumper::getInstance()
62 void ModelHighAPI_Dumper::clear(bool bufferOnly)
64 myDumpBuffer = std::ostringstream();
65 myDumpBuffer << std::setprecision(16);
70 myFullDump = std::ostringstream();
71 myFullDump << std::setprecision(16);
75 myFeatureCount.clear();
76 myLastEntityWithName = EntityPtr();
80 void ModelHighAPI_Dumper::clearNotDumped()
82 myNotDumpedEntities.clear();
85 const std::string& ModelHighAPI_Dumper::name(const EntityPtr& theEntity)
87 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
88 if (aFound != myNames.end())
89 return aFound->second.first;
91 // entity is not found, store it
93 bool isUserDefined = false;
94 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
97 aName = aFeature->name();
98 const std::string& aKind = aFeature->getKind();
99 DocumentPtr aDoc = aFeature->document();
100 int& aNbFeatures = myFeatureCount[aDoc][aKind];
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);
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;
118 myNames[theEntity] = std::pair<std::string, bool>(aName, isUserDefined);
119 myNotDumpedEntities.insert(theEntity);
120 return myNames[theEntity].first;
123 const std::string& ModelHighAPI_Dumper::parentName(const FeaturePtr& theEntity)
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()));
131 return name(anOwner);
134 static const std::string DUMMY;
138 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc,
139 const std::string& theFileName)
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;
146 // dump subfeatures and store result to file
147 return process(theDoc) && exportTo(theFileName);
150 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc)
154 std::list<FeaturePtr> aFeatures = theDoc->allFeatures();
155 std::list<FeaturePtr>::const_iterator aFeatIt = aFeatures.begin();
156 for (; aFeatIt != aFeatures.end(); ++aFeatIt) {
157 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFeatIt);
158 if (aCompFeat) // iteratively process composite features
159 isOk = process(aCompFeat) && isOk;
160 else if (!isDumped(*aFeatIt)) // dump common feature
161 dumpFeature(*aFeatIt);
166 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite, bool isForce)
168 // increase composite features stack
169 ++gCompositeStackDepth;
170 // dump composite itself
171 if (!isDumped(theComposite) || isForce)
172 dumpFeature(theComposite, isForce);
174 // sub-part is processed independently, because it provides separate document
175 if (theComposite->getKind() == PartSetPlugin_Part::ID()) {
176 // decrease composite features stack because we run into separate document
177 --gCompositeStackDepth;
179 ResultPartPtr aPartResult =
180 std::dynamic_pointer_cast<ModelAPI_ResultPart>(theComposite->lastResult());
183 DocumentPtr aSubDoc = aPartResult->partDoc();
184 // set name of document
185 const std::string& aPartName = myNames[theComposite].first;
186 std::string aDocName = aPartName + "_doc";
187 myNames[aSubDoc] = std::pair<std::string, bool>(aDocName, false);
189 // dump document in a separate line
190 *this << aDocName << " = " << aPartName << ".document()" << std::endl;
191 // dump features in the document
192 return process(aSubDoc);
196 bool isOk = processSubs(theComposite);
197 // decrease composite features stack
198 --gCompositeStackDepth;
203 bool ModelHighAPI_Dumper::processSubs(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
207 // dump all sub-features;
208 int aNbSubs = theComposite->numberOfSubs();
209 for (int anIndex = 0; anIndex < aNbSubs; ++anIndex) {
210 FeaturePtr aFeature = theComposite->subFeature(anIndex);
211 if (isDumped(aFeature))
214 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
215 if (aCompFeat) // iteratively process composite features
216 isOk = process(aCompFeat) && isOk;
218 dumpFeature(aFeature, true);
221 // It is necessary for the sketch to create its result when complete (command "model.do()").
222 // This option is set by flat theDumpModelDo.
223 // However, nested sketches are rebuilt by parent feature, so, they do not need
224 // explicit call of "model.do()". This will be controlled by the depth of the stack.
225 if (theDumpModelDo && gCompositeStackDepth <= 1)
226 *this << "model.do()" << std::endl;
230 bool ModelHighAPI_Dumper::exportTo(const std::string& theFileName)
233 OSD_OpenStream(aFile, theFileName.c_str(), std::ofstream::out);
234 if (!aFile.is_open())
238 for (ModulesMap::const_iterator aModIt = myModules.begin();
239 aModIt != myModules.end(); ++aModIt) {
240 aFile << "from " << aModIt->first << " import ";
241 if (aModIt->second.empty() ||
242 aModIt->second.find(std::string()) != aModIt->second.end())
243 aFile << "*"; // import whole module
245 // import specific features
246 std::set<std::string>::const_iterator anObjIt = aModIt->second.begin();
248 for (++anObjIt; anObjIt != aModIt->second.end(); ++anObjIt)
249 aFile << ", " << *anObjIt;
253 if (!myModules.empty())
256 aFile << "import model" << std::endl << std::endl;
257 aFile << "model.begin()" << std::endl;
259 // dump collected data
260 aFile << myFullDump.str();
263 aFile << "model.end()" << std::endl;
271 void ModelHighAPI_Dumper::importModule(const std::string& theModuleName,
272 const std::string& theObject)
274 myModules[theModuleName].insert(theObject);
277 void ModelHighAPI_Dumper::dumpEntitySetName()
279 if (!myLastEntityWithName)
282 #ifdef DUMP_USER_DEFINED_NAMES
283 const std::string& aName = name(myLastEntityWithName);
284 myDumpBuffer << aName;
285 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(myLastEntityWithName);
287 myDumpBuffer << ".feature()";
288 myDumpBuffer << ".data().setName(\"" << aName << "\")" << std::endl;
290 myNames[myLastEntityWithName].second = false; // don't dump "setName" for the entity twice
291 myLastEntityWithName = EntityPtr();
294 bool ModelHighAPI_Dumper::isDumped(const EntityPtr& theEntity) const
296 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
297 return aFound != myNames.end();
300 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char theChar)
302 myDumpBuffer << theChar;
306 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char* theString)
308 myDumpBuffer << theString;
312 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::string& theString)
314 myDumpBuffer << theString;
318 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const bool theValue)
320 myDumpBuffer << (theValue ? "True" : "False");
324 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const int theValue)
326 myDumpBuffer << theValue;
330 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const double theValue)
332 myDumpBuffer << theValue;
336 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
338 importModule("GeomAPI", "GeomAPI_Pnt");
339 myDumpBuffer << "GeomAPI_Pnt(" << thePoint->x() << ", "
340 << thePoint->y() << ", " << thePoint->z() << ")";
344 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir)
346 importModule("GeomAPI", "GeomAPI_Dir");
347 myDumpBuffer << "GeomAPI_Dir(" << theDir->x() << ", "
348 << theDir->y() << ", " << theDir->z() << ")";
352 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
353 const std::shared_ptr<GeomDataAPI_Dir>& theDir)
355 myDumpBuffer << theDir->x() << ", " << theDir->y() << ", " << theDir->z();
359 static void dumpArray(std::ostringstream& theOutput, int theSize,
360 double* theValues, std::string* theTexts)
362 for (int i = 0; i < theSize; ++i) {
365 if (theTexts[i].empty())
366 theOutput << theValues[i];
368 theOutput << "\"" << theTexts[i] << "\"";
372 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
373 const std::shared_ptr<GeomDataAPI_Point>& thePoint)
375 static const int aSize = 3;
376 double aValues[aSize] = {thePoint->x(), thePoint->y(), thePoint->z()};
377 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY(), thePoint->textZ()};
378 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
382 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
383 const std::shared_ptr<GeomDataAPI_Point2D>& thePoint)
385 static const int aSize = 2;
386 double aValues[aSize] = {thePoint->x(), thePoint->y()};
387 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY()};
388 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
392 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
393 const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool)
395 myDumpBuffer << (theAttrBool->value() ? "True" : "False");
399 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
400 const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt)
402 std::string aText = theAttrInt->text();
404 myDumpBuffer << theAttrInt->value();
406 myDumpBuffer << "\"" << aText << "\"";
410 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
411 const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal)
413 std::string aText = theAttrReal->text();
415 myDumpBuffer << theAttrReal->value();
417 myDumpBuffer << "\"" << aText << "\"";
421 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
422 const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr)
424 myDumpBuffer << "\"" << theAttrStr->value() << "\"";
428 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FeaturePtr& theEntity)
430 myDumpBuffer << name(theEntity);
431 if (myNames[theEntity].second)
432 myLastEntityWithName = theEntity;
433 myNotDumpedEntities.erase(theEntity);
437 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ObjectPtr& theObject)
439 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
440 myDumpBuffer << name(aFeature);
444 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const AttributePtr& theAttr)
446 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttr->owner());
447 myDumpBuffer << name(anOwner) << "." << attributeGetter(anOwner, theAttr->id()) << "()";
451 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
452 const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr)
454 if (theRefAttr->isObject())
455 *this << theRefAttr->object();
457 *this << theRefAttr->attr();
461 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
462 const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList)
465 std::list<std::pair<ObjectPtr, AttributePtr> > aList = theRefAttrList->list();
466 bool isAdded = false;
467 std::list<std::pair<ObjectPtr, AttributePtr> >::const_iterator anIt = aList.begin();
468 for (; anIt != aList.end(); ++anIt) {
470 myDumpBuffer << ", ";
474 *this << anIt->first;
475 else if (anIt->second)
476 * this << anIt->second;
482 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
483 const std::shared_ptr<ModelAPI_AttributeReference>& theReference)
485 *this << theReference->value();
489 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
490 const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList)
492 static const int aThreshold = 2;
493 // if number of elements in the list if greater than a threshold,
494 // dump it in a separate line with specific name
495 std::string aDumped = myDumpBuffer.str();
496 if (aDumped.empty() || theRefList->size() <= aThreshold) {
498 std::list<ObjectPtr> aList = theRefList->list();
499 bool isAdded = false;
500 std::list<ObjectPtr>::const_iterator anIt = aList.begin();
501 for (; anIt != aList.end(); ++anIt) {
503 myDumpBuffer << ", ";
511 // clear buffer and store list "as is"
512 myDumpBuffer = std::ostringstream();
514 // save buffer and clear it again
515 std::string aDumpedList = myDumpBuffer.str();
516 myDumpBuffer = std::ostringstream();
517 // obtain name of list
518 FeaturePtr anOwner = ModelAPI_Feature::feature(theRefList->owner());
519 std::string aListName = name(anOwner) + "_objects";
520 // store all previous data
521 myDumpBuffer << aListName << " = " << aDumpedList << std::endl
522 << aDumped << aListName;
527 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
528 const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect)
530 myDumpBuffer << "model.selection(";
532 if(!theAttrSelect->isInitialized()) {
537 GeomShapePtr aShape = theAttrSelect->value();
539 aShape = theAttrSelect->context()->shape();
547 myDumpBuffer << "\"" << aShape->shapeTypeStr() << "\", \"" << theAttrSelect->namingName() << "\")";
551 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
552 const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList)
557 std::string aShapeTypeStr;
559 bool isAdded = false;
561 for(int anIndex = 0; anIndex < theAttrSelList->size(); ++anIndex) {
562 AttributeSelectionPtr anAttribute = theAttrSelList->value(anIndex);
563 aShape = anAttribute->value();
565 aShape = anAttribute->context()->shape();
573 myDumpBuffer << ", ";
577 myDumpBuffer << "model.selection(\"" << aShape->shapeTypeStr() << "\", \"" << anAttribute->namingName() << "\")";
586 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
587 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&))
589 theDumper.myDumpBuffer << theEndl;
590 theDumper.dumpEntitySetName();
592 // store all not-dumped entities first
593 std::set<EntityPtr> aNotDumped = theDumper.myNotDumpedEntities;
594 std::string aBufCopy = theDumper.myDumpBuffer.str();
595 theDumper.clear(true);
596 std::set<EntityPtr>::const_iterator anIt = aNotDumped.begin();
597 for (; anIt != aNotDumped.end(); ++anIt) {
598 // if the feature is composite, dump it with all subs
599 CompositeFeaturePtr aCompFeat =
600 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anIt);
602 theDumper.process(aCompFeat, true);
604 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
605 theDumper.dumpFeature(aFeature, true);
609 // avoid multiple empty lines
610 size_t anInd = std::string::npos;
611 while ((anInd = aBufCopy.find("\n\n\n")) != std::string::npos)
612 aBufCopy.erase(anInd, 1);
613 // then store currently dumped string
614 theDumper.myFullDump << aBufCopy;