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)
121 CompositeFeaturePtr aLastComposite;
123 std::list<FeaturePtr> aFeatures = theDoc->allFeatures();
124 std::list<FeaturePtr>::const_iterator aFeatIt = aFeatures.begin();
125 for (; aFeatIt != aFeatures.end(); ++aFeatIt) {
126 // dump feature if and only if it is not a sub-feature of last composite feature
127 // (all subs of composite are dumped in special method)
128 if (!aLastComposite || !aLastComposite->isSub(*aFeatIt))
129 dumpFeature(*aFeatIt);
131 // iteratively process composite features
132 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFeatIt);
136 // sub-part is processed independently, because it provides separate document
137 if ((*aFeatIt)->getKind() == PartSetPlugin_Part::ID()) {
138 ResultPartPtr aPartResult =
139 std::dynamic_pointer_cast<ModelAPI_ResultPart>((*aFeatIt)->lastResult());
142 DocumentPtr aSubDoc = aPartResult->partDoc();
143 // set name of document equal to part name
144 myNames[aSubDoc] = myNames[*aFeatIt];
146 isOk = process(aSubDoc) && isOk;
148 isOk = process(aCompFeat) && isOk;
149 aLastComposite = aCompFeat;
155 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite)
157 // dump all sub-features;
158 int aNbSubs = theComposite->numberOfSubs();
159 for (int anIndex = 0; anIndex < aNbSubs; ++anIndex) {
160 FeaturePtr aFeature = theComposite->subFeature(anIndex);
161 dumpFeature(aFeature, true);
163 // dump command to update model
164 myDumpBuffer << "model.do()" << std::endl;
168 bool ModelHighAPI_Dumper::exportTo(const std::string& theFileName)
171 OSD_OpenStream(aFile, theFileName.c_str(), std::ofstream::out);
172 if (!aFile.is_open())
176 for (ModulesMap::const_iterator aModIt = myModules.begin();
177 aModIt != myModules.end(); ++aModIt) {
178 aFile << "from " << aModIt->first << " import ";
179 if (aModIt->second.empty() ||
180 aModIt->second.find(std::string()) != aModIt->second.end())
181 aFile << "*"; // import whole module
183 // import specific features
184 std::set<std::string>::const_iterator anObjIt = aModIt->second.begin();
186 for (++anObjIt; anObjIt != aModIt->second.end(); ++anObjIt)
187 aFile << ", " << *anObjIt;
191 if (!myModules.empty())
194 aFile << "import model" << std::endl << std::endl;
195 aFile << "model.begin()" << std::endl;
197 // dump collected data
198 aFile << myDumpBuffer.str();
201 aFile << "model.end()" << std::endl;
209 void ModelHighAPI_Dumper::importModule(const std::string& theModuleName,
210 const std::string& theObject)
212 myModules[theModuleName].insert(theObject);
215 void ModelHighAPI_Dumper::dumpEntitySetName()
217 if (!myLastEntityWithName)
220 #ifdef DUMP_USER_DEFINED_NAMES
221 const std::string& aName = name(myLastEntityWithName);
222 myDumpBuffer << aName;
223 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(myLastEntityWithName);
225 myDumpBuffer << ".feature()";
226 myDumpBuffer << ".data().setName(\"" << aName << "\")" << std::endl;
228 myLastEntityWithName = EntityPtr();
231 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char theChar)
233 myDumpBuffer << theChar;
237 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char* theString)
239 myDumpBuffer << theString;
243 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::string& theString)
245 myDumpBuffer << theString;
249 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const int theValue)
251 myDumpBuffer << theValue;
255 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const double theValue)
257 myDumpBuffer << theValue;
261 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
263 importModule("GeomAPI", "GeomAPI_Pnt");
264 myDumpBuffer << "GeomAPI_Pnt(" << thePoint->x() << ", "
265 << thePoint->y() << ", " << thePoint->z() << ")";
269 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir)
271 importModule("GeomAPI", "GeomAPI_Dir");
272 myDumpBuffer << "GeomAPI_Dir(" << theDir->x() << ", "
273 << theDir->y() << ", " << theDir->z() << ")";
277 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
278 const std::shared_ptr<GeomDataAPI_Dir>& theDir)
280 myDumpBuffer << theDir->x() << ", " << theDir->y() << ", " << theDir->z();
284 static void dumpArray(std::ostringstream& theOutput, int theSize,
285 double* theValues, std::string* theTexts)
287 for (int i = 0; i < theSize; ++i) {
290 if (theTexts[i].empty())
291 theOutput << theValues[i];
293 theOutput << "\"" << theTexts[i] << "\"";
297 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
298 const std::shared_ptr<GeomDataAPI_Point>& thePoint)
300 static const int aSize = 3;
301 double aValues[aSize] = {thePoint->x(), thePoint->y(), thePoint->z()};
302 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY(), thePoint->textZ()};
303 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
307 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
308 const std::shared_ptr<GeomDataAPI_Point2D>& thePoint)
310 static const int aSize = 2;
311 double aValues[aSize] = {thePoint->x(), thePoint->y()};
312 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY()};
313 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
317 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
318 const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool)
320 myDumpBuffer << (theAttrBool->value() ? "True" : "False");
324 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
325 const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt)
327 std::string aText = theAttrInt->text();
329 myDumpBuffer << theAttrInt->value();
331 myDumpBuffer << "\"" << aText << "\"";
335 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
336 const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal)
338 std::string aText = theAttrReal->text();
340 myDumpBuffer << theAttrReal->value();
342 myDumpBuffer << "\"" << aText << "\"";
346 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
347 const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr)
349 myDumpBuffer << "\"" << theAttrStr->value() << "\"";
353 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const EntityPtr& theEntity)
355 myDumpBuffer << name(theEntity);
356 if (myNames[theEntity].second)
357 myLastEntityWithName = theEntity;
361 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
362 const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr)
364 if (theRefAttr->isObject()) {
365 FeaturePtr aFeature = ModelAPI_Feature::feature(theRefAttr->object());
366 myDumpBuffer << name(aFeature);
368 AttributePtr anAttr = theRefAttr->attr();
369 FeaturePtr anOwner = ModelAPI_Feature::feature(anAttr->owner());
370 myDumpBuffer << name(anOwner) << "." << attributeGetter(anOwner, anAttr->id()) << "()";
375 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
376 const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect)
378 GeomShapePtr aShape = theAttrSelect->value();
380 aShape = theAttrSelect->context()->shape();
387 std::string aShapeTypeStr = aShape->shapeTypeStr();
389 myDumpBuffer << "model.selection(\"" << aShapeTypeStr << "\", \"" << theAttrSelect->namingName() << "\")";
395 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
396 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&))
398 theDumper.myDumpBuffer << theEndl;
399 theDumper.dumpEntitySetName();