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 (ModulesSet::const_iterator aModIt = myModules.begin();
445 aModIt != myModules.end(); ++aModIt) {
446 aFile << "from " << *aModIt << " import *" << std::endl;
448 if (!myModules.empty())
451 aFile << "from salome.shaper import model" << std::endl << std::endl;
452 aFile << "model.begin()" << std::endl;
454 // dump collected data
455 aFile << myFullDump.str();
456 aFile << myDumpBuffer.str();
459 aFile << "model.end()" << std::endl;
467 void ModelHighAPI_Dumper::importModule(const std::string& theModuleName)
469 myModules.insert(theModuleName);
472 void ModelHighAPI_Dumper::dumpEntitySetName()
474 const LastDumpedEntity& aLastDumped = myEntitiesStack.top();
475 bool isBufferEmpty = myDumpBuffer.str().empty();
477 // dump "setName" for the entity
478 if (aLastDumped.myUserName) {
479 EntityName& anEntityNames = myNames[aLastDumped.myEntity];
480 if (!anEntityNames.myIsDefault)
481 myDumpBuffer << anEntityNames.myCurrentName << ".setName(\""
482 << anEntityNames.myUserName << "\")" << std::endl;
483 // don't dump "setName" for the entity twice
484 anEntityNames.myUserName.clear();
485 anEntityNames.myIsDefault = true;
487 // dump "setName" for results
488 std::list<ResultPtr>::const_iterator aResIt = aLastDumped.myResults.begin();
489 std::list<ResultPtr>::const_iterator aResEnd = aLastDumped.myResults.end();
490 for (; aResIt != aResEnd; ++aResIt) {
492 EntityName& anEntityNames = myNames[*aResIt];
493 if (!anEntityNames.myIsDefault) {
495 myDumpBuffer << ".setName(\"" << anEntityNames.myUserName << "\")" << std::endl;
496 // don't dump "setName" for the entity twice
497 anEntityNames.myUserName.clear();
498 anEntityNames.myIsDefault = true;
501 if (!isDefaultColor(*aResIt)) {
502 AttributeIntArrayPtr aColor = (*aResIt)->data()->intArray(ModelAPI_Result::COLOR_ID());
503 if (aColor && aColor->isInitialized()) {
505 myDumpBuffer << ".setColor(" << aColor->value(0) << ", " << aColor->value(1)
506 << ", " << aColor->value(2) << ")" << std::endl;
509 // set result deflection
510 if (!isDefaultDeflection(*aResIt)) {
511 AttributeDoublePtr aDeflectionAttr =
512 (*aResIt)->data()->real(ModelAPI_Result::DEFLECTION_ID());
513 if(aDeflectionAttr.get() && aDeflectionAttr->isInitialized()) {
515 myDumpBuffer << ".setDeflection(" << aDeflectionAttr->value() << ")" << std::endl;
518 // set result transparency
519 if (!isDefaultTransparency(*aResIt)) {
520 AttributeDoublePtr aTransparencyAttr =
521 (*aResIt)->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
522 if(aTransparencyAttr.get() && aTransparencyAttr->isInitialized()) {
524 myDumpBuffer << ".setTransparency(" << aTransparencyAttr->value() << ")" << std::endl;
529 myNames[aLastDumped.myEntity].myIsDumped = true;
530 myEntitiesStack.pop();
532 // clean buffer if it was clear before
534 myFullDump << myDumpBuffer.str();
535 myDumpBuffer.str("");
539 bool ModelHighAPI_Dumper::isDumped(const EntityPtr& theEntity) const
541 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
542 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
543 return (aFound != myNames.end() && aFound->second.myIsDumped) ||
544 myFeaturesToSkip.find(aFeature) != myFeaturesToSkip.end();
547 bool ModelHighAPI_Dumper::isDumped(const AttributeRefAttrPtr& theRefAttr) const
550 if (theRefAttr->isObject())
551 aFeature = ModelAPI_Feature::feature(theRefAttr->object());
553 aFeature = ModelAPI_Feature::feature(theRefAttr->attr()->owner());
554 return aFeature && isDumped(EntityPtr(aFeature));
557 bool ModelHighAPI_Dumper::isDumped(const AttributeRefListPtr& theRefList) const
559 std::list<ObjectPtr> aRefs = theRefList->list();
560 std::list<ObjectPtr>::iterator anIt = aRefs.begin();
561 for (; anIt != aRefs.end(); ++anIt) {
562 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
563 if (aFeature && !isDumped(EntityPtr(aFeature)))
569 static bool isSketchSub(const FeaturePtr& theFeature)
571 static const std::string SKETCH("Sketch");
572 CompositeFeaturePtr anOwner = ModelAPI_Tools::compositeOwner(theFeature);
573 return anOwner && anOwner->getKind() == SKETCH;
576 bool ModelHighAPI_Dumper::isDefaultColor(const ResultPtr& theResult) const
578 AttributeIntArrayPtr aColor = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
579 if (!aColor || !aColor->isInitialized())
582 // check the result belongs to sketch entity, do not dump color in this way
583 ResultConstructionPtr aResConstr =
584 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theResult);
586 FeaturePtr aFeature = ModelAPI_Feature::feature(theResult->data()->owner());
587 if (isSketchSub(aFeature))
591 std::string aSection, aName, aDefault;
592 theResult->colorConfigInfo(aSection, aName, aDefault);
594 // dump current color
595 std::ostringstream aColorInfo;
596 aColorInfo << aColor->value(0) << "," << aColor->value(1) << "," << aColor->value(2);
598 return aDefault == aColorInfo.str();
601 bool ModelHighAPI_Dumper::isDefaultDeflection(const ResultPtr& theResult) const
603 AttributeDoublePtr aDeflectionAttr = theResult->data()->real(ModelAPI_Result::DEFLECTION_ID());
604 if(!aDeflectionAttr || !aDeflectionAttr->isInitialized()) {
608 double aCurrent = aDeflectionAttr->value();
609 double aDefault = -1;
611 bool isConstruction = false;
612 std::string aResultGroup = theResult->groupName();
613 if (aResultGroup == ModelAPI_ResultConstruction::group())
614 isConstruction = true;
615 else if (aResultGroup == ModelAPI_ResultBody::group()) {
616 GeomShapePtr aGeomShape = theResult->shape();
617 if (aGeomShape.get()) {
618 // if the shape could not be exploded on faces, it contains only wires, edges, and vertices
619 // correction of deviation for them should not influence to the application performance
620 GeomAPI_ShapeExplorer anExp(aGeomShape, GeomAPI_Shape::FACE);
621 isConstruction = !anExp.more();
625 aDefault = Config_PropManager::real("Visualization", "construction_deflection");
627 aDefault = Config_PropManager::real("Visualization", "body_deflection");
629 return fabs(aCurrent - aDefault) < 1.e-12;
632 bool ModelHighAPI_Dumper::isDefaultTransparency(const ResultPtr& theResult) const
634 AttributeDoublePtr anAttribute = theResult->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
635 if(!anAttribute || !anAttribute->isInitialized()) {
638 return fabs(anAttribute->value()) < 1.e-12;
641 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char theChar)
643 myDumpBuffer << theChar;
647 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char* theString)
649 myDumpBuffer << theString;
653 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::string& theString)
655 myDumpBuffer << theString;
659 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const bool theValue)
661 myDumpBuffer << (theValue ? "True" : "False");
665 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const int theValue)
667 myDumpBuffer << theValue;
671 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const double theValue)
673 myDumpBuffer << theValue;
677 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
679 importModule("GeomAPI");
680 myDumpBuffer << "GeomAPI_Pnt(" << thePoint->x() << ", "
681 << thePoint->y() << ", " << thePoint->z() << ")";
685 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir)
687 importModule("GeomAPI");
688 myDumpBuffer << "GeomAPI_Dir(" << theDir->x() << ", "
689 << theDir->y() << ", " << theDir->z() << ")";
693 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
694 const std::shared_ptr<GeomDataAPI_Dir>& theDir)
696 myDumpBuffer << theDir->x() << ", " << theDir->y() << ", " << theDir->z();
700 static void dumpArray(std::ostringstream& theOutput, int theSize,
701 double* theValues, std::string* theTexts)
703 for (int i = 0; i < theSize; ++i) {
706 if (theTexts[i].empty())
707 theOutput << theValues[i];
709 theOutput << "\"" << theTexts[i] << "\"";
713 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
714 const std::shared_ptr<GeomDataAPI_Point>& thePoint)
716 static const int aSize = 3;
717 double aValues[aSize] = {thePoint->x(), thePoint->y(), thePoint->z()};
718 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY(), thePoint->textZ()};
719 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
723 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
724 const std::shared_ptr<GeomDataAPI_Point2D>& thePoint)
726 static const int aSize = 2;
727 double aValues[aSize] = {thePoint->x(), thePoint->y()};
728 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY()};
729 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
733 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
734 const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool)
736 myDumpBuffer << (theAttrBool->value() ? "True" : "False");
740 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
741 const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt)
743 std::string aText = theAttrInt->text();
745 myDumpBuffer << theAttrInt->value();
747 myDumpBuffer << "\"" << aText << "\"";
751 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
752 const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal)
754 std::string aText = theAttrReal->text();
756 myDumpBuffer << theAttrReal->value();
758 myDumpBuffer << "\"" << aText << "\"";
762 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
763 const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr)
765 myDumpBuffer << "\"" << theAttrStr->value() << "\"";
769 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FolderPtr& theFolder)
771 myDumpBuffer << name(theFolder);
773 // add dumped folder to a stack
774 if (!myNames[theFolder].myIsDumped &&
775 (myEntitiesStack.empty() || myEntitiesStack.top().myEntity != theFolder))
776 myEntitiesStack.push(LastDumpedEntity(theFolder, !myNames[theFolder].myIsDefault));
781 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FeaturePtr& theEntity)
783 myDumpBuffer << name(theEntity);
785 if (!myNames[theEntity].myIsDumped) {
786 bool isUserDefinedName = !myNames[theEntity].myIsDefault;
787 // store results if they have user-defined names or colors
788 std::list<ResultPtr> aResultsWithNameOrColor;
789 std::list<ResultPtr> allRes;
790 ModelAPI_Tools::allResults(theEntity, allRes);
791 for(std::list<ResultPtr>::iterator aRes = allRes.begin(); aRes != allRes.end(); aRes++) {
792 if(!myNames[*aRes].myIsDefault || !isDefaultColor(*aRes) ||
793 !isDefaultDeflection(*aRes) || !isDefaultTransparency(*aRes))
794 aResultsWithNameOrColor.push_back(*aRes);
796 // store just dumped entity to stack
797 if (myEntitiesStack.empty() || myEntitiesStack.top().myEntity != theEntity)
798 myEntitiesStack.push(
799 LastDumpedEntity(theEntity, isUserDefinedName, aResultsWithNameOrColor));
802 // remove entity from the list of not dumped items
803 myNotDumpedEntities.erase(theEntity);
807 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ResultPtr& theResult)
809 // iterate in the structure of sub-results to the parent
810 ResultPtr aCurRes = theResult;
811 FeaturePtr aFeature = ModelAPI_Feature::feature(theResult);
812 std::list<int> anIndices; // indexes of results in the parent result, starting from topmost
813 while(aCurRes.get()) {
814 ResultBodyPtr aParent = ModelAPI_Tools::bodyOwner(aCurRes);
816 anIndices.push_front(ModelAPI_Tools::bodyIndex(aCurRes));
817 } else { // index of the result in the feature
818 std::list<ResultPtr>::const_iterator aRes = aFeature->results().cbegin();
819 for(int anIndex = 0; aRes != aFeature->results().cend(); aRes++, anIndex++) {
820 if (*aRes == aCurRes) {
821 anIndices.push_front(anIndex);
829 myDumpBuffer << name(aFeature);
830 for (std::list<int>::iterator anI = anIndices.begin(); anI != anIndices.end(); anI++) {
831 if (anI == anIndices.begin()) {
833 myDumpBuffer << ".result()";
836 myDumpBuffer << ".results()[" << *anI << "]";
839 myDumpBuffer << ".subResult(" << *anI << ")";
846 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ObjectPtr& theObject)
848 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
850 myDumpBuffer << name(aFeature);
854 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
863 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const AttributePtr& theAttr)
865 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttr->owner());
867 std::string aWrapperPrefix, aWrapperSuffix;
868 // Check the attribute belongs to copied (in multi-translation or multi-rotation) feature.
869 // In this case we need to cast explicitly feature to appropriate type.
870 AttributeBooleanPtr isCopy = anOwner->boolean("Copy");
871 if (isCopy.get() && isCopy->value()) {
872 aWrapperPrefix = featureWrapper(anOwner) + "(";
873 aWrapperSuffix = ")";
874 importModule("SketchAPI");
877 myDumpBuffer << aWrapperPrefix << name(anOwner) << aWrapperSuffix
878 << "." << attributeGetter(anOwner, theAttr->id()) << "()";
882 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
883 const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr)
885 if (theRefAttr->isObject())
886 *this << theRefAttr->object();
888 *this << theRefAttr->attr();
892 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
893 const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList)
896 std::list<std::pair<ObjectPtr, AttributePtr> > aList = theRefAttrList->list();
897 bool isAdded = false;
898 std::list<std::pair<ObjectPtr, AttributePtr> >::const_iterator anIt = aList.begin();
899 for (; anIt != aList.end(); ++anIt) {
901 myDumpBuffer << ", ";
905 *this << anIt->first;
906 else if (anIt->second)
907 * this << anIt->second;
913 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
914 const std::shared_ptr<ModelAPI_AttributeReference>& theReference)
916 *this << theReference->value();
920 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
921 const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList)
923 static const int aThreshold = 2;
924 // if number of elements in the list if greater than a threshold,
925 // dump it in a separate line with specific name
926 std::string aDumped = myDumpBuffer.str();
927 if (aDumped.empty() || theRefList->size() <= aThreshold) {
929 std::list<ObjectPtr> aList = theRefList->list();
930 bool isAdded = false;
931 std::list<ObjectPtr>::const_iterator anIt = aList.begin();
932 for (; anIt != aList.end(); ++anIt) {
934 myDumpBuffer << ", ";
942 // clear buffer and store list "as is"
943 myDumpBuffer.str("");
945 // save buffer and clear it again
946 std::string aDumpedList = myDumpBuffer.str();
947 myDumpBuffer.str("");
948 // obtain name of list
949 FeaturePtr anOwner = ModelAPI_Feature::feature(theRefList->owner());
950 std::string aListName = name(anOwner) + "_objects";
951 // store all previous data
952 myDumpBuffer << aListName << " = " << aDumpedList << std::endl
953 << aDumped << aListName;
958 static int possibleSelectionsByPoint(const GeomPointPtr& thePoint,
959 const ResultPtr& theResult,
960 const GeomShapePtr& theShape,
961 const FeaturePtr& theStartFeature,
962 const FeaturePtr& theEndFeature)
964 DocumentPtr aDoc1 = theStartFeature->document();
965 DocumentPtr aDoc2 = theEndFeature->document();
967 std::list<FeaturePtr> aFeatures = aDoc1->allFeatures();
968 if (aDoc1 != aDoc2) {
969 std::list<FeaturePtr> anAdditionalFeatures = aDoc2->allFeatures();
970 aFeatures.insert(aFeatures.end(), anAdditionalFeatures.begin(), anAdditionalFeatures.end());
973 CompositeFeaturePtr aLastCompositeFeature;
975 std::list<FeaturePtr>::const_iterator aFIt = aFeatures.begin();
976 while (aFIt != aFeatures.end() && *aFIt != theStartFeature) {
977 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFIt);
979 aLastCompositeFeature = aCompFeat;
983 // collect the list of composite features, containing the last feature;
984 // these features should be excluded from searching,
985 // because the feature cannot select sub-shapes from its parent
986 std::set<FeaturePtr> aEndFeatureParents = ModelAPI_Tools::getParents(theEndFeature);
988 int aNbPossibleSelections = 0;
989 for (; aFIt != aFeatures.end() && *aFIt != theEndFeature; ++aFIt) {
990 bool isSkipFeature = false;
991 if (aLastCompositeFeature && aLastCompositeFeature->isSub(*aFIt))
992 isSkipFeature = true;
993 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFIt);
995 ResultPartPtr aPartRes =
996 std::dynamic_pointer_cast<ModelAPI_ResultPart>(aCompFeat->firstResult());
998 aLastCompositeFeature = aCompFeat;
999 if (aEndFeatureParents.find(aCompFeat) != aEndFeatureParents.end()) {
1000 // do not process the parent for the last feature,
1001 // because it cannot select objects from its parent
1002 isSkipFeature = true;
1008 std::list<ModelGeomAlgo_Shape::SubshapeOfResult> anApproproate;
1009 if (ModelGeomAlgo_Shape::findSubshapeByPoint(*aFIt, thePoint, theShape->shapeType(),
1011 std::list<ModelGeomAlgo_Shape::SubshapeOfResult>::iterator anApIt = anApproproate.begin();
1012 for (; anApIt != anApproproate.end(); ++anApIt) {
1013 ++aNbPossibleSelections;
1015 // stop if the target shape and result are found
1016 GeomShapePtr aCurShape = anApIt->mySubshape;
1018 aCurShape = anApIt->myResult->shape();
1020 if (anApIt->myResult->isSame(theResult) && aCurShape->isSame(theShape))
1025 return aNbPossibleSelections;
1028 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1029 const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect)
1031 myDumpBuffer << "model.selection(";
1033 if(!theAttrSelect->isInitialized()) {
1034 myDumpBuffer << ")";
1038 GeomShapePtr aShape = theAttrSelect->value();
1040 aShape = theAttrSelect->context()->shape();
1044 myDumpBuffer << ")";
1048 // how to dump selection: construction features are dumped by name always
1049 bool isDumpByGeom = myGeometricalSelection;
1050 FeaturePtr aSelectedFeature;
1052 ResultPtr aRes = theAttrSelect->context();
1053 FeaturePtr aFeature = theAttrSelect->contextFeature();
1054 if (aRes && !aFeature)
1055 aSelectedFeature = ModelAPI_Feature::feature(aRes->data()->owner());
1056 isDumpByGeom = aSelectedFeature && aSelectedFeature->isInHistory();
1059 if (theAttrSelect->isGeometricalSelection() && aShape->shapeType() == GeomAPI_Shape::COMPOUND
1060 && theAttrSelect->context().get() && !aShape->isEqual(theAttrSelect->context()->shape())
1061 && theAttrSelect->context()->groupName() != ModelAPI_ResultPart::group()) {
1062 GeomAPI_ShapeIterator anIt(aShape);
1063 aShape = anIt.current();
1066 myDumpBuffer << "\"" << aShape->shapeTypeStr();
1067 bool aStandardDump = true;
1069 // check the selected item is a ResultPart;
1070 // in this case it is necessary to get shape with full transformation
1071 // for correct calculation of the middle point
1072 ResultPartPtr aResPart =
1073 std::dynamic_pointer_cast<ModelAPI_ResultPart>(theAttrSelect->context());
1074 if (aResPart && aShape->shapeType() == GeomAPI_Shape::COMPOUND)
1075 aShape = aResPart->shape();
1076 GeomPointPtr aMiddlePoint = aShape->middlePoint();
1077 // calculate number of features, which could be selected by the same point
1078 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttrSelect->owner());
1079 int aNbPossibleSelections = possibleSelectionsByPoint(aMiddlePoint,
1080 theAttrSelect->context(), aShape, aSelectedFeature, anOwner);
1082 // produce the index if the number of applicable features is greater than 1
1083 std::string anIndex;
1084 if (aNbPossibleSelections > 1) {
1085 std::ostringstream anOutput;
1086 anOutput << "_" << aNbPossibleSelections;
1087 anIndex = anOutput.str();
1090 myDumpBuffer << anIndex << "\", ("
1091 << aMiddlePoint->x() << ", "
1092 << aMiddlePoint->y() << ", "
1093 << aMiddlePoint->z() << ")";
1094 aStandardDump = false;
1095 } else if (myWeakNamingSelection && aShape.get() && theAttrSelect->context().get() &&
1096 aShape != theAttrSelect->context()->shape()) { // weak naming for local selection only
1097 GeomAlgoAPI_NExplode aNExplode(theAttrSelect->context()->shape(), aShape->shapeType());
1098 int anIndex = aNExplode.index(aShape);
1099 if (anIndex != 0) { // found a week-naming index, so, export it
1100 myDumpBuffer<<"\", \""<<
1101 theAttrSelect->contextName(theAttrSelect->context())<<"\", "<<anIndex;
1102 aStandardDump = false;
1106 myDumpBuffer << "\", \"" << theAttrSelect->namingName() << "\"";
1107 myDumpBuffer << ")";
1111 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1112 const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList)
1114 static const int aThreshold = 2;
1115 // if number of elements in the list if greater than a threshold,
1116 // dump it in a separate line with specific name
1117 std::string aDumped = myDumpBuffer.str();
1119 if (aDumped.empty() || theAttrSelList->size() <= aThreshold) {
1120 myDumpBuffer << "[";
1122 GeomShapePtr aShape;
1123 std::string aShapeTypeStr;
1125 bool isAdded = false;
1127 for(int anIndex = 0; anIndex < theAttrSelList->size(); ++anIndex) {
1128 AttributeSelectionPtr anAttribute = theAttrSelList->value(anIndex);
1129 aShape = anAttribute->value();
1131 ResultPtr aContext = anAttribute->context();
1133 aShape = aContext->shape();
1141 myDumpBuffer << ", ";
1145 *this << anAttribute;
1148 myDumpBuffer << "]";
1150 // clear buffer and store list "as is"
1151 myDumpBuffer.str("");
1152 *this << theAttrSelList;
1153 // save buffer and clear it again
1154 std::string aDumpedList = myDumpBuffer.str();
1155 myDumpBuffer.str("");
1156 // obtain name of list (the feature may contain several selection lists)
1157 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttrSelList->owner());
1158 std::string aListName = name(anOwner) + "_objects";
1159 std::list<AttributePtr> aSelLists =
1160 anOwner->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
1161 if (aSelLists.size() > 1) {
1163 for (std::list<AttributePtr>::iterator aSIt = aSelLists.begin();
1164 aSIt != aSelLists.end(); ++aSIt, ++anIndex)
1165 if ((*aSIt).get() == theAttrSelList.get())
1167 std::ostringstream aSStream;
1168 aSStream << aListName << "_" << anIndex;
1169 aListName = aSStream.str();
1171 // store all previous data
1172 myDumpBuffer << aListName << " = " << aDumpedList << std::endl
1173 << aDumped << aListName;
1178 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1179 const std::shared_ptr<ModelAPI_AttributeStringArray>& theArray)
1182 for(int anIndex = 0; anIndex < theArray->size(); ++anIndex) {
1186 myDumpBuffer<<"\""<<theArray->value(anIndex)<<"\"";
1194 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
1195 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&))
1197 theDumper.myDumpBuffer << theEndl;
1199 if (!theDumper.myEntitiesStack.empty()) {
1201 // all copies have been stored into stack, pop them all
1204 // Name for composite feature is dumped when all sub-entities are dumped
1205 // (see method ModelHighAPI_Dumper::processSubs).
1206 const ModelHighAPI_Dumper::LastDumpedEntity& aLastDumped = theDumper.myEntitiesStack.top();
1207 CompositeFeaturePtr aComposite =
1208 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aLastDumped.myEntity);
1210 theDumper.dumpEntitySetName();
1211 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aLastDumped.myEntity);
1213 AttributeBooleanPtr aCopyAttr = aFeature->boolean("Copy");
1214 isCopy = aCopyAttr.get() && aCopyAttr->value();
1217 } while (isCopy && !theDumper.myEntitiesStack.empty());
1220 // store all not-dumped entities first
1221 std::set<EntityPtr> aNotDumped = theDumper.myNotDumpedEntities;
1222 std::string aBufCopy = theDumper.myDumpBuffer.str();
1223 theDumper.clear(true);
1224 std::set<EntityPtr>::const_iterator anIt = aNotDumped.begin();
1225 for (; anIt != aNotDumped.end(); ++anIt) {
1226 // if the feature is composite, dump it with all subs
1227 CompositeFeaturePtr aCompFeat =
1228 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anIt);
1230 theDumper.process(aCompFeat, true);
1232 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
1233 theDumper.dumpFeature(aFeature, true);
1234 // dump the Projection feature which produces this "Copy" entity
1235 AttributeBooleanPtr aCopyAttr = aFeature->boolean("Copy");
1236 if (aCopyAttr.get() && aCopyAttr->value())
1238 const std::set<AttributePtr>& aRefs = aFeature->data()->refsToMe();
1239 std::set<AttributePtr>::iterator aRefIt = aRefs.begin();
1240 for (; aRefIt != aRefs.end(); ++aRefIt)
1241 if ((*aRefIt)->id() == "ProjectedFeature")
1242 { // process projection only
1243 FeaturePtr anOwner = ModelAPI_Feature::feature((*aRefIt)->owner());
1244 if (anOwner && !theDumper.isDumped(EntityPtr(anOwner)))
1245 theDumper.dumpFeature(anOwner, true);
1251 // avoid multiple empty lines
1252 size_t anInd = std::string::npos;
1253 while ((anInd = aBufCopy.find("\n\n\n")) != std::string::npos)
1254 aBufCopy.erase(anInd, 1);
1255 // then store currently dumped string
1256 theDumper.myFullDump << aBufCopy;
1258 // now, store all postponed features
1259 theDumper.dumpPostponed();