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()
59 myDumpBuffer = std::ostringstream();
60 myDumpBuffer << std::setprecision(16);
63 const std::string& ModelHighAPI_Dumper::name(const EntityPtr& theEntity)
65 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
66 if (aFound != myNames.end())
67 return aFound->second.first;
69 // entity is not found, store it
71 bool isNameDefined = false;
72 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
74 aName = aFeature->name();
75 isNameDefined = !aName.empty();
78 static long anIndex = 0;
79 // set default name: feature ID + index
80 std::ostringstream aConverter;
81 aConverter << aFeature->getKind() << "_" << ++anIndex;
82 aName = aConverter.str();
83 std::transform(aName.begin(), aName.end(), aName.begin(), ::tolower);
87 myNames[theEntity] = std::pair<std::string, bool>(aName, isNameDefined);
88 return myNames[theEntity].first;
91 const std::string& ModelHighAPI_Dumper::parentName(const FeaturePtr& theEntity)
93 const std::set<AttributePtr>& aRefs = theEntity->data()->refsToMe();
94 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
95 for (; aRefIt != aRefs.end(); ++aRefIt) {
96 CompositeFeaturePtr anOwner = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(
97 ModelAPI_Feature::feature((*aRefIt)->owner()));
102 static const std::string DUMMY;
106 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc,
107 const std::string& theFileName)
109 // dump top level document feature
110 static const std::string aDocName("partSet");
111 myNames[theDoc] = std::pair<std::string, bool>(aDocName, false);
112 *this << aDocName << " = model.moduleDocument()" << std::endl;
114 // dump subfeatures and store result to file
115 return process(theDoc) && exportTo(theFileName);
118 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc)
122 std::list<FeaturePtr> aFeatures = theDoc->allFeatures();
123 std::list<FeaturePtr>::const_iterator aFeatIt = aFeatures.begin();
124 for (; aFeatIt != aFeatures.end(); ++aFeatIt) {
125 dumpFeature(*aFeatIt);
126 // iteratively process composite features
127 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFeatIt);
131 // sub-part is processed independently, because it provides separate document
132 if ((*aFeatIt)->getKind() == PartSetPlugin_Part::ID()) {
133 ResultPartPtr aPartResult =
134 std::dynamic_pointer_cast<ModelAPI_ResultPart>((*aFeatIt)->lastResult());
137 DocumentPtr aSubDoc = aPartResult->partDoc();
138 // set name of document equal to part name
139 myNames[aSubDoc] = myNames[*aFeatIt];
141 isOk = process(aSubDoc) && isOk;
143 isOk = process(aCompFeat) && isOk;
148 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite)
150 // dump all sub-features;
151 int aNbSubs = theComposite->numberOfSubs();
152 for (int anIndex = 0; anIndex < aNbSubs; ++anIndex) {
153 FeaturePtr aFeature = theComposite->subFeature(anIndex);
154 dumpFeature(aFeature, true);
156 // dump command to update model
157 myDumpBuffer << "model.do()" << std::endl;
161 bool ModelHighAPI_Dumper::exportTo(const std::string& theFileName)
164 OSD_OpenStream(aFile, theFileName.c_str(), std::ofstream::out);
165 if (!aFile.is_open())
169 for (ModulesMap::const_iterator aModIt = myModules.begin();
170 aModIt != myModules.end(); ++aModIt) {
171 aFile << "from " << aModIt->first << " import ";
172 if (aModIt->second.empty() ||
173 aModIt->second.find(std::string()) != aModIt->second.end())
174 aFile << "*"; // import whole module
176 // import specific features
177 std::set<std::string>::const_iterator anObjIt = aModIt->second.begin();
179 for (++anObjIt; anObjIt != aModIt->second.end(); ++anObjIt)
180 aFile << ", " << *anObjIt;
184 if (!myModules.empty())
187 aFile << "import model" << std::endl << std::endl;
188 aFile << "model.begin()" << std::endl;
190 // dump collected data
191 aFile << myDumpBuffer.str();
194 aFile << "model.end()" << std::endl;
202 void ModelHighAPI_Dumper::importModule(const std::string& theModuleName,
203 const std::string& theObject)
205 myModules[theModuleName].insert(theObject);
208 void ModelHighAPI_Dumper::dumpEntitySetName()
210 if (!myLastEntityWithName)
213 #ifdef DUMP_USER_DEFINED_NAMES
214 const std::string& aName = name(myLastEntityWithName);
215 myDumpBuffer << aName;
216 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(myLastEntityWithName);
218 myDumpBuffer << ".feature()";
219 myDumpBuffer << ".data().setName(\"" << aName << "\")" << std::endl;
221 myLastEntityWithName = EntityPtr();
224 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char theChar)
226 myDumpBuffer << theChar;
230 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char* theString)
232 myDumpBuffer << theString;
236 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::string& theString)
238 myDumpBuffer << theString;
242 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const int theValue)
244 myDumpBuffer << theValue;
248 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const double theValue)
250 myDumpBuffer << theValue;
254 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
256 importModule("GeomAPI", "GeomAPI_Pnt");
257 myDumpBuffer << "GeomAPI_Pnt(" << thePoint->x() << ", "
258 << thePoint->y() << ", " << thePoint->z() << ")";
262 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir)
264 importModule("GeomAPI", "GeomAPI_Dir");
265 myDumpBuffer << "GeomAPI_Dir(" << theDir->x() << ", "
266 << theDir->y() << ", " << theDir->z() << ")";
270 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
271 const std::shared_ptr<GeomDataAPI_Dir>& theDir)
273 myDumpBuffer << theDir->x() << ", " << theDir->y() << ", " << theDir->z();
277 static void dumpArray(std::ostringstream& theOutput, int theSize,
278 double* theValues, std::string* theTexts)
280 for (int i = 0; i < theSize; ++i) {
283 if (theTexts[i].empty())
284 theOutput << theValues[i];
286 theOutput << "\"" << theTexts[i] << "\"";
290 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
291 const std::shared_ptr<GeomDataAPI_Point>& thePoint)
293 static const int aSize = 3;
294 double aValues[aSize] = {thePoint->x(), thePoint->y(), thePoint->z()};
295 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY(), thePoint->textZ()};
296 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
300 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
301 const std::shared_ptr<GeomDataAPI_Point2D>& thePoint)
303 static const int aSize = 2;
304 double aValues[aSize] = {thePoint->x(), thePoint->y()};
305 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY()};
306 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
310 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
311 const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool)
313 myDumpBuffer << (theAttrBool->value() ? "True" : "False");
317 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
318 const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt)
320 std::string aText = theAttrInt->text();
322 myDumpBuffer << theAttrInt->value();
324 myDumpBuffer << "\"" << aText << "\"";
328 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
329 const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal)
331 std::string aText = theAttrReal->text();
333 myDumpBuffer << theAttrReal->value();
335 myDumpBuffer << "\"" << aText << "\"";
339 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
340 const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr)
342 myDumpBuffer << "\"" << theAttrStr->value() << "\"";
346 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const EntityPtr& theEntity)
348 myDumpBuffer << name(theEntity);
349 if (myNames[theEntity].second)
350 myLastEntityWithName = theEntity;
354 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
355 const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr)
357 if (theRefAttr->isObject())
358 myDumpBuffer << name(theRefAttr->object());
360 AttributePtr anAttr = theRefAttr->attr();
361 FeaturePtr anOwner = ModelAPI_Feature::feature(anAttr->owner());
362 myDumpBuffer << name(anOwner) << "." << attributeGetter(anOwner, anAttr->id()) << "()";
367 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
368 const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect)
370 myDumpBuffer << "\"" << theAttrSelect->namingName() << "\"";
376 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
377 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&))
379 theDumper.myDumpBuffer << theEndl;
380 theDumper.dumpEntitySetName();