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 <SketchPlugin_SketchEntity.h>
38 #include <OSD_OpenFile.hxx>
42 #define DUMP_USER_DEFINED_NAMES
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 if (!isDumped(*aFeatIt))
158 dumpFeature(*aFeatIt);
160 // iteratively process composite features
161 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFeatIt);
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());
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);
177 // dump document in a single line
178 *this << aDocName << " = " << aPartName << ".document()" << std::endl;
180 isOk = process(aSubDoc) && isOk;
182 isOk = process(aCompFeat) && isOk;
187 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite)
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))
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())
201 dumpFeature(aFeature, isForce);
203 // dump empty line for appearance
204 myDumpBuffer << std::endl;
208 bool ModelHighAPI_Dumper::exportTo(const std::string& theFileName)
211 OSD_OpenStream(aFile, theFileName.c_str(), std::ofstream::out);
212 if (!aFile.is_open())
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
223 // import specific features
224 std::set<std::string>::const_iterator anObjIt = aModIt->second.begin();
226 for (++anObjIt; anObjIt != aModIt->second.end(); ++anObjIt)
227 aFile << ", " << *anObjIt;
231 if (!myModules.empty())
234 aFile << "import model" << std::endl << std::endl;
235 aFile << "model.begin()" << std::endl;
237 // dump collected data
238 aFile << myFullDump.str();
241 aFile << "model.end()" << std::endl;
249 void ModelHighAPI_Dumper::importModule(const std::string& theModuleName,
250 const std::string& theObject)
252 myModules[theModuleName].insert(theObject);
255 void ModelHighAPI_Dumper::dumpEntitySetName()
257 if (!myLastEntityWithName)
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);
265 myDumpBuffer << ".feature()";
266 myDumpBuffer << ".data().setName(\"" << aName << "\")" << std::endl;
268 myNames[myLastEntityWithName].second = false; // don't dump "setName" for the entity twice
269 myLastEntityWithName = EntityPtr();
272 bool ModelHighAPI_Dumper::isDumped(const EntityPtr& theEntity) const
274 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
275 return aFound != myNames.end();
278 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char theChar)
280 myDumpBuffer << theChar;
284 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char* theString)
286 myDumpBuffer << theString;
290 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::string& theString)
292 myDumpBuffer << theString;
296 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const bool theValue)
298 myDumpBuffer << (theValue ? "True" : "False");
302 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const int theValue)
304 myDumpBuffer << theValue;
308 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const double theValue)
310 myDumpBuffer << theValue;
314 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
316 importModule("GeomAPI", "GeomAPI_Pnt");
317 myDumpBuffer << "GeomAPI_Pnt(" << thePoint->x() << ", "
318 << thePoint->y() << ", " << thePoint->z() << ")";
322 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir)
324 importModule("GeomAPI", "GeomAPI_Dir");
325 myDumpBuffer << "GeomAPI_Dir(" << theDir->x() << ", "
326 << theDir->y() << ", " << theDir->z() << ")";
330 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
331 const std::shared_ptr<GeomDataAPI_Dir>& theDir)
333 myDumpBuffer << theDir->x() << ", " << theDir->y() << ", " << theDir->z();
337 static void dumpArray(std::ostringstream& theOutput, int theSize,
338 double* theValues, std::string* theTexts)
340 for (int i = 0; i < theSize; ++i) {
343 if (theTexts[i].empty())
344 theOutput << theValues[i];
346 theOutput << "\"" << theTexts[i] << "\"";
350 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
351 const std::shared_ptr<GeomDataAPI_Point>& thePoint)
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);
360 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
361 const std::shared_ptr<GeomDataAPI_Point2D>& thePoint)
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);
370 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
371 const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool)
373 myDumpBuffer << (theAttrBool->value() ? "True" : "False");
377 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
378 const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt)
380 std::string aText = theAttrInt->text();
382 myDumpBuffer << theAttrInt->value();
384 myDumpBuffer << "\"" << aText << "\"";
388 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
389 const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal)
391 std::string aText = theAttrReal->text();
393 myDumpBuffer << theAttrReal->value();
395 myDumpBuffer << "\"" << aText << "\"";
399 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
400 const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr)
402 myDumpBuffer << "\"" << theAttrStr->value() << "\"";
406 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FeaturePtr& theEntity)
408 myDumpBuffer << name(theEntity);
409 if (myNames[theEntity].second)
410 myLastEntityWithName = theEntity;
411 myNotDumpedEntities.erase(theEntity);
415 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ObjectPtr& theObject)
417 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
418 myDumpBuffer << name(aFeature);
422 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const AttributePtr& theAttr)
424 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttr->owner());
425 myDumpBuffer << name(anOwner) << "." << attributeGetter(anOwner, theAttr->id()) << "()";
429 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
430 const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr)
432 if (theRefAttr->isObject())
433 *this << theRefAttr->object();
435 *this << theRefAttr->attr();
439 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
440 const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList)
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) {
448 myDumpBuffer << ", ";
452 *this << anIt->first;
453 else if (anIt->second)
454 * this << anIt->second;
460 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
461 const std::shared_ptr<ModelAPI_AttributeReference>& theReference)
463 *this << theReference->value();
467 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
468 const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList)
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) {
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) {
481 myDumpBuffer << ", ";
489 // clear buffer and store list "as is"
490 myDumpBuffer = std::ostringstream();
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;
505 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
506 const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect)
508 myDumpBuffer << "model.selection(";
510 if(!theAttrSelect->isInitialized()) {
515 GeomShapePtr aShape = theAttrSelect->value();
517 aShape = theAttrSelect->context()->shape();
525 myDumpBuffer << "\"" << aShape->shapeTypeStr() << "\", \"" << theAttrSelect->namingName() << "\")";
529 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
530 const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList)
535 std::string aShapeTypeStr;
537 bool isAdded = false;
539 for(int anIndex = 0; anIndex < theAttrSelList->size(); ++anIndex) {
540 AttributeSelectionPtr anAttribute = theAttrSelList->value(anIndex);
541 aShape = anAttribute->value();
543 aShape = anAttribute->context()->shape();
551 myDumpBuffer << ", ";
555 myDumpBuffer << "model.selection(\"" << aShape->shapeTypeStr() << "\", \"" << anAttribute->namingName() << "\")";
564 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
565 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&))
567 theDumper.myDumpBuffer << theEndl;
568 theDumper.dumpEntitySetName();
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);
579 // if the feature is composite, dump all its subs
580 CompositeFeaturePtr aCompFeat =
581 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
583 theDumper.process(aCompFeat);
586 // then store currently dumped string
587 theDumper.myFullDump << aBufCopy;