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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "ModelHighAPI_Dumper.h"
23 #include <Config_PropManager.h>
25 #include <GeomAPI_Pnt.h>
26 #include <GeomAPI_Dir.h>
27 #include <GeomAPI_ShapeExplorer.h>
29 #include <GeomDataAPI_Dir.h>
30 #include <GeomDataAPI_Point.h>
31 #include <GeomDataAPI_Point2D.h>
33 #include <ModelAPI_AttributeBoolean.h>
34 #include <ModelAPI_AttributeDouble.h>
35 #include <ModelAPI_AttributeIntArray.h>
36 #include <ModelAPI_AttributeInteger.h>
37 #include <ModelAPI_AttributeRefAttr.h>
38 #include <ModelAPI_AttributeRefAttrList.h>
39 #include <ModelAPI_AttributeReference.h>
40 #include <ModelAPI_AttributeRefList.h>
41 #include <ModelAPI_AttributeSelection.h>
42 #include <ModelAPI_AttributeSelectionList.h>
43 #include <ModelAPI_AttributeString.h>
44 #include <ModelAPI_AttributeStringArray.h>
45 #include <ModelAPI_CompositeFeature.h>
46 #include <ModelAPI_Document.h>
47 #include <ModelAPI_Entity.h>
48 #include <ModelAPI_Feature.h>
49 #include <ModelAPI_Result.h>
50 #include <ModelAPI_ResultBody.h>
51 #include <ModelAPI_ResultCompSolid.h>
52 #include <ModelAPI_ResultConstruction.h>
53 #include <ModelAPI_ResultPart.h>
55 #include <PartSetPlugin_Part.h>
57 #include <OSD_OpenFile.hxx>
61 static int gCompositeStackDepth = 0;
63 ModelHighAPI_Dumper* ModelHighAPI_Dumper::mySelf = 0;
65 ModelHighAPI_Dumper::ModelHighAPI_Dumper()
70 void ModelHighAPI_Dumper::setInstance(ModelHighAPI_Dumper* theDumper)
76 ModelHighAPI_Dumper* ModelHighAPI_Dumper::getInstance()
81 void ModelHighAPI_Dumper::clear(bool bufferOnly)
84 myDumpBuffer << std::setprecision(16);
90 myFullDump << std::setprecision(16);
94 myFeatureCount.clear();
95 while (!myEntitiesStack.empty())
96 myEntitiesStack.pop();
100 void ModelHighAPI_Dumper::clearNotDumped()
102 myNotDumpedEntities.clear();
105 // Convert string to integer. If the string is not a number, return -1
106 static int toInt(const std::string& theString)
108 std::string::const_iterator aChar = theString.begin();
109 for (; aChar != theString.end(); ++aChar)
110 if (!std::isdigit(*aChar))
112 if (aChar != theString.end())
113 return -1; // not a number
114 return std::stoi(theString);
117 const std::string& ModelHighAPI_Dumper::name(const EntityPtr& theEntity,
118 bool theSaveNotDumped,
119 bool theUseEntityName)
121 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
122 if (aFound != myNames.end())
123 return aFound->second.myCurrentName;
125 // entity is not found, store it
127 bool isDefaultName = false;
128 std::ostringstream aDefaultName;
129 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
131 aName = aFeature->name();
132 const std::string& aKind = aFeature->getKind();
133 DocumentPtr aDoc = aFeature->document();
134 int& aNbFeatures = myFeatureCount[aDoc][aKind];
137 size_t anIndex = aName.find(aKind);
138 if (anIndex == 0 && aName[aKind.length()] == '_') { // name starts with "FeatureKind_"
139 std::string anIdStr = aName.substr(aKind.length() + 1);
140 int anId = toInt(anIdStr);
142 // Check number of already registered objects of such kind. Index of current object
143 // should be the same to identify feature's name as automatically generated.
144 if (aNbFeatures == anId) {
145 // name is not user-defined
146 isDefaultName = true;
150 // obtain default name for the feature
151 if (theUseEntityName)
152 aDefaultName << aName;
155 NbFeaturesMap::const_iterator aFIt = myFeatureCount.begin();
156 for (; aFIt != myFeatureCount.end(); ++aFIt) {
157 std::map<std::string, int>::const_iterator aFound = aFIt->second.find(aKind);
158 if (aFound != aFIt->second.end())
159 aFullIndex += aFound->second;
161 aDefaultName << aKind << "_" << aFullIndex;
165 myNames[theEntity] = EntityName(aDefaultName.str(), aName, isDefaultName);
166 if (theSaveNotDumped)
167 myNotDumpedEntities.insert(theEntity);
169 // store names of results
171 saveResultNames(aFeature);
173 return myNames[theEntity].myCurrentName;
176 const std::string& ModelHighAPI_Dumper::parentName(const FeaturePtr& theEntity)
178 const std::set<AttributePtr>& aRefs = theEntity->data()->refsToMe();
179 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
180 for (; aRefIt != aRefs.end(); ++aRefIt) {
181 CompositeFeaturePtr anOwner = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(
182 ModelAPI_Feature::feature((*aRefIt)->owner()));
184 return name(anOwner);
187 static const std::string DUMMY;
191 void ModelHighAPI_Dumper::saveResultNames(const FeaturePtr& theFeature)
193 // Default name of the feature
194 const std::string& aKind = theFeature->getKind();
195 DocumentPtr aDoc = theFeature->document();
196 int aNbFeatures = myFeatureCount[aDoc][aKind];
197 std::ostringstream aNameStream;
198 aNameStream << aKind << "_" << aNbFeatures;
199 std::string aFeatureName = aNameStream.str();
201 // Save only names of results which is not correspond to default feature name
202 const std::list<ResultPtr>& aResults = theFeature->results();
203 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
204 for (int i = 1; aResIt != aResults.end(); ++aResIt, ++i) {
205 bool isUserDefined = true;
206 std::string aResName = (*aResIt)->data()->name();
207 size_t anIndex = aResName.find(aFeatureName);
209 std::string aSuffix = aResName.substr(aFeatureName.length());
210 if (aSuffix.empty() && i == 1) // first result may not constain index in the name
211 isUserDefined = false;
213 if (aSuffix[0] == '_' && std::stoi(aSuffix.substr(1)) == i)
214 isUserDefined = false;
218 myNames[*aResIt] = EntityName(aResName,
219 (isUserDefined ? aResName : std::string()), !isUserDefined);
221 // check names of sub-results for CompSolid
222 ResultCompSolidPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aResIt);
224 int aNbSubs = aCompSolid->numberOfSubs();
225 for (int j = 0; j < aNbSubs; ++j) {
226 isUserDefined = true;
227 ResultPtr aSub = aCompSolid->subResult(j);
228 std::string aSubName = aSub->data()->name();
229 size_t anIndex = aSubName.find(aResName);
231 std::string aSuffix = aSubName.substr(aResName.length());
232 if (aSuffix.empty() && aNbSubs == 1) // first result may not constain index in the name
233 isUserDefined = false;
235 if (aSuffix[0] == '_' && std::stoi(aSuffix.substr(1)) == j + 1)
236 isUserDefined = false;
240 myNames[aSub] = EntityName(aSubName,
241 (isUserDefined ? aSubName : std::string()), !isUserDefined);
247 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc,
248 const std::string& theFileName)
250 // dump top level document feature
251 static const std::string aDocName("partSet");
252 myNames[theDoc] = EntityName(aDocName, std::string(), true);
253 *this << aDocName << " = model.moduleDocument()" << std::endl;
255 // dump subfeatures and store result to file
256 return process(theDoc) && exportTo(theFileName);
259 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc)
262 std::list<FeaturePtr> aFeatures = theDoc->allFeatures();
263 std::list<FeaturePtr>::const_iterator aFeatIt = aFeatures.begin();
264 // firstly, dump all parameters
265 for (; aFeatIt != aFeatures.end(); ++ aFeatIt)
266 dumpParameter(*aFeatIt);
267 // dump all other features
268 for (aFeatIt = aFeatures.begin(); aFeatIt != aFeatures.end(); ++aFeatIt) {
269 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFeatIt);
270 if (aCompFeat) // iteratively process composite features
271 isOk = process(aCompFeat) && isOk;
272 else if (!isDumped(*aFeatIt)) // dump common feature
273 dumpFeature(*aFeatIt);
278 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
281 // increase composite features stack
282 ++gCompositeStackDepth;
283 // dump composite itself
284 if (!isDumped(theComposite) || isForce)
285 dumpFeature(FeaturePtr(theComposite), isForce);
287 // sub-part is processed independently, because it provides separate document
288 if (theComposite->getKind() == PartSetPlugin_Part::ID()) {
289 // dump name of the part if it is different from default
290 if (!myEntitiesStack.empty())
293 // decrease composite features stack because we run into separate document
294 --gCompositeStackDepth;
296 ResultPartPtr aPartResult =
297 std::dynamic_pointer_cast<ModelAPI_ResultPart>(theComposite->lastResult());
300 DocumentPtr aSubDoc = aPartResult->partDoc();
303 // set name of document
304 const std::string& aPartName = myNames[theComposite].myCurrentName;
305 std::string aDocName = aPartName + "_doc";
306 myNames[aSubDoc] = EntityName(aDocName, std::string(), true);
308 // dump document in a separate line
309 *this << aDocName << " = " << aPartName << ".document()" << std::endl;
310 // dump features in the document
311 bool aRes = process(aSubDoc);
312 *this << "model.do()" << std::endl;
317 bool isOk = processSubs(theComposite);
318 // decrease composite features stack
319 --gCompositeStackDepth;
324 bool ModelHighAPI_Dumper::processSubs(
325 const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
329 // dump all sub-features;
330 bool isSubDumped = false;
331 int aNbSubs = theComposite->numberOfSubs();
332 for (int anIndex = 0; anIndex < aNbSubs; ++anIndex) {
333 FeaturePtr aFeature = theComposite->subFeature(anIndex);
334 if (isDumped(aFeature))
338 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
339 if (aCompFeat) // iteratively process composite features
340 isOk = process(aCompFeat) && isOk;
342 dumpFeature(aFeature, true);
345 bool isDumpSetName = !myEntitiesStack.empty() &&
346 myEntitiesStack.top().myEntity == EntityPtr(theComposite);
347 bool isForceModelDo = isSubDumped && isDumpSetName &&
348 (myEntitiesStack.top().myUserName || !myEntitiesStack.top().myResults.empty());
349 // It is necessary for the sketch to create its result when complete (command "model.do()").
350 // This option is set by flat theDumpModelDo.
351 // However, nested sketches are rebuilt by parent feature, so, they do not need
352 // explicit call of "model.do()". This will be controlled by the depth of the stack.
353 if (isForceModelDo || (theDumpModelDo && gCompositeStackDepth <= 1))
354 *this << "model.do()" << std::endl;
356 // dump "setName" for composite feature
362 void ModelHighAPI_Dumper::dumpSubFeatureNameAndColor(const std::string theSubFeatureGet,
363 const FeaturePtr& theSubFeature)
365 name(theSubFeature, false);
366 myNames[theSubFeature] = EntityName(theSubFeatureGet, theSubFeature->name(), false);
368 // store results if they have user-defined names or colors
369 std::list<ResultPtr> aResultsWithNameOrColor;
370 const std::list<ResultPtr>& aResults = theSubFeature->results();
371 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
372 for (; aResIt != aResults.end(); ++aResIt) {
373 std::string aResName = (*aResIt)->data()->name();
374 myNames[*aResIt] = EntityName(aResName, aResName, false);
375 aResultsWithNameOrColor.push_back(*aResIt);
378 // store just dumped entity to stack
379 myEntitiesStack.push(LastDumpedEntity(theSubFeature, true, aResultsWithNameOrColor));
384 bool ModelHighAPI_Dumper::exportTo(const std::string& theFileName)
387 OSD_OpenStream(aFile, theFileName.c_str(), std::ofstream::out);
388 if (!aFile.is_open())
392 for (ModulesMap::const_iterator aModIt = myModules.begin();
393 aModIt != myModules.end(); ++aModIt) {
394 aFile << "from " << aModIt->first << " import ";
395 if (aModIt->second.empty() ||
396 aModIt->second.find(std::string()) != aModIt->second.end())
397 aFile << "*"; // import whole module
399 // import specific features
400 std::set<std::string>::const_iterator anObjIt = aModIt->second.begin();
402 for (++anObjIt; anObjIt != aModIt->second.end(); ++anObjIt)
403 aFile << ", " << *anObjIt;
407 if (!myModules.empty())
410 aFile << "from salome.shaper import model" << std::endl << std::endl;
411 aFile << "model.begin()" << std::endl;
413 // dump collected data
414 aFile << myFullDump.str();
415 aFile << myDumpBuffer.str();
418 aFile << "model.end()" << std::endl;
426 void ModelHighAPI_Dumper::importModule(const std::string& theModuleName,
427 const std::string& theObject)
429 myModules[theModuleName].insert(theObject);
432 void ModelHighAPI_Dumper::dumpEntitySetName()
434 const LastDumpedEntity& aLastDumped = myEntitiesStack.top();
436 // dump "setName" for the entity
437 if (aLastDumped.myUserName) {
438 EntityName& anEntityNames = myNames[aLastDumped.myEntity];
439 if (!anEntityNames.myIsDefault)
440 myDumpBuffer << anEntityNames.myCurrentName << ".setName(\""
441 << anEntityNames.myUserName << "\")" << std::endl;
442 // don't dump "setName" for the entity twice
443 anEntityNames.myUserName.clear();
444 anEntityNames.myIsDefault = true;
446 // dump "setName" for results
447 std::list<ResultPtr>::const_iterator aResIt = aLastDumped.myResults.begin();
448 std::list<ResultPtr>::const_iterator aResEnd = aLastDumped.myResults.end();
449 for (; aResIt != aResEnd; ++aResIt) {
451 EntityName& anEntityNames = myNames[*aResIt];
452 if (!anEntityNames.myIsDefault) {
454 myDumpBuffer << ".setName(\"" << anEntityNames.myUserName << "\")" << std::endl;
455 // don't dump "setName" for the entity twice
456 anEntityNames.myUserName.clear();
457 anEntityNames.myIsDefault = true;
460 if (!isDefaultColor(*aResIt)) {
461 AttributeIntArrayPtr aColor = (*aResIt)->data()->intArray(ModelAPI_Result::COLOR_ID());
462 if (aColor && aColor->isInitialized()) {
464 myDumpBuffer << ".setColor(" << aColor->value(0) << ", " << aColor->value(1)
465 << ", " << aColor->value(2) << ")" << std::endl;
468 // set result deflection
469 if (!isDefaultDeflection(*aResIt)) {
470 AttributeDoublePtr aDeflectionAttr =
471 (*aResIt)->data()->real(ModelAPI_Result::DEFLECTION_ID());
472 if(aDeflectionAttr.get() && aDeflectionAttr->isInitialized()) {
474 myDumpBuffer << ".setDeflection(" << aDeflectionAttr->value() << ")" << std::endl;
479 myNames[aLastDumped.myEntity].myIsDumped = true;
480 myEntitiesStack.pop();
483 bool ModelHighAPI_Dumper::isDumped(const EntityPtr& theEntity) const
485 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
486 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
487 return aFound != myNames.end() || myFeaturesToSkip.find(aFeature) != myFeaturesToSkip.end();
490 bool ModelHighAPI_Dumper::isDefaultColor(const ResultPtr& theResult) const
492 AttributeIntArrayPtr aColor = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
493 if (!aColor || !aColor->isInitialized())
496 std::string aSection, aName, aDefault;
497 theResult->colorConfigInfo(aSection, aName, aDefault);
499 // dump current color
500 std::ostringstream aColorInfo;
501 aColorInfo << aColor->value(0) << "," << aColor->value(1) << "," << aColor->value(2);
503 return aDefault == aColorInfo.str();
506 bool ModelHighAPI_Dumper::isDefaultDeflection(const ResultPtr& theResult) const
508 AttributeDoublePtr aDeflectionAttr = theResult->data()->real(ModelAPI_Result::DEFLECTION_ID());
509 if(!aDeflectionAttr || !aDeflectionAttr->isInitialized()) {
513 double aCurrent = aDeflectionAttr->value();
514 double aDefault = -1;
516 bool isConstruction = false;
517 std::string aResultGroup = theResult->groupName();
518 if (aResultGroup == ModelAPI_ResultConstruction::group())
519 isConstruction = true;
520 else if (aResultGroup == ModelAPI_ResultBody::group()) {
521 GeomShapePtr aGeomShape = theResult->shape();
522 if (aGeomShape.get()) {
523 // if the shape could not be exploded on faces, it contains only wires, edges, and vertices
524 // correction of deviation for them should not influence to the application performance
525 GeomAPI_ShapeExplorer anExp(aGeomShape, GeomAPI_Shape::FACE);
526 isConstruction = !anExp.more();
530 aDefault = Config_PropManager::real("Visualization", "construction_deflection");
532 aDefault = Config_PropManager::real("Visualization", "body_deflection");
534 return fabs(aCurrent - aDefault) < 1.e-12;
537 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char theChar)
539 myDumpBuffer << theChar;
543 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char* theString)
545 myDumpBuffer << theString;
549 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::string& theString)
551 myDumpBuffer << theString;
555 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const bool theValue)
557 myDumpBuffer << (theValue ? "True" : "False");
561 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const int theValue)
563 myDumpBuffer << theValue;
567 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const double theValue)
569 myDumpBuffer << theValue;
573 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
575 importModule("GeomAPI", "GeomAPI_Pnt");
576 myDumpBuffer << "GeomAPI_Pnt(" << thePoint->x() << ", "
577 << thePoint->y() << ", " << thePoint->z() << ")";
581 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir)
583 importModule("GeomAPI", "GeomAPI_Dir");
584 myDumpBuffer << "GeomAPI_Dir(" << theDir->x() << ", "
585 << theDir->y() << ", " << theDir->z() << ")";
589 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
590 const std::shared_ptr<GeomDataAPI_Dir>& theDir)
592 myDumpBuffer << theDir->x() << ", " << theDir->y() << ", " << theDir->z();
596 static void dumpArray(std::ostringstream& theOutput, int theSize,
597 double* theValues, std::string* theTexts)
599 for (int i = 0; i < theSize; ++i) {
602 if (theTexts[i].empty())
603 theOutput << theValues[i];
605 theOutput << "\"" << theTexts[i] << "\"";
609 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
610 const std::shared_ptr<GeomDataAPI_Point>& thePoint)
612 static const int aSize = 3;
613 double aValues[aSize] = {thePoint->x(), thePoint->y(), thePoint->z()};
614 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY(), thePoint->textZ()};
615 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
619 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
620 const std::shared_ptr<GeomDataAPI_Point2D>& thePoint)
622 static const int aSize = 2;
623 double aValues[aSize] = {thePoint->x(), thePoint->y()};
624 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY()};
625 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
629 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
630 const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool)
632 myDumpBuffer << (theAttrBool->value() ? "True" : "False");
636 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
637 const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt)
639 std::string aText = theAttrInt->text();
641 myDumpBuffer << theAttrInt->value();
643 myDumpBuffer << "\"" << aText << "\"";
647 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
648 const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal)
650 std::string aText = theAttrReal->text();
652 myDumpBuffer << theAttrReal->value();
654 myDumpBuffer << "\"" << aText << "\"";
658 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
659 const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr)
661 myDumpBuffer << "\"" << theAttrStr->value() << "\"";
665 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FeaturePtr& theEntity)
667 myDumpBuffer << name(theEntity);
669 if (!myNames[theEntity].myIsDumped) {
670 bool isUserDefinedName = !myNames[theEntity].myIsDefault;
671 // store results if they have user-defined names or colors
672 std::list<ResultPtr> aResultsWithNameOrColor;
673 const std::list<ResultPtr>& aResults = theEntity->results();
674 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
675 for (; aResIt != aResults.end(); ++aResIt) {
676 if (!myNames[*aResIt].myIsDefault || !isDefaultColor(*aResIt) ||
677 !isDefaultDeflection(*aResIt))
678 aResultsWithNameOrColor.push_back(*aResIt);
680 ResultCompSolidPtr aCompSolid =
681 std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aResIt);
683 int aNbSubs = aCompSolid->numberOfSubs();
684 for (int i = 0; i < aNbSubs; ++i) {
685 ResultPtr aCurRes = aCompSolid->subResult(i);
686 if (!myNames[aCurRes].myIsDefault || !isDefaultColor(aCurRes) ||
687 !isDefaultDeflection(aCurRes))
688 aResultsWithNameOrColor.push_back(aCurRes);
692 // store just dumped entity to stack
693 if (myEntitiesStack.empty() || myEntitiesStack.top().myEntity != theEntity)
694 myEntitiesStack.push(
695 LastDumpedEntity(theEntity, isUserDefinedName, aResultsWithNameOrColor));
698 // remove entity from the list of not dumped items
699 myNotDumpedEntities.erase(theEntity);
703 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ResultPtr& theResult)
705 FeaturePtr aFeature = ModelAPI_Feature::feature(theResult);
708 std::list<ResultPtr> aResults = aFeature->results();
709 for(std::list<ResultPtr>::const_iterator
710 anIt = aResults.cbegin(); anIt != aResults.cend(); ++anIt, ++anIndex) {
711 if(theResult->isSame(*anIt)) {
715 ResultCompSolidPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*anIt);
717 int aNbSubs = aCompSolid->numberOfSubs();
718 for (aSubIndex = 0; aSubIndex < aNbSubs; ++aSubIndex)
719 if (theResult->isSame(aCompSolid->subResult(aSubIndex)))
721 if (aSubIndex < aNbSubs)
727 myDumpBuffer << name(aFeature);
729 myDumpBuffer << ".result()";
731 myDumpBuffer << ".results()[" << anIndex << "]";
733 if (aSubIndex >= 0) {
734 myDumpBuffer << ".subResult(" << aSubIndex << ")";
739 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ObjectPtr& theObject)
741 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
743 myDumpBuffer << name(aFeature);
747 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
756 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const AttributePtr& theAttr)
758 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttr->owner());
760 std::string aWrapperPrefix, aWrapperSuffix;
761 // Check the attribute belongs to copied (in multi-translation or multi-rotation) feature.
762 // In this case we need to cast explicitly feature to appropriate type.
763 AttributeBooleanPtr isCopy = anOwner->boolean("Copy");
764 if (isCopy.get() && isCopy->value()) {
765 aWrapperPrefix = featureWrapper(anOwner) + "(";
766 aWrapperSuffix = ")";
767 importModule("SketchAPI");
770 myDumpBuffer << aWrapperPrefix << name(anOwner) << aWrapperSuffix
771 << "." << attributeGetter(anOwner, theAttr->id()) << "()";
775 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
776 const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr)
778 if (theRefAttr->isObject())
779 *this << theRefAttr->object();
781 *this << theRefAttr->attr();
785 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
786 const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList)
789 std::list<std::pair<ObjectPtr, AttributePtr> > aList = theRefAttrList->list();
790 bool isAdded = false;
791 std::list<std::pair<ObjectPtr, AttributePtr> >::const_iterator anIt = aList.begin();
792 for (; anIt != aList.end(); ++anIt) {
794 myDumpBuffer << ", ";
798 *this << anIt->first;
799 else if (anIt->second)
800 * this << anIt->second;
806 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
807 const std::shared_ptr<ModelAPI_AttributeReference>& theReference)
809 *this << theReference->value();
813 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
814 const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList)
816 static const int aThreshold = 2;
817 // if number of elements in the list if greater than a threshold,
818 // dump it in a separate line with specific name
819 std::string aDumped = myDumpBuffer.str();
820 if (aDumped.empty() || theRefList->size() <= aThreshold) {
822 std::list<ObjectPtr> aList = theRefList->list();
823 bool isAdded = false;
824 std::list<ObjectPtr>::const_iterator anIt = aList.begin();
825 for (; anIt != aList.end(); ++anIt) {
827 myDumpBuffer << ", ";
835 // clear buffer and store list "as is"
836 myDumpBuffer.str("");
838 // save buffer and clear it again
839 std::string aDumpedList = myDumpBuffer.str();
840 myDumpBuffer.str("");
841 // obtain name of list
842 FeaturePtr anOwner = ModelAPI_Feature::feature(theRefList->owner());
843 std::string aListName = name(anOwner) + "_objects";
844 // store all previous data
845 myDumpBuffer << aListName << " = " << aDumpedList << std::endl
846 << aDumped << aListName;
851 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
852 const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect)
854 myDumpBuffer << "model.selection(";
856 if(!theAttrSelect->isInitialized()) {
861 GeomShapePtr aShape = theAttrSelect->value();
863 aShape = theAttrSelect->context()->shape();
871 myDumpBuffer << "\"" << aShape->shapeTypeStr() << "\", \"" <<
872 theAttrSelect->namingName() << "\")";
876 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
877 const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList)
882 std::string aShapeTypeStr;
884 bool isAdded = false;
886 for(int anIndex = 0; anIndex < theAttrSelList->size(); ++anIndex) {
887 AttributeSelectionPtr anAttribute = theAttrSelList->value(anIndex);
888 aShape = anAttribute->value();
890 aShape = anAttribute->context()->shape();
898 myDumpBuffer << ", ";
902 myDumpBuffer << "model.selection(\"" <<
903 aShape->shapeTypeStr() << "\", \"" << anAttribute->namingName() << "\")";
910 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
911 const std::shared_ptr<ModelAPI_AttributeStringArray>& theArray)
914 for(int anIndex = 0; anIndex < theArray->size(); ++anIndex) {
918 myDumpBuffer<<"\""<<theArray->value(anIndex)<<"\"";
926 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
927 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&))
929 theDumper.myDumpBuffer << theEndl;
931 if (!theDumper.myEntitiesStack.empty()) {
933 // all copies have been stored into stack, pop them all
936 // Name for composite feature is dumped when all sub-entities are dumped
937 // (see method ModelHighAPI_Dumper::processSubs).
938 const ModelHighAPI_Dumper::LastDumpedEntity& aLastDumped = theDumper.myEntitiesStack.top();
939 CompositeFeaturePtr aComposite =
940 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aLastDumped.myEntity);
942 theDumper.dumpEntitySetName();
943 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aLastDumped.myEntity);
945 AttributeBooleanPtr aCopyAttr = aFeature->boolean("Copy");
946 isCopy = aCopyAttr.get() && aCopyAttr->value();
952 // store all not-dumped entities first
953 std::set<EntityPtr> aNotDumped = theDumper.myNotDumpedEntities;
954 std::string aBufCopy = theDumper.myDumpBuffer.str();
955 theDumper.clear(true);
956 std::set<EntityPtr>::const_iterator anIt = aNotDumped.begin();
957 for (; anIt != aNotDumped.end(); ++anIt) {
958 // if the feature is composite, dump it with all subs
959 CompositeFeaturePtr aCompFeat =
960 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anIt);
962 theDumper.process(aCompFeat, true);
964 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
965 theDumper.dumpFeature(aFeature, true);
969 // avoid multiple empty lines
970 size_t anInd = std::string::npos;
971 while ((anInd = aBufCopy.find("\n\n\n")) != std::string::npos)
972 aBufCopy.erase(anInd, 1);
973 // then store currently dumped string
974 theDumper.myFullDump << aBufCopy;