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_AttributeRefList.h>
23 #include <ModelAPI_AttributeSelection.h>
24 #include <ModelAPI_AttributeSelectionList.h>
25 #include <ModelAPI_AttributeString.h>
26 #include <ModelAPI_CompositeFeature.h>
27 #include <ModelAPI_Document.h>
28 #include <ModelAPI_Entity.h>
29 #include <ModelAPI_Feature.h>
30 #include <ModelAPI_Result.h>
31 #include <ModelAPI_ResultPart.h>
33 #include <PartSetPlugin_Part.h>
35 #include <SketchPlugin_SketchEntity.h>
37 #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 myLastEntityWithName = EntityPtr();
79 void ModelHighAPI_Dumper::clearNotDumped()
81 myNotDumpedEntities.clear();
84 const std::string& ModelHighAPI_Dumper::name(const EntityPtr& theEntity)
86 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
87 if (aFound != myNames.end())
88 return aFound->second.first;
90 // entity is not found, store it
92 bool isNameDefined = false;
93 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
95 aName = aFeature->name();
96 isNameDefined = !aName.empty();
99 static long anIndex = 0;
100 // set default name: feature ID + index
101 std::ostringstream aConverter;
102 aConverter << aFeature->getKind() << "_" << ++anIndex;
103 aName = aConverter.str();
104 std::transform(aName.begin(), aName.end(), aName.begin(), ::tolower);
108 myNames[theEntity] = std::pair<std::string, bool>(aName, isNameDefined);
109 myNotDumpedEntities.insert(theEntity);
110 return myNames[theEntity].first;
113 const std::string& ModelHighAPI_Dumper::parentName(const FeaturePtr& theEntity)
115 const std::set<AttributePtr>& aRefs = theEntity->data()->refsToMe();
116 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
117 for (; aRefIt != aRefs.end(); ++aRefIt) {
118 CompositeFeaturePtr anOwner = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(
119 ModelAPI_Feature::feature((*aRefIt)->owner()));
121 return name(anOwner);
124 static const std::string DUMMY;
128 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc,
129 const std::string& theFileName)
131 // dump top level document feature
132 static const std::string aDocName("partSet");
133 myNames[theDoc] = std::pair<std::string, bool>(aDocName, false);
134 *this << aDocName << " = model.moduleDocument()" << std::endl;
136 // dump subfeatures and store result to file
137 return process(theDoc) && exportTo(theFileName);
140 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc)
144 std::list<FeaturePtr> aFeatures = theDoc->allFeatures();
145 std::list<FeaturePtr>::const_iterator aFeatIt = aFeatures.begin();
146 for (; aFeatIt != aFeatures.end(); ++aFeatIt) {
147 if (!isDumped(*aFeatIt))
148 dumpFeature(*aFeatIt);
150 // iteratively process composite features
151 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFeatIt);
155 // sub-part is processed independently, because it provides separate document
156 if ((*aFeatIt)->getKind() == PartSetPlugin_Part::ID()) {
157 ResultPartPtr aPartResult =
158 std::dynamic_pointer_cast<ModelAPI_ResultPart>((*aFeatIt)->lastResult());
161 DocumentPtr aSubDoc = aPartResult->partDoc();
162 // set name of document equal to part name
163 myNames[aSubDoc] = myNames[*aFeatIt];
165 isOk = process(aSubDoc) && isOk;
167 isOk = process(aCompFeat) && isOk;
172 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite)
174 // dump all sub-features;
175 int aNbSubs = theComposite->numberOfSubs();
176 for (int anIndex = 0; anIndex < aNbSubs; ++anIndex) {
177 FeaturePtr aFeature = theComposite->subFeature(anIndex);
178 if (isDumped(aFeature))
181 // check the feature is a sketch entity and a copy of another entity
182 std::shared_ptr<SketchPlugin_SketchEntity> aSketchEntity =
183 std::dynamic_pointer_cast<SketchPlugin_SketchEntity>(aFeature);
184 if (aSketchEntity && aSketchEntity->isCopy())
186 dumpFeature(aFeature, isForce);
188 // dump command to update model
189 myDumpBuffer << "model.do()" << std::endl;
193 bool ModelHighAPI_Dumper::exportTo(const std::string& theFileName)
196 OSD_OpenStream(aFile, theFileName.c_str(), std::ofstream::out);
197 if (!aFile.is_open())
201 for (ModulesMap::const_iterator aModIt = myModules.begin();
202 aModIt != myModules.end(); ++aModIt) {
203 aFile << "from " << aModIt->first << " import ";
204 if (aModIt->second.empty() ||
205 aModIt->second.find(std::string()) != aModIt->second.end())
206 aFile << "*"; // import whole module
208 // import specific features
209 std::set<std::string>::const_iterator anObjIt = aModIt->second.begin();
211 for (++anObjIt; anObjIt != aModIt->second.end(); ++anObjIt)
212 aFile << ", " << *anObjIt;
216 if (!myModules.empty())
219 aFile << "import model" << std::endl << std::endl;
220 aFile << "model.begin()" << std::endl;
222 // dump collected data
223 aFile << myFullDump.str();
226 aFile << "model.end()" << std::endl;
234 void ModelHighAPI_Dumper::importModule(const std::string& theModuleName,
235 const std::string& theObject)
237 myModules[theModuleName].insert(theObject);
240 void ModelHighAPI_Dumper::dumpEntitySetName()
242 if (!myLastEntityWithName)
245 #ifdef DUMP_USER_DEFINED_NAMES
246 const std::string& aName = name(myLastEntityWithName);
247 myDumpBuffer << aName;
248 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(myLastEntityWithName);
250 myDumpBuffer << ".feature()";
251 myDumpBuffer << ".data().setName(\"" << aName << "\")" << std::endl;
253 myNames[myLastEntityWithName].second = false; // don't dump "setName" for the entity twice
254 myLastEntityWithName = EntityPtr();
257 bool ModelHighAPI_Dumper::isDumped(const EntityPtr& theEntity) const
259 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
260 return aFound != myNames.end();
263 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char theChar)
265 myDumpBuffer << theChar;
269 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char* theString)
271 myDumpBuffer << theString;
275 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::string& theString)
277 myDumpBuffer << theString;
281 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const int theValue)
283 myDumpBuffer << theValue;
287 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const double theValue)
289 myDumpBuffer << theValue;
293 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
295 importModule("GeomAPI", "GeomAPI_Pnt");
296 myDumpBuffer << "GeomAPI_Pnt(" << thePoint->x() << ", "
297 << thePoint->y() << ", " << thePoint->z() << ")";
301 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir)
303 importModule("GeomAPI", "GeomAPI_Dir");
304 myDumpBuffer << "GeomAPI_Dir(" << theDir->x() << ", "
305 << theDir->y() << ", " << theDir->z() << ")";
309 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
310 const std::shared_ptr<GeomDataAPI_Dir>& theDir)
312 myDumpBuffer << theDir->x() << ", " << theDir->y() << ", " << theDir->z();
316 static void dumpArray(std::ostringstream& theOutput, int theSize,
317 double* theValues, std::string* theTexts)
319 for (int i = 0; i < theSize; ++i) {
322 if (theTexts[i].empty())
323 theOutput << theValues[i];
325 theOutput << "\"" << theTexts[i] << "\"";
329 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
330 const std::shared_ptr<GeomDataAPI_Point>& thePoint)
332 static const int aSize = 3;
333 double aValues[aSize] = {thePoint->x(), thePoint->y(), thePoint->z()};
334 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY(), thePoint->textZ()};
335 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
339 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
340 const std::shared_ptr<GeomDataAPI_Point2D>& thePoint)
342 static const int aSize = 2;
343 double aValues[aSize] = {thePoint->x(), thePoint->y()};
344 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY()};
345 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
349 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
350 const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool)
352 myDumpBuffer << (theAttrBool->value() ? "True" : "False");
356 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
357 const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt)
359 std::string aText = theAttrInt->text();
361 myDumpBuffer << theAttrInt->value();
363 myDumpBuffer << "\"" << aText << "\"";
367 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
368 const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal)
370 std::string aText = theAttrReal->text();
372 myDumpBuffer << theAttrReal->value();
374 myDumpBuffer << "\"" << aText << "\"";
378 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
379 const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr)
381 myDumpBuffer << "\"" << theAttrStr->value() << "\"";
385 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FeaturePtr& theEntity)
387 myDumpBuffer << name(theEntity);
388 if (myNames[theEntity].second)
389 myLastEntityWithName = theEntity;
390 myNotDumpedEntities.erase(theEntity);
394 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ObjectPtr& theObject)
396 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
397 myDumpBuffer << name(aFeature);
401 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const AttributePtr& theAttr)
403 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttr->owner());
404 myDumpBuffer << name(anOwner) << "." << attributeGetter(anOwner, theAttr->id()) << "()";
408 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
409 const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr)
411 if (theRefAttr->isObject())
412 *this << theRefAttr->object();
414 *this << theRefAttr->attr();
418 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
419 const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList)
422 std::list<std::pair<ObjectPtr, AttributePtr> > aList = theRefAttrList->list();
423 bool isAdded = false;
424 std::list<std::pair<ObjectPtr, AttributePtr> >::const_iterator anIt = aList.begin();
425 for (; anIt != aList.end(); ++anIt) {
427 myDumpBuffer << ", ";
431 *this << anIt->first;
432 else if (anIt->second)
433 * this << anIt->second;
439 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
440 const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList)
442 static const int aThreshold = 2;
443 // if number of elements in the list if greater than a threshold,
444 // dump it in a separate line with specific name
445 std::string aDumped = myDumpBuffer.str();
446 if (aDumped.empty() || theRefList->size() <= aThreshold) {
448 std::list<ObjectPtr> aList = theRefList->list();
449 bool isAdded = false;
450 std::list<ObjectPtr>::const_iterator anIt = aList.begin();
451 for (; anIt != aList.end(); ++anIt) {
453 myDumpBuffer << ", ";
461 // clear buffer and store list "as is"
462 myDumpBuffer = std::ostringstream();
464 // save buffer and clear it again
465 std::string aDumpedList = myDumpBuffer.str();
466 myDumpBuffer = std::ostringstream();
467 // obtain name of list
468 FeaturePtr anOwner = ModelAPI_Feature::feature(theRefList->owner());
469 std::string aListName = name(anOwner) + "_objects";
470 // store all previous data
471 myDumpBuffer << aListName << " = " << aDumpedList << std::endl
472 << aDumped << aListName;
477 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
478 const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect)
480 myDumpBuffer << "model.selection(";
482 if(!theAttrSelect->isInitialized()) {
487 GeomShapePtr aShape = theAttrSelect->value();
489 aShape = theAttrSelect->context()->shape();
497 myDumpBuffer << "\"" << aShape->shapeTypeStr() << "\", \"" << theAttrSelect->namingName() << "\")";
501 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
502 const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList)
507 std::string aShapeTypeStr;
509 bool isAdded = false;
511 for(int anIndex = 0; anIndex < theAttrSelList->size(); ++anIndex) {
512 AttributeSelectionPtr anAttribute = theAttrSelList->value(anIndex);
513 aShape = anAttribute->value();
515 aShape = anAttribute->context()->shape();
523 myDumpBuffer << ", ";
527 myDumpBuffer << "model.selection(\"" << aShape->shapeTypeStr() << "\", \"" << anAttribute->namingName() << "\")";
536 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
537 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&))
539 theDumper.myDumpBuffer << theEndl;
540 theDumper.dumpEntitySetName();
542 // store all not-dumped entities first
543 std::set<EntityPtr> aNotDumped = theDumper.myNotDumpedEntities;
544 std::string aBufCopy = theDumper.myDumpBuffer.str();
545 theDumper.clear(true);
546 std::set<EntityPtr>::const_iterator anIt = aNotDumped.begin();
547 for (; anIt != aNotDumped.end(); ++anIt) {
548 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
549 theDumper.dumpFeature(aFeature, true);
552 // then store currently dumped string
553 theDumper.myFullDump << aBufCopy;