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_AttributeSelection.h>
22 #include <ModelAPI_AttributeString.h>
23 #include <ModelAPI_CompositeFeature.h>
24 #include <ModelAPI_Document.h>
25 #include <ModelAPI_Entity.h>
26 #include <ModelAPI_Feature.h>
27 #include <ModelAPI_Result.h>
28 #include <ModelAPI_ResultPart.h>
30 #include <PartSetPlugin_Part.h>
32 #include <OSD_OpenFile.hxx>
37 //#define DUMP_USER_DEFINED_NAMES
39 ModelHighAPI_Dumper* ModelHighAPI_Dumper::mySelf = 0;
41 ModelHighAPI_Dumper::ModelHighAPI_Dumper()
46 void ModelHighAPI_Dumper::setInstance(ModelHighAPI_Dumper* theDumper)
52 ModelHighAPI_Dumper* ModelHighAPI_Dumper::getInstance()
57 void ModelHighAPI_Dumper::clear(bool bufferOnly)
59 myDumpBuffer = std::ostringstream();
60 myDumpBuffer << std::setprecision(16);
65 myFullDump = std::ostringstream();
66 myFullDump << std::setprecision(16);
70 myLastEntityWithName = EntityPtr();
74 void ModelHighAPI_Dumper::clearNotDumped()
76 myNotDumpedEntities.clear();
79 const std::string& ModelHighAPI_Dumper::name(const EntityPtr& theEntity)
81 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
82 if (aFound != myNames.end())
83 return aFound->second.first;
85 // entity is not found, store it
87 bool isNameDefined = false;
88 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
90 aName = aFeature->name();
91 isNameDefined = !aName.empty();
94 static long anIndex = 0;
95 // set default name: feature ID + index
96 std::ostringstream aConverter;
97 aConverter << aFeature->getKind() << "_" << ++anIndex;
98 aName = aConverter.str();
99 std::transform(aName.begin(), aName.end(), aName.begin(), ::tolower);
103 myNames[theEntity] = std::pair<std::string, bool>(aName, isNameDefined);
104 myNotDumpedEntities.insert(theEntity);
105 return myNames[theEntity].first;
108 const std::string& ModelHighAPI_Dumper::parentName(const FeaturePtr& theEntity)
110 const std::set<AttributePtr>& aRefs = theEntity->data()->refsToMe();
111 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
112 for (; aRefIt != aRefs.end(); ++aRefIt) {
113 CompositeFeaturePtr anOwner = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(
114 ModelAPI_Feature::feature((*aRefIt)->owner()));
116 return name(anOwner);
119 static const std::string DUMMY;
123 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc,
124 const std::string& theFileName)
126 // dump top level document feature
127 static const std::string aDocName("partSet");
128 myNames[theDoc] = std::pair<std::string, bool>(aDocName, false);
129 *this << aDocName << " = model.moduleDocument()" << std::endl;
131 // dump subfeatures and store result to file
132 return process(theDoc) && exportTo(theFileName);
135 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc)
139 std::list<FeaturePtr> aFeatures = theDoc->allFeatures();
140 std::list<FeaturePtr>::const_iterator aFeatIt = aFeatures.begin();
141 for (; aFeatIt != aFeatures.end(); ++aFeatIt) {
142 if (!isDumped(*aFeatIt))
143 dumpFeature(*aFeatIt);
145 // iteratively process composite features
146 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFeatIt);
150 // sub-part is processed independently, because it provides separate document
151 if ((*aFeatIt)->getKind() == PartSetPlugin_Part::ID()) {
152 ResultPartPtr aPartResult =
153 std::dynamic_pointer_cast<ModelAPI_ResultPart>((*aFeatIt)->lastResult());
156 DocumentPtr aSubDoc = aPartResult->partDoc();
157 // set name of document equal to part name
158 myNames[aSubDoc] = myNames[*aFeatIt];
160 isOk = process(aSubDoc) && isOk;
162 isOk = process(aCompFeat) && isOk;
167 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite)
169 // dump all sub-features;
170 int aNbSubs = theComposite->numberOfSubs();
171 for (int anIndex = 0; anIndex < aNbSubs; ++anIndex) {
172 FeaturePtr aFeature = theComposite->subFeature(anIndex);
173 if (!isDumped(aFeature))
174 dumpFeature(aFeature, true);
176 // dump command to update model
177 myDumpBuffer << "model.do()" << std::endl;
181 bool ModelHighAPI_Dumper::exportTo(const std::string& theFileName)
184 OSD_OpenStream(aFile, theFileName.c_str(), std::ofstream::out);
185 if (!aFile.is_open())
189 for (ModulesMap::const_iterator aModIt = myModules.begin();
190 aModIt != myModules.end(); ++aModIt) {
191 aFile << "from " << aModIt->first << " import ";
192 if (aModIt->second.empty() ||
193 aModIt->second.find(std::string()) != aModIt->second.end())
194 aFile << "*"; // import whole module
196 // import specific features
197 std::set<std::string>::const_iterator anObjIt = aModIt->second.begin();
199 for (++anObjIt; anObjIt != aModIt->second.end(); ++anObjIt)
200 aFile << ", " << *anObjIt;
204 if (!myModules.empty())
207 aFile << "import model" << std::endl << std::endl;
208 aFile << "model.begin()" << std::endl;
210 // dump collected data
211 aFile << myFullDump.str();
214 aFile << "model.end()" << std::endl;
222 void ModelHighAPI_Dumper::importModule(const std::string& theModuleName,
223 const std::string& theObject)
225 myModules[theModuleName].insert(theObject);
228 void ModelHighAPI_Dumper::dumpEntitySetName()
230 if (!myLastEntityWithName)
233 #ifdef DUMP_USER_DEFINED_NAMES
234 const std::string& aName = name(myLastEntityWithName);
235 myDumpBuffer << aName;
236 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(myLastEntityWithName);
238 myDumpBuffer << ".feature()";
239 myDumpBuffer << ".data().setName(\"" << aName << "\")" << std::endl;
241 myLastEntityWithName = EntityPtr();
244 bool ModelHighAPI_Dumper::isDumped(const EntityPtr& theEntity) const
246 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
247 return aFound != myNames.end();
250 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char theChar)
252 myDumpBuffer << theChar;
256 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char* theString)
258 myDumpBuffer << theString;
262 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::string& theString)
264 myDumpBuffer << theString;
268 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const int theValue)
270 myDumpBuffer << theValue;
274 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const double theValue)
276 myDumpBuffer << theValue;
280 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
282 importModule("GeomAPI", "GeomAPI_Pnt");
283 myDumpBuffer << "GeomAPI_Pnt(" << thePoint->x() << ", "
284 << thePoint->y() << ", " << thePoint->z() << ")";
288 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir)
290 importModule("GeomAPI", "GeomAPI_Dir");
291 myDumpBuffer << "GeomAPI_Dir(" << theDir->x() << ", "
292 << theDir->y() << ", " << theDir->z() << ")";
296 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
297 const std::shared_ptr<GeomDataAPI_Dir>& theDir)
299 myDumpBuffer << theDir->x() << ", " << theDir->y() << ", " << theDir->z();
303 static void dumpArray(std::ostringstream& theOutput, int theSize,
304 double* theValues, std::string* theTexts)
306 for (int i = 0; i < theSize; ++i) {
309 if (theTexts[i].empty())
310 theOutput << theValues[i];
312 theOutput << "\"" << theTexts[i] << "\"";
316 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
317 const std::shared_ptr<GeomDataAPI_Point>& thePoint)
319 static const int aSize = 3;
320 double aValues[aSize] = {thePoint->x(), thePoint->y(), thePoint->z()};
321 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY(), thePoint->textZ()};
322 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
326 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
327 const std::shared_ptr<GeomDataAPI_Point2D>& thePoint)
329 static const int aSize = 2;
330 double aValues[aSize] = {thePoint->x(), thePoint->y()};
331 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY()};
332 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
336 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
337 const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool)
339 myDumpBuffer << (theAttrBool->value() ? "True" : "False");
343 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
344 const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt)
346 std::string aText = theAttrInt->text();
348 myDumpBuffer << theAttrInt->value();
350 myDumpBuffer << "\"" << aText << "\"";
354 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
355 const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal)
357 std::string aText = theAttrReal->text();
359 myDumpBuffer << theAttrReal->value();
361 myDumpBuffer << "\"" << aText << "\"";
365 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
366 const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr)
368 myDumpBuffer << "\"" << theAttrStr->value() << "\"";
372 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const EntityPtr& theEntity)
374 myDumpBuffer << name(theEntity);
375 if (myNames[theEntity].second)
376 myLastEntityWithName = theEntity;
377 myNotDumpedEntities.erase(theEntity);
381 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
382 const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr)
384 if (theRefAttr->isObject()) {
385 FeaturePtr aFeature = ModelAPI_Feature::feature(theRefAttr->object());
386 myDumpBuffer << name(aFeature);
388 AttributePtr anAttr = theRefAttr->attr();
389 FeaturePtr anOwner = ModelAPI_Feature::feature(anAttr->owner());
390 myDumpBuffer << name(anOwner) << "." << attributeGetter(anOwner, anAttr->id()) << "()";
395 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
396 const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect)
398 GeomShapePtr aShape = theAttrSelect->value();
400 aShape = theAttrSelect->context()->shape();
407 std::string aShapeTypeStr = aShape->shapeTypeStr();
409 myDumpBuffer << "model.selection(\"" << aShapeTypeStr << "\", \"" << theAttrSelect->namingName() << "\")";
415 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
416 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&))
418 theDumper.myDumpBuffer << theEndl;
419 theDumper.dumpEntitySetName();
421 // store all not-dumped entities first
422 std::set<EntityPtr> aNotDumped = theDumper.myNotDumpedEntities;
423 std::string aBufCopy = theDumper.myDumpBuffer.str();
424 theDumper.clear(true);
425 std::set<EntityPtr>::const_iterator anIt = aNotDumped.begin();
426 for (; anIt != aNotDumped.end(); ++anIt) {
427 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
428 theDumper.dumpFeature(aFeature, true);
431 // then store currently dumped string
432 theDumper.myFullDump << aBufCopy;