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_ResultConstruction.h>
53 #include <ModelAPI_ResultPart.h>
54 #include <ModelAPI_Tools.h>
56 #include <ModelGeomAlgo_Shape.h>
58 #include <PartSetPlugin_Part.h>
60 #include <OSD_OpenFile.hxx>
64 static int gCompositeStackDepth = 0;
66 ModelHighAPI_Dumper* ModelHighAPI_Dumper::mySelf = 0;
68 ModelHighAPI_Dumper::ModelHighAPI_Dumper()
69 : myGeometricalSelection(false)
74 void ModelHighAPI_Dumper::setInstance(ModelHighAPI_Dumper* theDumper)
80 ModelHighAPI_Dumper* ModelHighAPI_Dumper::getInstance()
85 void ModelHighAPI_Dumper::clear(bool bufferOnly)
88 myDumpBuffer << std::setprecision(16);
94 myFullDump << std::setprecision(16);
98 myFeatureCount.clear();
99 while (!myEntitiesStack.empty())
100 myEntitiesStack.pop();
103 myDumpPostponedInProgress = false;
107 void ModelHighAPI_Dumper::clearNotDumped()
109 myNotDumpedEntities.clear();
112 // Convert string to integer. If the string is not a number, return -1
113 static int toInt(const std::string& theString)
115 std::string::const_iterator aChar = theString.begin();
116 for (; aChar != theString.end(); ++aChar)
117 if (!std::isdigit(*aChar))
119 if (aChar != theString.end())
120 return -1; // not a number
121 return std::stoi(theString);
124 const std::string& ModelHighAPI_Dumper::name(const EntityPtr& theEntity,
125 bool theSaveNotDumped,
126 bool theUseEntityName)
128 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
129 if (aFound != myNames.end())
130 return aFound->second.myCurrentName;
132 // entity is not found, store it
133 std::string aName, aKind;
134 bool isDefaultName = false;
135 bool isSaveNotDumped = theSaveNotDumped;
136 std::ostringstream aDefaultName;
137 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
139 aName = aFeature->name();
140 aKind = aFeature->getKind();
142 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(theEntity);
144 aName = aFolder->data()->name();
145 aKind = ModelAPI_Folder::ID();
146 isSaveNotDumped = false;
150 ObjectPtr anObject = std::dynamic_pointer_cast<ModelAPI_Object>(theEntity);
152 DocumentPtr aDoc = anObject->document();
153 std::pair<int, int>& aNbFeatures = myFeatureCount[aDoc][aKind];
154 aNbFeatures.first += 1;
156 size_t anIndex = aName.find(aKind);
157 if (anIndex == 0 && aName[aKind.length()] == '_') { // name starts with "FeatureKind_"
158 std::string anIdStr = aName.substr(aKind.length() + 1);
159 int anId = toInt(anIdStr);
161 // Check number of already registered objects of such kind. Index of current object
162 // should be the same to identify feature's name as automatically generated.
163 if (aNbFeatures.first == anId && aNbFeatures.second < anId) {
164 // name is not user-defined
165 isDefaultName = true;
168 if (anId > aNbFeatures.second)
169 aNbFeatures.second = anId;
172 // obtain default name for the feature
173 if (theUseEntityName)
174 aDefaultName << aName;
177 NbFeaturesMap::const_iterator aFIt = myFeatureCount.begin();
178 for (; aFIt != myFeatureCount.end(); ++aFIt) {
179 std::map<std::string, std::pair<int, int> >::const_iterator aFound =
180 aFIt->second.find(aKind);
181 if (aFound != aFIt->second.end())
182 aFullIndex += aFound->second.first;
184 aDefaultName << aKind << "_" << aFullIndex;
188 myNames[theEntity] = EntityName(aDefaultName.str(), aName, isDefaultName);
190 myNotDumpedEntities.insert(theEntity);
192 // store names of results
194 saveResultNames(aFeature);
196 return myNames[theEntity].myCurrentName;
199 const std::string& ModelHighAPI_Dumper::parentName(const FeaturePtr& theEntity)
201 const std::set<AttributePtr>& aRefs = theEntity->data()->refsToMe();
202 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
203 for (; aRefIt != aRefs.end(); ++aRefIt) {
204 CompositeFeaturePtr anOwner = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(
205 ModelAPI_Feature::feature((*aRefIt)->owner()));
207 return name(anOwner);
210 static const std::string DUMMY;
214 void ModelHighAPI_Dumper::saveResultNames(const FeaturePtr& theFeature)
216 // Default name of the feature
217 bool isFeatureDefaultName = myNames[theFeature].myIsDefault;
219 // Save only names of results which is not correspond to default feature name
220 const std::list<ResultPtr>& aResults = theFeature->results();
221 std::list<ResultPtr> allRes;
222 ModelAPI_Tools::allResults(theFeature, allRes);
223 for(std::list<ResultPtr>::iterator aRes = allRes.begin(); aRes != allRes.end(); aRes++) {
224 std::pair<std::string, bool> aName = ModelAPI_Tools::getDefaultName(*aRes);
225 std::string aDefaultName = aName.first;
226 std::string aResName = (*aRes)->data()->name();
227 bool isUserDefined = !(isFeatureDefaultName && aDefaultName == aResName);
229 EntityName(aResName, (isUserDefined ? aResName : std::string()), !isUserDefined);
233 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc,
234 const std::string& theFileName)
236 // dump top level document feature
237 static const std::string aDocName("partSet");
238 myNames[theDoc] = EntityName(aDocName, std::string(), true);
239 *this << aDocName << " = model.moduleDocument()" << std::endl;
241 // dump subfeatures and store result to file
242 return process(theDoc) && exportTo(theFileName);
245 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc)
248 std::list<ObjectPtr> anObjects = theDoc->allObjects();
249 std::list<ObjectPtr>::const_iterator anObjIt = anObjects.begin();
250 // firstly, dump all parameters
251 for (; anObjIt != anObjects.end(); ++ anObjIt) {
252 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIt);
254 dumpParameter(aFeature);
256 // dump all other features
257 for (anObjIt = anObjects.begin(); anObjIt != anObjects.end(); ++anObjIt) {
258 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anObjIt);
259 if (aCompFeat) // iteratively process composite features
260 isOk = process(aCompFeat) && isOk;
261 else if (!isDumped(EntityPtr(*anObjIt))) {
263 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(*anObjIt);
267 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIt);
268 if (aFeature) // dump common feature
269 dumpFeature(aFeature);
273 // dump folders if any
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(EntityPtr(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(EntityPtr(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::postpone(const EntityPtr& theEntity)
365 name(theEntity, false);
366 myPostponed.push_back(theEntity);
369 void ModelHighAPI_Dumper::dumpPostponed(bool theDumpFolders)
371 if (myDumpPostponedInProgress)
374 myDumpPostponedInProgress = true;
375 // make a copy of postponed entities, because the list will be updated
376 // if some features are not able to be dumped
377 std::list<EntityPtr> aPostponedCopy = myPostponed;
380 // iterate over postponed entities and try to dump them
381 std::list<EntityPtr>::const_iterator anIt = aPostponedCopy.begin();
382 for (; anIt != aPostponedCopy.end(); ++anIt) {
383 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(*anIt);
388 myPostponed.push_back(*anIt);
391 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
393 dumpFeature(aFeature, true);
396 myDumpPostponedInProgress = false;
399 void ModelHighAPI_Dumper::dumpSubFeatureNameAndColor(const std::string theSubFeatureGet,
400 const FeaturePtr& theSubFeature)
402 name(theSubFeature, false);
403 myNames[theSubFeature] = EntityName(theSubFeatureGet, theSubFeature->name(), false);
405 // store results if they have user-defined names or colors
406 std::list<ResultPtr> aResultsWithNameOrColor;
407 const std::list<ResultPtr>& aResults = theSubFeature->results();
408 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
409 for (; aResIt != aResults.end(); ++aResIt) {
410 std::string aResName = (*aResIt)->data()->name();
411 myNames[*aResIt] = EntityName(aResName, aResName, false);
412 aResultsWithNameOrColor.push_back(*aResIt);
415 // store just dumped entity to stack
416 myEntitiesStack.push(LastDumpedEntity(theSubFeature, true, aResultsWithNameOrColor));
421 bool ModelHighAPI_Dumper::exportTo(const std::string& theFileName)
424 OSD_OpenStream(aFile, theFileName.c_str(), std::ofstream::out);
425 if (!aFile.is_open())
428 // standard header (encoding + imported modules)
429 aFile << "# -*- coding: utf-8 -*-" << std::endl << std::endl;
430 for (ModulesMap::const_iterator aModIt = myModules.begin();
431 aModIt != myModules.end(); ++aModIt) {
432 aFile << "from " << aModIt->first << " import ";
433 if (aModIt->second.empty() ||
434 aModIt->second.find(std::string()) != aModIt->second.end())
435 aFile << "*"; // import whole module
437 // import specific features
438 std::set<std::string>::const_iterator anObjIt = aModIt->second.begin();
440 for (++anObjIt; anObjIt != aModIt->second.end(); ++anObjIt)
441 aFile << ", " << *anObjIt;
445 if (!myModules.empty())
448 aFile << "from salome.shaper import model" << std::endl << std::endl;
449 aFile << "model.begin()" << std::endl;
451 // dump collected data
452 aFile << myFullDump.str();
453 aFile << myDumpBuffer.str();
456 aFile << "model.end()" << std::endl;
464 void ModelHighAPI_Dumper::importModule(const std::string& theModuleName,
465 const std::string& theObject)
467 myModules[theModuleName].insert(theObject);
470 void ModelHighAPI_Dumper::dumpEntitySetName()
472 const LastDumpedEntity& aLastDumped = myEntitiesStack.top();
473 bool isBufferEmpty = myDumpBuffer.str().empty();
475 // dump "setName" for the entity
476 if (aLastDumped.myUserName) {
477 EntityName& anEntityNames = myNames[aLastDumped.myEntity];
478 if (!anEntityNames.myIsDefault)
479 myDumpBuffer << anEntityNames.myCurrentName << ".setName(\""
480 << anEntityNames.myUserName << "\")" << std::endl;
481 // don't dump "setName" for the entity twice
482 anEntityNames.myUserName.clear();
483 anEntityNames.myIsDefault = true;
485 // dump "setName" for results
486 std::list<ResultPtr>::const_iterator aResIt = aLastDumped.myResults.begin();
487 std::list<ResultPtr>::const_iterator aResEnd = aLastDumped.myResults.end();
488 for (; aResIt != aResEnd; ++aResIt) {
490 EntityName& anEntityNames = myNames[*aResIt];
491 if (!anEntityNames.myIsDefault) {
493 myDumpBuffer << ".setName(\"" << anEntityNames.myUserName << "\")" << std::endl;
494 // don't dump "setName" for the entity twice
495 anEntityNames.myUserName.clear();
496 anEntityNames.myIsDefault = true;
499 if (!isDefaultColor(*aResIt)) {
500 AttributeIntArrayPtr aColor = (*aResIt)->data()->intArray(ModelAPI_Result::COLOR_ID());
501 if (aColor && aColor->isInitialized()) {
503 myDumpBuffer << ".setColor(" << aColor->value(0) << ", " << aColor->value(1)
504 << ", " << aColor->value(2) << ")" << std::endl;
507 // set result deflection
508 if (!isDefaultDeflection(*aResIt)) {
509 AttributeDoublePtr aDeflectionAttr =
510 (*aResIt)->data()->real(ModelAPI_Result::DEFLECTION_ID());
511 if(aDeflectionAttr.get() && aDeflectionAttr->isInitialized()) {
513 myDumpBuffer << ".setDeflection(" << aDeflectionAttr->value() << ")" << std::endl;
516 // set result transparency
517 if (!isDefaultTransparency(*aResIt)) {
518 AttributeDoublePtr aTransparencyAttr =
519 (*aResIt)->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
520 if(aTransparencyAttr.get() && aTransparencyAttr->isInitialized()) {
522 myDumpBuffer << ".setTransparency(" << aTransparencyAttr->value() << ")" << std::endl;
527 myNames[aLastDumped.myEntity].myIsDumped = true;
528 myEntitiesStack.pop();
530 // clean buffer if it was clear before
532 myFullDump << myDumpBuffer.str();
533 myDumpBuffer.str("");
537 bool ModelHighAPI_Dumper::isDumped(const EntityPtr& theEntity) const
539 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
540 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
541 return (aFound != myNames.end() && aFound->second.myIsDumped) ||
542 myFeaturesToSkip.find(aFeature) != myFeaturesToSkip.end();
545 bool ModelHighAPI_Dumper::isDumped(const AttributeRefAttrPtr& theRefAttr) const
548 if (theRefAttr->isObject())
549 aFeature = ModelAPI_Feature::feature(theRefAttr->object());
551 aFeature = ModelAPI_Feature::feature(theRefAttr->attr()->owner());
552 return aFeature && isDumped(EntityPtr(aFeature));
555 bool ModelHighAPI_Dumper::isDumped(const AttributeRefListPtr& theRefList) const
557 std::list<ObjectPtr> aRefs = theRefList->list();
558 std::list<ObjectPtr>::iterator anIt = aRefs.begin();
559 for (; anIt != aRefs.end(); ++anIt) {
560 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
561 if (aFeature && !isDumped(EntityPtr(aFeature)))
567 bool ModelHighAPI_Dumper::isDefaultColor(const ResultPtr& theResult) const
569 AttributeIntArrayPtr aColor = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
570 if (!aColor || !aColor->isInitialized())
573 std::string aSection, aName, aDefault;
574 theResult->colorConfigInfo(aSection, aName, aDefault);
576 // dump current color
577 std::ostringstream aColorInfo;
578 aColorInfo << aColor->value(0) << "," << aColor->value(1) << "," << aColor->value(2);
580 return aDefault == aColorInfo.str();
583 bool ModelHighAPI_Dumper::isDefaultDeflection(const ResultPtr& theResult) const
585 AttributeDoublePtr aDeflectionAttr = theResult->data()->real(ModelAPI_Result::DEFLECTION_ID());
586 if(!aDeflectionAttr || !aDeflectionAttr->isInitialized()) {
590 double aCurrent = aDeflectionAttr->value();
591 double aDefault = -1;
593 bool isConstruction = false;
594 std::string aResultGroup = theResult->groupName();
595 if (aResultGroup == ModelAPI_ResultConstruction::group())
596 isConstruction = true;
597 else if (aResultGroup == ModelAPI_ResultBody::group()) {
598 GeomShapePtr aGeomShape = theResult->shape();
599 if (aGeomShape.get()) {
600 // if the shape could not be exploded on faces, it contains only wires, edges, and vertices
601 // correction of deviation for them should not influence to the application performance
602 GeomAPI_ShapeExplorer anExp(aGeomShape, GeomAPI_Shape::FACE);
603 isConstruction = !anExp.more();
607 aDefault = Config_PropManager::real("Visualization", "construction_deflection");
609 aDefault = Config_PropManager::real("Visualization", "body_deflection");
611 return fabs(aCurrent - aDefault) < 1.e-12;
614 bool ModelHighAPI_Dumper::isDefaultTransparency(const ResultPtr& theResult) const
616 AttributeDoublePtr anAttribute = theResult->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
617 if(!anAttribute || !anAttribute->isInitialized()) {
620 return fabs(anAttribute->value()) < 1.e-12;
623 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char theChar)
625 myDumpBuffer << theChar;
629 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char* theString)
631 myDumpBuffer << theString;
635 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::string& theString)
637 myDumpBuffer << theString;
641 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const bool theValue)
643 myDumpBuffer << (theValue ? "True" : "False");
647 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const int theValue)
649 myDumpBuffer << theValue;
653 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const double theValue)
655 myDumpBuffer << theValue;
659 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
661 importModule("GeomAPI", "GeomAPI_Pnt");
662 myDumpBuffer << "GeomAPI_Pnt(" << thePoint->x() << ", "
663 << thePoint->y() << ", " << thePoint->z() << ")";
667 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir)
669 importModule("GeomAPI", "GeomAPI_Dir");
670 myDumpBuffer << "GeomAPI_Dir(" << theDir->x() << ", "
671 << theDir->y() << ", " << theDir->z() << ")";
675 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
676 const std::shared_ptr<GeomDataAPI_Dir>& theDir)
678 myDumpBuffer << theDir->x() << ", " << theDir->y() << ", " << theDir->z();
682 static void dumpArray(std::ostringstream& theOutput, int theSize,
683 double* theValues, std::string* theTexts)
685 for (int i = 0; i < theSize; ++i) {
688 if (theTexts[i].empty())
689 theOutput << theValues[i];
691 theOutput << "\"" << theTexts[i] << "\"";
695 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
696 const std::shared_ptr<GeomDataAPI_Point>& thePoint)
698 static const int aSize = 3;
699 double aValues[aSize] = {thePoint->x(), thePoint->y(), thePoint->z()};
700 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY(), thePoint->textZ()};
701 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
705 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
706 const std::shared_ptr<GeomDataAPI_Point2D>& thePoint)
708 static const int aSize = 2;
709 double aValues[aSize] = {thePoint->x(), thePoint->y()};
710 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY()};
711 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
715 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
716 const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool)
718 myDumpBuffer << (theAttrBool->value() ? "True" : "False");
722 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
723 const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt)
725 std::string aText = theAttrInt->text();
727 myDumpBuffer << theAttrInt->value();
729 myDumpBuffer << "\"" << aText << "\"";
733 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
734 const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal)
736 std::string aText = theAttrReal->text();
738 myDumpBuffer << theAttrReal->value();
740 myDumpBuffer << "\"" << aText << "\"";
744 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
745 const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr)
747 myDumpBuffer << "\"" << theAttrStr->value() << "\"";
751 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FolderPtr& theFolder)
753 myDumpBuffer << name(theFolder);
755 // add dumped folder to a stack
756 if (!myNames[theFolder].myIsDumped &&
757 (myEntitiesStack.empty() || myEntitiesStack.top().myEntity != theFolder))
758 myEntitiesStack.push(LastDumpedEntity(theFolder, !myNames[theFolder].myIsDefault));
763 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FeaturePtr& theEntity)
765 myDumpBuffer << name(theEntity);
767 if (!myNames[theEntity].myIsDumped) {
768 bool isUserDefinedName = !myNames[theEntity].myIsDefault;
769 // store results if they have user-defined names or colors
770 std::list<ResultPtr> aResultsWithNameOrColor;
771 std::list<ResultPtr> allRes;
772 ModelAPI_Tools::allResults(theEntity, allRes);
773 for(std::list<ResultPtr>::iterator aRes = allRes.begin(); aRes != allRes.end(); aRes++) {
774 if(!myNames[*aRes].myIsDefault || !isDefaultColor(*aRes) ||
775 !isDefaultDeflection(*aRes) || !isDefaultTransparency(*aRes))
776 aResultsWithNameOrColor.push_back(*aRes);
778 // store just dumped entity to stack
779 if (myEntitiesStack.empty() || myEntitiesStack.top().myEntity != theEntity)
780 myEntitiesStack.push(
781 LastDumpedEntity(theEntity, isUserDefinedName, aResultsWithNameOrColor));
784 // remove entity from the list of not dumped items
785 myNotDumpedEntities.erase(theEntity);
789 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ResultPtr& theResult)
791 // iterate in the structure of sub-results to the parent
792 ResultPtr aCurRes = theResult;
793 FeaturePtr aFeature = ModelAPI_Feature::feature(theResult);
794 std::list<int> anIndices; // indexes of results in the parent result, starting from topmost
795 while(aCurRes.get()) {
796 ResultBodyPtr aParent = ModelAPI_Tools::bodyOwner(aCurRes);
798 anIndices.push_front(ModelAPI_Tools::bodyIndex(aCurRes));
799 } else { // index of the result in the feature
800 std::list<ResultPtr>::const_iterator aRes = aFeature->results().cbegin();
801 for(int anIndex = 0; aRes != aFeature->results().cend(); aRes++, anIndex++) {
802 if (*aRes == aCurRes) {
803 anIndices.push_front(anIndex);
811 myDumpBuffer << name(aFeature);
812 for (std::list<int>::iterator anI = anIndices.begin(); anI != anIndices.end(); anI++) {
813 if (anI == anIndices.begin()) {
815 myDumpBuffer << ".result()";
818 myDumpBuffer << ".results()[" << *anI << "]";
821 myDumpBuffer << ".subResult(" << *anI << ")";
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 static int possibleSelectionsByPoint(const GeomPointPtr& thePoint,
941 const GeomAPI_Shape::ShapeType& theType,
942 const FeaturePtr& theStartFeature,
943 const FeaturePtr& theEndFeature)
945 DocumentPtr aDoc1 = theStartFeature->document();
946 DocumentPtr aDoc2 = theEndFeature->document();
948 std::list<FeaturePtr> aFeatures = aDoc1->allFeatures();
949 if (aDoc1 != aDoc2) {
950 std::list<FeaturePtr> anAdditionalFeatures = aDoc2->allFeatures();
951 aFeatures.insert(aFeatures.end(), anAdditionalFeatures.begin(), anAdditionalFeatures.end());
954 CompositeFeaturePtr aLastCompositeFeature;
956 std::list<FeaturePtr>::const_iterator aFIt = aFeatures.begin();
957 while (aFIt != aFeatures.end() && *aFIt != theStartFeature) {
958 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFIt);
960 aLastCompositeFeature = aCompFeat;
965 GeomShapePtr aSubshape;
966 int aNbPossibleSelections = 0;
967 for (; aFIt != aFeatures.end() && *aFIt != theEndFeature; ++aFIt) {
968 if (aLastCompositeFeature && aLastCompositeFeature->isSub(*aFIt))
970 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFIt);
972 aLastCompositeFeature = aCompFeat;
974 if (ModelGeomAlgo_Shape::findSubshapeByPoint(*aFIt, thePoint, theType, aResult, aSubshape))
975 ++aNbPossibleSelections;
977 return aNbPossibleSelections;
980 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
981 const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect)
983 myDumpBuffer << "model.selection(";
985 if(!theAttrSelect->isInitialized()) {
990 GeomShapePtr aShape = theAttrSelect->value();
992 aShape = theAttrSelect->context()->shape();
1000 // how to dump selection: construction features are dumped by name always
1001 bool isDumpByGeom = myGeometricalSelection;
1002 FeaturePtr aSelectedFeature;
1004 ResultPtr aRes = theAttrSelect->context();
1006 aSelectedFeature = ModelAPI_Feature::feature(aRes->data()->owner());
1007 if (aSelectedFeature)
1008 isDumpByGeom = aSelectedFeature->isInHistory();
1012 myDumpBuffer << "\"" << aShape->shapeTypeStr();
1014 GeomPointPtr aMiddlePoint = aShape->middlePoint();
1015 // calculate number of features, which could be selected by the same point
1016 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttrSelect->owner());
1017 int aNbPossibleSelections =
1018 possibleSelectionsByPoint(aMiddlePoint, aShape->shapeType(), aSelectedFeature, anOwner);
1020 // produce the index if the number of applicable features is greater than 1
1021 std::string anIndex;
1022 if (aNbPossibleSelections > 1) {
1023 std::ostringstream anOutput;
1024 anOutput << "_" << aNbPossibleSelections;
1025 anIndex = anOutput.str();
1028 myDumpBuffer << anIndex << "\", ";
1029 *this << aMiddlePoint;
1032 myDumpBuffer << "\", \"" << theAttrSelect->namingName() << "\"";
1033 myDumpBuffer << ")";
1037 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1038 const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList)
1040 static const int aThreshold = 2;
1041 // if number of elements in the list if greater than a threshold,
1042 // dump it in a separate line with specific name
1043 std::string aDumped = myDumpBuffer.str();
1045 if (aDumped.empty() || theAttrSelList->size() <= aThreshold) {
1046 myDumpBuffer << "[";
1048 GeomShapePtr aShape;
1049 std::string aShapeTypeStr;
1051 bool isAdded = false;
1053 for(int anIndex = 0; anIndex < theAttrSelList->size(); ++anIndex) {
1054 AttributeSelectionPtr anAttribute = theAttrSelList->value(anIndex);
1055 aShape = anAttribute->value();
1057 ResultPtr aContext = anAttribute->context();
1059 aShape = aContext->shape();
1067 myDumpBuffer << ", ";
1071 *this << anAttribute;
1074 myDumpBuffer << "]";
1076 // clear buffer and store list "as is"
1077 myDumpBuffer.str("");
1078 *this << theAttrSelList;
1079 // save buffer and clear it again
1080 std::string aDumpedList = myDumpBuffer.str();
1081 myDumpBuffer.str("");
1082 // obtain name of list (the feature may contain several selection lists)
1083 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttrSelList->owner());
1084 std::string aListName = name(anOwner) + "_objects";
1085 std::list<AttributePtr> aSelLists =
1086 anOwner->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
1087 if (aSelLists.size() > 1) {
1089 for (std::list<AttributePtr>::iterator aSIt = aSelLists.begin();
1090 aSIt != aSelLists.end(); ++aSIt, ++anIndex)
1091 if ((*aSIt).get() == theAttrSelList.get())
1093 std::ostringstream aSStream;
1094 aSStream << aListName << "_" << anIndex;
1095 aListName = aSStream.str();
1097 // store all previous data
1098 myDumpBuffer << aListName << " = " << aDumpedList << std::endl
1099 << aDumped << aListName;
1104 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1105 const std::shared_ptr<ModelAPI_AttributeStringArray>& theArray)
1108 for(int anIndex = 0; anIndex < theArray->size(); ++anIndex) {
1112 myDumpBuffer<<"\""<<theArray->value(anIndex)<<"\"";
1120 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
1121 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&))
1123 theDumper.myDumpBuffer << theEndl;
1125 if (!theDumper.myEntitiesStack.empty()) {
1127 // all copies have been stored into stack, pop them all
1130 // Name for composite feature is dumped when all sub-entities are dumped
1131 // (see method ModelHighAPI_Dumper::processSubs).
1132 const ModelHighAPI_Dumper::LastDumpedEntity& aLastDumped = theDumper.myEntitiesStack.top();
1133 CompositeFeaturePtr aComposite =
1134 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aLastDumped.myEntity);
1136 theDumper.dumpEntitySetName();
1137 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aLastDumped.myEntity);
1139 AttributeBooleanPtr aCopyAttr = aFeature->boolean("Copy");
1140 isCopy = aCopyAttr.get() && aCopyAttr->value();
1143 } while (isCopy && !theDumper.myEntitiesStack.empty());
1146 // store all not-dumped entities first
1147 std::set<EntityPtr> aNotDumped = theDumper.myNotDumpedEntities;
1148 std::string aBufCopy = theDumper.myDumpBuffer.str();
1149 theDumper.clear(true);
1150 std::set<EntityPtr>::const_iterator anIt = aNotDumped.begin();
1151 for (; anIt != aNotDumped.end(); ++anIt) {
1152 // if the feature is composite, dump it with all subs
1153 CompositeFeaturePtr aCompFeat =
1154 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anIt);
1156 theDumper.process(aCompFeat, true);
1158 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
1159 theDumper.dumpFeature(aFeature, true);
1160 // dump the Projection feature which produces this "Copy" entity
1161 AttributeBooleanPtr aCopyAttr = aFeature->boolean("Copy");
1162 if (aCopyAttr.get() && aCopyAttr->value())
1164 const std::set<AttributePtr>& aRefs = aFeature->data()->refsToMe();
1165 std::set<AttributePtr>::iterator aRefIt = aRefs.begin();
1166 for (; aRefIt != aRefs.end(); ++aRefIt)
1167 if ((*aRefIt)->id() == "ProjectedFeature")
1168 { // process projection only
1169 FeaturePtr anOwner = ModelAPI_Feature::feature((*aRefIt)->owner());
1170 if (anOwner && !theDumper.isDumped(EntityPtr(anOwner)))
1171 theDumper.dumpFeature(anOwner, true);
1177 // avoid multiple empty lines
1178 size_t anInd = std::string::npos;
1179 while ((anInd = aBufCopy.find("\n\n\n")) != std::string::npos)
1180 aBufCopy.erase(anInd, 1);
1181 // then store currently dumped string
1182 theDumper.myFullDump << aBufCopy;
1184 // now, store all postponed features
1185 theDumper.dumpPostponed();