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_Folder.h>
50 #include <ModelAPI_Result.h>
51 #include <ModelAPI_ResultBody.h>
52 #include <ModelAPI_ResultCompSolid.h>
53 #include <ModelAPI_ResultConstruction.h>
54 #include <ModelAPI_ResultPart.h>
55 #include <ModelAPI_Tools.h>
57 #include <PartSetPlugin_Part.h>
59 #include <OSD_OpenFile.hxx>
63 static int gCompositeStackDepth = 0;
65 ModelHighAPI_Dumper* ModelHighAPI_Dumper::mySelf = 0;
67 ModelHighAPI_Dumper::ModelHighAPI_Dumper()
72 void ModelHighAPI_Dumper::setInstance(ModelHighAPI_Dumper* theDumper)
78 ModelHighAPI_Dumper* ModelHighAPI_Dumper::getInstance()
83 void ModelHighAPI_Dumper::clear(bool bufferOnly)
86 myDumpBuffer << std::setprecision(16);
92 myFullDump << std::setprecision(16);
96 myFeatureCount.clear();
97 while (!myEntitiesStack.empty())
98 myEntitiesStack.pop();
101 myDumpPostponedInProgress = false;
105 void ModelHighAPI_Dumper::clearNotDumped()
107 myNotDumpedEntities.clear();
110 // Convert string to integer. If the string is not a number, return -1
111 static int toInt(const std::string& theString)
113 std::string::const_iterator aChar = theString.begin();
114 for (; aChar != theString.end(); ++aChar)
115 if (!std::isdigit(*aChar))
117 if (aChar != theString.end())
118 return -1; // not a number
119 return std::stoi(theString);
122 const std::string& ModelHighAPI_Dumper::name(const EntityPtr& theEntity,
123 bool theSaveNotDumped,
124 bool theUseEntityName)
126 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
127 if (aFound != myNames.end())
128 return aFound->second.myCurrentName;
130 // entity is not found, store it
131 std::string aName, aKind;
132 bool isDefaultName = false;
133 bool isSaveNotDumped = theSaveNotDumped;
134 std::ostringstream aDefaultName;
135 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
137 aName = aFeature->name();
138 aKind = aFeature->getKind();
140 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(theEntity);
142 aName = aFolder->data()->name();
143 aKind = ModelAPI_Folder::ID();
144 isSaveNotDumped = false;
148 ObjectPtr anObject = std::dynamic_pointer_cast<ModelAPI_Object>(theEntity);
150 DocumentPtr aDoc = anObject->document();
151 int& aNbFeatures = myFeatureCount[aDoc][aKind];
154 size_t anIndex = aName.find(aKind);
155 if (anIndex == 0 && aName[aKind.length()] == '_') { // name starts with "FeatureKind_"
156 std::string anIdStr = aName.substr(aKind.length() + 1);
157 int anId = toInt(anIdStr);
159 // Check number of already registered objects of such kind. Index of current object
160 // should be the same to identify feature's name as automatically generated.
161 if (aNbFeatures == anId) {
162 // name is not user-defined
163 isDefaultName = true;
167 // obtain default name for the feature
168 if (theUseEntityName)
169 aDefaultName << aName;
172 NbFeaturesMap::const_iterator aFIt = myFeatureCount.begin();
173 for (; aFIt != myFeatureCount.end(); ++aFIt) {
174 std::map<std::string, int>::const_iterator aFound = aFIt->second.find(aKind);
175 if (aFound != aFIt->second.end())
176 aFullIndex += aFound->second;
178 aDefaultName << aKind << "_" << aFullIndex;
182 myNames[theEntity] = EntityName(aDefaultName.str(), aName, isDefaultName);
184 myNotDumpedEntities.insert(theEntity);
186 // store names of results
188 saveResultNames(aFeature);
190 return myNames[theEntity].myCurrentName;
193 const std::string& ModelHighAPI_Dumper::parentName(const FeaturePtr& theEntity)
195 const std::set<AttributePtr>& aRefs = theEntity->data()->refsToMe();
196 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
197 for (; aRefIt != aRefs.end(); ++aRefIt) {
198 CompositeFeaturePtr anOwner = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(
199 ModelAPI_Feature::feature((*aRefIt)->owner()));
201 return name(anOwner);
204 static const std::string DUMMY;
208 void ModelHighAPI_Dumper::saveResultNames(const FeaturePtr& theFeature)
210 // Default name of the feature
211 bool isFeatureDefaultName = myNames[theFeature].myIsDefault;
213 // Save only names of results which is not correspond to default feature name
214 const std::list<ResultPtr>& aResults = theFeature->results();
215 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
216 for (int i = 0; aResIt != aResults.end(); ++aResIt, ++i) {
217 std::string aDefaultName = ModelAPI_Tools::getDefaultName(*aResIt, i);
218 std::string aResName = (*aResIt)->data()->name();
220 bool isUserDefined = !(isFeatureDefaultName && aDefaultName == aResName);
222 myNames[*aResIt] = EntityName(aResName,
223 (isUserDefined ? aResName : std::string()), !isUserDefined);
225 // check names of sub-results for CompSolid
226 ResultCompSolidPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aResIt);
228 int aNbSubs = aCompSolid->numberOfSubs();
229 for (int j = 0; j < aNbSubs; ++j) {
230 ResultPtr aSub = aCompSolid->subResult(j);
231 std::string aSubName = aSub->data()->name();
232 aDefaultName = ModelAPI_Tools::getDefaultName(aSub, j);
234 bool isUserDefinedSubName = isUserDefined || aDefaultName != aSubName;
235 myNames[aSub] = EntityName(aSubName,
236 (isUserDefinedSubName ? aSubName : std::string()), !isUserDefinedSubName);
242 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc,
243 const std::string& theFileName)
245 // dump top level document feature
246 static const std::string aDocName("partSet");
247 myNames[theDoc] = EntityName(aDocName, std::string(), true);
248 *this << aDocName << " = model.moduleDocument()" << std::endl;
250 // dump subfeatures and store result to file
251 return process(theDoc) && exportTo(theFileName);
254 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc)
257 std::list<ObjectPtr> anObjects = theDoc->allObjects();
258 std::list<ObjectPtr>::const_iterator anObjIt = anObjects.begin();
259 // firstly, dump all parameters
260 for (; anObjIt != anObjects.end(); ++ anObjIt) {
261 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIt);
263 dumpParameter(aFeature);
265 // dump all other features
266 for (anObjIt = anObjects.begin(); anObjIt != anObjects.end(); ++anObjIt) {
267 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anObjIt);
268 if (aCompFeat) // iteratively process composite features
269 isOk = process(aCompFeat) && isOk;
270 else if (!isDumped(EntityPtr(*anObjIt))) {
272 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(*anObjIt);
276 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIt);
277 if (aFeature) // dump common feature
278 dumpFeature(aFeature);
285 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
288 // increase composite features stack
289 ++gCompositeStackDepth;
290 // dump composite itself
291 if (!isDumped(EntityPtr(theComposite)) || isForce)
292 dumpFeature(FeaturePtr(theComposite), isForce);
294 // sub-part is processed independently, because it provides separate document
295 if (theComposite->getKind() == PartSetPlugin_Part::ID()) {
296 // dump name of the part if it is different from default
297 if (!myEntitiesStack.empty())
300 // decrease composite features stack because we run into separate document
301 --gCompositeStackDepth;
303 ResultPartPtr aPartResult =
304 std::dynamic_pointer_cast<ModelAPI_ResultPart>(theComposite->lastResult());
307 DocumentPtr aSubDoc = aPartResult->partDoc();
310 // set name of document
311 const std::string& aPartName = myNames[theComposite].myCurrentName;
312 std::string aDocName = aPartName + "_doc";
313 myNames[aSubDoc] = EntityName(aDocName, std::string(), true);
315 // dump document in a separate line
316 *this << aDocName << " = " << aPartName << ".document()" << std::endl;
317 // dump features in the document
318 bool aRes = process(aSubDoc);
319 *this << "model.do()" << std::endl;
324 bool isOk = processSubs(theComposite);
325 // decrease composite features stack
326 --gCompositeStackDepth;
331 bool ModelHighAPI_Dumper::processSubs(
332 const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
336 // dump all sub-features;
337 bool isSubDumped = false;
338 int aNbSubs = theComposite->numberOfSubs();
339 for (int anIndex = 0; anIndex < aNbSubs; ++anIndex) {
340 FeaturePtr aFeature = theComposite->subFeature(anIndex);
341 if (isDumped(EntityPtr(aFeature)))
345 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
346 if (aCompFeat) // iteratively process composite features
347 isOk = process(aCompFeat) && isOk;
349 dumpFeature(aFeature, true);
352 bool isDumpSetName = !myEntitiesStack.empty() &&
353 myEntitiesStack.top().myEntity == EntityPtr(theComposite);
354 bool isForceModelDo = isSubDumped && isDumpSetName &&
355 (myEntitiesStack.top().myUserName || !myEntitiesStack.top().myResults.empty());
356 // It is necessary for the sketch to create its result when complete (command "model.do()").
357 // This option is set by flat theDumpModelDo.
358 // However, nested sketches are rebuilt by parent feature, so, they do not need
359 // explicit call of "model.do()". This will be controlled by the depth of the stack.
360 if (isForceModelDo || (theDumpModelDo && gCompositeStackDepth <= 1))
361 *this << "model.do()" << std::endl;
363 // dump "setName" for composite feature
366 // dump folders if any
371 void ModelHighAPI_Dumper::postpone(const EntityPtr& theEntity)
374 name(theEntity, false);
375 myPostponed.push_back(theEntity);
378 void ModelHighAPI_Dumper::dumpPostponed()
380 if (myDumpPostponedInProgress)
383 myDumpPostponedInProgress = true;
384 // make a copy of postponed entities, because the list will be updated
385 // if some features are not able to be dumped
386 std::list<EntityPtr> aPostponedCopy = myPostponed;
389 // iterate over postponed entities and try to dump them
390 std::list<EntityPtr>::const_iterator anIt = aPostponedCopy.begin();
391 for (; anIt != aPostponedCopy.end(); ++anIt) {
392 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(*anIt);
396 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
398 dumpFeature(aFeature);
401 myDumpPostponedInProgress = false;
404 void ModelHighAPI_Dumper::dumpSubFeatureNameAndColor(const std::string theSubFeatureGet,
405 const FeaturePtr& theSubFeature)
407 name(theSubFeature, false);
408 myNames[theSubFeature] = EntityName(theSubFeatureGet, theSubFeature->name(), false);
410 // store results if they have user-defined names or colors
411 std::list<ResultPtr> aResultsWithNameOrColor;
412 const std::list<ResultPtr>& aResults = theSubFeature->results();
413 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
414 for (; aResIt != aResults.end(); ++aResIt) {
415 std::string aResName = (*aResIt)->data()->name();
416 myNames[*aResIt] = EntityName(aResName, aResName, false);
417 aResultsWithNameOrColor.push_back(*aResIt);
420 // store just dumped entity to stack
421 myEntitiesStack.push(LastDumpedEntity(theSubFeature, true, aResultsWithNameOrColor));
426 bool ModelHighAPI_Dumper::exportTo(const std::string& theFileName)
429 OSD_OpenStream(aFile, theFileName.c_str(), std::ofstream::out);
430 if (!aFile.is_open())
434 for (ModulesMap::const_iterator aModIt = myModules.begin();
435 aModIt != myModules.end(); ++aModIt) {
436 aFile << "from " << aModIt->first << " import ";
437 if (aModIt->second.empty() ||
438 aModIt->second.find(std::string()) != aModIt->second.end())
439 aFile << "*"; // import whole module
441 // import specific features
442 std::set<std::string>::const_iterator anObjIt = aModIt->second.begin();
444 for (++anObjIt; anObjIt != aModIt->second.end(); ++anObjIt)
445 aFile << ", " << *anObjIt;
449 if (!myModules.empty())
452 aFile << "from salome.shaper import model" << std::endl << std::endl;
453 aFile << "model.begin()" << std::endl;
455 // dump collected data
456 aFile << myFullDump.str();
457 aFile << myDumpBuffer.str();
460 aFile << "model.end()" << std::endl;
468 void ModelHighAPI_Dumper::importModule(const std::string& theModuleName,
469 const std::string& theObject)
471 myModules[theModuleName].insert(theObject);
474 void ModelHighAPI_Dumper::dumpEntitySetName()
476 const LastDumpedEntity& aLastDumped = myEntitiesStack.top();
478 // dump "setName" for the entity
479 if (aLastDumped.myUserName) {
480 EntityName& anEntityNames = myNames[aLastDumped.myEntity];
481 if (!anEntityNames.myIsDefault)
482 myDumpBuffer << anEntityNames.myCurrentName << ".setName(\""
483 << anEntityNames.myUserName << "\")" << std::endl;
484 // don't dump "setName" for the entity twice
485 anEntityNames.myUserName.clear();
486 anEntityNames.myIsDefault = true;
488 // dump "setName" for results
489 std::list<ResultPtr>::const_iterator aResIt = aLastDumped.myResults.begin();
490 std::list<ResultPtr>::const_iterator aResEnd = aLastDumped.myResults.end();
491 for (; aResIt != aResEnd; ++aResIt) {
493 EntityName& anEntityNames = myNames[*aResIt];
494 if (!anEntityNames.myIsDefault) {
496 myDumpBuffer << ".setName(\"" << anEntityNames.myUserName << "\")" << std::endl;
497 // don't dump "setName" for the entity twice
498 anEntityNames.myUserName.clear();
499 anEntityNames.myIsDefault = true;
502 if (!isDefaultColor(*aResIt)) {
503 AttributeIntArrayPtr aColor = (*aResIt)->data()->intArray(ModelAPI_Result::COLOR_ID());
504 if (aColor && aColor->isInitialized()) {
506 myDumpBuffer << ".setColor(" << aColor->value(0) << ", " << aColor->value(1)
507 << ", " << aColor->value(2) << ")" << std::endl;
510 // set result deflection
511 if (!isDefaultDeflection(*aResIt)) {
512 AttributeDoublePtr aDeflectionAttr =
513 (*aResIt)->data()->real(ModelAPI_Result::DEFLECTION_ID());
514 if(aDeflectionAttr.get() && aDeflectionAttr->isInitialized()) {
516 myDumpBuffer << ".setDeflection(" << aDeflectionAttr->value() << ")" << std::endl;
519 // set result transparency
520 if (!isDefaultTransparency(*aResIt)) {
521 AttributeDoublePtr aTransparencyAttr =
522 (*aResIt)->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
523 if(aTransparencyAttr.get() && aTransparencyAttr->isInitialized()) {
525 myDumpBuffer << ".setTransparency(" << aTransparencyAttr->value() << ")" << std::endl;
530 myNames[aLastDumped.myEntity].myIsDumped = true;
531 myEntitiesStack.pop();
534 bool ModelHighAPI_Dumper::isDumped(const EntityPtr& theEntity) const
536 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
537 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
538 return (aFound != myNames.end() && aFound->second.myIsDumped) ||
539 myFeaturesToSkip.find(aFeature) != myFeaturesToSkip.end();
542 bool ModelHighAPI_Dumper::isDumped(const AttributeRefAttrPtr& theRefAttr) const
545 if (theRefAttr->isObject())
546 aFeature = ModelAPI_Feature::feature(theRefAttr->object());
548 aFeature = ModelAPI_Feature::feature(theRefAttr->attr()->owner());
549 return aFeature && isDumped(EntityPtr(aFeature));
552 bool ModelHighAPI_Dumper::isDumped(const AttributeRefListPtr& theRefList) const
554 std::list<ObjectPtr> aRefs = theRefList->list();
555 std::list<ObjectPtr>::iterator anIt = aRefs.begin();
556 for (; anIt != aRefs.end(); ++anIt) {
557 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
558 if (aFeature && !isDumped(EntityPtr(aFeature)))
564 bool ModelHighAPI_Dumper::isDefaultColor(const ResultPtr& theResult) const
566 AttributeIntArrayPtr aColor = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
567 if (!aColor || !aColor->isInitialized())
570 std::string aSection, aName, aDefault;
571 theResult->colorConfigInfo(aSection, aName, aDefault);
573 // dump current color
574 std::ostringstream aColorInfo;
575 aColorInfo << aColor->value(0) << "," << aColor->value(1) << "," << aColor->value(2);
577 return aDefault == aColorInfo.str();
580 bool ModelHighAPI_Dumper::isDefaultDeflection(const ResultPtr& theResult) const
582 AttributeDoublePtr aDeflectionAttr = theResult->data()->real(ModelAPI_Result::DEFLECTION_ID());
583 if(!aDeflectionAttr || !aDeflectionAttr->isInitialized()) {
587 double aCurrent = aDeflectionAttr->value();
588 double aDefault = -1;
590 bool isConstruction = false;
591 std::string aResultGroup = theResult->groupName();
592 if (aResultGroup == ModelAPI_ResultConstruction::group())
593 isConstruction = true;
594 else if (aResultGroup == ModelAPI_ResultBody::group()) {
595 GeomShapePtr aGeomShape = theResult->shape();
596 if (aGeomShape.get()) {
597 // if the shape could not be exploded on faces, it contains only wires, edges, and vertices
598 // correction of deviation for them should not influence to the application performance
599 GeomAPI_ShapeExplorer anExp(aGeomShape, GeomAPI_Shape::FACE);
600 isConstruction = !anExp.more();
604 aDefault = Config_PropManager::real("Visualization", "construction_deflection");
606 aDefault = Config_PropManager::real("Visualization", "body_deflection");
608 return fabs(aCurrent - aDefault) < 1.e-12;
611 bool ModelHighAPI_Dumper::isDefaultTransparency(const ResultPtr& theResult) const
613 AttributeDoublePtr anAttribute = theResult->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
614 if(!anAttribute || !anAttribute->isInitialized()) {
617 return fabs(anAttribute->value()) < 1.e-12;
620 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char theChar)
622 myDumpBuffer << theChar;
626 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char* theString)
628 myDumpBuffer << theString;
632 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::string& theString)
634 myDumpBuffer << theString;
638 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const bool theValue)
640 myDumpBuffer << (theValue ? "True" : "False");
644 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const int theValue)
646 myDumpBuffer << theValue;
650 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const double theValue)
652 myDumpBuffer << theValue;
656 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
658 importModule("GeomAPI", "GeomAPI_Pnt");
659 myDumpBuffer << "GeomAPI_Pnt(" << thePoint->x() << ", "
660 << thePoint->y() << ", " << thePoint->z() << ")";
664 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir)
666 importModule("GeomAPI", "GeomAPI_Dir");
667 myDumpBuffer << "GeomAPI_Dir(" << theDir->x() << ", "
668 << theDir->y() << ", " << theDir->z() << ")";
672 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
673 const std::shared_ptr<GeomDataAPI_Dir>& theDir)
675 myDumpBuffer << theDir->x() << ", " << theDir->y() << ", " << theDir->z();
679 static void dumpArray(std::ostringstream& theOutput, int theSize,
680 double* theValues, std::string* theTexts)
682 for (int i = 0; i < theSize; ++i) {
685 if (theTexts[i].empty())
686 theOutput << theValues[i];
688 theOutput << "\"" << theTexts[i] << "\"";
692 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
693 const std::shared_ptr<GeomDataAPI_Point>& thePoint)
695 static const int aSize = 3;
696 double aValues[aSize] = {thePoint->x(), thePoint->y(), thePoint->z()};
697 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY(), thePoint->textZ()};
698 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
702 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
703 const std::shared_ptr<GeomDataAPI_Point2D>& thePoint)
705 static const int aSize = 2;
706 double aValues[aSize] = {thePoint->x(), thePoint->y()};
707 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY()};
708 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
712 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
713 const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool)
715 myDumpBuffer << (theAttrBool->value() ? "True" : "False");
719 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
720 const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt)
722 std::string aText = theAttrInt->text();
724 myDumpBuffer << theAttrInt->value();
726 myDumpBuffer << "\"" << aText << "\"";
730 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
731 const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal)
733 std::string aText = theAttrReal->text();
735 myDumpBuffer << theAttrReal->value();
737 myDumpBuffer << "\"" << aText << "\"";
741 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
742 const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr)
744 myDumpBuffer << "\"" << theAttrStr->value() << "\"";
748 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FolderPtr& theFolder)
750 myDumpBuffer << name(theFolder);
754 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FeaturePtr& theEntity)
756 myDumpBuffer << name(theEntity);
758 if (!myNames[theEntity].myIsDumped) {
759 bool isUserDefinedName = !myNames[theEntity].myIsDefault;
760 // store results if they have user-defined names or colors
761 std::list<ResultPtr> aResultsWithNameOrColor;
762 const std::list<ResultPtr>& aResults = theEntity->results();
763 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
764 for (; aResIt != aResults.end(); ++aResIt) {
765 if (!myNames[*aResIt].myIsDefault || !isDefaultColor(*aResIt) ||
766 !isDefaultDeflection(*aResIt) || !isDefaultTransparency(*aResIt))
767 aResultsWithNameOrColor.push_back(*aResIt);
769 ResultCompSolidPtr aCompSolid =
770 std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*aResIt);
772 int aNbSubs = aCompSolid->numberOfSubs();
773 for (int i = 0; i < aNbSubs; ++i) {
774 ResultPtr aCurRes = aCompSolid->subResult(i);
775 if (!myNames[aCurRes].myIsDefault || !isDefaultColor(aCurRes) ||
776 !isDefaultDeflection(aCurRes) || !isDefaultTransparency(aCurRes))
777 aResultsWithNameOrColor.push_back(aCurRes);
781 // store just dumped entity to stack
782 if (myEntitiesStack.empty() || myEntitiesStack.top().myEntity != theEntity)
783 myEntitiesStack.push(
784 LastDumpedEntity(theEntity, isUserDefinedName, aResultsWithNameOrColor));
787 // remove entity from the list of not dumped items
788 myNotDumpedEntities.erase(theEntity);
792 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ResultPtr& theResult)
794 FeaturePtr aFeature = ModelAPI_Feature::feature(theResult);
797 std::list<ResultPtr> aResults = aFeature->results();
798 for(std::list<ResultPtr>::const_iterator
799 anIt = aResults.cbegin(); anIt != aResults.cend(); ++anIt, ++anIndex) {
800 if(theResult->isSame(*anIt)) {
804 ResultCompSolidPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(*anIt);
806 int aNbSubs = aCompSolid->numberOfSubs();
807 for (aSubIndex = 0; aSubIndex < aNbSubs; ++aSubIndex)
808 if (theResult->isSame(aCompSolid->subResult(aSubIndex)))
810 if (aSubIndex < aNbSubs)
816 myDumpBuffer << name(aFeature);
818 myDumpBuffer << ".result()";
820 myDumpBuffer << ".results()[" << anIndex << "]";
822 if (aSubIndex >= 0) {
823 myDumpBuffer << ".subResult(" << aSubIndex << ")";
828 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ObjectPtr& theObject)
830 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
832 myDumpBuffer << name(aFeature);
836 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
845 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const AttributePtr& theAttr)
847 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttr->owner());
849 std::string aWrapperPrefix, aWrapperSuffix;
850 // Check the attribute belongs to copied (in multi-translation or multi-rotation) feature.
851 // In this case we need to cast explicitly feature to appropriate type.
852 AttributeBooleanPtr isCopy = anOwner->boolean("Copy");
853 if (isCopy.get() && isCopy->value()) {
854 aWrapperPrefix = featureWrapper(anOwner) + "(";
855 aWrapperSuffix = ")";
856 importModule("SketchAPI");
859 myDumpBuffer << aWrapperPrefix << name(anOwner) << aWrapperSuffix
860 << "." << attributeGetter(anOwner, theAttr->id()) << "()";
864 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
865 const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr)
867 if (theRefAttr->isObject())
868 *this << theRefAttr->object();
870 *this << theRefAttr->attr();
874 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
875 const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList)
878 std::list<std::pair<ObjectPtr, AttributePtr> > aList = theRefAttrList->list();
879 bool isAdded = false;
880 std::list<std::pair<ObjectPtr, AttributePtr> >::const_iterator anIt = aList.begin();
881 for (; anIt != aList.end(); ++anIt) {
883 myDumpBuffer << ", ";
887 *this << anIt->first;
888 else if (anIt->second)
889 * this << anIt->second;
895 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
896 const std::shared_ptr<ModelAPI_AttributeReference>& theReference)
898 *this << theReference->value();
902 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
903 const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList)
905 static const int aThreshold = 2;
906 // if number of elements in the list if greater than a threshold,
907 // dump it in a separate line with specific name
908 std::string aDumped = myDumpBuffer.str();
909 if (aDumped.empty() || theRefList->size() <= aThreshold) {
911 std::list<ObjectPtr> aList = theRefList->list();
912 bool isAdded = false;
913 std::list<ObjectPtr>::const_iterator anIt = aList.begin();
914 for (; anIt != aList.end(); ++anIt) {
916 myDumpBuffer << ", ";
924 // clear buffer and store list "as is"
925 myDumpBuffer.str("");
927 // save buffer and clear it again
928 std::string aDumpedList = myDumpBuffer.str();
929 myDumpBuffer.str("");
930 // obtain name of list
931 FeaturePtr anOwner = ModelAPI_Feature::feature(theRefList->owner());
932 std::string aListName = name(anOwner) + "_objects";
933 // store all previous data
934 myDumpBuffer << aListName << " = " << aDumpedList << std::endl
935 << aDumped << aListName;
940 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
941 const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect)
943 myDumpBuffer << "model.selection(";
945 if(!theAttrSelect->isInitialized()) {
950 GeomShapePtr aShape = theAttrSelect->value();
952 aShape = theAttrSelect->context()->shape();
960 myDumpBuffer << "\"" << aShape->shapeTypeStr() << "\", \"" <<
961 theAttrSelect->namingName() << "\")";
965 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
966 const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList)
971 std::string aShapeTypeStr;
973 bool isAdded = false;
975 for(int anIndex = 0; anIndex < theAttrSelList->size(); ++anIndex) {
976 AttributeSelectionPtr anAttribute = theAttrSelList->value(anIndex);
977 aShape = anAttribute->value();
979 ResultPtr aContext = anAttribute->context();
981 aShape = aContext->shape();
989 myDumpBuffer << ", ";
993 myDumpBuffer << "model.selection(\"" <<
994 aShape->shapeTypeStr() << "\", \"" << anAttribute->namingName() << "\")";
1001 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1002 const std::shared_ptr<ModelAPI_AttributeStringArray>& theArray)
1005 for(int anIndex = 0; anIndex < theArray->size(); ++anIndex) {
1009 myDumpBuffer<<"\""<<theArray->value(anIndex)<<"\"";
1017 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
1018 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&))
1020 theDumper.myDumpBuffer << theEndl;
1022 if (!theDumper.myEntitiesStack.empty()) {
1024 // all copies have been stored into stack, pop them all
1027 // Name for composite feature is dumped when all sub-entities are dumped
1028 // (see method ModelHighAPI_Dumper::processSubs).
1029 const ModelHighAPI_Dumper::LastDumpedEntity& aLastDumped = theDumper.myEntitiesStack.top();
1030 CompositeFeaturePtr aComposite =
1031 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aLastDumped.myEntity);
1033 theDumper.dumpEntitySetName();
1034 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aLastDumped.myEntity);
1036 AttributeBooleanPtr aCopyAttr = aFeature->boolean("Copy");
1037 isCopy = aCopyAttr.get() && aCopyAttr->value();
1040 } while (isCopy && !theDumper.myEntitiesStack.empty());
1043 // store all not-dumped entities first
1044 std::set<EntityPtr> aNotDumped = theDumper.myNotDumpedEntities;
1045 std::string aBufCopy = theDumper.myDumpBuffer.str();
1046 theDumper.clear(true);
1047 std::set<EntityPtr>::const_iterator anIt = aNotDumped.begin();
1048 for (; anIt != aNotDumped.end(); ++anIt) {
1049 // if the feature is composite, dump it with all subs
1050 CompositeFeaturePtr aCompFeat =
1051 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anIt);
1053 theDumper.process(aCompFeat, true);
1055 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
1056 theDumper.dumpFeature(aFeature, true);
1057 // dump the Projection feature which produces this "Copy" entity
1058 AttributeBooleanPtr aCopyAttr = aFeature->boolean("Copy");
1059 if (aCopyAttr.get() && aCopyAttr->value())
1061 const std::set<AttributePtr>& aRefs = aFeature->data()->refsToMe();
1062 std::set<AttributePtr>::iterator aRefIt = aRefs.begin();
1063 for (; aRefIt != aRefs.end(); ++aRefIt)
1064 if ((*aRefIt)->id() == "ProjectedFeature")
1065 { // process projection only
1066 FeaturePtr anOwner = ModelAPI_Feature::feature((*aRefIt)->owner());
1067 if (anOwner && !theDumper.isDumped(EntityPtr(anOwner)))
1068 theDumper.dumpFeature(anOwner, true);
1074 // avoid multiple empty lines
1075 size_t anInd = std::string::npos;
1076 while ((anInd = aBufCopy.find("\n\n\n")) != std::string::npos)
1077 aBufCopy.erase(anInd, 1);
1078 // then store currently dumped string
1079 theDumper.myFullDump << aBufCopy;
1081 // now, store all postponed features
1082 theDumper.dumpPostponed();