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_Session.h>
55 #include <ModelAPI_Tools.h>
57 #include <ModelGeomAlgo_Shape.h>
59 #include <PartSetPlugin_Part.h>
61 #include <OSD_OpenFile.hxx>
65 static int gCompositeStackDepth = 0;
67 ModelHighAPI_Dumper* ModelHighAPI_Dumper::mySelf = 0;
69 ModelHighAPI_Dumper::ModelHighAPI_Dumper()
70 : myGeometricalSelection(false)
75 void ModelHighAPI_Dumper::setInstance(ModelHighAPI_Dumper* theDumper)
81 ModelHighAPI_Dumper* ModelHighAPI_Dumper::getInstance()
86 void ModelHighAPI_Dumper::clear(bool bufferOnly)
89 myDumpBuffer << std::setprecision(16);
95 myFullDump << std::setprecision(16);
99 myFeatureCount.clear();
100 while (!myEntitiesStack.empty())
101 myEntitiesStack.pop();
104 myDumpPostponedInProgress = false;
108 void ModelHighAPI_Dumper::clearNotDumped()
110 myNotDumpedEntities.clear();
113 // Convert string to integer. If the string is not a number, return -1
114 static int toInt(const std::string& theString)
116 std::string::const_iterator aChar = theString.begin();
117 for (; aChar != theString.end(); ++aChar)
118 if (!std::isdigit(*aChar))
120 if (aChar != theString.end())
121 return -1; // not a number
122 return std::stoi(theString);
125 const std::string& ModelHighAPI_Dumper::name(const EntityPtr& theEntity,
126 bool theSaveNotDumped,
127 bool theUseEntityName)
129 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
130 if (aFound != myNames.end())
131 return aFound->second.myCurrentName;
133 // entity is not found, store it
134 std::string aName, aKind;
135 bool isDefaultName = false;
136 bool isSaveNotDumped = theSaveNotDumped;
137 std::ostringstream aDefaultName;
138 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
140 aName = aFeature->name();
141 aKind = aFeature->getKind();
143 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(theEntity);
145 aName = aFolder->data()->name();
146 aKind = ModelAPI_Folder::ID();
147 isSaveNotDumped = false;
151 ObjectPtr anObject = std::dynamic_pointer_cast<ModelAPI_Object>(theEntity);
153 DocumentPtr aDoc = anObject->document();
154 std::pair<int, int>& aNbFeatures = myFeatureCount[aDoc][aKind];
155 aNbFeatures.first += 1;
157 size_t anIndex = aName.find(aKind);
158 if (anIndex == 0 && aName[aKind.length()] == '_') { // name starts with "FeatureKind_"
159 std::string anIdStr = aName.substr(aKind.length() + 1);
160 int anId = toInt(anIdStr);
162 // Check number of already registered objects of such kind. Index of current object
163 // should be the same to identify feature's name as automatically generated.
164 if (aNbFeatures.first == anId && aNbFeatures.second < anId) {
165 // name is not user-defined
166 isDefaultName = true;
168 // check there are postponed features of this kind,
169 // dump their names, because the sequence of features may be changed
170 for (std::list<EntityPtr>::const_iterator aPpIt = myPostponed.begin();
171 aPpIt != myPostponed.end(); ++aPpIt) {
172 FeaturePtr aCurFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*aPpIt);
173 if (aCurFeature && aCurFeature->getKind() == aKind) {
174 myNames[*aPpIt].myIsDefault = false;
175 isDefaultName = false;
180 if (anId > aNbFeatures.second)
181 aNbFeatures.second = anId;
184 // obtain default name for the feature
185 if (theUseEntityName)
186 aDefaultName << aName;
189 NbFeaturesMap::const_iterator aFIt = myFeatureCount.begin();
190 for (; aFIt != myFeatureCount.end(); ++aFIt) {
191 std::map<std::string, std::pair<int, int> >::const_iterator aFound =
192 aFIt->second.find(aKind);
193 if (aFound != aFIt->second.end())
194 aFullIndex += aFound->second.first;
196 aDefaultName << aKind << "_" << aFullIndex;
200 myNames[theEntity] = EntityName(aDefaultName.str(), aName, isDefaultName);
202 myNotDumpedEntities.insert(theEntity);
204 // store names of results
206 saveResultNames(aFeature);
208 return myNames[theEntity].myCurrentName;
211 const std::string& ModelHighAPI_Dumper::parentName(const FeaturePtr& theEntity)
213 const std::set<AttributePtr>& aRefs = theEntity->data()->refsToMe();
214 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
215 for (; aRefIt != aRefs.end(); ++aRefIt) {
216 CompositeFeaturePtr anOwner = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(
217 ModelAPI_Feature::feature((*aRefIt)->owner()));
219 return name(anOwner);
222 static const std::string DUMMY;
226 void ModelHighAPI_Dumper::saveResultNames(const FeaturePtr& theFeature)
228 // Default name of the feature
229 bool isFeatureDefaultName = myNames[theFeature].myIsDefault;
231 // Save only names of results which is not correspond to default feature name
232 const std::list<ResultPtr>& aResults = theFeature->results();
233 std::list<ResultPtr> allRes;
234 ModelAPI_Tools::allResults(theFeature, allRes);
235 for(std::list<ResultPtr>::iterator aRes = allRes.begin(); aRes != allRes.end(); aRes++) {
236 std::pair<std::string, bool> aName = ModelAPI_Tools::getDefaultName(*aRes);
237 std::string aDefaultName = aName.first;
238 std::string aResName = (*aRes)->data()->name();
239 bool isUserDefined = !(isFeatureDefaultName && aDefaultName == aResName);
241 EntityName(aResName, (isUserDefined ? aResName : std::string()), !isUserDefined);
245 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc,
246 const std::string& theFileName)
248 // dump top level document feature
249 static const std::string aDocName("partSet");
250 myNames[theDoc] = EntityName(aDocName, std::string(), true);
251 *this << aDocName << " = model.moduleDocument()" << std::endl;
253 // dump subfeatures and store result to file
254 return process(theDoc) && exportTo(theFileName);
257 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc)
260 std::list<ObjectPtr> anObjects = theDoc->allObjects();
261 std::list<ObjectPtr>::const_iterator anObjIt = anObjects.begin();
262 // firstly, dump all parameters
263 for (; anObjIt != anObjects.end(); ++ anObjIt) {
264 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIt);
266 dumpParameter(aFeature);
268 // dump all other features
269 for (anObjIt = anObjects.begin(); anObjIt != anObjects.end(); ++anObjIt) {
270 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anObjIt);
271 if (aCompFeat) // iteratively process composite features
272 isOk = process(aCompFeat) && isOk;
273 else if (!isDumped(EntityPtr(*anObjIt))) {
275 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(*anObjIt);
279 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIt);
280 if (aFeature) // dump common feature
281 dumpFeature(aFeature);
285 // dump folders if any
290 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
293 // increase composite features stack
294 ++gCompositeStackDepth;
295 // dump composite itself
296 if (!isDumped(EntityPtr(theComposite)) || isForce)
297 dumpFeature(FeaturePtr(theComposite), isForce);
299 // sub-part is processed independently, because it provides separate document
300 if (theComposite->getKind() == PartSetPlugin_Part::ID()) {
301 // dump name of the part if it is different from default
302 if (!myEntitiesStack.empty())
305 // decrease composite features stack because we run into separate document
306 --gCompositeStackDepth;
308 ResultPartPtr aPartResult =
309 std::dynamic_pointer_cast<ModelAPI_ResultPart>(theComposite->lastResult());
312 DocumentPtr aSubDoc = aPartResult->partDoc();
315 // set name of document
316 const std::string& aPartName = myNames[theComposite].myCurrentName;
317 std::string aDocName = aPartName + "_doc";
318 myNames[aSubDoc] = EntityName(aDocName, std::string(), true);
320 // dump document in a separate line
321 *this << aDocName << " = " << aPartName << ".document()" << std::endl;
322 // dump features in the document
323 bool aRes = process(aSubDoc);
324 *this << "model.do()" << std::endl;
329 bool isOk = processSubs(theComposite);
330 // decrease composite features stack
331 --gCompositeStackDepth;
336 bool ModelHighAPI_Dumper::processSubs(
337 const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
341 // dump all sub-features;
342 bool isSubDumped = false;
343 int aNbSubs = theComposite->numberOfSubs();
344 for (int anIndex = 0; anIndex < aNbSubs; ++anIndex) {
345 FeaturePtr aFeature = theComposite->subFeature(anIndex);
346 if (isDumped(EntityPtr(aFeature)))
350 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
351 if (aCompFeat) // iteratively process composite features
352 isOk = process(aCompFeat) && isOk;
354 dumpFeature(aFeature, true);
357 bool isDumpSetName = !myEntitiesStack.empty() &&
358 myEntitiesStack.top().myEntity == EntityPtr(theComposite);
359 bool isForceModelDo = isSubDumped && isDumpSetName &&
360 (myEntitiesStack.top().myUserName || !myEntitiesStack.top().myResults.empty());
361 // It is necessary for the sketch to create its result when complete (command "model.do()").
362 // This option is set by flat theDumpModelDo.
363 // However, nested sketches are rebuilt by parent feature, so, they do not need
364 // explicit call of "model.do()". This will be controlled by the depth of the stack.
365 if (isForceModelDo || (theDumpModelDo && gCompositeStackDepth <= 1))
366 *this << "model.do()" << std::endl;
368 // dump "setName" for composite feature
374 void ModelHighAPI_Dumper::postpone(const EntityPtr& theEntity)
377 name(theEntity, false);
378 myPostponed.push_back(theEntity);
381 void ModelHighAPI_Dumper::dumpPostponed(bool theDumpFolders)
383 if (myDumpPostponedInProgress)
386 myDumpPostponedInProgress = true;
387 // make a copy of postponed entities, because the list will be updated
388 // if some features are not able to be dumped
389 std::list<EntityPtr> aPostponedCopy = myPostponed;
392 // iterate over postponed entities and try to dump them
393 std::list<EntityPtr>::const_iterator anIt = aPostponedCopy.begin();
394 for (; anIt != aPostponedCopy.end(); ++anIt) {
395 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(*anIt);
400 myPostponed.push_back(*anIt);
403 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
405 dumpFeature(aFeature, true);
408 myDumpPostponedInProgress = false;
411 void ModelHighAPI_Dumper::dumpSubFeatureNameAndColor(const std::string theSubFeatureGet,
412 const FeaturePtr& theSubFeature)
414 name(theSubFeature, false);
415 myNames[theSubFeature] = EntityName(theSubFeatureGet, theSubFeature->name(), false);
417 // store results if they have user-defined names or colors
418 std::list<ResultPtr> aResultsWithNameOrColor;
419 const std::list<ResultPtr>& aResults = theSubFeature->results();
420 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
421 for (; aResIt != aResults.end(); ++aResIt) {
422 std::string aResName = (*aResIt)->data()->name();
423 myNames[*aResIt] = EntityName(aResName, aResName, false);
424 aResultsWithNameOrColor.push_back(*aResIt);
427 // store just dumped entity to stack
428 myEntitiesStack.push(LastDumpedEntity(theSubFeature, true, aResultsWithNameOrColor));
433 bool ModelHighAPI_Dumper::exportTo(const std::string& theFileName)
436 OSD_OpenStream(aFile, theFileName.c_str(), std::ofstream::out);
437 if (!aFile.is_open())
440 // standard header (encoding + imported modules)
441 aFile << "# -*- coding: utf-8 -*-" << std::endl << std::endl;
442 for (ModulesMap::const_iterator aModIt = myModules.begin();
443 aModIt != myModules.end(); ++aModIt) {
444 aFile << "from " << aModIt->first << " import ";
445 if (aModIt->second.empty() ||
446 aModIt->second.find(std::string()) != aModIt->second.end())
447 aFile << "*"; // import whole module
449 // import specific features
450 std::set<std::string>::const_iterator anObjIt = aModIt->second.begin();
452 for (++anObjIt; anObjIt != aModIt->second.end(); ++anObjIt)
453 aFile << ", " << *anObjIt;
457 if (!myModules.empty())
460 aFile << "from salome.shaper import model" << std::endl << std::endl;
461 aFile << "model.begin()" << std::endl;
463 // dump collected data
464 aFile << myFullDump.str();
465 aFile << myDumpBuffer.str();
468 aFile << "model.end()" << std::endl;
476 void ModelHighAPI_Dumper::importModule(const std::string& theModuleName,
477 const std::string& theObject)
479 myModules[theModuleName].insert(theObject);
482 void ModelHighAPI_Dumper::dumpEntitySetName()
484 const LastDumpedEntity& aLastDumped = myEntitiesStack.top();
485 bool isBufferEmpty = myDumpBuffer.str().empty();
487 // dump "setName" for the entity
488 if (aLastDumped.myUserName) {
489 EntityName& anEntityNames = myNames[aLastDumped.myEntity];
490 if (!anEntityNames.myIsDefault)
491 myDumpBuffer << anEntityNames.myCurrentName << ".setName(\""
492 << anEntityNames.myUserName << "\")" << std::endl;
493 // don't dump "setName" for the entity twice
494 anEntityNames.myUserName.clear();
495 anEntityNames.myIsDefault = true;
497 // dump "setName" for results
498 std::list<ResultPtr>::const_iterator aResIt = aLastDumped.myResults.begin();
499 std::list<ResultPtr>::const_iterator aResEnd = aLastDumped.myResults.end();
500 for (; aResIt != aResEnd; ++aResIt) {
502 EntityName& anEntityNames = myNames[*aResIt];
503 if (!anEntityNames.myIsDefault) {
505 myDumpBuffer << ".setName(\"" << anEntityNames.myUserName << "\")" << std::endl;
506 // don't dump "setName" for the entity twice
507 anEntityNames.myUserName.clear();
508 anEntityNames.myIsDefault = true;
511 if (!isDefaultColor(*aResIt)) {
512 AttributeIntArrayPtr aColor = (*aResIt)->data()->intArray(ModelAPI_Result::COLOR_ID());
513 if (aColor && aColor->isInitialized()) {
515 myDumpBuffer << ".setColor(" << aColor->value(0) << ", " << aColor->value(1)
516 << ", " << aColor->value(2) << ")" << std::endl;
519 // set result deflection
520 if (!isDefaultDeflection(*aResIt)) {
521 AttributeDoublePtr aDeflectionAttr =
522 (*aResIt)->data()->real(ModelAPI_Result::DEFLECTION_ID());
523 if(aDeflectionAttr.get() && aDeflectionAttr->isInitialized()) {
525 myDumpBuffer << ".setDeflection(" << aDeflectionAttr->value() << ")" << std::endl;
528 // set result transparency
529 if (!isDefaultTransparency(*aResIt)) {
530 AttributeDoublePtr aTransparencyAttr =
531 (*aResIt)->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
532 if(aTransparencyAttr.get() && aTransparencyAttr->isInitialized()) {
534 myDumpBuffer << ".setTransparency(" << aTransparencyAttr->value() << ")" << std::endl;
539 myNames[aLastDumped.myEntity].myIsDumped = true;
540 myEntitiesStack.pop();
542 // clean buffer if it was clear before
544 myFullDump << myDumpBuffer.str();
545 myDumpBuffer.str("");
549 bool ModelHighAPI_Dumper::isDumped(const EntityPtr& theEntity) const
551 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
552 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
553 return (aFound != myNames.end() && aFound->second.myIsDumped) ||
554 myFeaturesToSkip.find(aFeature) != myFeaturesToSkip.end();
557 bool ModelHighAPI_Dumper::isDumped(const AttributeRefAttrPtr& theRefAttr) const
560 if (theRefAttr->isObject())
561 aFeature = ModelAPI_Feature::feature(theRefAttr->object());
563 aFeature = ModelAPI_Feature::feature(theRefAttr->attr()->owner());
564 return aFeature && isDumped(EntityPtr(aFeature));
567 bool ModelHighAPI_Dumper::isDumped(const AttributeRefListPtr& theRefList) const
569 std::list<ObjectPtr> aRefs = theRefList->list();
570 std::list<ObjectPtr>::iterator anIt = aRefs.begin();
571 for (; anIt != aRefs.end(); ++anIt) {
572 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
573 if (aFeature && !isDumped(EntityPtr(aFeature)))
579 static bool isSketchSub(const FeaturePtr& theFeature)
581 static const std::string SKETCH("Sketch");
582 CompositeFeaturePtr anOwner = ModelAPI_Tools::compositeOwner(theFeature);
583 return anOwner && anOwner->getKind() == SKETCH;
586 bool ModelHighAPI_Dumper::isDefaultColor(const ResultPtr& theResult) const
588 AttributeIntArrayPtr aColor = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
589 if (!aColor || !aColor->isInitialized())
592 // check the result belongs to sketch entity, do not dump color in this way
593 ResultConstructionPtr aResConstr =
594 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theResult);
596 FeaturePtr aFeature = ModelAPI_Feature::feature(theResult->data()->owner());
597 if (isSketchSub(aFeature))
601 std::string aSection, aName, aDefault;
602 theResult->colorConfigInfo(aSection, aName, aDefault);
604 // dump current color
605 std::ostringstream aColorInfo;
606 aColorInfo << aColor->value(0) << "," << aColor->value(1) << "," << aColor->value(2);
608 return aDefault == aColorInfo.str();
611 bool ModelHighAPI_Dumper::isDefaultDeflection(const ResultPtr& theResult) const
613 AttributeDoublePtr aDeflectionAttr = theResult->data()->real(ModelAPI_Result::DEFLECTION_ID());
614 if(!aDeflectionAttr || !aDeflectionAttr->isInitialized()) {
618 double aCurrent = aDeflectionAttr->value();
619 double aDefault = -1;
621 bool isConstruction = false;
622 std::string aResultGroup = theResult->groupName();
623 if (aResultGroup == ModelAPI_ResultConstruction::group())
624 isConstruction = true;
625 else if (aResultGroup == ModelAPI_ResultBody::group()) {
626 GeomShapePtr aGeomShape = theResult->shape();
627 if (aGeomShape.get()) {
628 // if the shape could not be exploded on faces, it contains only wires, edges, and vertices
629 // correction of deviation for them should not influence to the application performance
630 GeomAPI_ShapeExplorer anExp(aGeomShape, GeomAPI_Shape::FACE);
631 isConstruction = !anExp.more();
635 aDefault = Config_PropManager::real("Visualization", "construction_deflection");
637 aDefault = Config_PropManager::real("Visualization", "body_deflection");
639 return fabs(aCurrent - aDefault) < 1.e-12;
642 bool ModelHighAPI_Dumper::isDefaultTransparency(const ResultPtr& theResult) const
644 AttributeDoublePtr anAttribute = theResult->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
645 if(!anAttribute || !anAttribute->isInitialized()) {
648 return fabs(anAttribute->value()) < 1.e-12;
651 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char theChar)
653 myDumpBuffer << theChar;
657 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char* theString)
659 myDumpBuffer << theString;
663 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::string& theString)
665 myDumpBuffer << theString;
669 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const bool theValue)
671 myDumpBuffer << (theValue ? "True" : "False");
675 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const int theValue)
677 myDumpBuffer << theValue;
681 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const double theValue)
683 myDumpBuffer << theValue;
687 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
689 importModule("GeomAPI", "GeomAPI_Pnt");
690 myDumpBuffer << "GeomAPI_Pnt(" << thePoint->x() << ", "
691 << thePoint->y() << ", " << thePoint->z() << ")";
695 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir)
697 importModule("GeomAPI", "GeomAPI_Dir");
698 myDumpBuffer << "GeomAPI_Dir(" << theDir->x() << ", "
699 << theDir->y() << ", " << theDir->z() << ")";
703 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
704 const std::shared_ptr<GeomDataAPI_Dir>& theDir)
706 myDumpBuffer << theDir->x() << ", " << theDir->y() << ", " << theDir->z();
710 static void dumpArray(std::ostringstream& theOutput, int theSize,
711 double* theValues, std::string* theTexts)
713 for (int i = 0; i < theSize; ++i) {
716 if (theTexts[i].empty())
717 theOutput << theValues[i];
719 theOutput << "\"" << theTexts[i] << "\"";
723 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
724 const std::shared_ptr<GeomDataAPI_Point>& thePoint)
726 static const int aSize = 3;
727 double aValues[aSize] = {thePoint->x(), thePoint->y(), thePoint->z()};
728 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY(), thePoint->textZ()};
729 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
733 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
734 const std::shared_ptr<GeomDataAPI_Point2D>& thePoint)
736 static const int aSize = 2;
737 double aValues[aSize] = {thePoint->x(), thePoint->y()};
738 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY()};
739 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
743 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
744 const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool)
746 myDumpBuffer << (theAttrBool->value() ? "True" : "False");
750 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
751 const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt)
753 std::string aText = theAttrInt->text();
755 myDumpBuffer << theAttrInt->value();
757 myDumpBuffer << "\"" << aText << "\"";
761 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
762 const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal)
764 std::string aText = theAttrReal->text();
766 myDumpBuffer << theAttrReal->value();
768 myDumpBuffer << "\"" << aText << "\"";
772 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
773 const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr)
775 myDumpBuffer << "\"" << theAttrStr->value() << "\"";
779 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FolderPtr& theFolder)
781 myDumpBuffer << name(theFolder);
783 // add dumped folder to a stack
784 if (!myNames[theFolder].myIsDumped &&
785 (myEntitiesStack.empty() || myEntitiesStack.top().myEntity != theFolder))
786 myEntitiesStack.push(LastDumpedEntity(theFolder, !myNames[theFolder].myIsDefault));
791 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FeaturePtr& theEntity)
793 myDumpBuffer << name(theEntity);
795 if (!myNames[theEntity].myIsDumped) {
796 bool isUserDefinedName = !myNames[theEntity].myIsDefault;
797 // store results if they have user-defined names or colors
798 std::list<ResultPtr> aResultsWithNameOrColor;
799 std::list<ResultPtr> allRes;
800 ModelAPI_Tools::allResults(theEntity, allRes);
801 for(std::list<ResultPtr>::iterator aRes = allRes.begin(); aRes != allRes.end(); aRes++) {
802 if(!myNames[*aRes].myIsDefault || !isDefaultColor(*aRes) ||
803 !isDefaultDeflection(*aRes) || !isDefaultTransparency(*aRes))
804 aResultsWithNameOrColor.push_back(*aRes);
806 // store just dumped entity to stack
807 if (myEntitiesStack.empty() || myEntitiesStack.top().myEntity != theEntity)
808 myEntitiesStack.push(
809 LastDumpedEntity(theEntity, isUserDefinedName, aResultsWithNameOrColor));
812 // remove entity from the list of not dumped items
813 myNotDumpedEntities.erase(theEntity);
817 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ResultPtr& theResult)
819 // iterate in the structure of sub-results to the parent
820 ResultPtr aCurRes = theResult;
821 FeaturePtr aFeature = ModelAPI_Feature::feature(theResult);
822 std::list<int> anIndices; // indexes of results in the parent result, starting from topmost
823 while(aCurRes.get()) {
824 ResultBodyPtr aParent = ModelAPI_Tools::bodyOwner(aCurRes);
826 anIndices.push_front(ModelAPI_Tools::bodyIndex(aCurRes));
827 } else { // index of the result in the feature
828 std::list<ResultPtr>::const_iterator aRes = aFeature->results().cbegin();
829 for(int anIndex = 0; aRes != aFeature->results().cend(); aRes++, anIndex++) {
830 if (*aRes == aCurRes) {
831 anIndices.push_front(anIndex);
839 myDumpBuffer << name(aFeature);
840 for (std::list<int>::iterator anI = anIndices.begin(); anI != anIndices.end(); anI++) {
841 if (anI == anIndices.begin()) {
843 myDumpBuffer << ".result()";
846 myDumpBuffer << ".results()[" << *anI << "]";
849 myDumpBuffer << ".subResult(" << *anI << ")";
856 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ObjectPtr& theObject)
858 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
860 myDumpBuffer << name(aFeature);
864 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
873 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const AttributePtr& theAttr)
875 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttr->owner());
877 std::string aWrapperPrefix, aWrapperSuffix;
878 // Check the attribute belongs to copied (in multi-translation or multi-rotation) feature.
879 // In this case we need to cast explicitly feature to appropriate type.
880 AttributeBooleanPtr isCopy = anOwner->boolean("Copy");
881 if (isCopy.get() && isCopy->value()) {
882 aWrapperPrefix = featureWrapper(anOwner) + "(";
883 aWrapperSuffix = ")";
884 importModule("SketchAPI");
887 myDumpBuffer << aWrapperPrefix << name(anOwner) << aWrapperSuffix
888 << "." << attributeGetter(anOwner, theAttr->id()) << "()";
892 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
893 const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr)
895 if (theRefAttr->isObject())
896 *this << theRefAttr->object();
898 *this << theRefAttr->attr();
902 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
903 const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList)
906 std::list<std::pair<ObjectPtr, AttributePtr> > aList = theRefAttrList->list();
907 bool isAdded = false;
908 std::list<std::pair<ObjectPtr, AttributePtr> >::const_iterator anIt = aList.begin();
909 for (; anIt != aList.end(); ++anIt) {
911 myDumpBuffer << ", ";
915 *this << anIt->first;
916 else if (anIt->second)
917 * this << anIt->second;
923 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
924 const std::shared_ptr<ModelAPI_AttributeReference>& theReference)
926 *this << theReference->value();
930 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
931 const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList)
933 static const int aThreshold = 2;
934 // if number of elements in the list if greater than a threshold,
935 // dump it in a separate line with specific name
936 std::string aDumped = myDumpBuffer.str();
937 if (aDumped.empty() || theRefList->size() <= aThreshold) {
939 std::list<ObjectPtr> aList = theRefList->list();
940 bool isAdded = false;
941 std::list<ObjectPtr>::const_iterator anIt = aList.begin();
942 for (; anIt != aList.end(); ++anIt) {
944 myDumpBuffer << ", ";
952 // clear buffer and store list "as is"
953 myDumpBuffer.str("");
955 // save buffer and clear it again
956 std::string aDumpedList = myDumpBuffer.str();
957 myDumpBuffer.str("");
958 // obtain name of list
959 FeaturePtr anOwner = ModelAPI_Feature::feature(theRefList->owner());
960 std::string aListName = name(anOwner) + "_objects";
961 // store all previous data
962 myDumpBuffer << aListName << " = " << aDumpedList << std::endl
963 << aDumped << aListName;
968 static int possibleSelectionsByPoint(const GeomPointPtr& thePoint,
969 const ResultPtr& theResult,
970 const GeomShapePtr& theShape,
971 const FeaturePtr& theStartFeature,
972 const FeaturePtr& theEndFeature)
974 DocumentPtr aDoc1 = theStartFeature->document();
975 DocumentPtr aDoc2 = theEndFeature->document();
977 std::list<FeaturePtr> aFeatures = aDoc1->allFeatures();
978 if (aDoc1 != aDoc2) {
979 std::list<FeaturePtr> anAdditionalFeatures = aDoc2->allFeatures();
980 aFeatures.insert(aFeatures.end(), anAdditionalFeatures.begin(), anAdditionalFeatures.end());
983 CompositeFeaturePtr aLastCompositeFeature;
985 std::list<FeaturePtr>::const_iterator aFIt = aFeatures.begin();
986 while (aFIt != aFeatures.end() && *aFIt != theStartFeature) {
987 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFIt);
989 aLastCompositeFeature = aCompFeat;
993 // collect the list of composite features, containing the last feature;
994 // these features should be excluded from searching,
995 // because the feature cannot select sub-shapes from its parent
996 std::set<FeaturePtr> aEndFeatureParents = ModelAPI_Tools::getParents(theEndFeature);
998 int aNbPossibleSelections = 0;
999 for (; aFIt != aFeatures.end() && *aFIt != theEndFeature; ++aFIt) {
1000 bool isSkipFeature = false;
1001 if (aLastCompositeFeature && aLastCompositeFeature->isSub(*aFIt))
1002 isSkipFeature = true;
1003 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFIt);
1005 ResultPartPtr aPartRes =
1006 std::dynamic_pointer_cast<ModelAPI_ResultPart>(aCompFeat->firstResult());
1008 aLastCompositeFeature = aCompFeat;
1009 if (aEndFeatureParents.find(aCompFeat) != aEndFeatureParents.end()) {
1010 // do not process the parent for the last feature,
1011 // because it cannot select objects from its parent
1012 isSkipFeature = true;
1018 std::list<ModelGeomAlgo_Shape::SubshapeOfResult> anApproproate;
1019 if (ModelGeomAlgo_Shape::findSubshapeByPoint(*aFIt, thePoint, theShape->shapeType(),
1021 std::list<ModelGeomAlgo_Shape::SubshapeOfResult>::iterator anApIt = anApproproate.begin();
1022 for (; anApIt != anApproproate.end(); ++anApIt) {
1023 ++aNbPossibleSelections;
1025 // stop if the target shape and result are found
1026 GeomShapePtr aCurShape = anApIt->mySubshape;
1028 aCurShape = anApIt->myResult->shape();
1030 if (anApIt->myResult->isSame(theResult) && aCurShape->isSame(theShape))
1035 return aNbPossibleSelections;
1038 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1039 const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect)
1041 myDumpBuffer << "model.selection(";
1043 if(!theAttrSelect->isInitialized()) {
1044 myDumpBuffer << ")";
1048 GeomShapePtr aShape = theAttrSelect->value();
1050 aShape = theAttrSelect->context()->shape();
1054 myDumpBuffer << ")";
1058 // how to dump selection: construction features are dumped by name always
1059 bool isDumpByGeom = myGeometricalSelection;
1060 FeaturePtr aSelectedFeature;
1062 ResultPtr aRes = theAttrSelect->context();
1063 FeaturePtr aFeature = theAttrSelect->contextFeature();
1064 if (aRes && !aFeature)
1065 aSelectedFeature = ModelAPI_Feature::feature(aRes->data()->owner());
1066 isDumpByGeom = aSelectedFeature && aSelectedFeature->isInHistory();
1069 myDumpBuffer << "\"" << aShape->shapeTypeStr();
1071 // check the selected item is a ResultPart;
1072 // in this case it is necessary to get shape with full transformation
1073 // for correct calculation of the middle point
1074 ResultPartPtr aResPart =
1075 std::dynamic_pointer_cast<ModelAPI_ResultPart>(theAttrSelect->context());
1076 if (aResPart && aShape->shapeType() == GeomAPI_Shape::COMPOUND)
1077 aShape = aResPart->shape();
1078 GeomPointPtr aMiddlePoint = aShape->middlePoint();
1079 // calculate number of features, which could be selected by the same point
1080 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttrSelect->owner());
1081 int aNbPossibleSelections = possibleSelectionsByPoint(aMiddlePoint,
1082 theAttrSelect->context(), aShape, aSelectedFeature, anOwner);
1084 // produce the index if the number of applicable features is greater than 1
1085 std::string anIndex;
1086 if (aNbPossibleSelections > 1) {
1087 std::ostringstream anOutput;
1088 anOutput << "_" << aNbPossibleSelections;
1089 anIndex = anOutput.str();
1092 myDumpBuffer << anIndex << "\", ";
1093 *this << aMiddlePoint;
1096 myDumpBuffer << "\", \"" << theAttrSelect->namingName() << "\"";
1097 myDumpBuffer << ")";
1101 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1102 const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList)
1104 static const int aThreshold = 2;
1105 // if number of elements in the list if greater than a threshold,
1106 // dump it in a separate line with specific name
1107 std::string aDumped = myDumpBuffer.str();
1109 if (aDumped.empty() || theAttrSelList->size() <= aThreshold) {
1110 myDumpBuffer << "[";
1112 GeomShapePtr aShape;
1113 std::string aShapeTypeStr;
1115 bool isAdded = false;
1117 for(int anIndex = 0; anIndex < theAttrSelList->size(); ++anIndex) {
1118 AttributeSelectionPtr anAttribute = theAttrSelList->value(anIndex);
1119 aShape = anAttribute->value();
1121 ResultPtr aContext = anAttribute->context();
1123 aShape = aContext->shape();
1131 myDumpBuffer << ", ";
1135 *this << anAttribute;
1138 myDumpBuffer << "]";
1140 // clear buffer and store list "as is"
1141 myDumpBuffer.str("");
1142 *this << theAttrSelList;
1143 // save buffer and clear it again
1144 std::string aDumpedList = myDumpBuffer.str();
1145 myDumpBuffer.str("");
1146 // obtain name of list (the feature may contain several selection lists)
1147 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttrSelList->owner());
1148 std::string aListName = name(anOwner) + "_objects";
1149 std::list<AttributePtr> aSelLists =
1150 anOwner->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
1151 if (aSelLists.size() > 1) {
1153 for (std::list<AttributePtr>::iterator aSIt = aSelLists.begin();
1154 aSIt != aSelLists.end(); ++aSIt, ++anIndex)
1155 if ((*aSIt).get() == theAttrSelList.get())
1157 std::ostringstream aSStream;
1158 aSStream << aListName << "_" << anIndex;
1159 aListName = aSStream.str();
1161 // store all previous data
1162 myDumpBuffer << aListName << " = " << aDumpedList << std::endl
1163 << aDumped << aListName;
1168 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1169 const std::shared_ptr<ModelAPI_AttributeStringArray>& theArray)
1172 for(int anIndex = 0; anIndex < theArray->size(); ++anIndex) {
1176 myDumpBuffer<<"\""<<theArray->value(anIndex)<<"\"";
1184 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
1185 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&))
1187 theDumper.myDumpBuffer << theEndl;
1189 if (!theDumper.myEntitiesStack.empty()) {
1191 // all copies have been stored into stack, pop them all
1194 // Name for composite feature is dumped when all sub-entities are dumped
1195 // (see method ModelHighAPI_Dumper::processSubs).
1196 const ModelHighAPI_Dumper::LastDumpedEntity& aLastDumped = theDumper.myEntitiesStack.top();
1197 CompositeFeaturePtr aComposite =
1198 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aLastDumped.myEntity);
1200 theDumper.dumpEntitySetName();
1201 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aLastDumped.myEntity);
1203 AttributeBooleanPtr aCopyAttr = aFeature->boolean("Copy");
1204 isCopy = aCopyAttr.get() && aCopyAttr->value();
1207 } while (isCopy && !theDumper.myEntitiesStack.empty());
1210 // store all not-dumped entities first
1211 std::set<EntityPtr> aNotDumped = theDumper.myNotDumpedEntities;
1212 std::string aBufCopy = theDumper.myDumpBuffer.str();
1213 theDumper.clear(true);
1214 std::set<EntityPtr>::const_iterator anIt = aNotDumped.begin();
1215 for (; anIt != aNotDumped.end(); ++anIt) {
1216 // if the feature is composite, dump it with all subs
1217 CompositeFeaturePtr aCompFeat =
1218 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anIt);
1220 theDumper.process(aCompFeat, true);
1222 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
1223 theDumper.dumpFeature(aFeature, true);
1224 // dump the Projection feature which produces this "Copy" entity
1225 AttributeBooleanPtr aCopyAttr = aFeature->boolean("Copy");
1226 if (aCopyAttr.get() && aCopyAttr->value())
1228 const std::set<AttributePtr>& aRefs = aFeature->data()->refsToMe();
1229 std::set<AttributePtr>::iterator aRefIt = aRefs.begin();
1230 for (; aRefIt != aRefs.end(); ++aRefIt)
1231 if ((*aRefIt)->id() == "ProjectedFeature")
1232 { // process projection only
1233 FeaturePtr anOwner = ModelAPI_Feature::feature((*aRefIt)->owner());
1234 if (anOwner && !theDumper.isDumped(EntityPtr(anOwner)))
1235 theDumper.dumpFeature(anOwner, true);
1241 // avoid multiple empty lines
1242 size_t anInd = std::string::npos;
1243 while ((anInd = aBufCopy.find("\n\n\n")) != std::string::npos)
1244 aBufCopy.erase(anInd, 1);
1245 // then store currently dumped string
1246 theDumper.myFullDump << aBufCopy;
1248 // now, store all postponed features
1249 theDumper.dumpPostponed();