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 <GeomAPI_ShapeIterator.h>
29 #include <GeomAlgoAPI_NExplode.h>
31 #include <GeomDataAPI_Dir.h>
32 #include <GeomDataAPI_Point.h>
33 #include <GeomDataAPI_Point2D.h>
35 #include <ModelAPI_AttributeBoolean.h>
36 #include <ModelAPI_AttributeDouble.h>
37 #include <ModelAPI_AttributeIntArray.h>
38 #include <ModelAPI_AttributeInteger.h>
39 #include <ModelAPI_AttributeRefAttr.h>
40 #include <ModelAPI_AttributeRefAttrList.h>
41 #include <ModelAPI_AttributeReference.h>
42 #include <ModelAPI_AttributeRefList.h>
43 #include <ModelAPI_AttributeSelection.h>
44 #include <ModelAPI_AttributeSelectionList.h>
45 #include <ModelAPI_AttributeString.h>
46 #include <ModelAPI_AttributeStringArray.h>
47 #include <ModelAPI_CompositeFeature.h>
48 #include <ModelAPI_Document.h>
49 #include <ModelAPI_Entity.h>
50 #include <ModelAPI_Feature.h>
51 #include <ModelAPI_Folder.h>
52 #include <ModelAPI_Result.h>
53 #include <ModelAPI_ResultBody.h>
54 #include <ModelAPI_ResultConstruction.h>
55 #include <ModelAPI_ResultPart.h>
56 #include <ModelAPI_Session.h>
57 #include <ModelAPI_Tools.h>
59 #include <ModelGeomAlgo_Shape.h>
61 #include <PartSetPlugin_Part.h>
63 #include <OSD_OpenFile.hxx>
67 static int gCompositeStackDepth = 0;
69 ModelHighAPI_Dumper* ModelHighAPI_Dumper::mySelf = 0;
71 ModelHighAPI_Dumper::ModelHighAPI_Dumper()
72 : myGeometricalSelection(false)
77 void ModelHighAPI_Dumper::setInstance(ModelHighAPI_Dumper* theDumper)
83 ModelHighAPI_Dumper* ModelHighAPI_Dumper::getInstance()
88 void ModelHighAPI_Dumper::clear(bool bufferOnly)
91 myDumpBuffer << std::setprecision(16);
97 myFullDump << std::setprecision(16);
101 myFeatureCount.clear();
102 while (!myEntitiesStack.empty())
103 myEntitiesStack.pop();
106 myDumpPostponedInProgress = false;
110 void ModelHighAPI_Dumper::clearNotDumped()
112 myNotDumpedEntities.clear();
115 // Convert string to integer. If the string is not a number, return -1
116 static int toInt(const std::string& theString)
118 std::string::const_iterator aChar = theString.begin();
119 for (; aChar != theString.end(); ++aChar)
120 if (!std::isdigit(*aChar))
122 if (aChar != theString.end())
123 return -1; // not a number
124 return std::stoi(theString);
127 const std::string& ModelHighAPI_Dumper::name(const EntityPtr& theEntity,
128 bool theSaveNotDumped,
129 bool theUseEntityName)
131 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
132 if (aFound != myNames.end())
133 return aFound->second.myCurrentName;
135 // entity is not found, store it
136 std::string aName, aKind;
137 bool isDefaultName = false;
138 bool isSaveNotDumped = theSaveNotDumped;
139 std::ostringstream aDefaultName;
140 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
142 aName = aFeature->name();
143 aKind = aFeature->getKind();
145 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(theEntity);
147 aName = aFolder->data()->name();
148 aKind = ModelAPI_Folder::ID();
149 isSaveNotDumped = false;
153 ObjectPtr anObject = std::dynamic_pointer_cast<ModelAPI_Object>(theEntity);
155 DocumentPtr aDoc = anObject->document();
156 std::pair<int, int>& aNbFeatures = myFeatureCount[aDoc][aKind];
157 aNbFeatures.first += 1;
159 size_t anIndex = aName.find(aKind);
160 if (anIndex == 0 && aName[aKind.length()] == '_') { // name starts with "FeatureKind_"
161 std::string anIdStr = aName.substr(aKind.length() + 1);
162 int anId = toInt(anIdStr);
164 // Check number of already registered objects of such kind. Index of current object
165 // should be the same to identify feature's name as automatically generated.
166 if (aNbFeatures.first == anId && aNbFeatures.second < anId) {
167 // name is not user-defined
168 isDefaultName = true;
170 // check there are postponed features of this kind,
171 // dump their names, because the sequence of features may be changed
172 for (std::list<EntityPtr>::const_iterator aPpIt = myPostponed.begin();
173 aPpIt != myPostponed.end(); ++aPpIt) {
174 FeaturePtr aCurFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*aPpIt);
175 if (aCurFeature && aCurFeature->getKind() == aKind) {
176 myNames[*aPpIt].myIsDefault = false;
177 isDefaultName = false;
182 if (anId > aNbFeatures.second)
183 aNbFeatures.second = anId;
186 // obtain default name for the feature
187 if (theUseEntityName)
188 aDefaultName << aName;
191 NbFeaturesMap::const_iterator aFIt = myFeatureCount.begin();
192 for (; aFIt != myFeatureCount.end(); ++aFIt) {
193 std::map<std::string, std::pair<int, int> >::const_iterator aFound =
194 aFIt->second.find(aKind);
195 if (aFound != aFIt->second.end())
196 aFullIndex += aFound->second.first;
198 aDefaultName << aKind << "_" << aFullIndex;
202 myNames[theEntity] = EntityName(aDefaultName.str(), aName, isDefaultName);
204 myNotDumpedEntities.insert(theEntity);
206 // store names of results
208 saveResultNames(aFeature);
210 return myNames[theEntity].myCurrentName;
213 const std::string& ModelHighAPI_Dumper::parentName(const FeaturePtr& theEntity)
215 const std::set<AttributePtr>& aRefs = theEntity->data()->refsToMe();
216 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
217 for (; aRefIt != aRefs.end(); ++aRefIt) {
218 CompositeFeaturePtr anOwner = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(
219 ModelAPI_Feature::feature((*aRefIt)->owner()));
221 return name(anOwner);
224 static const std::string DUMMY;
228 void ModelHighAPI_Dumper::saveResultNames(const FeaturePtr& theFeature)
230 // Default name of the feature
231 bool isFeatureDefaultName = myNames[theFeature].myIsDefault;
233 // Save only names of results which is not correspond to default feature name
234 const std::list<ResultPtr>& aResults = theFeature->results();
235 std::list<ResultPtr> allRes;
236 ModelAPI_Tools::allResults(theFeature, allRes);
237 for(std::list<ResultPtr>::iterator aRes = allRes.begin(); aRes != allRes.end(); aRes++) {
238 std::pair<std::string, bool> aName = ModelAPI_Tools::getDefaultName(*aRes);
239 std::string aDefaultName = aName.first;
240 std::string aResName = (*aRes)->data()->name();
241 bool isUserDefined = !(isFeatureDefaultName && aDefaultName == aResName);
243 EntityName(aResName, (isUserDefined ? aResName : std::string()), !isUserDefined);
247 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc,
248 const std::string& theFileName)
250 // dump top level document feature
251 static const std::string aDocName("partSet");
252 myNames[theDoc] = EntityName(aDocName, std::string(), true);
253 *this << aDocName << " = model.moduleDocument()" << std::endl;
255 // dump subfeatures and store result to file
256 return process(theDoc) && exportTo(theFileName);
259 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc)
262 std::list<ObjectPtr> anObjects = theDoc->allObjects();
263 std::list<ObjectPtr>::const_iterator anObjIt = anObjects.begin();
264 // firstly, dump all parameters
265 for (; anObjIt != anObjects.end(); ++ anObjIt) {
266 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIt);
268 dumpParameter(aFeature);
270 // dump all other features
271 for (anObjIt = anObjects.begin(); anObjIt != anObjects.end(); ++anObjIt) {
272 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anObjIt);
273 if (aCompFeat) // iteratively process composite features
274 isOk = process(aCompFeat) && isOk;
275 else if (!isDumped(EntityPtr(*anObjIt))) {
277 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(*anObjIt);
281 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIt);
282 if (aFeature) // dump common feature
283 dumpFeature(aFeature);
287 // dump folders if any
292 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
295 // increase composite features stack
296 ++gCompositeStackDepth;
297 // dump composite itself
298 if (!isDumped(EntityPtr(theComposite)) || isForce)
299 dumpFeature(FeaturePtr(theComposite), isForce);
301 // sub-part is processed independently, because it provides separate document
302 if (theComposite->getKind() == PartSetPlugin_Part::ID()) {
303 // dump name of the part if it is different from default
304 if (!myEntitiesStack.empty())
307 // decrease composite features stack because we run into separate document
308 --gCompositeStackDepth;
310 ResultPartPtr aPartResult =
311 std::dynamic_pointer_cast<ModelAPI_ResultPart>(theComposite->lastResult());
314 DocumentPtr aSubDoc = aPartResult->partDoc();
317 // set name of document
318 const std::string& aPartName = myNames[theComposite].myCurrentName;
319 std::string aDocName = aPartName + "_doc";
320 myNames[aSubDoc] = EntityName(aDocName, std::string(), true);
322 // dump document in a separate line
323 *this << aDocName << " = " << aPartName << ".document()" << std::endl;
324 // dump features in the document
325 bool aRes = process(aSubDoc);
326 *this << "model.do()" << std::endl;
331 bool isOk = processSubs(theComposite);
332 // decrease composite features stack
333 --gCompositeStackDepth;
338 bool ModelHighAPI_Dumper::processSubs(
339 const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
343 // dump all sub-features;
344 bool isSubDumped = false;
345 int aNbSubs = theComposite->numberOfSubs();
346 for (int anIndex = 0; anIndex < aNbSubs; ++anIndex) {
347 FeaturePtr aFeature = theComposite->subFeature(anIndex);
348 if (isDumped(EntityPtr(aFeature)))
352 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
353 if (aCompFeat) // iteratively process composite features
354 isOk = process(aCompFeat) && isOk;
356 dumpFeature(aFeature, true);
359 bool isDumpSetName = !myEntitiesStack.empty() &&
360 myEntitiesStack.top().myEntity == EntityPtr(theComposite);
361 bool isForceModelDo = isSubDumped && isDumpSetName &&
362 (myEntitiesStack.top().myUserName || !myEntitiesStack.top().myResults.empty());
363 // It is necessary for the sketch to create its result when complete (command "model.do()").
364 // This option is set by flat theDumpModelDo.
365 // However, nested sketches are rebuilt by parent feature, so, they do not need
366 // explicit call of "model.do()". This will be controlled by the depth of the stack.
367 if (isForceModelDo || (theDumpModelDo && gCompositeStackDepth <= 1))
368 *this << "model.do()" << std::endl;
370 // dump "setName" for composite feature
376 void ModelHighAPI_Dumper::postpone(const EntityPtr& theEntity)
379 name(theEntity, false);
380 myPostponed.push_back(theEntity);
383 void ModelHighAPI_Dumper::dumpPostponed(bool theDumpFolders)
385 if (myDumpPostponedInProgress)
388 myDumpPostponedInProgress = true;
389 // make a copy of postponed entities, because the list will be updated
390 // if some features are not able to be dumped
391 std::list<EntityPtr> aPostponedCopy = myPostponed;
394 // iterate over postponed entities and try to dump them
395 std::list<EntityPtr>::const_iterator anIt = aPostponedCopy.begin();
396 for (; anIt != aPostponedCopy.end(); ++anIt) {
397 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(*anIt);
402 myPostponed.push_back(*anIt);
405 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
407 dumpFeature(aFeature, true);
410 myDumpPostponedInProgress = false;
413 void ModelHighAPI_Dumper::dumpSubFeatureNameAndColor(const std::string theSubFeatureGet,
414 const FeaturePtr& theSubFeature)
416 name(theSubFeature, false);
417 myNames[theSubFeature] = EntityName(theSubFeatureGet, theSubFeature->name(), false);
419 // store results if they have user-defined names or colors
420 std::list<ResultPtr> aResultsWithNameOrColor;
421 const std::list<ResultPtr>& aResults = theSubFeature->results();
422 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
423 for (; aResIt != aResults.end(); ++aResIt) {
424 std::string aResName = (*aResIt)->data()->name();
425 myNames[*aResIt] = EntityName(aResName, aResName, false);
426 aResultsWithNameOrColor.push_back(*aResIt);
429 // store just dumped entity to stack
430 myEntitiesStack.push(LastDumpedEntity(theSubFeature, true, aResultsWithNameOrColor));
435 bool ModelHighAPI_Dumper::exportTo(const std::string& theFileName)
438 OSD_OpenStream(aFile, theFileName.c_str(), std::ofstream::out);
439 if (!aFile.is_open())
442 // standard header (encoding + imported modules)
443 aFile << "# -*- coding: utf-8 -*-" << std::endl << std::endl;
444 for (ModulesMap::const_iterator aModIt = myModules.begin();
445 aModIt != myModules.end(); ++aModIt) {
446 aFile << "from " << aModIt->first << " import ";
447 if (aModIt->second.empty() ||
448 aModIt->second.find(std::string()) != aModIt->second.end())
449 aFile << "*"; // import whole module
451 // import specific features
452 std::set<std::string>::const_iterator anObjIt = aModIt->second.begin();
454 for (++anObjIt; anObjIt != aModIt->second.end(); ++anObjIt)
455 aFile << ", " << *anObjIt;
459 if (!myModules.empty())
462 aFile << "from salome.shaper import model" << std::endl << std::endl;
463 aFile << "model.begin()" << std::endl;
465 // dump collected data
466 aFile << myFullDump.str();
467 aFile << myDumpBuffer.str();
470 aFile << "model.end()" << std::endl;
478 void ModelHighAPI_Dumper::importModule(const std::string& theModuleName,
479 const std::string& theObject)
481 myModules[theModuleName].insert(theObject);
484 void ModelHighAPI_Dumper::dumpEntitySetName()
486 const LastDumpedEntity& aLastDumped = myEntitiesStack.top();
487 bool isBufferEmpty = myDumpBuffer.str().empty();
489 // dump "setName" for the entity
490 if (aLastDumped.myUserName) {
491 EntityName& anEntityNames = myNames[aLastDumped.myEntity];
492 if (!anEntityNames.myIsDefault)
493 myDumpBuffer << anEntityNames.myCurrentName << ".setName(\""
494 << anEntityNames.myUserName << "\")" << std::endl;
495 // don't dump "setName" for the entity twice
496 anEntityNames.myUserName.clear();
497 anEntityNames.myIsDefault = true;
499 // dump "setName" for results
500 std::list<ResultPtr>::const_iterator aResIt = aLastDumped.myResults.begin();
501 std::list<ResultPtr>::const_iterator aResEnd = aLastDumped.myResults.end();
502 for (; aResIt != aResEnd; ++aResIt) {
504 EntityName& anEntityNames = myNames[*aResIt];
505 if (!anEntityNames.myIsDefault) {
507 myDumpBuffer << ".setName(\"" << anEntityNames.myUserName << "\")" << std::endl;
508 // don't dump "setName" for the entity twice
509 anEntityNames.myUserName.clear();
510 anEntityNames.myIsDefault = true;
513 if (!isDefaultColor(*aResIt)) {
514 AttributeIntArrayPtr aColor = (*aResIt)->data()->intArray(ModelAPI_Result::COLOR_ID());
515 if (aColor && aColor->isInitialized()) {
517 myDumpBuffer << ".setColor(" << aColor->value(0) << ", " << aColor->value(1)
518 << ", " << aColor->value(2) << ")" << std::endl;
521 // set result deflection
522 if (!isDefaultDeflection(*aResIt)) {
523 AttributeDoublePtr aDeflectionAttr =
524 (*aResIt)->data()->real(ModelAPI_Result::DEFLECTION_ID());
525 if(aDeflectionAttr.get() && aDeflectionAttr->isInitialized()) {
527 myDumpBuffer << ".setDeflection(" << aDeflectionAttr->value() << ")" << std::endl;
530 // set result transparency
531 if (!isDefaultTransparency(*aResIt)) {
532 AttributeDoublePtr aTransparencyAttr =
533 (*aResIt)->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
534 if(aTransparencyAttr.get() && aTransparencyAttr->isInitialized()) {
536 myDumpBuffer << ".setTransparency(" << aTransparencyAttr->value() << ")" << std::endl;
541 myNames[aLastDumped.myEntity].myIsDumped = true;
542 myEntitiesStack.pop();
544 // clean buffer if it was clear before
546 myFullDump << myDumpBuffer.str();
547 myDumpBuffer.str("");
551 bool ModelHighAPI_Dumper::isDumped(const EntityPtr& theEntity) const
553 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
554 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
555 return (aFound != myNames.end() && aFound->second.myIsDumped) ||
556 myFeaturesToSkip.find(aFeature) != myFeaturesToSkip.end();
559 bool ModelHighAPI_Dumper::isDumped(const AttributeRefAttrPtr& theRefAttr) const
562 if (theRefAttr->isObject())
563 aFeature = ModelAPI_Feature::feature(theRefAttr->object());
565 aFeature = ModelAPI_Feature::feature(theRefAttr->attr()->owner());
566 return aFeature && isDumped(EntityPtr(aFeature));
569 bool ModelHighAPI_Dumper::isDumped(const AttributeRefListPtr& theRefList) const
571 std::list<ObjectPtr> aRefs = theRefList->list();
572 std::list<ObjectPtr>::iterator anIt = aRefs.begin();
573 for (; anIt != aRefs.end(); ++anIt) {
574 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
575 if (aFeature && !isDumped(EntityPtr(aFeature)))
581 static bool isSketchSub(const FeaturePtr& theFeature)
583 static const std::string SKETCH("Sketch");
584 CompositeFeaturePtr anOwner = ModelAPI_Tools::compositeOwner(theFeature);
585 return anOwner && anOwner->getKind() == SKETCH;
588 bool ModelHighAPI_Dumper::isDefaultColor(const ResultPtr& theResult) const
590 AttributeIntArrayPtr aColor = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
591 if (!aColor || !aColor->isInitialized())
594 // check the result belongs to sketch entity, do not dump color in this way
595 ResultConstructionPtr aResConstr =
596 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theResult);
598 FeaturePtr aFeature = ModelAPI_Feature::feature(theResult->data()->owner());
599 if (isSketchSub(aFeature))
603 std::string aSection, aName, aDefault;
604 theResult->colorConfigInfo(aSection, aName, aDefault);
606 // dump current color
607 std::ostringstream aColorInfo;
608 aColorInfo << aColor->value(0) << "," << aColor->value(1) << "," << aColor->value(2);
610 return aDefault == aColorInfo.str();
613 bool ModelHighAPI_Dumper::isDefaultDeflection(const ResultPtr& theResult) const
615 AttributeDoublePtr aDeflectionAttr = theResult->data()->real(ModelAPI_Result::DEFLECTION_ID());
616 if(!aDeflectionAttr || !aDeflectionAttr->isInitialized()) {
620 double aCurrent = aDeflectionAttr->value();
621 double aDefault = -1;
623 bool isConstruction = false;
624 std::string aResultGroup = theResult->groupName();
625 if (aResultGroup == ModelAPI_ResultConstruction::group())
626 isConstruction = true;
627 else if (aResultGroup == ModelAPI_ResultBody::group()) {
628 GeomShapePtr aGeomShape = theResult->shape();
629 if (aGeomShape.get()) {
630 // if the shape could not be exploded on faces, it contains only wires, edges, and vertices
631 // correction of deviation for them should not influence to the application performance
632 GeomAPI_ShapeExplorer anExp(aGeomShape, GeomAPI_Shape::FACE);
633 isConstruction = !anExp.more();
637 aDefault = Config_PropManager::real("Visualization", "construction_deflection");
639 aDefault = Config_PropManager::real("Visualization", "body_deflection");
641 return fabs(aCurrent - aDefault) < 1.e-12;
644 bool ModelHighAPI_Dumper::isDefaultTransparency(const ResultPtr& theResult) const
646 AttributeDoublePtr anAttribute = theResult->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
647 if(!anAttribute || !anAttribute->isInitialized()) {
650 return fabs(anAttribute->value()) < 1.e-12;
653 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char theChar)
655 myDumpBuffer << theChar;
659 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char* theString)
661 myDumpBuffer << theString;
665 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::string& theString)
667 myDumpBuffer << theString;
671 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const bool theValue)
673 myDumpBuffer << (theValue ? "True" : "False");
677 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const int theValue)
679 myDumpBuffer << theValue;
683 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const double theValue)
685 myDumpBuffer << theValue;
689 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
691 importModule("GeomAPI", "GeomAPI_Pnt");
692 myDumpBuffer << "GeomAPI_Pnt(" << thePoint->x() << ", "
693 << thePoint->y() << ", " << thePoint->z() << ")";
697 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir)
699 importModule("GeomAPI", "GeomAPI_Dir");
700 myDumpBuffer << "GeomAPI_Dir(" << theDir->x() << ", "
701 << theDir->y() << ", " << theDir->z() << ")";
705 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
706 const std::shared_ptr<GeomDataAPI_Dir>& theDir)
708 myDumpBuffer << theDir->x() << ", " << theDir->y() << ", " << theDir->z();
712 static void dumpArray(std::ostringstream& theOutput, int theSize,
713 double* theValues, std::string* theTexts)
715 for (int i = 0; i < theSize; ++i) {
718 if (theTexts[i].empty())
719 theOutput << theValues[i];
721 theOutput << "\"" << theTexts[i] << "\"";
725 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
726 const std::shared_ptr<GeomDataAPI_Point>& thePoint)
728 static const int aSize = 3;
729 double aValues[aSize] = {thePoint->x(), thePoint->y(), thePoint->z()};
730 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY(), thePoint->textZ()};
731 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
735 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
736 const std::shared_ptr<GeomDataAPI_Point2D>& thePoint)
738 static const int aSize = 2;
739 double aValues[aSize] = {thePoint->x(), thePoint->y()};
740 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY()};
741 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
745 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
746 const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool)
748 myDumpBuffer << (theAttrBool->value() ? "True" : "False");
752 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
753 const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt)
755 std::string aText = theAttrInt->text();
757 myDumpBuffer << theAttrInt->value();
759 myDumpBuffer << "\"" << aText << "\"";
763 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
764 const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal)
766 std::string aText = theAttrReal->text();
768 myDumpBuffer << theAttrReal->value();
770 myDumpBuffer << "\"" << aText << "\"";
774 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
775 const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr)
777 myDumpBuffer << "\"" << theAttrStr->value() << "\"";
781 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FolderPtr& theFolder)
783 myDumpBuffer << name(theFolder);
785 // add dumped folder to a stack
786 if (!myNames[theFolder].myIsDumped &&
787 (myEntitiesStack.empty() || myEntitiesStack.top().myEntity != theFolder))
788 myEntitiesStack.push(LastDumpedEntity(theFolder, !myNames[theFolder].myIsDefault));
793 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FeaturePtr& theEntity)
795 myDumpBuffer << name(theEntity);
797 if (!myNames[theEntity].myIsDumped) {
798 bool isUserDefinedName = !myNames[theEntity].myIsDefault;
799 // store results if they have user-defined names or colors
800 std::list<ResultPtr> aResultsWithNameOrColor;
801 std::list<ResultPtr> allRes;
802 ModelAPI_Tools::allResults(theEntity, allRes);
803 for(std::list<ResultPtr>::iterator aRes = allRes.begin(); aRes != allRes.end(); aRes++) {
804 if(!myNames[*aRes].myIsDefault || !isDefaultColor(*aRes) ||
805 !isDefaultDeflection(*aRes) || !isDefaultTransparency(*aRes))
806 aResultsWithNameOrColor.push_back(*aRes);
808 // store just dumped entity to stack
809 if (myEntitiesStack.empty() || myEntitiesStack.top().myEntity != theEntity)
810 myEntitiesStack.push(
811 LastDumpedEntity(theEntity, isUserDefinedName, aResultsWithNameOrColor));
814 // remove entity from the list of not dumped items
815 myNotDumpedEntities.erase(theEntity);
819 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ResultPtr& theResult)
821 // iterate in the structure of sub-results to the parent
822 ResultPtr aCurRes = theResult;
823 FeaturePtr aFeature = ModelAPI_Feature::feature(theResult);
824 std::list<int> anIndices; // indexes of results in the parent result, starting from topmost
825 while(aCurRes.get()) {
826 ResultBodyPtr aParent = ModelAPI_Tools::bodyOwner(aCurRes);
828 anIndices.push_front(ModelAPI_Tools::bodyIndex(aCurRes));
829 } else { // index of the result in the feature
830 std::list<ResultPtr>::const_iterator aRes = aFeature->results().cbegin();
831 for(int anIndex = 0; aRes != aFeature->results().cend(); aRes++, anIndex++) {
832 if (*aRes == aCurRes) {
833 anIndices.push_front(anIndex);
841 myDumpBuffer << name(aFeature);
842 for (std::list<int>::iterator anI = anIndices.begin(); anI != anIndices.end(); anI++) {
843 if (anI == anIndices.begin()) {
845 myDumpBuffer << ".result()";
848 myDumpBuffer << ".results()[" << *anI << "]";
851 myDumpBuffer << ".subResult(" << *anI << ")";
858 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ObjectPtr& theObject)
860 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
862 myDumpBuffer << name(aFeature);
866 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
875 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const AttributePtr& theAttr)
877 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttr->owner());
879 std::string aWrapperPrefix, aWrapperSuffix;
880 // Check the attribute belongs to copied (in multi-translation or multi-rotation) feature.
881 // In this case we need to cast explicitly feature to appropriate type.
882 AttributeBooleanPtr isCopy = anOwner->boolean("Copy");
883 if (isCopy.get() && isCopy->value()) {
884 aWrapperPrefix = featureWrapper(anOwner) + "(";
885 aWrapperSuffix = ")";
886 importModule("SketchAPI");
889 myDumpBuffer << aWrapperPrefix << name(anOwner) << aWrapperSuffix
890 << "." << attributeGetter(anOwner, theAttr->id()) << "()";
894 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
895 const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr)
897 if (theRefAttr->isObject())
898 *this << theRefAttr->object();
900 *this << theRefAttr->attr();
904 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
905 const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList)
908 std::list<std::pair<ObjectPtr, AttributePtr> > aList = theRefAttrList->list();
909 bool isAdded = false;
910 std::list<std::pair<ObjectPtr, AttributePtr> >::const_iterator anIt = aList.begin();
911 for (; anIt != aList.end(); ++anIt) {
913 myDumpBuffer << ", ";
917 *this << anIt->first;
918 else if (anIt->second)
919 * this << anIt->second;
925 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
926 const std::shared_ptr<ModelAPI_AttributeReference>& theReference)
928 *this << theReference->value();
932 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
933 const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList)
935 static const int aThreshold = 2;
936 // if number of elements in the list if greater than a threshold,
937 // dump it in a separate line with specific name
938 std::string aDumped = myDumpBuffer.str();
939 if (aDumped.empty() || theRefList->size() <= aThreshold) {
941 std::list<ObjectPtr> aList = theRefList->list();
942 bool isAdded = false;
943 std::list<ObjectPtr>::const_iterator anIt = aList.begin();
944 for (; anIt != aList.end(); ++anIt) {
946 myDumpBuffer << ", ";
954 // clear buffer and store list "as is"
955 myDumpBuffer.str("");
957 // save buffer and clear it again
958 std::string aDumpedList = myDumpBuffer.str();
959 myDumpBuffer.str("");
960 // obtain name of list
961 FeaturePtr anOwner = ModelAPI_Feature::feature(theRefList->owner());
962 std::string aListName = name(anOwner) + "_objects";
963 // store all previous data
964 myDumpBuffer << aListName << " = " << aDumpedList << std::endl
965 << aDumped << aListName;
970 static int possibleSelectionsByPoint(const GeomPointPtr& thePoint,
971 const ResultPtr& theResult,
972 const GeomShapePtr& theShape,
973 const FeaturePtr& theStartFeature,
974 const FeaturePtr& theEndFeature)
976 DocumentPtr aDoc1 = theStartFeature->document();
977 DocumentPtr aDoc2 = theEndFeature->document();
979 std::list<FeaturePtr> aFeatures = aDoc1->allFeatures();
980 if (aDoc1 != aDoc2) {
981 std::list<FeaturePtr> anAdditionalFeatures = aDoc2->allFeatures();
982 aFeatures.insert(aFeatures.end(), anAdditionalFeatures.begin(), anAdditionalFeatures.end());
985 CompositeFeaturePtr aLastCompositeFeature;
987 std::list<FeaturePtr>::const_iterator aFIt = aFeatures.begin();
988 while (aFIt != aFeatures.end() && *aFIt != theStartFeature) {
989 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFIt);
991 aLastCompositeFeature = aCompFeat;
995 // collect the list of composite features, containing the last feature;
996 // these features should be excluded from searching,
997 // because the feature cannot select sub-shapes from its parent
998 std::set<FeaturePtr> aEndFeatureParents = ModelAPI_Tools::getParents(theEndFeature);
1000 int aNbPossibleSelections = 0;
1001 for (; aFIt != aFeatures.end() && *aFIt != theEndFeature; ++aFIt) {
1002 bool isSkipFeature = false;
1003 if (aLastCompositeFeature && aLastCompositeFeature->isSub(*aFIt))
1004 isSkipFeature = true;
1005 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFIt);
1007 ResultPartPtr aPartRes =
1008 std::dynamic_pointer_cast<ModelAPI_ResultPart>(aCompFeat->firstResult());
1010 aLastCompositeFeature = aCompFeat;
1011 if (aEndFeatureParents.find(aCompFeat) != aEndFeatureParents.end()) {
1012 // do not process the parent for the last feature,
1013 // because it cannot select objects from its parent
1014 isSkipFeature = true;
1020 std::list<ModelGeomAlgo_Shape::SubshapeOfResult> anApproproate;
1021 if (ModelGeomAlgo_Shape::findSubshapeByPoint(*aFIt, thePoint, theShape->shapeType(),
1023 std::list<ModelGeomAlgo_Shape::SubshapeOfResult>::iterator anApIt = anApproproate.begin();
1024 for (; anApIt != anApproproate.end(); ++anApIt) {
1025 ++aNbPossibleSelections;
1027 // stop if the target shape and result are found
1028 GeomShapePtr aCurShape = anApIt->mySubshape;
1030 aCurShape = anApIt->myResult->shape();
1032 if (anApIt->myResult->isSame(theResult) && aCurShape->isSame(theShape))
1037 return aNbPossibleSelections;
1040 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1041 const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect)
1043 myDumpBuffer << "model.selection(";
1045 if(!theAttrSelect->isInitialized()) {
1046 myDumpBuffer << ")";
1050 GeomShapePtr aShape = theAttrSelect->value();
1052 aShape = theAttrSelect->context()->shape();
1056 myDumpBuffer << ")";
1060 // how to dump selection: construction features are dumped by name always
1061 bool isDumpByGeom = myGeometricalSelection;
1062 FeaturePtr aSelectedFeature;
1064 ResultPtr aRes = theAttrSelect->context();
1065 FeaturePtr aFeature = theAttrSelect->contextFeature();
1066 if (aRes && !aFeature)
1067 aSelectedFeature = ModelAPI_Feature::feature(aRes->data()->owner());
1068 isDumpByGeom = aSelectedFeature && aSelectedFeature->isInHistory();
1071 if (theAttrSelect->isGeometricalSelection() && aShape->shapeType() == GeomAPI_Shape::COMPOUND) {
1072 GeomAPI_ShapeIterator anIt(aShape);
1073 aShape = anIt.current();
1076 myDumpBuffer << "\"" << aShape->shapeTypeStr();
1077 bool aStandardDump = true;
1079 // check the selected item is a ResultPart;
1080 // in this case it is necessary to get shape with full transformation
1081 // for correct calculation of the middle point
1082 ResultPartPtr aResPart =
1083 std::dynamic_pointer_cast<ModelAPI_ResultPart>(theAttrSelect->context());
1084 if (aResPart && aShape->shapeType() == GeomAPI_Shape::COMPOUND)
1085 aShape = aResPart->shape();
1086 GeomPointPtr aMiddlePoint = aShape->middlePoint();
1087 // calculate number of features, which could be selected by the same point
1088 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttrSelect->owner());
1089 int aNbPossibleSelections = possibleSelectionsByPoint(aMiddlePoint,
1090 theAttrSelect->context(), aShape, aSelectedFeature, anOwner);
1092 // produce the index if the number of applicable features is greater than 1
1093 std::string anIndex;
1094 if (aNbPossibleSelections > 1) {
1095 std::ostringstream anOutput;
1096 anOutput << "_" << aNbPossibleSelections;
1097 anIndex = anOutput.str();
1100 myDumpBuffer << anIndex << "\", ("
1101 << aMiddlePoint->x() << ", "
1102 << aMiddlePoint->y() << ", "
1103 << aMiddlePoint->z() << ")";
1104 aStandardDump = false;
1105 } else if (myWeakNamingSelection && aShape.get() && theAttrSelect->context().get() &&
1106 aShape != theAttrSelect->context()->shape()) { // weak naming for local selection only
1107 GeomAlgoAPI_NExplode aNExplode(theAttrSelect->context()->shape(), aShape->shapeType());
1108 int anIndex = aNExplode.index(aShape);
1109 if (anIndex != 0) { // found a week-naming index, so, export it
1110 myDumpBuffer<<"\", \""<<
1111 theAttrSelect->contextName(theAttrSelect->context())<<"\", "<<anIndex;
1112 aStandardDump = false;
1116 myDumpBuffer << "\", \"" << theAttrSelect->namingName() << "\"";
1117 myDumpBuffer << ")";
1121 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1122 const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList)
1124 static const int aThreshold = 2;
1125 // if number of elements in the list if greater than a threshold,
1126 // dump it in a separate line with specific name
1127 std::string aDumped = myDumpBuffer.str();
1129 if (aDumped.empty() || theAttrSelList->size() <= aThreshold) {
1130 myDumpBuffer << "[";
1132 GeomShapePtr aShape;
1133 std::string aShapeTypeStr;
1135 bool isAdded = false;
1137 for(int anIndex = 0; anIndex < theAttrSelList->size(); ++anIndex) {
1138 AttributeSelectionPtr anAttribute = theAttrSelList->value(anIndex);
1139 aShape = anAttribute->value();
1141 ResultPtr aContext = anAttribute->context();
1143 aShape = aContext->shape();
1151 myDumpBuffer << ", ";
1155 *this << anAttribute;
1158 myDumpBuffer << "]";
1160 // clear buffer and store list "as is"
1161 myDumpBuffer.str("");
1162 *this << theAttrSelList;
1163 // save buffer and clear it again
1164 std::string aDumpedList = myDumpBuffer.str();
1165 myDumpBuffer.str("");
1166 // obtain name of list (the feature may contain several selection lists)
1167 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttrSelList->owner());
1168 std::string aListName = name(anOwner) + "_objects";
1169 std::list<AttributePtr> aSelLists =
1170 anOwner->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
1171 if (aSelLists.size() > 1) {
1173 for (std::list<AttributePtr>::iterator aSIt = aSelLists.begin();
1174 aSIt != aSelLists.end(); ++aSIt, ++anIndex)
1175 if ((*aSIt).get() == theAttrSelList.get())
1177 std::ostringstream aSStream;
1178 aSStream << aListName << "_" << anIndex;
1179 aListName = aSStream.str();
1181 // store all previous data
1182 myDumpBuffer << aListName << " = " << aDumpedList << std::endl
1183 << aDumped << aListName;
1188 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1189 const std::shared_ptr<ModelAPI_AttributeStringArray>& theArray)
1192 for(int anIndex = 0; anIndex < theArray->size(); ++anIndex) {
1196 myDumpBuffer<<"\""<<theArray->value(anIndex)<<"\"";
1204 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
1205 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&))
1207 theDumper.myDumpBuffer << theEndl;
1209 if (!theDumper.myEntitiesStack.empty()) {
1211 // all copies have been stored into stack, pop them all
1214 // Name for composite feature is dumped when all sub-entities are dumped
1215 // (see method ModelHighAPI_Dumper::processSubs).
1216 const ModelHighAPI_Dumper::LastDumpedEntity& aLastDumped = theDumper.myEntitiesStack.top();
1217 CompositeFeaturePtr aComposite =
1218 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aLastDumped.myEntity);
1220 theDumper.dumpEntitySetName();
1221 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aLastDumped.myEntity);
1223 AttributeBooleanPtr aCopyAttr = aFeature->boolean("Copy");
1224 isCopy = aCopyAttr.get() && aCopyAttr->value();
1227 } while (isCopy && !theDumper.myEntitiesStack.empty());
1230 // store all not-dumped entities first
1231 std::set<EntityPtr> aNotDumped = theDumper.myNotDumpedEntities;
1232 std::string aBufCopy = theDumper.myDumpBuffer.str();
1233 theDumper.clear(true);
1234 std::set<EntityPtr>::const_iterator anIt = aNotDumped.begin();
1235 for (; anIt != aNotDumped.end(); ++anIt) {
1236 // if the feature is composite, dump it with all subs
1237 CompositeFeaturePtr aCompFeat =
1238 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anIt);
1240 theDumper.process(aCompFeat, true);
1242 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
1243 theDumper.dumpFeature(aFeature, true);
1244 // dump the Projection feature which produces this "Copy" entity
1245 AttributeBooleanPtr aCopyAttr = aFeature->boolean("Copy");
1246 if (aCopyAttr.get() && aCopyAttr->value())
1248 const std::set<AttributePtr>& aRefs = aFeature->data()->refsToMe();
1249 std::set<AttributePtr>::iterator aRefIt = aRefs.begin();
1250 for (; aRefIt != aRefs.end(); ++aRefIt)
1251 if ((*aRefIt)->id() == "ProjectedFeature")
1252 { // process projection only
1253 FeaturePtr anOwner = ModelAPI_Feature::feature((*aRefIt)->owner());
1254 if (anOwner && !theDumper.isDumped(EntityPtr(anOwner)))
1255 theDumper.dumpFeature(anOwner, true);
1261 // avoid multiple empty lines
1262 size_t anInd = std::string::npos;
1263 while ((anInd = aBufCopy.find("\n\n\n")) != std::string::npos)
1264 aBufCopy.erase(anInd, 1);
1265 // then store currently dumped string
1266 theDumper.myFullDump << aBufCopy;
1268 // now, store all postponed features
1269 theDumper.dumpPostponed();