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>
28 #include <GeomAlgoAPI_NExplode.h>
30 #include <GeomDataAPI_Dir.h>
31 #include <GeomDataAPI_Point.h>
32 #include <GeomDataAPI_Point2D.h>
34 #include <ModelAPI_AttributeBoolean.h>
35 #include <ModelAPI_AttributeDouble.h>
36 #include <ModelAPI_AttributeIntArray.h>
37 #include <ModelAPI_AttributeInteger.h>
38 #include <ModelAPI_AttributeRefAttr.h>
39 #include <ModelAPI_AttributeRefAttrList.h>
40 #include <ModelAPI_AttributeReference.h>
41 #include <ModelAPI_AttributeRefList.h>
42 #include <ModelAPI_AttributeSelection.h>
43 #include <ModelAPI_AttributeSelectionList.h>
44 #include <ModelAPI_AttributeString.h>
45 #include <ModelAPI_AttributeStringArray.h>
46 #include <ModelAPI_CompositeFeature.h>
47 #include <ModelAPI_Document.h>
48 #include <ModelAPI_Entity.h>
49 #include <ModelAPI_Feature.h>
50 #include <ModelAPI_Folder.h>
51 #include <ModelAPI_Result.h>
52 #include <ModelAPI_ResultBody.h>
53 #include <ModelAPI_ResultConstruction.h>
54 #include <ModelAPI_ResultPart.h>
55 #include <ModelAPI_Session.h>
56 #include <ModelAPI_Tools.h>
58 #include <ModelGeomAlgo_Shape.h>
60 #include <PartSetPlugin_Part.h>
62 #include <OSD_OpenFile.hxx>
66 static int gCompositeStackDepth = 0;
68 ModelHighAPI_Dumper* ModelHighAPI_Dumper::mySelf = 0;
70 ModelHighAPI_Dumper::ModelHighAPI_Dumper()
71 : myGeometricalSelection(false)
76 void ModelHighAPI_Dumper::setInstance(ModelHighAPI_Dumper* theDumper)
82 ModelHighAPI_Dumper* ModelHighAPI_Dumper::getInstance()
87 void ModelHighAPI_Dumper::clear(bool bufferOnly)
90 myDumpBuffer << std::setprecision(16);
96 myFullDump << std::setprecision(16);
100 myFeatureCount.clear();
101 while (!myEntitiesStack.empty())
102 myEntitiesStack.pop();
105 myDumpPostponedInProgress = false;
109 void ModelHighAPI_Dumper::clearNotDumped()
111 myNotDumpedEntities.clear();
114 // Convert string to integer. If the string is not a number, return -1
115 static int toInt(const std::string& theString)
117 std::string::const_iterator aChar = theString.begin();
118 for (; aChar != theString.end(); ++aChar)
119 if (!std::isdigit(*aChar))
121 if (aChar != theString.end())
122 return -1; // not a number
123 return std::stoi(theString);
126 const std::string& ModelHighAPI_Dumper::name(const EntityPtr& theEntity,
127 bool theSaveNotDumped,
128 bool theUseEntityName)
130 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
131 if (aFound != myNames.end())
132 return aFound->second.myCurrentName;
134 // entity is not found, store it
135 std::string aName, aKind;
136 bool isDefaultName = false;
137 bool isSaveNotDumped = theSaveNotDumped;
138 std::ostringstream aDefaultName;
139 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
141 aName = aFeature->name();
142 aKind = aFeature->getKind();
144 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(theEntity);
146 aName = aFolder->data()->name();
147 aKind = ModelAPI_Folder::ID();
148 isSaveNotDumped = false;
152 ObjectPtr anObject = std::dynamic_pointer_cast<ModelAPI_Object>(theEntity);
154 DocumentPtr aDoc = anObject->document();
155 std::pair<int, int>& aNbFeatures = myFeatureCount[aDoc][aKind];
156 aNbFeatures.first += 1;
158 size_t anIndex = aName.find(aKind);
159 if (anIndex == 0 && aName[aKind.length()] == '_') { // name starts with "FeatureKind_"
160 std::string anIdStr = aName.substr(aKind.length() + 1);
161 int anId = toInt(anIdStr);
163 // Check number of already registered objects of such kind. Index of current object
164 // should be the same to identify feature's name as automatically generated.
165 if (aNbFeatures.first == anId && aNbFeatures.second < anId) {
166 // name is not user-defined
167 isDefaultName = true;
169 // check there are postponed features of this kind,
170 // dump their names, because the sequence of features may be changed
171 for (std::list<EntityPtr>::const_iterator aPpIt = myPostponed.begin();
172 aPpIt != myPostponed.end(); ++aPpIt) {
173 FeaturePtr aCurFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*aPpIt);
174 if (aCurFeature && aCurFeature->getKind() == aKind) {
175 myNames[*aPpIt].myIsDefault = false;
176 isDefaultName = false;
181 if (anId > aNbFeatures.second)
182 aNbFeatures.second = anId;
185 // obtain default name for the feature
186 if (theUseEntityName)
187 aDefaultName << aName;
190 NbFeaturesMap::const_iterator aFIt = myFeatureCount.begin();
191 for (; aFIt != myFeatureCount.end(); ++aFIt) {
192 std::map<std::string, std::pair<int, int> >::const_iterator aFound =
193 aFIt->second.find(aKind);
194 if (aFound != aFIt->second.end())
195 aFullIndex += aFound->second.first;
197 aDefaultName << aKind << "_" << aFullIndex;
201 myNames[theEntity] = EntityName(aDefaultName.str(), aName, isDefaultName);
203 myNotDumpedEntities.insert(theEntity);
205 // store names of results
207 saveResultNames(aFeature);
209 return myNames[theEntity].myCurrentName;
212 const std::string& ModelHighAPI_Dumper::parentName(const FeaturePtr& theEntity)
214 const std::set<AttributePtr>& aRefs = theEntity->data()->refsToMe();
215 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
216 for (; aRefIt != aRefs.end(); ++aRefIt) {
217 CompositeFeaturePtr anOwner = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(
218 ModelAPI_Feature::feature((*aRefIt)->owner()));
220 return name(anOwner);
223 static const std::string DUMMY;
227 void ModelHighAPI_Dumper::saveResultNames(const FeaturePtr& theFeature)
229 // Default name of the feature
230 bool isFeatureDefaultName = myNames[theFeature].myIsDefault;
232 // Save only names of results which is not correspond to default feature name
233 const std::list<ResultPtr>& aResults = theFeature->results();
234 std::list<ResultPtr> allRes;
235 ModelAPI_Tools::allResults(theFeature, allRes);
236 for(std::list<ResultPtr>::iterator aRes = allRes.begin(); aRes != allRes.end(); aRes++) {
237 std::pair<std::string, bool> aName = ModelAPI_Tools::getDefaultName(*aRes);
238 std::string aDefaultName = aName.first;
239 std::string aResName = (*aRes)->data()->name();
240 bool isUserDefined = !(isFeatureDefaultName && aDefaultName == aResName);
242 EntityName(aResName, (isUserDefined ? aResName : std::string()), !isUserDefined);
246 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc,
247 const std::string& theFileName)
249 // dump top level document feature
250 static const std::string aDocName("partSet");
251 myNames[theDoc] = EntityName(aDocName, std::string(), true);
252 *this << aDocName << " = model.moduleDocument()" << std::endl;
254 // dump subfeatures and store result to file
255 return process(theDoc) && exportTo(theFileName);
258 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc)
261 std::list<ObjectPtr> anObjects = theDoc->allObjects();
262 std::list<ObjectPtr>::const_iterator anObjIt = anObjects.begin();
263 // firstly, dump all parameters
264 for (; anObjIt != anObjects.end(); ++ anObjIt) {
265 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIt);
267 dumpParameter(aFeature);
269 // dump all other features
270 for (anObjIt = anObjects.begin(); anObjIt != anObjects.end(); ++anObjIt) {
271 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anObjIt);
272 if (aCompFeat) // iteratively process composite features
273 isOk = process(aCompFeat) && isOk;
274 else if (!isDumped(EntityPtr(*anObjIt))) {
276 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(*anObjIt);
280 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIt);
281 if (aFeature) // dump common feature
282 dumpFeature(aFeature);
286 // dump folders if any
291 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
294 // increase composite features stack
295 ++gCompositeStackDepth;
296 // dump composite itself
297 if (!isDumped(EntityPtr(theComposite)) || isForce)
298 dumpFeature(FeaturePtr(theComposite), isForce);
300 // sub-part is processed independently, because it provides separate document
301 if (theComposite->getKind() == PartSetPlugin_Part::ID()) {
302 // dump name of the part if it is different from default
303 if (!myEntitiesStack.empty())
306 // decrease composite features stack because we run into separate document
307 --gCompositeStackDepth;
309 ResultPartPtr aPartResult =
310 std::dynamic_pointer_cast<ModelAPI_ResultPart>(theComposite->lastResult());
313 DocumentPtr aSubDoc = aPartResult->partDoc();
316 // set name of document
317 const std::string& aPartName = myNames[theComposite].myCurrentName;
318 std::string aDocName = aPartName + "_doc";
319 myNames[aSubDoc] = EntityName(aDocName, std::string(), true);
321 // dump document in a separate line
322 *this << aDocName << " = " << aPartName << ".document()" << std::endl;
323 // dump features in the document
324 bool aRes = process(aSubDoc);
325 *this << "model.do()" << std::endl;
330 bool isOk = processSubs(theComposite);
331 // decrease composite features stack
332 --gCompositeStackDepth;
337 bool ModelHighAPI_Dumper::processSubs(
338 const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
342 // dump all sub-features;
343 bool isSubDumped = false;
344 int aNbSubs = theComposite->numberOfSubs();
345 for (int anIndex = 0; anIndex < aNbSubs; ++anIndex) {
346 FeaturePtr aFeature = theComposite->subFeature(anIndex);
347 if (isDumped(EntityPtr(aFeature)))
351 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
352 if (aCompFeat) // iteratively process composite features
353 isOk = process(aCompFeat) && isOk;
355 dumpFeature(aFeature, true);
358 bool isDumpSetName = !myEntitiesStack.empty() &&
359 myEntitiesStack.top().myEntity == EntityPtr(theComposite);
360 bool isForceModelDo = isSubDumped && isDumpSetName &&
361 (myEntitiesStack.top().myUserName || !myEntitiesStack.top().myResults.empty());
362 // It is necessary for the sketch to create its result when complete (command "model.do()").
363 // This option is set by flat theDumpModelDo.
364 // However, nested sketches are rebuilt by parent feature, so, they do not need
365 // explicit call of "model.do()". This will be controlled by the depth of the stack.
366 if (isForceModelDo || (theDumpModelDo && gCompositeStackDepth <= 1))
367 *this << "model.do()" << std::endl;
369 // dump "setName" for composite feature
375 void ModelHighAPI_Dumper::postpone(const EntityPtr& theEntity)
378 name(theEntity, false);
379 myPostponed.push_back(theEntity);
382 void ModelHighAPI_Dumper::dumpPostponed(bool theDumpFolders)
384 if (myDumpPostponedInProgress)
387 myDumpPostponedInProgress = true;
388 // make a copy of postponed entities, because the list will be updated
389 // if some features are not able to be dumped
390 std::list<EntityPtr> aPostponedCopy = myPostponed;
393 // iterate over postponed entities and try to dump them
394 std::list<EntityPtr>::const_iterator anIt = aPostponedCopy.begin();
395 for (; anIt != aPostponedCopy.end(); ++anIt) {
396 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(*anIt);
401 myPostponed.push_back(*anIt);
404 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
406 dumpFeature(aFeature, true);
409 myDumpPostponedInProgress = false;
412 void ModelHighAPI_Dumper::dumpSubFeatureNameAndColor(const std::string theSubFeatureGet,
413 const FeaturePtr& theSubFeature)
415 name(theSubFeature, false);
416 myNames[theSubFeature] = EntityName(theSubFeatureGet, theSubFeature->name(), false);
418 // store results if they have user-defined names or colors
419 std::list<ResultPtr> aResultsWithNameOrColor;
420 const std::list<ResultPtr>& aResults = theSubFeature->results();
421 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
422 for (; aResIt != aResults.end(); ++aResIt) {
423 std::string aResName = (*aResIt)->data()->name();
424 myNames[*aResIt] = EntityName(aResName, aResName, false);
425 aResultsWithNameOrColor.push_back(*aResIt);
428 // store just dumped entity to stack
429 myEntitiesStack.push(LastDumpedEntity(theSubFeature, true, aResultsWithNameOrColor));
434 bool ModelHighAPI_Dumper::exportTo(const std::string& theFileName)
437 OSD_OpenStream(aFile, theFileName.c_str(), std::ofstream::out);
438 if (!aFile.is_open())
441 // standard header (encoding + imported modules)
442 aFile << "# -*- coding: utf-8 -*-" << std::endl << std::endl;
443 for (ModulesMap::const_iterator aModIt = myModules.begin();
444 aModIt != myModules.end(); ++aModIt) {
445 aFile << "from " << aModIt->first << " import ";
446 if (aModIt->second.empty() ||
447 aModIt->second.find(std::string()) != aModIt->second.end())
448 aFile << "*"; // import whole module
450 // import specific features
451 std::set<std::string>::const_iterator anObjIt = aModIt->second.begin();
453 for (++anObjIt; anObjIt != aModIt->second.end(); ++anObjIt)
454 aFile << ", " << *anObjIt;
458 if (!myModules.empty())
461 aFile << "from salome.shaper import model" << std::endl << std::endl;
462 aFile << "model.begin()" << std::endl;
464 // dump collected data
465 aFile << myFullDump.str();
466 aFile << myDumpBuffer.str();
469 aFile << "model.end()" << std::endl;
477 void ModelHighAPI_Dumper::importModule(const std::string& theModuleName,
478 const std::string& theObject)
480 myModules[theModuleName].insert(theObject);
483 void ModelHighAPI_Dumper::dumpEntitySetName()
485 const LastDumpedEntity& aLastDumped = myEntitiesStack.top();
486 bool isBufferEmpty = myDumpBuffer.str().empty();
488 // dump "setName" for the entity
489 if (aLastDumped.myUserName) {
490 EntityName& anEntityNames = myNames[aLastDumped.myEntity];
491 if (!anEntityNames.myIsDefault)
492 myDumpBuffer << anEntityNames.myCurrentName << ".setName(\""
493 << anEntityNames.myUserName << "\")" << std::endl;
494 // don't dump "setName" for the entity twice
495 anEntityNames.myUserName.clear();
496 anEntityNames.myIsDefault = true;
498 // dump "setName" for results
499 std::list<ResultPtr>::const_iterator aResIt = aLastDumped.myResults.begin();
500 std::list<ResultPtr>::const_iterator aResEnd = aLastDumped.myResults.end();
501 for (; aResIt != aResEnd; ++aResIt) {
503 EntityName& anEntityNames = myNames[*aResIt];
504 if (!anEntityNames.myIsDefault) {
506 myDumpBuffer << ".setName(\"" << anEntityNames.myUserName << "\")" << std::endl;
507 // don't dump "setName" for the entity twice
508 anEntityNames.myUserName.clear();
509 anEntityNames.myIsDefault = true;
512 if (!isDefaultColor(*aResIt)) {
513 AttributeIntArrayPtr aColor = (*aResIt)->data()->intArray(ModelAPI_Result::COLOR_ID());
514 if (aColor && aColor->isInitialized()) {
516 myDumpBuffer << ".setColor(" << aColor->value(0) << ", " << aColor->value(1)
517 << ", " << aColor->value(2) << ")" << std::endl;
520 // set result deflection
521 if (!isDefaultDeflection(*aResIt)) {
522 AttributeDoublePtr aDeflectionAttr =
523 (*aResIt)->data()->real(ModelAPI_Result::DEFLECTION_ID());
524 if(aDeflectionAttr.get() && aDeflectionAttr->isInitialized()) {
526 myDumpBuffer << ".setDeflection(" << aDeflectionAttr->value() << ")" << std::endl;
529 // set result transparency
530 if (!isDefaultTransparency(*aResIt)) {
531 AttributeDoublePtr aTransparencyAttr =
532 (*aResIt)->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
533 if(aTransparencyAttr.get() && aTransparencyAttr->isInitialized()) {
535 myDumpBuffer << ".setTransparency(" << aTransparencyAttr->value() << ")" << std::endl;
540 myNames[aLastDumped.myEntity].myIsDumped = true;
541 myEntitiesStack.pop();
543 // clean buffer if it was clear before
545 myFullDump << myDumpBuffer.str();
546 myDumpBuffer.str("");
550 bool ModelHighAPI_Dumper::isDumped(const EntityPtr& theEntity) const
552 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
553 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
554 return (aFound != myNames.end() && aFound->second.myIsDumped) ||
555 myFeaturesToSkip.find(aFeature) != myFeaturesToSkip.end();
558 bool ModelHighAPI_Dumper::isDumped(const AttributeRefAttrPtr& theRefAttr) const
561 if (theRefAttr->isObject())
562 aFeature = ModelAPI_Feature::feature(theRefAttr->object());
564 aFeature = ModelAPI_Feature::feature(theRefAttr->attr()->owner());
565 return aFeature && isDumped(EntityPtr(aFeature));
568 bool ModelHighAPI_Dumper::isDumped(const AttributeRefListPtr& theRefList) const
570 std::list<ObjectPtr> aRefs = theRefList->list();
571 std::list<ObjectPtr>::iterator anIt = aRefs.begin();
572 for (; anIt != aRefs.end(); ++anIt) {
573 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
574 if (aFeature && !isDumped(EntityPtr(aFeature)))
580 static bool isSketchSub(const FeaturePtr& theFeature)
582 static const std::string SKETCH("Sketch");
583 CompositeFeaturePtr anOwner = ModelAPI_Tools::compositeOwner(theFeature);
584 return anOwner && anOwner->getKind() == SKETCH;
587 bool ModelHighAPI_Dumper::isDefaultColor(const ResultPtr& theResult) const
589 AttributeIntArrayPtr aColor = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
590 if (!aColor || !aColor->isInitialized())
593 // check the result belongs to sketch entity, do not dump color in this way
594 ResultConstructionPtr aResConstr =
595 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theResult);
597 FeaturePtr aFeature = ModelAPI_Feature::feature(theResult->data()->owner());
598 if (isSketchSub(aFeature))
602 std::string aSection, aName, aDefault;
603 theResult->colorConfigInfo(aSection, aName, aDefault);
605 // dump current color
606 std::ostringstream aColorInfo;
607 aColorInfo << aColor->value(0) << "," << aColor->value(1) << "," << aColor->value(2);
609 return aDefault == aColorInfo.str();
612 bool ModelHighAPI_Dumper::isDefaultDeflection(const ResultPtr& theResult) const
614 AttributeDoublePtr aDeflectionAttr = theResult->data()->real(ModelAPI_Result::DEFLECTION_ID());
615 if(!aDeflectionAttr || !aDeflectionAttr->isInitialized()) {
619 double aCurrent = aDeflectionAttr->value();
620 double aDefault = -1;
622 bool isConstruction = false;
623 std::string aResultGroup = theResult->groupName();
624 if (aResultGroup == ModelAPI_ResultConstruction::group())
625 isConstruction = true;
626 else if (aResultGroup == ModelAPI_ResultBody::group()) {
627 GeomShapePtr aGeomShape = theResult->shape();
628 if (aGeomShape.get()) {
629 // if the shape could not be exploded on faces, it contains only wires, edges, and vertices
630 // correction of deviation for them should not influence to the application performance
631 GeomAPI_ShapeExplorer anExp(aGeomShape, GeomAPI_Shape::FACE);
632 isConstruction = !anExp.more();
636 aDefault = Config_PropManager::real("Visualization", "construction_deflection");
638 aDefault = Config_PropManager::real("Visualization", "body_deflection");
640 return fabs(aCurrent - aDefault) < 1.e-12;
643 bool ModelHighAPI_Dumper::isDefaultTransparency(const ResultPtr& theResult) const
645 AttributeDoublePtr anAttribute = theResult->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
646 if(!anAttribute || !anAttribute->isInitialized()) {
649 return fabs(anAttribute->value()) < 1.e-12;
652 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char theChar)
654 myDumpBuffer << theChar;
658 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char* theString)
660 myDumpBuffer << theString;
664 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::string& theString)
666 myDumpBuffer << theString;
670 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const bool theValue)
672 myDumpBuffer << (theValue ? "True" : "False");
676 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const int theValue)
678 myDumpBuffer << theValue;
682 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const double theValue)
684 myDumpBuffer << theValue;
688 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
690 importModule("GeomAPI", "GeomAPI_Pnt");
691 myDumpBuffer << "GeomAPI_Pnt(" << thePoint->x() << ", "
692 << thePoint->y() << ", " << thePoint->z() << ")";
696 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir)
698 importModule("GeomAPI", "GeomAPI_Dir");
699 myDumpBuffer << "GeomAPI_Dir(" << theDir->x() << ", "
700 << theDir->y() << ", " << theDir->z() << ")";
704 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
705 const std::shared_ptr<GeomDataAPI_Dir>& theDir)
707 myDumpBuffer << theDir->x() << ", " << theDir->y() << ", " << theDir->z();
711 static void dumpArray(std::ostringstream& theOutput, int theSize,
712 double* theValues, std::string* theTexts)
714 for (int i = 0; i < theSize; ++i) {
717 if (theTexts[i].empty())
718 theOutput << theValues[i];
720 theOutput << "\"" << theTexts[i] << "\"";
724 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
725 const std::shared_ptr<GeomDataAPI_Point>& thePoint)
727 static const int aSize = 3;
728 double aValues[aSize] = {thePoint->x(), thePoint->y(), thePoint->z()};
729 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY(), thePoint->textZ()};
730 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
734 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
735 const std::shared_ptr<GeomDataAPI_Point2D>& thePoint)
737 static const int aSize = 2;
738 double aValues[aSize] = {thePoint->x(), thePoint->y()};
739 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY()};
740 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
744 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
745 const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool)
747 myDumpBuffer << (theAttrBool->value() ? "True" : "False");
751 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
752 const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt)
754 std::string aText = theAttrInt->text();
756 myDumpBuffer << theAttrInt->value();
758 myDumpBuffer << "\"" << aText << "\"";
762 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
763 const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal)
765 std::string aText = theAttrReal->text();
767 myDumpBuffer << theAttrReal->value();
769 myDumpBuffer << "\"" << aText << "\"";
773 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
774 const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr)
776 myDumpBuffer << "\"" << theAttrStr->value() << "\"";
780 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FolderPtr& theFolder)
782 myDumpBuffer << name(theFolder);
784 // add dumped folder to a stack
785 if (!myNames[theFolder].myIsDumped &&
786 (myEntitiesStack.empty() || myEntitiesStack.top().myEntity != theFolder))
787 myEntitiesStack.push(LastDumpedEntity(theFolder, !myNames[theFolder].myIsDefault));
792 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FeaturePtr& theEntity)
794 myDumpBuffer << name(theEntity);
796 if (!myNames[theEntity].myIsDumped) {
797 bool isUserDefinedName = !myNames[theEntity].myIsDefault;
798 // store results if they have user-defined names or colors
799 std::list<ResultPtr> aResultsWithNameOrColor;
800 std::list<ResultPtr> allRes;
801 ModelAPI_Tools::allResults(theEntity, allRes);
802 for(std::list<ResultPtr>::iterator aRes = allRes.begin(); aRes != allRes.end(); aRes++) {
803 if(!myNames[*aRes].myIsDefault || !isDefaultColor(*aRes) ||
804 !isDefaultDeflection(*aRes) || !isDefaultTransparency(*aRes))
805 aResultsWithNameOrColor.push_back(*aRes);
807 // store just dumped entity to stack
808 if (myEntitiesStack.empty() || myEntitiesStack.top().myEntity != theEntity)
809 myEntitiesStack.push(
810 LastDumpedEntity(theEntity, isUserDefinedName, aResultsWithNameOrColor));
813 // remove entity from the list of not dumped items
814 myNotDumpedEntities.erase(theEntity);
818 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ResultPtr& theResult)
820 // iterate in the structure of sub-results to the parent
821 ResultPtr aCurRes = theResult;
822 FeaturePtr aFeature = ModelAPI_Feature::feature(theResult);
823 std::list<int> anIndices; // indexes of results in the parent result, starting from topmost
824 while(aCurRes.get()) {
825 ResultBodyPtr aParent = ModelAPI_Tools::bodyOwner(aCurRes);
827 anIndices.push_front(ModelAPI_Tools::bodyIndex(aCurRes));
828 } else { // index of the result in the feature
829 std::list<ResultPtr>::const_iterator aRes = aFeature->results().cbegin();
830 for(int anIndex = 0; aRes != aFeature->results().cend(); aRes++, anIndex++) {
831 if (*aRes == aCurRes) {
832 anIndices.push_front(anIndex);
840 myDumpBuffer << name(aFeature);
841 for (std::list<int>::iterator anI = anIndices.begin(); anI != anIndices.end(); anI++) {
842 if (anI == anIndices.begin()) {
844 myDumpBuffer << ".result()";
847 myDumpBuffer << ".results()[" << *anI << "]";
850 myDumpBuffer << ".subResult(" << *anI << ")";
857 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ObjectPtr& theObject)
859 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
861 myDumpBuffer << name(aFeature);
865 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
874 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const AttributePtr& theAttr)
876 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttr->owner());
878 std::string aWrapperPrefix, aWrapperSuffix;
879 // Check the attribute belongs to copied (in multi-translation or multi-rotation) feature.
880 // In this case we need to cast explicitly feature to appropriate type.
881 AttributeBooleanPtr isCopy = anOwner->boolean("Copy");
882 if (isCopy.get() && isCopy->value()) {
883 aWrapperPrefix = featureWrapper(anOwner) + "(";
884 aWrapperSuffix = ")";
885 importModule("SketchAPI");
888 myDumpBuffer << aWrapperPrefix << name(anOwner) << aWrapperSuffix
889 << "." << attributeGetter(anOwner, theAttr->id()) << "()";
893 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
894 const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr)
896 if (theRefAttr->isObject())
897 *this << theRefAttr->object();
899 *this << theRefAttr->attr();
903 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
904 const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList)
907 std::list<std::pair<ObjectPtr, AttributePtr> > aList = theRefAttrList->list();
908 bool isAdded = false;
909 std::list<std::pair<ObjectPtr, AttributePtr> >::const_iterator anIt = aList.begin();
910 for (; anIt != aList.end(); ++anIt) {
912 myDumpBuffer << ", ";
916 *this << anIt->first;
917 else if (anIt->second)
918 * this << anIt->second;
924 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
925 const std::shared_ptr<ModelAPI_AttributeReference>& theReference)
927 *this << theReference->value();
931 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
932 const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList)
934 static const int aThreshold = 2;
935 // if number of elements in the list if greater than a threshold,
936 // dump it in a separate line with specific name
937 std::string aDumped = myDumpBuffer.str();
938 if (aDumped.empty() || theRefList->size() <= aThreshold) {
940 std::list<ObjectPtr> aList = theRefList->list();
941 bool isAdded = false;
942 std::list<ObjectPtr>::const_iterator anIt = aList.begin();
943 for (; anIt != aList.end(); ++anIt) {
945 myDumpBuffer << ", ";
953 // clear buffer and store list "as is"
954 myDumpBuffer.str("");
956 // save buffer and clear it again
957 std::string aDumpedList = myDumpBuffer.str();
958 myDumpBuffer.str("");
959 // obtain name of list
960 FeaturePtr anOwner = ModelAPI_Feature::feature(theRefList->owner());
961 std::string aListName = name(anOwner) + "_objects";
962 // store all previous data
963 myDumpBuffer << aListName << " = " << aDumpedList << std::endl
964 << aDumped << aListName;
969 static int possibleSelectionsByPoint(const GeomPointPtr& thePoint,
970 const ResultPtr& theResult,
971 const GeomShapePtr& theShape,
972 const FeaturePtr& theStartFeature,
973 const FeaturePtr& theEndFeature)
975 DocumentPtr aDoc1 = theStartFeature->document();
976 DocumentPtr aDoc2 = theEndFeature->document();
978 std::list<FeaturePtr> aFeatures = aDoc1->allFeatures();
979 if (aDoc1 != aDoc2) {
980 std::list<FeaturePtr> anAdditionalFeatures = aDoc2->allFeatures();
981 aFeatures.insert(aFeatures.end(), anAdditionalFeatures.begin(), anAdditionalFeatures.end());
984 CompositeFeaturePtr aLastCompositeFeature;
986 std::list<FeaturePtr>::const_iterator aFIt = aFeatures.begin();
987 while (aFIt != aFeatures.end() && *aFIt != theStartFeature) {
988 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFIt);
990 aLastCompositeFeature = aCompFeat;
994 // collect the list of composite features, containing the last feature;
995 // these features should be excluded from searching,
996 // because the feature cannot select sub-shapes from its parent
997 std::set<FeaturePtr> aEndFeatureParents = ModelAPI_Tools::getParents(theEndFeature);
999 int aNbPossibleSelections = 0;
1000 for (; aFIt != aFeatures.end() && *aFIt != theEndFeature; ++aFIt) {
1001 bool isSkipFeature = false;
1002 if (aLastCompositeFeature && aLastCompositeFeature->isSub(*aFIt))
1003 isSkipFeature = true;
1004 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFIt);
1006 ResultPartPtr aPartRes =
1007 std::dynamic_pointer_cast<ModelAPI_ResultPart>(aCompFeat->firstResult());
1009 aLastCompositeFeature = aCompFeat;
1010 if (aEndFeatureParents.find(aCompFeat) != aEndFeatureParents.end()) {
1011 // do not process the parent for the last feature,
1012 // because it cannot select objects from its parent
1013 isSkipFeature = true;
1019 std::list<ModelGeomAlgo_Shape::SubshapeOfResult> anApproproate;
1020 if (ModelGeomAlgo_Shape::findSubshapeByPoint(*aFIt, thePoint, theShape->shapeType(),
1022 std::list<ModelGeomAlgo_Shape::SubshapeOfResult>::iterator anApIt = anApproproate.begin();
1023 for (; anApIt != anApproproate.end(); ++anApIt) {
1024 ++aNbPossibleSelections;
1026 // stop if the target shape and result are found
1027 GeomShapePtr aCurShape = anApIt->mySubshape;
1029 aCurShape = anApIt->myResult->shape();
1031 if (anApIt->myResult->isSame(theResult) && aCurShape->isSame(theShape))
1036 return aNbPossibleSelections;
1039 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1040 const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect)
1042 myDumpBuffer << "model.selection(";
1044 if(!theAttrSelect->isInitialized()) {
1045 myDumpBuffer << ")";
1049 GeomShapePtr aShape = theAttrSelect->value();
1051 aShape = theAttrSelect->context()->shape();
1055 myDumpBuffer << ")";
1059 // how to dump selection: construction features are dumped by name always
1060 bool isDumpByGeom = myGeometricalSelection;
1061 FeaturePtr aSelectedFeature;
1063 ResultPtr aRes = theAttrSelect->context();
1064 FeaturePtr aFeature = theAttrSelect->contextFeature();
1065 if (aRes && !aFeature)
1066 aSelectedFeature = ModelAPI_Feature::feature(aRes->data()->owner());
1067 isDumpByGeom = aSelectedFeature && aSelectedFeature->isInHistory();
1070 myDumpBuffer << "\"" << aShape->shapeTypeStr();
1071 bool aStandardDump = true;
1073 // check the selected item is a ResultPart;
1074 // in this case it is necessary to get shape with full transformation
1075 // for correct calculation of the middle point
1076 ResultPartPtr aResPart =
1077 std::dynamic_pointer_cast<ModelAPI_ResultPart>(theAttrSelect->context());
1078 if (aResPart && aShape->shapeType() == GeomAPI_Shape::COMPOUND)
1079 aShape = aResPart->shape();
1080 GeomPointPtr aMiddlePoint = aShape->middlePoint();
1081 // calculate number of features, which could be selected by the same point
1082 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttrSelect->owner());
1083 int aNbPossibleSelections = possibleSelectionsByPoint(aMiddlePoint,
1084 theAttrSelect->context(), aShape, aSelectedFeature, anOwner);
1086 // produce the index if the number of applicable features is greater than 1
1087 std::string anIndex;
1088 if (aNbPossibleSelections > 1) {
1089 std::ostringstream anOutput;
1090 anOutput << "_" << aNbPossibleSelections;
1091 anIndex = anOutput.str();
1094 myDumpBuffer << anIndex << "\", ("
1095 << aMiddlePoint->x() << ", "
1096 << aMiddlePoint->y() << ", "
1097 << aMiddlePoint->z() << ")";
1098 aStandardDump = false;
1099 } else if (myWeakNamingSelection && aShape.get() && theAttrSelect->context().get() &&
1100 aShape != theAttrSelect->context()->shape()) { // weak naming for local selection only
1101 GeomAlgoAPI_NExplode aNExplode(theAttrSelect->context()->shape(), aShape->shapeType());
1102 int anIndex = aNExplode.index(aShape);
1103 if (anIndex != 0) { // found a week-naming index, so, export it
1104 myDumpBuffer<<"\", \""<<
1105 theAttrSelect->contextName(theAttrSelect->context())<<"\", "<<anIndex;
1106 aStandardDump = false;
1110 myDumpBuffer << "\", \"" << theAttrSelect->namingName() << "\"";
1111 myDumpBuffer << ")";
1115 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1116 const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList)
1118 static const int aThreshold = 2;
1119 // if number of elements in the list if greater than a threshold,
1120 // dump it in a separate line with specific name
1121 std::string aDumped = myDumpBuffer.str();
1123 if (aDumped.empty() || theAttrSelList->size() <= aThreshold) {
1124 myDumpBuffer << "[";
1126 GeomShapePtr aShape;
1127 std::string aShapeTypeStr;
1129 bool isAdded = false;
1131 for(int anIndex = 0; anIndex < theAttrSelList->size(); ++anIndex) {
1132 AttributeSelectionPtr anAttribute = theAttrSelList->value(anIndex);
1133 aShape = anAttribute->value();
1135 ResultPtr aContext = anAttribute->context();
1137 aShape = aContext->shape();
1145 myDumpBuffer << ", ";
1149 *this << anAttribute;
1152 myDumpBuffer << "]";
1154 // clear buffer and store list "as is"
1155 myDumpBuffer.str("");
1156 *this << theAttrSelList;
1157 // save buffer and clear it again
1158 std::string aDumpedList = myDumpBuffer.str();
1159 myDumpBuffer.str("");
1160 // obtain name of list (the feature may contain several selection lists)
1161 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttrSelList->owner());
1162 std::string aListName = name(anOwner) + "_objects";
1163 std::list<AttributePtr> aSelLists =
1164 anOwner->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
1165 if (aSelLists.size() > 1) {
1167 for (std::list<AttributePtr>::iterator aSIt = aSelLists.begin();
1168 aSIt != aSelLists.end(); ++aSIt, ++anIndex)
1169 if ((*aSIt).get() == theAttrSelList.get())
1171 std::ostringstream aSStream;
1172 aSStream << aListName << "_" << anIndex;
1173 aListName = aSStream.str();
1175 // store all previous data
1176 myDumpBuffer << aListName << " = " << aDumpedList << std::endl
1177 << aDumped << aListName;
1182 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1183 const std::shared_ptr<ModelAPI_AttributeStringArray>& theArray)
1186 for(int anIndex = 0; anIndex < theArray->size(); ++anIndex) {
1190 myDumpBuffer<<"\""<<theArray->value(anIndex)<<"\"";
1198 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
1199 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&))
1201 theDumper.myDumpBuffer << theEndl;
1203 if (!theDumper.myEntitiesStack.empty()) {
1205 // all copies have been stored into stack, pop them all
1208 // Name for composite feature is dumped when all sub-entities are dumped
1209 // (see method ModelHighAPI_Dumper::processSubs).
1210 const ModelHighAPI_Dumper::LastDumpedEntity& aLastDumped = theDumper.myEntitiesStack.top();
1211 CompositeFeaturePtr aComposite =
1212 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aLastDumped.myEntity);
1214 theDumper.dumpEntitySetName();
1215 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aLastDumped.myEntity);
1217 AttributeBooleanPtr aCopyAttr = aFeature->boolean("Copy");
1218 isCopy = aCopyAttr.get() && aCopyAttr->value();
1221 } while (isCopy && !theDumper.myEntitiesStack.empty());
1224 // store all not-dumped entities first
1225 std::set<EntityPtr> aNotDumped = theDumper.myNotDumpedEntities;
1226 std::string aBufCopy = theDumper.myDumpBuffer.str();
1227 theDumper.clear(true);
1228 std::set<EntityPtr>::const_iterator anIt = aNotDumped.begin();
1229 for (; anIt != aNotDumped.end(); ++anIt) {
1230 // if the feature is composite, dump it with all subs
1231 CompositeFeaturePtr aCompFeat =
1232 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anIt);
1234 theDumper.process(aCompFeat, true);
1236 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
1237 theDumper.dumpFeature(aFeature, true);
1238 // dump the Projection feature which produces this "Copy" entity
1239 AttributeBooleanPtr aCopyAttr = aFeature->boolean("Copy");
1240 if (aCopyAttr.get() && aCopyAttr->value())
1242 const std::set<AttributePtr>& aRefs = aFeature->data()->refsToMe();
1243 std::set<AttributePtr>::iterator aRefIt = aRefs.begin();
1244 for (; aRefIt != aRefs.end(); ++aRefIt)
1245 if ((*aRefIt)->id() == "ProjectedFeature")
1246 { // process projection only
1247 FeaturePtr anOwner = ModelAPI_Feature::feature((*aRefIt)->owner());
1248 if (anOwner && !theDumper.isDumped(EntityPtr(anOwner)))
1249 theDumper.dumpFeature(anOwner, true);
1255 // avoid multiple empty lines
1256 size_t anInd = std::string::npos;
1257 while ((anInd = aBufCopy.find("\n\n\n")) != std::string::npos)
1258 aBufCopy.erase(anInd, 1);
1259 // then store currently dumped string
1260 theDumper.myFullDump << aBufCopy;
1262 // now, store all postponed features
1263 theDumper.dumpPostponed();