1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
20 #include "ModelHighAPI_Dumper.h"
22 #include <Config_PropManager.h>
24 #include <GeomAPI_Pnt.h>
25 #include <GeomAPI_Dir.h>
26 #include <GeomAPI_ShapeExplorer.h>
28 #include <GeomDataAPI_Dir.h>
29 #include <GeomDataAPI_Point.h>
30 #include <GeomDataAPI_Point2D.h>
32 #include <ModelAPI_AttributeBoolean.h>
33 #include <ModelAPI_AttributeDouble.h>
34 #include <ModelAPI_AttributeIntArray.h>
35 #include <ModelAPI_AttributeInteger.h>
36 #include <ModelAPI_AttributeRefAttr.h>
37 #include <ModelAPI_AttributeRefAttrList.h>
38 #include <ModelAPI_AttributeReference.h>
39 #include <ModelAPI_AttributeRefList.h>
40 #include <ModelAPI_AttributeSelection.h>
41 #include <ModelAPI_AttributeSelectionList.h>
42 #include <ModelAPI_AttributeString.h>
43 #include <ModelAPI_AttributeStringArray.h>
44 #include <ModelAPI_CompositeFeature.h>
45 #include <ModelAPI_Document.h>
46 #include <ModelAPI_Entity.h>
47 #include <ModelAPI_Feature.h>
48 #include <ModelAPI_Result.h>
49 #include <ModelAPI_ResultBody.h>
50 #include <ModelAPI_ResultCompSolid.h>
51 #include <ModelAPI_ResultConstruction.h>
52 #include <ModelAPI_ResultPart.h>
54 #include <PartSetPlugin_Part.h>
56 #include <OSD_OpenFile.hxx>
60 static int gCompositeStackDepth = 0;
62 ModelHighAPI_Dumper* ModelHighAPI_Dumper::mySelf = 0;
64 ModelHighAPI_Dumper::ModelHighAPI_Dumper()
69 void ModelHighAPI_Dumper::setInstance(ModelHighAPI_Dumper* theDumper)
75 ModelHighAPI_Dumper* ModelHighAPI_Dumper::getInstance()
80 void ModelHighAPI_Dumper::clear(bool bufferOnly)
83 myDumpBuffer << std::setprecision(16);
89 myFullDump << std::setprecision(16);
93 myFeatureCount.clear();
94 while (!myEntitiesStack.empty())
95 myEntitiesStack.pop();
99 void ModelHighAPI_Dumper::clearNotDumped()
101 myNotDumpedEntities.clear();
104 // Convert string to integer. If the string is not a number, return -1
105 static int toInt(const std::string& theString)
107 std::string::const_iterator aChar = theString.begin();
108 for (; aChar != theString.end(); ++aChar)
109 if (!std::isdigit(*aChar))
111 if (aChar != theString.end())
112 return -1; // not a number
113 return std::stoi(theString);
116 const std::string& ModelHighAPI_Dumper::name(const EntityPtr& theEntity,
117 bool theSaveNotDumped,
118 bool theUseEntityName)
120 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
121 if (aFound != myNames.end())
122 return aFound->second.myCurrentName;
124 // entity is not found, store it
126 bool isDefaultName = false;
127 std::ostringstream aDefaultName;
128 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
130 aName = aFeature->name();
131 const std::string& aKind = aFeature->getKind();
132 DocumentPtr aDoc = aFeature->document();
133 int& aNbFeatures = myFeatureCount[aDoc][aKind];
136 size_t anIndex = aName.find(aKind);
137 if (anIndex == 0 && aName[aKind.length()] == '_') { // name starts with "FeatureKind_"
138 std::string anIdStr = aName.substr(aKind.length() + 1);
139 int anId = toInt(anIdStr);
141 // Check number of already registered objects of such kind. Index of current object
142 // should be the same to identify feature's name as automatically generated.
143 if (aNbFeatures == anId) {
144 // name is not user-defined
145 isDefaultName = true;
149 // obtain default name for the feature
150 if (theUseEntityName)
151 aDefaultName << aName;
154 NbFeaturesMap::const_iterator aFIt = myFeatureCount.begin();
155 for (; aFIt != myFeatureCount.end(); ++aFIt) {
156 std::map<std::string, int>::const_iterator aFound = aFIt->second.find(aKind);
157 if (aFound != aFIt->second.end())
158 aFullIndex += aFound->second;
160 aDefaultName << aKind << "_" << aFullIndex;
164 myNames[theEntity] = EntityName(aDefaultName.str(), aName, isDefaultName);
165 if (theSaveNotDumped)
166 myNotDumpedEntities.insert(theEntity);
168 // store names of results
170 saveResultNames(aFeature);
172 return myNames[theEntity].myCurrentName;
175 const std::string& ModelHighAPI_Dumper::parentName(const FeaturePtr& theEntity)
177 const std::set<AttributePtr>& aRefs = theEntity->data()->refsToMe();
178 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
179 for (; aRefIt != aRefs.end(); ++aRefIt) {
180 CompositeFeaturePtr anOwner = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(
181 ModelAPI_Feature::feature((*aRefIt)->owner()));
183 return name(anOwner);
186 static const std::string DUMMY;
190 void ModelHighAPI_Dumper::saveResultNames(const FeaturePtr& theFeature)
192 // Default name of the feature
193 const std::string& aKind = theFeature->getKind();
194 DocumentPtr aDoc = theFeature->document();
195 int aNbFeatures = myFeatureCount[aDoc][aKind];
196 std::ostringstream aNameStream;
197 aNameStream << aKind << "_" << aNbFeatures;
198 std::string aFeatureName = aNameStream.str();
200 // Save only names of results which is not correspond to default feature name
201 const std::list<ResultPtr>& aResults = theFeature->results();
202 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
203 for (int i = 1; aResIt != aResults.end(); ++aResIt, ++i) {
204 bool isUserDefined = true;
205 std::string aResName = (*aResIt)->data()->name();
206 size_t anIndex = aResName.find(aFeatureName);
208 std::string aSuffix = aResName.substr(aFeatureName.length());
209 if (aSuffix.empty() && i == 1) // first result may not constain index in the name
210 isUserDefined = false;
212 if (aSuffix[0] == '_' && std::stoi(aSuffix.substr(1)) == i)
213 isUserDefined = false;
217 myNames[*aResIt] = EntityName(aResName,
218 (isUserDefined ? aResName : std::string()), !isUserDefined);
220 // check names of sub-results for CompSolid
221 ResultCompSolidPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aResIt);
223 int aNbSubs = aCompSolid->numberOfSubs();
224 for (int j = 0; j < aNbSubs; ++j) {
225 isUserDefined = true;
226 ResultPtr aSub = aCompSolid->subResult(j);
227 std::string aSubName = aSub->data()->name();
228 size_t anIndex = aSubName.find(aResName);
230 std::string aSuffix = aSubName.substr(aResName.length());
231 if (aSuffix.empty() && aNbSubs == 1) // first result may not constain index in the name
232 isUserDefined = false;
234 if (aSuffix[0] == '_' && std::stoi(aSuffix.substr(1)) == j + 1)
235 isUserDefined = false;
239 myNames[aSub] = EntityName(aSubName,
240 (isUserDefined ? aSubName : std::string()), !isUserDefined);
246 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc,
247 const std::string& theFileName)
249 // dump top level document feature
250 static const std::string aDocName("partSet");
251 myNames[theDoc] = EntityName(aDocName, std::string(), true);
252 *this << aDocName << " = model.moduleDocument()" << std::endl;
254 // dump subfeatures and store result to file
255 return process(theDoc) && exportTo(theFileName);
258 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc)
261 std::list<FeaturePtr> aFeatures = theDoc->allFeatures();
262 std::list<FeaturePtr>::const_iterator aFeatIt = aFeatures.begin();
263 // firstly, dump all parameters
264 for (; aFeatIt != aFeatures.end(); ++ aFeatIt)
265 dumpParameter(*aFeatIt);
266 // dump all other features
267 for (aFeatIt = aFeatures.begin(); aFeatIt != aFeatures.end(); ++aFeatIt) {
268 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFeatIt);
269 if (aCompFeat) // iteratively process composite features
270 isOk = process(aCompFeat) && isOk;
271 else if (!isDumped(*aFeatIt)) // dump common feature
272 dumpFeature(*aFeatIt);
277 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
280 // increase composite features stack
281 ++gCompositeStackDepth;
282 // dump composite itself
283 if (!isDumped(theComposite) || isForce)
284 dumpFeature(FeaturePtr(theComposite), isForce);
286 // sub-part is processed independently, because it provides separate document
287 if (theComposite->getKind() == PartSetPlugin_Part::ID()) {
288 // dump name of the part if it is different from default
289 if (!myEntitiesStack.empty())
292 // decrease composite features stack because we run into separate document
293 --gCompositeStackDepth;
295 ResultPartPtr aPartResult =
296 std::dynamic_pointer_cast<ModelAPI_ResultPart>(theComposite->lastResult());
299 DocumentPtr aSubDoc = aPartResult->partDoc();
302 // set name of document
303 const std::string& aPartName = myNames[theComposite].myCurrentName;
304 std::string aDocName = aPartName + "_doc";
305 myNames[aSubDoc] = EntityName(aDocName, std::string(), true);
307 // dump document in a separate line
308 *this << aDocName << " = " << aPartName << ".document()" << std::endl;
309 // dump features in the document
310 bool aRes = process(aSubDoc);
311 *this << "model.do()" << std::endl;
316 bool isOk = processSubs(theComposite);
317 // decrease composite features stack
318 --gCompositeStackDepth;
323 bool ModelHighAPI_Dumper::processSubs(
324 const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
328 // dump all sub-features;
329 bool isSubDumped = false;
330 int aNbSubs = theComposite->numberOfSubs();
331 for (int anIndex = 0; anIndex < aNbSubs; ++anIndex) {
332 FeaturePtr aFeature = theComposite->subFeature(anIndex);
333 if (isDumped(aFeature))
337 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
338 if (aCompFeat) // iteratively process composite features
339 isOk = process(aCompFeat) && isOk;
341 dumpFeature(aFeature, true);
344 bool isDumpSetName = !myEntitiesStack.empty() &&
345 myEntitiesStack.top().myEntity == EntityPtr(theComposite);
346 bool isForceModelDo = isSubDumped && isDumpSetName &&
347 (myEntitiesStack.top().myUserName || !myEntitiesStack.top().myResults.empty());
348 // It is necessary for the sketch to create its result when complete (command "model.do()").
349 // This option is set by flat theDumpModelDo.
350 // However, nested sketches are rebuilt by parent feature, so, they do not need
351 // explicit call of "model.do()". This will be controlled by the depth of the stack.
352 if (isForceModelDo || (theDumpModelDo && gCompositeStackDepth <= 1))
353 *this << "model.do()" << std::endl;
355 // dump "setName" for composite feature
361 void ModelHighAPI_Dumper::dumpSubFeatureNameAndColor(const std::string theSubFeatureGet,
362 const FeaturePtr& theSubFeature)
364 name(theSubFeature, false);
365 myNames[theSubFeature] = EntityName(theSubFeatureGet, theSubFeature->name(), false);
367 // store results if they have user-defined names or colors
368 std::list<ResultPtr> aResultsWithNameOrColor;
369 const std::list<ResultPtr>& aResults = theSubFeature->results();
370 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
371 for (; aResIt != aResults.end(); ++aResIt) {
372 std::string aResName = (*aResIt)->data()->name();
373 myNames[*aResIt] = EntityName(aResName, aResName, false);
374 aResultsWithNameOrColor.push_back(*aResIt);
377 // store just dumped entity to stack
378 myEntitiesStack.push(LastDumpedEntity(theSubFeature, true, aResultsWithNameOrColor));
383 bool ModelHighAPI_Dumper::exportTo(const std::string& theFileName)
386 OSD_OpenStream(aFile, theFileName.c_str(), std::ofstream::out);
387 if (!aFile.is_open())
391 for (ModulesMap::const_iterator aModIt = myModules.begin();
392 aModIt != myModules.end(); ++aModIt) {
393 aFile << "from " << aModIt->first << " import ";
394 if (aModIt->second.empty() ||
395 aModIt->second.find(std::string()) != aModIt->second.end())
396 aFile << "*"; // import whole module
398 // import specific features
399 std::set<std::string>::const_iterator anObjIt = aModIt->second.begin();
401 for (++anObjIt; anObjIt != aModIt->second.end(); ++anObjIt)
402 aFile << ", " << *anObjIt;
406 if (!myModules.empty())
409 aFile << "from salome.shaper import model" << std::endl << std::endl;
410 aFile << "model.begin()" << std::endl;
412 // dump collected data
413 aFile << myFullDump.str();
414 aFile << myDumpBuffer.str();
417 aFile << "model.end()" << std::endl;
425 void ModelHighAPI_Dumper::importModule(const std::string& theModuleName,
426 const std::string& theObject)
428 myModules[theModuleName].insert(theObject);
431 void ModelHighAPI_Dumper::dumpEntitySetName()
433 const LastDumpedEntity& aLastDumped = myEntitiesStack.top();
435 // dump "setName" for the entity
436 if (aLastDumped.myUserName) {
437 EntityName& anEntityNames = myNames[aLastDumped.myEntity];
438 if (!anEntityNames.myIsDefault)
439 myDumpBuffer << anEntityNames.myCurrentName << ".setName(\""
440 << anEntityNames.myUserName << "\")" << std::endl;
441 // don't dump "setName" for the entity twice
442 anEntityNames.myUserName.clear();
443 anEntityNames.myIsDefault = true;
445 // dump "setName" for results
446 std::list<ResultPtr>::const_iterator aResIt = aLastDumped.myResults.begin();
447 std::list<ResultPtr>::const_iterator aResEnd = aLastDumped.myResults.end();
448 for (; aResIt != aResEnd; ++aResIt) {
450 EntityName& anEntityNames = myNames[*aResIt];
451 if (!anEntityNames.myIsDefault) {
453 myDumpBuffer << ".setName(\"" << anEntityNames.myUserName << "\")" << std::endl;
454 // don't dump "setName" for the entity twice
455 anEntityNames.myUserName.clear();
456 anEntityNames.myIsDefault = true;
459 if (!isDefaultColor(*aResIt)) {
460 AttributeIntArrayPtr aColor = (*aResIt)->data()->intArray(ModelAPI_Result::COLOR_ID());
461 if (aColor && aColor->isInitialized()) {
463 myDumpBuffer << ".setColor(" << aColor->value(0) << ", " << aColor->value(1)
464 << ", " << aColor->value(2) << ")" << std::endl;
467 // set result deflection
468 if (!isDefaultDeflection(*aResIt)) {
469 AttributeDoublePtr aDeflectionAttr =
470 (*aResIt)->data()->real(ModelAPI_Result::DEFLECTION_ID());
471 if(aDeflectionAttr.get() && aDeflectionAttr->isInitialized()) {
473 myDumpBuffer << ".setDeflection(" << aDeflectionAttr->value() << ")" << std::endl;
478 myNames[aLastDumped.myEntity].myIsDumped = true;
479 myEntitiesStack.pop();
482 bool ModelHighAPI_Dumper::isDumped(const EntityPtr& theEntity) const
484 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
485 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
486 return aFound != myNames.end() || myFeaturesToSkip.find(aFeature) != myFeaturesToSkip.end();
489 bool ModelHighAPI_Dumper::isDefaultColor(const ResultPtr& theResult) const
491 AttributeIntArrayPtr aColor = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
492 if (!aColor || !aColor->isInitialized())
495 std::string aSection, aName, aDefault;
496 theResult->colorConfigInfo(aSection, aName, aDefault);
498 // dump current color
499 std::ostringstream aColorInfo;
500 aColorInfo << aColor->value(0) << "," << aColor->value(1) << "," << aColor->value(2);
502 return aDefault == aColorInfo.str();
505 bool ModelHighAPI_Dumper::isDefaultDeflection(const ResultPtr& theResult) const
507 AttributeDoublePtr aDeflectionAttr = theResult->data()->real(ModelAPI_Result::DEFLECTION_ID());
508 if(!aDeflectionAttr || !aDeflectionAttr->isInitialized()) {
512 double aCurrent = aDeflectionAttr->value();
513 double aDefault = -1;
515 bool isConstruction = false;
516 std::string aResultGroup = theResult->groupName();
517 if (aResultGroup == ModelAPI_ResultConstruction::group())
518 isConstruction = true;
519 else if (aResultGroup == ModelAPI_ResultBody::group()) {
520 GeomShapePtr aGeomShape = theResult->shape();
521 if (aGeomShape.get()) {
522 // if the shape could not be exploded on faces, it contains only wires, edges, and vertices
523 // correction of deviation for them should not influence to the application performance
524 GeomAPI_ShapeExplorer anExp(aGeomShape, GeomAPI_Shape::FACE);
525 isConstruction = !anExp.more();
529 aDefault = Config_PropManager::real("Visualization", "construction_deflection");
531 aDefault = Config_PropManager::real("Visualization", "body_deflection");
533 return fabs(aCurrent - aDefault) < 1.e-12;
536 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char theChar)
538 myDumpBuffer << theChar;
542 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char* theString)
544 myDumpBuffer << theString;
548 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::string& theString)
550 myDumpBuffer << theString;
554 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const bool theValue)
556 myDumpBuffer << (theValue ? "True" : "False");
560 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const int theValue)
562 myDumpBuffer << theValue;
566 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const double theValue)
568 myDumpBuffer << theValue;
572 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
574 importModule("GeomAPI", "GeomAPI_Pnt");
575 myDumpBuffer << "GeomAPI_Pnt(" << thePoint->x() << ", "
576 << thePoint->y() << ", " << thePoint->z() << ")";
580 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir)
582 importModule("GeomAPI", "GeomAPI_Dir");
583 myDumpBuffer << "GeomAPI_Dir(" << theDir->x() << ", "
584 << theDir->y() << ", " << theDir->z() << ")";
588 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
589 const std::shared_ptr<GeomDataAPI_Dir>& theDir)
591 myDumpBuffer << theDir->x() << ", " << theDir->y() << ", " << theDir->z();
595 static void dumpArray(std::ostringstream& theOutput, int theSize,
596 double* theValues, std::string* theTexts)
598 for (int i = 0; i < theSize; ++i) {
601 if (theTexts[i].empty())
602 theOutput << theValues[i];
604 theOutput << "\"" << theTexts[i] << "\"";
608 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
609 const std::shared_ptr<GeomDataAPI_Point>& thePoint)
611 static const int aSize = 3;
612 double aValues[aSize] = {thePoint->x(), thePoint->y(), thePoint->z()};
613 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY(), thePoint->textZ()};
614 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
618 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
619 const std::shared_ptr<GeomDataAPI_Point2D>& thePoint)
621 static const int aSize = 2;
622 double aValues[aSize] = {thePoint->x(), thePoint->y()};
623 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY()};
624 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
628 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
629 const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool)
631 myDumpBuffer << (theAttrBool->value() ? "True" : "False");
635 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
636 const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt)
638 std::string aText = theAttrInt->text();
640 myDumpBuffer << theAttrInt->value();
642 myDumpBuffer << "\"" << aText << "\"";
646 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
647 const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal)
649 std::string aText = theAttrReal->text();
651 myDumpBuffer << theAttrReal->value();
653 myDumpBuffer << "\"" << aText << "\"";
657 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
658 const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr)
660 myDumpBuffer << "\"" << theAttrStr->value() << "\"";
664 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FeaturePtr& theEntity)
666 myDumpBuffer << name(theEntity);
668 if (!myNames[theEntity].myIsDumped) {
669 bool isUserDefinedName = !myNames[theEntity].myIsDefault;
670 // store results if they have user-defined names or colors
671 std::list<ResultPtr> aResultsWithNameOrColor;
672 const std::list<ResultPtr>& aResults = theEntity->results();
673 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
674 for (; aResIt != aResults.end(); ++aResIt) {
675 if (!myNames[*aResIt].myIsDefault || !isDefaultColor(*aResIt) ||
676 !isDefaultDeflection(*aResIt))
677 aResultsWithNameOrColor.push_back(*aResIt);
679 ResultCompSolidPtr aCompSolid =
680 std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aResIt);
682 int aNbSubs = aCompSolid->numberOfSubs();
683 for (int i = 0; i < aNbSubs; ++i) {
684 ResultPtr aCurRes = aCompSolid->subResult(i);
685 if (!myNames[aCurRes].myIsDefault || !isDefaultColor(aCurRes) ||
686 !isDefaultDeflection(aCurRes))
687 aResultsWithNameOrColor.push_back(aCurRes);
691 // store just dumped entity to stack
692 if (myEntitiesStack.empty() || myEntitiesStack.top().myEntity != theEntity)
693 myEntitiesStack.push(
694 LastDumpedEntity(theEntity, isUserDefinedName, aResultsWithNameOrColor));
697 // remove entity from the list of not dumped items
698 myNotDumpedEntities.erase(theEntity);
702 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ResultPtr& theResult)
704 FeaturePtr aFeature = ModelAPI_Feature::feature(theResult);
707 std::list<ResultPtr> aResults = aFeature->results();
708 for(std::list<ResultPtr>::const_iterator
709 anIt = aResults.cbegin(); anIt != aResults.cend(); ++anIt, ++anIndex) {
710 if(theResult->isSame(*anIt)) {
714 ResultCompSolidPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*anIt);
716 int aNbSubs = aCompSolid->numberOfSubs();
717 for (aSubIndex = 0; aSubIndex < aNbSubs; ++aSubIndex)
718 if (theResult->isSame(aCompSolid->subResult(aSubIndex)))
720 if (aSubIndex < aNbSubs)
726 myDumpBuffer << name(aFeature);
728 myDumpBuffer << ".result()";
730 myDumpBuffer << ".results()[" << anIndex << "]";
732 if (aSubIndex >= 0) {
733 myDumpBuffer << ".subResult(" << aSubIndex << ")";
738 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ObjectPtr& theObject)
740 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
742 myDumpBuffer << name(aFeature);
746 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
755 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const AttributePtr& theAttr)
757 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttr->owner());
759 std::string aWrapperPrefix, aWrapperSuffix;
760 // Check the attribute belongs to copied (in multi-translation or multi-rotation) feature.
761 // In this case we need to cast explicitly feature to appropriate type.
762 AttributeBooleanPtr isCopy = anOwner->boolean("Copy");
763 if (isCopy.get() && isCopy->value()) {
764 aWrapperPrefix = featureWrapper(anOwner) + "(";
765 aWrapperSuffix = ")";
766 importModule("SketchAPI");
769 myDumpBuffer << aWrapperPrefix << name(anOwner) << aWrapperSuffix
770 << "." << attributeGetter(anOwner, theAttr->id()) << "()";
774 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
775 const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr)
777 if (theRefAttr->isObject())
778 *this << theRefAttr->object();
780 *this << theRefAttr->attr();
784 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
785 const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList)
788 std::list<std::pair<ObjectPtr, AttributePtr> > aList = theRefAttrList->list();
789 bool isAdded = false;
790 std::list<std::pair<ObjectPtr, AttributePtr> >::const_iterator anIt = aList.begin();
791 for (; anIt != aList.end(); ++anIt) {
793 myDumpBuffer << ", ";
797 *this << anIt->first;
798 else if (anIt->second)
799 * this << anIt->second;
805 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
806 const std::shared_ptr<ModelAPI_AttributeReference>& theReference)
808 *this << theReference->value();
812 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
813 const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList)
815 static const int aThreshold = 2;
816 // if number of elements in the list if greater than a threshold,
817 // dump it in a separate line with specific name
818 std::string aDumped = myDumpBuffer.str();
819 if (aDumped.empty() || theRefList->size() <= aThreshold) {
821 std::list<ObjectPtr> aList = theRefList->list();
822 bool isAdded = false;
823 std::list<ObjectPtr>::const_iterator anIt = aList.begin();
824 for (; anIt != aList.end(); ++anIt) {
826 myDumpBuffer << ", ";
834 // clear buffer and store list "as is"
835 myDumpBuffer.str("");
837 // save buffer and clear it again
838 std::string aDumpedList = myDumpBuffer.str();
839 myDumpBuffer.str("");
840 // obtain name of list
841 FeaturePtr anOwner = ModelAPI_Feature::feature(theRefList->owner());
842 std::string aListName = name(anOwner) + "_objects";
843 // store all previous data
844 myDumpBuffer << aListName << " = " << aDumpedList << std::endl
845 << aDumped << aListName;
850 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
851 const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect)
853 myDumpBuffer << "model.selection(";
855 if(!theAttrSelect->isInitialized()) {
860 GeomShapePtr aShape = theAttrSelect->value();
862 aShape = theAttrSelect->context()->shape();
870 myDumpBuffer << "\"" << aShape->shapeTypeStr() << "\", \"" <<
871 theAttrSelect->namingName() << "\")";
875 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
876 const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList)
881 std::string aShapeTypeStr;
883 bool isAdded = false;
885 for(int anIndex = 0; anIndex < theAttrSelList->size(); ++anIndex) {
886 AttributeSelectionPtr anAttribute = theAttrSelList->value(anIndex);
887 aShape = anAttribute->value();
889 aShape = anAttribute->context()->shape();
897 myDumpBuffer << ", ";
901 myDumpBuffer << "model.selection(\"" <<
902 aShape->shapeTypeStr() << "\", \"" << anAttribute->namingName() << "\")";
909 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
910 const std::shared_ptr<ModelAPI_AttributeStringArray>& theArray)
913 for(int anIndex = 0; anIndex < theArray->size(); ++anIndex) {
917 myDumpBuffer<<"\""<<theArray->value(anIndex)<<"\"";
925 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
926 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&))
928 theDumper.myDumpBuffer << theEndl;
930 if (!theDumper.myEntitiesStack.empty()) {
932 // all copies have been stored into stack, pop them all
935 // Name for composite feature is dumped when all sub-entities are dumped
936 // (see method ModelHighAPI_Dumper::processSubs).
937 const ModelHighAPI_Dumper::LastDumpedEntity& aLastDumped = theDumper.myEntitiesStack.top();
938 CompositeFeaturePtr aComposite =
939 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aLastDumped.myEntity);
941 theDumper.dumpEntitySetName();
942 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aLastDumped.myEntity);
944 AttributeBooleanPtr aCopyAttr = aFeature->boolean("Copy");
945 isCopy = aCopyAttr.get() && aCopyAttr->value();
951 // store all not-dumped entities first
952 std::set<EntityPtr> aNotDumped = theDumper.myNotDumpedEntities;
953 std::string aBufCopy = theDumper.myDumpBuffer.str();
954 theDumper.clear(true);
955 std::set<EntityPtr>::const_iterator anIt = aNotDumped.begin();
956 for (; anIt != aNotDumped.end(); ++anIt) {
957 // if the feature is composite, dump it with all subs
958 CompositeFeaturePtr aCompFeat =
959 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anIt);
961 theDumper.process(aCompFeat, true);
963 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
964 theDumper.dumpFeature(aFeature, true);
968 // avoid multiple empty lines
969 size_t anInd = std::string::npos;
970 while ((anInd = aBufCopy.find("\n\n\n")) != std::string::npos)
971 aBufCopy.erase(anInd, 1);
972 // then store currently dumped string
973 theDumper.myFullDump << aBufCopy;