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_AttributeSelectionList.h>
23 #include <ModelAPI_AttributeString.h>
24 #include <ModelAPI_CompositeFeature.h>
25 #include <ModelAPI_Document.h>
26 #include <ModelAPI_Entity.h>
27 #include <ModelAPI_Feature.h>
28 #include <ModelAPI_Result.h>
29 #include <ModelAPI_ResultPart.h>
31 #include <PartSetPlugin_Part.h>
33 #include <OSD_OpenFile.hxx>
38 //#define DUMP_USER_DEFINED_NAMES
40 ModelHighAPI_Dumper* ModelHighAPI_Dumper::mySelf = 0;
42 ModelHighAPI_Dumper::ModelHighAPI_Dumper()
47 void ModelHighAPI_Dumper::setInstance(ModelHighAPI_Dumper* theDumper)
53 ModelHighAPI_Dumper* ModelHighAPI_Dumper::getInstance()
58 void ModelHighAPI_Dumper::clear(bool bufferOnly)
60 myDumpBuffer = std::ostringstream();
61 myDumpBuffer << std::setprecision(16);
66 myFullDump = std::ostringstream();
67 myFullDump << std::setprecision(16);
71 myLastEntityWithName = EntityPtr();
75 void ModelHighAPI_Dumper::clearNotDumped()
77 myNotDumpedEntities.clear();
80 const std::string& ModelHighAPI_Dumper::name(const EntityPtr& theEntity)
82 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
83 if (aFound != myNames.end())
84 return aFound->second.first;
86 // entity is not found, store it
88 bool isNameDefined = false;
89 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
91 aName = aFeature->name();
92 isNameDefined = !aName.empty();
95 static long anIndex = 0;
96 // set default name: feature ID + index
97 std::ostringstream aConverter;
98 aConverter << aFeature->getKind() << "_" << ++anIndex;
99 aName = aConverter.str();
100 std::transform(aName.begin(), aName.end(), aName.begin(), ::tolower);
104 myNames[theEntity] = std::pair<std::string, bool>(aName, isNameDefined);
105 myNotDumpedEntities.insert(theEntity);
106 return myNames[theEntity].first;
109 const std::string& ModelHighAPI_Dumper::parentName(const FeaturePtr& theEntity)
111 const std::set<AttributePtr>& aRefs = theEntity->data()->refsToMe();
112 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
113 for (; aRefIt != aRefs.end(); ++aRefIt) {
114 CompositeFeaturePtr anOwner = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(
115 ModelAPI_Feature::feature((*aRefIt)->owner()));
117 return name(anOwner);
120 static const std::string DUMMY;
124 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc,
125 const std::string& theFileName)
127 // dump top level document feature
128 static const std::string aDocName("partSet");
129 myNames[theDoc] = std::pair<std::string, bool>(aDocName, false);
130 *this << aDocName << " = model.moduleDocument()" << std::endl;
132 // dump subfeatures and store result to file
133 return process(theDoc) && exportTo(theFileName);
136 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc)
140 std::list<FeaturePtr> aFeatures = theDoc->allFeatures();
141 std::list<FeaturePtr>::const_iterator aFeatIt = aFeatures.begin();
142 for (; aFeatIt != aFeatures.end(); ++aFeatIt) {
143 if (!isDumped(*aFeatIt))
144 dumpFeature(*aFeatIt);
146 // iteratively process composite features
147 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFeatIt);
151 // sub-part is processed independently, because it provides separate document
152 if ((*aFeatIt)->getKind() == PartSetPlugin_Part::ID()) {
153 ResultPartPtr aPartResult =
154 std::dynamic_pointer_cast<ModelAPI_ResultPart>((*aFeatIt)->lastResult());
157 DocumentPtr aSubDoc = aPartResult->partDoc();
158 // set name of document equal to part name
159 myNames[aSubDoc] = myNames[*aFeatIt];
161 isOk = process(aSubDoc) && isOk;
163 isOk = process(aCompFeat) && isOk;
168 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite)
170 // dump all sub-features;
171 int aNbSubs = theComposite->numberOfSubs();
172 for (int anIndex = 0; anIndex < aNbSubs; ++anIndex) {
173 FeaturePtr aFeature = theComposite->subFeature(anIndex);
174 if (!isDumped(aFeature))
175 dumpFeature(aFeature, true);
177 // dump command to update model
178 myDumpBuffer << "model.do()" << std::endl;
182 bool ModelHighAPI_Dumper::exportTo(const std::string& theFileName)
185 OSD_OpenStream(aFile, theFileName.c_str(), std::ofstream::out);
186 if (!aFile.is_open())
190 for (ModulesMap::const_iterator aModIt = myModules.begin();
191 aModIt != myModules.end(); ++aModIt) {
192 aFile << "from " << aModIt->first << " import ";
193 if (aModIt->second.empty() ||
194 aModIt->second.find(std::string()) != aModIt->second.end())
195 aFile << "*"; // import whole module
197 // import specific features
198 std::set<std::string>::const_iterator anObjIt = aModIt->second.begin();
200 for (++anObjIt; anObjIt != aModIt->second.end(); ++anObjIt)
201 aFile << ", " << *anObjIt;
205 if (!myModules.empty())
208 aFile << "import model" << std::endl << std::endl;
209 aFile << "model.begin()" << std::endl;
211 // dump collected data
212 aFile << myFullDump.str();
215 aFile << "model.end()" << std::endl;
223 void ModelHighAPI_Dumper::importModule(const std::string& theModuleName,
224 const std::string& theObject)
226 myModules[theModuleName].insert(theObject);
229 void ModelHighAPI_Dumper::dumpEntitySetName()
231 if (!myLastEntityWithName)
234 #ifdef DUMP_USER_DEFINED_NAMES
235 const std::string& aName = name(myLastEntityWithName);
236 myDumpBuffer << aName;
237 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(myLastEntityWithName);
239 myDumpBuffer << ".feature()";
240 myDumpBuffer << ".data().setName(\"" << aName << "\")" << std::endl;
242 myLastEntityWithName = EntityPtr();
245 bool ModelHighAPI_Dumper::isDumped(const EntityPtr& theEntity) const
247 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
248 return aFound != myNames.end();
251 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char theChar)
253 myDumpBuffer << theChar;
257 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char* theString)
259 myDumpBuffer << theString;
263 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::string& theString)
265 myDumpBuffer << theString;
269 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const int theValue)
271 myDumpBuffer << theValue;
275 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const double theValue)
277 myDumpBuffer << theValue;
281 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
283 importModule("GeomAPI", "GeomAPI_Pnt");
284 myDumpBuffer << "GeomAPI_Pnt(" << thePoint->x() << ", "
285 << thePoint->y() << ", " << thePoint->z() << ")";
289 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir)
291 importModule("GeomAPI", "GeomAPI_Dir");
292 myDumpBuffer << "GeomAPI_Dir(" << theDir->x() << ", "
293 << theDir->y() << ", " << theDir->z() << ")";
297 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
298 const std::shared_ptr<GeomDataAPI_Dir>& theDir)
300 myDumpBuffer << theDir->x() << ", " << theDir->y() << ", " << theDir->z();
304 static void dumpArray(std::ostringstream& theOutput, int theSize,
305 double* theValues, std::string* theTexts)
307 for (int i = 0; i < theSize; ++i) {
310 if (theTexts[i].empty())
311 theOutput << theValues[i];
313 theOutput << "\"" << theTexts[i] << "\"";
317 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
318 const std::shared_ptr<GeomDataAPI_Point>& thePoint)
320 static const int aSize = 3;
321 double aValues[aSize] = {thePoint->x(), thePoint->y(), thePoint->z()};
322 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY(), thePoint->textZ()};
323 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
327 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
328 const std::shared_ptr<GeomDataAPI_Point2D>& thePoint)
330 static const int aSize = 2;
331 double aValues[aSize] = {thePoint->x(), thePoint->y()};
332 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY()};
333 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
337 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
338 const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool)
340 myDumpBuffer << (theAttrBool->value() ? "True" : "False");
344 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
345 const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt)
347 std::string aText = theAttrInt->text();
349 myDumpBuffer << theAttrInt->value();
351 myDumpBuffer << "\"" << aText << "\"";
355 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
356 const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal)
358 std::string aText = theAttrReal->text();
360 myDumpBuffer << theAttrReal->value();
362 myDumpBuffer << "\"" << aText << "\"";
366 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
367 const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr)
369 myDumpBuffer << "\"" << theAttrStr->value() << "\"";
373 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const EntityPtr& theEntity)
375 myDumpBuffer << name(theEntity);
376 if (myNames[theEntity].second)
377 myLastEntityWithName = theEntity;
378 myNotDumpedEntities.erase(theEntity);
382 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
383 const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr)
385 if (theRefAttr->isObject()) {
386 FeaturePtr aFeature = ModelAPI_Feature::feature(theRefAttr->object());
387 myDumpBuffer << name(aFeature);
389 AttributePtr anAttr = theRefAttr->attr();
390 FeaturePtr anOwner = ModelAPI_Feature::feature(anAttr->owner());
391 myDumpBuffer << name(anOwner) << "." << attributeGetter(anOwner, anAttr->id()) << "()";
396 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
397 const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect)
399 myDumpBuffer << "model.selection(";
401 if(!theAttrSelect->isInitialized()) {
406 GeomShapePtr aShape = theAttrSelect->value();
408 aShape = theAttrSelect->context()->shape();
416 myDumpBuffer << "\"" << aShape->shapeTypeStr() << "\", \"" << theAttrSelect->namingName() << "\")";
420 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
421 const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList)
426 std::string aShapeTypeStr;
428 bool isAdded = false;
430 for(int anIndex = 0; anIndex < theAttrSelList->size(); ++anIndex) {
431 AttributeSelectionPtr anAttribute = theAttrSelList->value(anIndex);
432 aShape = anAttribute->value();
434 aShape = anAttribute->context()->shape();
442 myDumpBuffer << ", ";
446 myDumpBuffer << "model.selection(\"" << aShape->shapeTypeStr() << "\", \"" << anAttribute->namingName() << "\")";
455 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
456 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&))
458 theDumper.myDumpBuffer << theEndl;
459 theDumper.dumpEntitySetName();
461 // store all not-dumped entities first
462 std::set<EntityPtr> aNotDumped = theDumper.myNotDumpedEntities;
463 std::string aBufCopy = theDumper.myDumpBuffer.str();
464 theDumper.clear(true);
465 std::set<EntityPtr>::const_iterator anIt = aNotDumped.begin();
466 for (; anIt != aNotDumped.end(); ++anIt) {
467 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
468 theDumper.dumpFeature(aFeature, true);
471 // then store currently dumped string
472 theDumper.myFullDump << aBufCopy;