1 // Copyright (C) 2014-2019 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 email : webmaster.salome@opencascade.com
20 #include "ModelHighAPI_Dumper.h"
22 #include <Config_PropManager.h>
24 #include <GeomAPI_Pnt.h>
25 #include <GeomAPI_Dir.h>
26 #include <GeomAPI_ShapeExplorer.h>
27 #include <GeomAPI_ShapeIterator.h>
28 #include <GeomAlgoAPI_NExplode.h>
30 #include <GeomDataAPI_Dir.h>
31 #include <GeomDataAPI_Point.h>
32 #include <GeomDataAPI_Point2D.h>
34 #include <ModelAPI_AttributeBoolean.h>
35 #include <ModelAPI_AttributeDouble.h>
36 #include <ModelAPI_AttributeIntArray.h>
37 #include <ModelAPI_AttributeInteger.h>
38 #include <ModelAPI_AttributeRefAttr.h>
39 #include <ModelAPI_AttributeRefAttrList.h>
40 #include <ModelAPI_AttributeReference.h>
41 #include <ModelAPI_AttributeRefList.h>
42 #include <ModelAPI_AttributeSelection.h>
43 #include <ModelAPI_AttributeSelectionList.h>
44 #include <ModelAPI_AttributeString.h>
45 #include <ModelAPI_AttributeStringArray.h>
46 #include <ModelAPI_CompositeFeature.h>
47 #include <ModelAPI_Document.h>
48 #include <ModelAPI_Entity.h>
49 #include <ModelAPI_Feature.h>
50 #include <ModelAPI_Folder.h>
51 #include <ModelAPI_Result.h>
52 #include <ModelAPI_ResultBody.h>
53 #include <ModelAPI_ResultConstruction.h>
54 #include <ModelAPI_ResultPart.h>
55 #include <ModelAPI_Session.h>
56 #include <ModelAPI_Tools.h>
58 #include <ModelGeomAlgo_Shape.h>
60 #include <PartSetPlugin_Part.h>
62 #include <OSD_OpenFile.hxx>
66 static int gCompositeStackDepth = 0;
68 ModelHighAPI_Dumper* ModelHighAPI_Dumper::mySelf = 0;
70 ModelHighAPI_Dumper::ModelHighAPI_Dumper()
71 : myGeometricalSelection(false)
76 void ModelHighAPI_Dumper::setInstance(ModelHighAPI_Dumper* theDumper)
82 ModelHighAPI_Dumper* ModelHighAPI_Dumper::getInstance()
87 void ModelHighAPI_Dumper::clear(bool bufferOnly)
90 myDumpBuffer << std::setprecision(16);
96 myFullDump << std::setprecision(16);
100 myFeatureCount.clear();
101 while (!myEntitiesStack.empty())
102 myEntitiesStack.pop();
105 myDumpPostponedInProgress = false;
109 void ModelHighAPI_Dumper::clearNotDumped()
111 myNotDumpedEntities.clear();
114 // Convert string to integer. If the string is not a number, return -1
115 static int toInt(const std::string& theString)
117 std::string::const_iterator aChar = theString.begin();
118 for (; aChar != theString.end(); ++aChar)
119 if (!std::isdigit(*aChar))
121 if (aChar != theString.end())
122 return -1; // not a number
123 return std::stoi(theString);
126 const std::string& ModelHighAPI_Dumper::name(const EntityPtr& theEntity,
127 bool theSaveNotDumped,
128 bool theUseEntityName)
130 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
131 if (aFound != myNames.end())
132 return aFound->second.myCurrentName;
134 // entity is not found, store it
135 std::string aName, aKind;
136 bool isDefaultName = false;
137 bool isSaveNotDumped = theSaveNotDumped;
138 std::ostringstream aDefaultName;
139 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
141 aName = aFeature->name();
142 aKind = aFeature->getKind();
144 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(theEntity);
146 aName = aFolder->data()->name();
147 aKind = ModelAPI_Folder::ID();
148 isSaveNotDumped = false;
152 ObjectPtr anObject = std::dynamic_pointer_cast<ModelAPI_Object>(theEntity);
154 DocumentPtr aDoc = anObject->document();
155 std::pair<int, int>& aNbFeatures = myFeatureCount[aDoc][aKind];
156 aNbFeatures.first += 1;
158 size_t anIndex = aName.find(aKind);
159 if (anIndex == 0 && aName[aKind.length()] == '_') { // name starts with "FeatureKind_"
160 std::string anIdStr = aName.substr(aKind.length() + 1);
161 int anId = toInt(anIdStr);
163 // Check number of already registered objects of such kind. Index of current object
164 // should be the same to identify feature's name as automatically generated.
165 if (aNbFeatures.first == anId && aNbFeatures.second < anId) {
166 // name is not user-defined
167 isDefaultName = true;
169 // check there are postponed features of this kind,
170 // dump their names, because the sequence of features may be changed
171 for (std::list<EntityPtr>::const_iterator aPpIt = myPostponed.begin();
172 aPpIt != myPostponed.end(); ++aPpIt) {
173 FeaturePtr aCurFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*aPpIt);
174 if (aCurFeature && aCurFeature->getKind() == aKind) {
175 myNames[*aPpIt].myIsDefault = false;
176 isDefaultName = false;
181 if (anId > aNbFeatures.second)
182 aNbFeatures.second = anId;
185 // obtain default name for the feature
186 if (theUseEntityName)
187 aDefaultName << aName;
190 NbFeaturesMap::const_iterator aFIt = myFeatureCount.begin();
191 for (; aFIt != myFeatureCount.end(); ++aFIt) {
192 std::map<std::string, std::pair<int, int> >::const_iterator aFound =
193 aFIt->second.find(aKind);
194 if (aFound != aFIt->second.end())
195 aFullIndex += aFound->second.first;
197 aDefaultName << aKind << "_" << aFullIndex;
201 myNames[theEntity] = EntityName(aDefaultName.str(), aName, isDefaultName);
203 myNotDumpedEntities.insert(theEntity);
205 // store names of results
207 saveResultNames(aFeature);
209 return myNames[theEntity].myCurrentName;
212 const std::string& ModelHighAPI_Dumper::parentName(const FeaturePtr& theEntity)
214 const std::set<AttributePtr>& aRefs = theEntity->data()->refsToMe();
215 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
216 for (; aRefIt != aRefs.end(); ++aRefIt) {
217 CompositeFeaturePtr anOwner = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(
218 ModelAPI_Feature::feature((*aRefIt)->owner()));
220 return name(anOwner);
223 static const std::string DUMMY;
227 void ModelHighAPI_Dumper::saveResultNames(const FeaturePtr& theFeature)
229 // Default name of the feature
230 bool isFeatureDefaultName = myNames[theFeature].myIsDefault;
232 // Save only names of results which is not correspond to default feature name
233 const std::list<ResultPtr>& aResults = theFeature->results();
234 std::list<ResultPtr> allRes;
235 ModelAPI_Tools::allResults(theFeature, allRes);
236 for(std::list<ResultPtr>::iterator aRes = allRes.begin(); aRes != allRes.end(); aRes++) {
237 std::pair<std::string, bool> aName = ModelAPI_Tools::getDefaultName(*aRes);
238 std::string aDefaultName = aName.first;
239 std::string aResName = (*aRes)->data()->name();
240 bool isUserDefined = !(isFeatureDefaultName && aDefaultName == aResName);
242 EntityName(aResName, (isUserDefined ? aResName : std::string()), !isUserDefined);
246 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc,
247 const std::string& theFileName)
249 // dump top level document feature
250 static const std::string aDocName("partSet");
251 myNames[theDoc] = EntityName(aDocName, std::string(), true);
252 *this << aDocName << " = model.moduleDocument()" << std::endl;
254 // dump subfeatures and store result to file
255 return process(theDoc) && exportTo(theFileName);
258 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc)
261 std::list<ObjectPtr> anObjects = theDoc->allObjects();
262 std::list<ObjectPtr>::const_iterator anObjIt = anObjects.begin();
263 // firstly, dump all parameters
264 for (; anObjIt != anObjects.end(); ++ anObjIt) {
265 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIt);
267 dumpParameter(aFeature);
269 // dump all other features
270 for (anObjIt = anObjects.begin(); anObjIt != anObjects.end(); ++anObjIt) {
271 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anObjIt);
272 if (aCompFeat) // iteratively process composite features
273 isOk = process(aCompFeat) && isOk;
274 else if (!isDumped(EntityPtr(*anObjIt))) {
276 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(*anObjIt);
280 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIt);
281 if (aFeature) // dump common feature
282 dumpFeature(aFeature);
286 // dump folders if any
291 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
294 // increase composite features stack
295 ++gCompositeStackDepth;
296 // dump composite itself
297 if (!isDumped(EntityPtr(theComposite)) || isForce)
298 dumpFeature(FeaturePtr(theComposite), isForce);
300 // sub-part is processed independently, because it provides separate document
301 if (theComposite->getKind() == PartSetPlugin_Part::ID()) {
302 // dump name of the part if it is different from default
303 if (!myEntitiesStack.empty())
306 // decrease composite features stack because we run into separate document
307 --gCompositeStackDepth;
309 ResultPartPtr aPartResult =
310 std::dynamic_pointer_cast<ModelAPI_ResultPart>(theComposite->lastResult());
313 DocumentPtr aSubDoc = aPartResult->partDoc();
316 // set name of document
317 const std::string& aPartName = myNames[theComposite].myCurrentName;
318 std::string aDocName = aPartName + "_doc";
319 myNames[aSubDoc] = EntityName(aDocName, std::string(), true);
321 // dump document in a separate line
322 *this << aDocName << " = " << aPartName << ".document()" << std::endl;
323 // dump features in the document
324 bool aRes = process(aSubDoc);
325 *this << "model.do()" << std::endl;
330 bool isOk = processSubs(theComposite);
331 // decrease composite features stack
332 --gCompositeStackDepth;
337 bool ModelHighAPI_Dumper::processSubs(
338 const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
342 // dump all sub-features;
343 bool isSubDumped = false;
344 int aNbSubs = theComposite->numberOfSubs();
345 for (int anIndex = 0; anIndex < aNbSubs; ++anIndex) {
346 FeaturePtr aFeature = theComposite->subFeature(anIndex);
347 if (isDumped(EntityPtr(aFeature)))
351 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
352 if (aCompFeat) // iteratively process composite features
353 isOk = process(aCompFeat) && isOk;
355 dumpFeature(aFeature, true);
358 bool isDumpSetName = !myEntitiesStack.empty() &&
359 myEntitiesStack.top().myEntity == EntityPtr(theComposite);
360 bool isForceModelDo = isSubDumped && isDumpSetName &&
361 (myEntitiesStack.top().myUserName || !myEntitiesStack.top().myResults.empty());
362 // It is necessary for the sketch to create its result when complete (command "model.do()").
363 // This option is set by flat theDumpModelDo.
364 // However, nested sketches are rebuilt by parent feature, so, they do not need
365 // explicit call of "model.do()". This will be controlled by the depth of the stack.
366 if (isForceModelDo || (theDumpModelDo && gCompositeStackDepth <= 1))
367 *this << "model.do()" << std::endl;
369 // dump "setName" for composite feature
375 void ModelHighAPI_Dumper::postpone(const EntityPtr& theEntity)
378 name(theEntity, false);
379 myPostponed.push_back(theEntity);
382 void ModelHighAPI_Dumper::dumpPostponed(bool theDumpFolders)
384 if (myDumpPostponedInProgress)
387 myDumpPostponedInProgress = true;
388 // make a copy of postponed entities, because the list will be updated
389 // if some features are not able to be dumped
390 std::list<EntityPtr> aPostponedCopy = myPostponed;
393 // iterate over postponed entities and try to dump them
394 std::list<EntityPtr>::const_iterator anIt = aPostponedCopy.begin();
395 for (; anIt != aPostponedCopy.end(); ++anIt) {
396 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(*anIt);
401 myPostponed.push_back(*anIt);
404 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
406 dumpFeature(aFeature, true);
409 myDumpPostponedInProgress = false;
412 void ModelHighAPI_Dumper::dumpSubFeatureNameAndColor(const std::string theSubFeatureGet,
413 const FeaturePtr& theSubFeature)
415 name(theSubFeature, false);
416 myNames[theSubFeature] = EntityName(theSubFeatureGet, theSubFeature->name(), false);
418 // store results if they have user-defined names or colors
419 std::list<ResultPtr> aResultsWithNameOrColor;
420 const std::list<ResultPtr>& aResults = theSubFeature->results();
421 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
422 for (; aResIt != aResults.end(); ++aResIt) {
423 std::string aResName = (*aResIt)->data()->name();
424 myNames[*aResIt] = EntityName(aResName, aResName, false);
425 aResultsWithNameOrColor.push_back(*aResIt);
428 // store just dumped entity to stack
429 myEntitiesStack.push(LastDumpedEntity(theSubFeature, true, aResultsWithNameOrColor));
434 bool ModelHighAPI_Dumper::exportTo(const std::string& theFileName)
437 OSD_OpenStream(aFile, theFileName.c_str(), std::ofstream::out);
438 if (!aFile.is_open())
441 // standard header (encoding + imported modules)
442 aFile << "# -*- coding: utf-8 -*-" << std::endl << std::endl;
443 for (ModulesSet::const_iterator aModIt = myModules.begin();
444 aModIt != myModules.end(); ++aModIt) {
445 aFile << "from " << *aModIt << " import *" << std::endl;
447 if (!myModules.empty())
450 aFile << "from salome.shaper import model" << std::endl << std::endl;
451 aFile << "model.begin()" << std::endl;
453 // dump collected data
454 aFile << myFullDump.str();
455 aFile << myDumpBuffer.str();
458 aFile << "model.end()" << std::endl;
466 void ModelHighAPI_Dumper::importModule(const std::string& theModuleName)
468 myModules.insert(theModuleName);
471 void ModelHighAPI_Dumper::dumpEntitySetName()
473 const LastDumpedEntity& aLastDumped = myEntitiesStack.top();
474 bool isBufferEmpty = myDumpBuffer.str().empty();
476 // dump "setName" for the entity
477 if (aLastDumped.myUserName) {
478 EntityName& anEntityNames = myNames[aLastDumped.myEntity];
479 if (!anEntityNames.myIsDefault)
480 myDumpBuffer << anEntityNames.myCurrentName << ".setName(\""
481 << anEntityNames.myUserName << "\")" << std::endl;
482 // don't dump "setName" for the entity twice
483 anEntityNames.myUserName.clear();
484 anEntityNames.myIsDefault = true;
486 // dump "setName" for results
487 std::list<ResultPtr>::const_iterator aResIt = aLastDumped.myResults.begin();
488 std::list<ResultPtr>::const_iterator aResEnd = aLastDumped.myResults.end();
489 for (; aResIt != aResEnd; ++aResIt) {
491 EntityName& anEntityNames = myNames[*aResIt];
492 if (!anEntityNames.myIsDefault) {
494 myDumpBuffer << ".setName(\"" << anEntityNames.myUserName << "\")" << std::endl;
495 // don't dump "setName" for the entity twice
496 anEntityNames.myUserName.clear();
497 anEntityNames.myIsDefault = true;
500 if (!isDefaultColor(*aResIt)) {
501 AttributeIntArrayPtr aColor = (*aResIt)->data()->intArray(ModelAPI_Result::COLOR_ID());
502 if (aColor && aColor->isInitialized()) {
504 myDumpBuffer << ".setColor(" << aColor->value(0) << ", " << aColor->value(1)
505 << ", " << aColor->value(2) << ")" << std::endl;
508 // set result deflection
509 if (!isDefaultDeflection(*aResIt)) {
510 AttributeDoublePtr aDeflectionAttr =
511 (*aResIt)->data()->real(ModelAPI_Result::DEFLECTION_ID());
512 if(aDeflectionAttr.get() && aDeflectionAttr->isInitialized()) {
514 myDumpBuffer << ".setDeflection(" << aDeflectionAttr->value() << ")" << std::endl;
517 // set result transparency
518 if (!isDefaultTransparency(*aResIt)) {
519 AttributeDoublePtr aTransparencyAttr =
520 (*aResIt)->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
521 if(aTransparencyAttr.get() && aTransparencyAttr->isInitialized()) {
523 myDumpBuffer << ".setTransparency(" << aTransparencyAttr->value() << ")" << std::endl;
528 myNames[aLastDumped.myEntity].myIsDumped = true;
529 myEntitiesStack.pop();
531 // clean buffer if it was clear before
533 myFullDump << myDumpBuffer.str();
534 myDumpBuffer.str("");
538 bool ModelHighAPI_Dumper::isDumped(const EntityPtr& theEntity) const
540 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
541 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
542 return (aFound != myNames.end() && aFound->second.myIsDumped) ||
543 myFeaturesToSkip.find(aFeature) != myFeaturesToSkip.end();
546 bool ModelHighAPI_Dumper::isDumped(const AttributeRefAttrPtr& theRefAttr) const
549 if (theRefAttr->isObject())
550 aFeature = ModelAPI_Feature::feature(theRefAttr->object());
552 aFeature = ModelAPI_Feature::feature(theRefAttr->attr()->owner());
553 return aFeature && isDumped(EntityPtr(aFeature));
556 bool ModelHighAPI_Dumper::isDumped(const AttributeRefListPtr& theRefList) const
558 std::list<ObjectPtr> aRefs = theRefList->list();
559 std::list<ObjectPtr>::iterator anIt = aRefs.begin();
560 for (; anIt != aRefs.end(); ++anIt) {
561 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
562 if (aFeature && !isDumped(EntityPtr(aFeature)))
568 static bool isSketchSub(const FeaturePtr& theFeature)
570 static const std::string SKETCH("Sketch");
571 CompositeFeaturePtr anOwner = ModelAPI_Tools::compositeOwner(theFeature);
572 return anOwner && anOwner->getKind() == SKETCH;
575 bool ModelHighAPI_Dumper::isDefaultColor(const ResultPtr& theResult) const
577 AttributeIntArrayPtr aColor = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
578 if (!aColor || !aColor->isInitialized())
581 // check the result belongs to sketch entity, do not dump color in this way
582 ResultConstructionPtr aResConstr =
583 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theResult);
585 FeaturePtr aFeature = ModelAPI_Feature::feature(theResult->data()->owner());
586 if (isSketchSub(aFeature))
590 std::string aSection, aName, aDefault;
591 theResult->colorConfigInfo(aSection, aName, aDefault);
593 // dump current color
594 std::ostringstream aColorInfo;
595 aColorInfo << aColor->value(0) << "," << aColor->value(1) << "," << aColor->value(2);
597 return aDefault == aColorInfo.str();
600 bool ModelHighAPI_Dumper::isDefaultDeflection(const ResultPtr& theResult) const
602 AttributeDoublePtr aDeflectionAttr = theResult->data()->real(ModelAPI_Result::DEFLECTION_ID());
603 if(!aDeflectionAttr || !aDeflectionAttr->isInitialized()) {
607 double aCurrent = aDeflectionAttr->value();
608 double aDefault = -1;
610 bool isConstruction = false;
611 std::string aResultGroup = theResult->groupName();
612 if (aResultGroup == ModelAPI_ResultConstruction::group())
613 isConstruction = true;
614 else if (aResultGroup == ModelAPI_ResultBody::group()) {
615 GeomShapePtr aGeomShape = theResult->shape();
616 if (aGeomShape.get()) {
617 // if the shape could not be exploded on faces, it contains only wires, edges, and vertices
618 // correction of deviation for them should not influence to the application performance
619 GeomAPI_ShapeExplorer anExp(aGeomShape, GeomAPI_Shape::FACE);
620 isConstruction = !anExp.more();
624 aDefault = Config_PropManager::real("Visualization", "construction_deflection");
626 aDefault = Config_PropManager::real("Visualization", "body_deflection");
628 return fabs(aCurrent - aDefault) < 1.e-12;
631 bool ModelHighAPI_Dumper::isDefaultTransparency(const ResultPtr& theResult) const
633 AttributeDoublePtr anAttribute = theResult->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
634 if(!anAttribute || !anAttribute->isInitialized()) {
637 return fabs(anAttribute->value()) < 1.e-12;
640 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char theChar)
642 myDumpBuffer << theChar;
646 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char* theString)
648 myDumpBuffer << theString;
652 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::string& theString)
654 myDumpBuffer << theString;
658 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const bool theValue)
660 myDumpBuffer << (theValue ? "True" : "False");
664 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const int theValue)
666 myDumpBuffer << theValue;
670 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const double theValue)
672 myDumpBuffer << theValue;
676 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
678 importModule("GeomAPI");
679 myDumpBuffer << "GeomAPI_Pnt(" << thePoint->x() << ", "
680 << thePoint->y() << ", " << thePoint->z() << ")";
684 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir)
686 importModule("GeomAPI");
687 myDumpBuffer << "GeomAPI_Dir(" << theDir->x() << ", "
688 << theDir->y() << ", " << theDir->z() << ")";
692 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
693 const std::shared_ptr<GeomDataAPI_Dir>& theDir)
695 myDumpBuffer << theDir->x() << ", " << theDir->y() << ", " << theDir->z();
699 static void dumpArray(std::ostringstream& theOutput, int theSize,
700 double* theValues, std::string* theTexts)
702 for (int i = 0; i < theSize; ++i) {
705 if (theTexts[i].empty())
706 theOutput << theValues[i];
708 theOutput << "\"" << theTexts[i] << "\"";
712 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
713 const std::shared_ptr<GeomDataAPI_Point>& thePoint)
715 static const int aSize = 3;
716 double aValues[aSize] = {thePoint->x(), thePoint->y(), thePoint->z()};
717 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY(), thePoint->textZ()};
718 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
722 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
723 const std::shared_ptr<GeomDataAPI_Point2D>& thePoint)
725 static const int aSize = 2;
726 double aValues[aSize] = {thePoint->x(), thePoint->y()};
727 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY()};
728 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
732 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
733 const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool)
735 myDumpBuffer << (theAttrBool->value() ? "True" : "False");
739 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
740 const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt)
742 std::string aText = theAttrInt->text();
744 myDumpBuffer << theAttrInt->value();
746 myDumpBuffer << "\"" << aText << "\"";
750 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
751 const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal)
753 std::string aText = theAttrReal->text();
755 myDumpBuffer << theAttrReal->value();
757 myDumpBuffer << "\"" << aText << "\"";
761 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
762 const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr)
764 myDumpBuffer << "\"" << theAttrStr->value() << "\"";
768 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FolderPtr& theFolder)
770 myDumpBuffer << name(theFolder);
772 // add dumped folder to a stack
773 if (!myNames[theFolder].myIsDumped &&
774 (myEntitiesStack.empty() || myEntitiesStack.top().myEntity != theFolder))
775 myEntitiesStack.push(LastDumpedEntity(theFolder, !myNames[theFolder].myIsDefault));
780 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FeaturePtr& theEntity)
782 myDumpBuffer << name(theEntity);
784 if (!myNames[theEntity].myIsDumped) {
785 bool isUserDefinedName = !myNames[theEntity].myIsDefault;
786 // store results if they have user-defined names or colors
787 std::list<ResultPtr> aResultsWithNameOrColor;
788 std::list<ResultPtr> allRes;
789 ModelAPI_Tools::allResults(theEntity, allRes);
790 for(std::list<ResultPtr>::iterator aRes = allRes.begin(); aRes != allRes.end(); aRes++) {
791 if(!myNames[*aRes].myIsDefault || !isDefaultColor(*aRes) ||
792 !isDefaultDeflection(*aRes) || !isDefaultTransparency(*aRes))
793 aResultsWithNameOrColor.push_back(*aRes);
795 // store just dumped entity to stack
796 if (myEntitiesStack.empty() || myEntitiesStack.top().myEntity != theEntity)
797 myEntitiesStack.push(
798 LastDumpedEntity(theEntity, isUserDefinedName, aResultsWithNameOrColor));
801 // remove entity from the list of not dumped items
802 myNotDumpedEntities.erase(theEntity);
806 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ResultPtr& theResult)
808 // iterate in the structure of sub-results to the parent
809 ResultPtr aCurRes = theResult;
810 FeaturePtr aFeature = ModelAPI_Feature::feature(theResult);
811 std::list<int> anIndices; // indexes of results in the parent result, starting from topmost
812 while(aCurRes.get()) {
813 ResultBodyPtr aParent = ModelAPI_Tools::bodyOwner(aCurRes);
815 anIndices.push_front(ModelAPI_Tools::bodyIndex(aCurRes));
816 } else { // index of the result in the feature
817 std::list<ResultPtr>::const_iterator aRes = aFeature->results().cbegin();
818 for(int anIndex = 0; aRes != aFeature->results().cend(); aRes++, anIndex++) {
819 if (*aRes == aCurRes) {
820 anIndices.push_front(anIndex);
828 myDumpBuffer << name(aFeature);
829 for (std::list<int>::iterator anI = anIndices.begin(); anI != anIndices.end(); anI++) {
830 if (anI == anIndices.begin()) {
832 myDumpBuffer << ".result()";
835 myDumpBuffer << ".results()[" << *anI << "]";
838 myDumpBuffer << ".subResult(" << *anI << ")";
845 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ObjectPtr& theObject)
847 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
849 myDumpBuffer << name(aFeature);
853 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
862 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const AttributePtr& theAttr)
864 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttr->owner());
866 std::string aWrapperPrefix, aWrapperSuffix;
867 // Check the attribute belongs to copied (in multi-translation or multi-rotation) feature.
868 // In this case we need to cast explicitly feature to appropriate type.
869 AttributeBooleanPtr isCopy = anOwner->boolean("Copy");
870 if (isCopy.get() && isCopy->value()) {
871 aWrapperPrefix = featureWrapper(anOwner) + "(";
872 aWrapperSuffix = ")";
873 importModule("SketchAPI");
876 myDumpBuffer << aWrapperPrefix << name(anOwner) << aWrapperSuffix
877 << "." << attributeGetter(anOwner, theAttr->id()) << "()";
881 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
882 const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr)
884 if (theRefAttr->isObject())
885 *this << theRefAttr->object();
887 *this << theRefAttr->attr();
891 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
892 const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList)
895 std::list<std::pair<ObjectPtr, AttributePtr> > aList = theRefAttrList->list();
896 bool isAdded = false;
897 std::list<std::pair<ObjectPtr, AttributePtr> >::const_iterator anIt = aList.begin();
898 for (; anIt != aList.end(); ++anIt) {
900 myDumpBuffer << ", ";
904 *this << anIt->first;
905 else if (anIt->second)
906 * this << anIt->second;
912 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
913 const std::shared_ptr<ModelAPI_AttributeReference>& theReference)
915 *this << theReference->value();
919 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
920 const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList)
922 static const int aThreshold = 2;
923 // if number of elements in the list if greater than a threshold,
924 // dump it in a separate line with specific name
925 std::string aDumped = myDumpBuffer.str();
926 if (aDumped.empty() || theRefList->size() <= aThreshold) {
928 std::list<ObjectPtr> aList = theRefList->list();
929 bool isAdded = false;
930 std::list<ObjectPtr>::const_iterator anIt = aList.begin();
931 for (; anIt != aList.end(); ++anIt) {
933 myDumpBuffer << ", ";
941 // clear buffer and store list "as is"
942 myDumpBuffer.str("");
944 // save buffer and clear it again
945 std::string aDumpedList = myDumpBuffer.str();
946 myDumpBuffer.str("");
947 // obtain name of list
948 FeaturePtr anOwner = ModelAPI_Feature::feature(theRefList->owner());
949 std::string aListName = name(anOwner) + "_objects";
950 // store all previous data
951 myDumpBuffer << aListName << " = " << aDumpedList << std::endl
952 << aDumped << aListName;
957 static int possibleSelectionsByPoint(const GeomPointPtr& thePoint,
958 const ResultPtr& theResult,
959 const GeomShapePtr& theShape,
960 const FeaturePtr& theStartFeature,
961 const FeaturePtr& theEndFeature)
963 DocumentPtr aDoc1 = theStartFeature->document();
964 DocumentPtr aDoc2 = theEndFeature->document();
966 std::list<FeaturePtr> aFeatures = aDoc1->allFeatures();
967 if (aDoc1 != aDoc2) {
968 std::list<FeaturePtr> anAdditionalFeatures = aDoc2->allFeatures();
969 aFeatures.insert(aFeatures.end(), anAdditionalFeatures.begin(), anAdditionalFeatures.end());
972 CompositeFeaturePtr aLastCompositeFeature;
974 std::list<FeaturePtr>::const_iterator aFIt = aFeatures.begin();
975 while (aFIt != aFeatures.end() && *aFIt != theStartFeature) {
976 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFIt);
978 aLastCompositeFeature = aCompFeat;
982 // collect the list of composite features, containing the last feature;
983 // these features should be excluded from searching,
984 // because the feature cannot select sub-shapes from its parent
985 std::set<FeaturePtr> aEndFeatureParents = ModelAPI_Tools::getParents(theEndFeature);
987 int aNbPossibleSelections = 0;
988 for (; aFIt != aFeatures.end() && *aFIt != theEndFeature; ++aFIt) {
989 bool isSkipFeature = false;
990 if (aLastCompositeFeature && aLastCompositeFeature->isSub(*aFIt))
991 isSkipFeature = true;
992 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFIt);
994 ResultPartPtr aPartRes =
995 std::dynamic_pointer_cast<ModelAPI_ResultPart>(aCompFeat->firstResult());
997 aLastCompositeFeature = aCompFeat;
998 if (aEndFeatureParents.find(aCompFeat) != aEndFeatureParents.end()) {
999 // do not process the parent for the last feature,
1000 // because it cannot select objects from its parent
1001 isSkipFeature = true;
1007 std::list<ModelGeomAlgo_Shape::SubshapeOfResult> anApproproate;
1008 if (ModelGeomAlgo_Shape::findSubshapeByPoint(*aFIt, thePoint, theShape->shapeType(),
1010 std::list<ModelGeomAlgo_Shape::SubshapeOfResult>::iterator anApIt = anApproproate.begin();
1011 for (; anApIt != anApproproate.end(); ++anApIt) {
1012 ++aNbPossibleSelections;
1014 // stop if the target shape and result are found
1015 GeomShapePtr aCurShape = anApIt->mySubshape;
1017 aCurShape = anApIt->myResult->shape();
1019 if (anApIt->myResult->isSame(theResult) && aCurShape->isSame(theShape))
1024 return aNbPossibleSelections;
1027 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1028 const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect)
1030 myDumpBuffer << "model.selection(";
1032 if(!theAttrSelect->isInitialized()) {
1033 myDumpBuffer << ")";
1037 GeomShapePtr aShape = theAttrSelect->value();
1039 aShape = theAttrSelect->context()->shape();
1043 myDumpBuffer << ")";
1047 // how to dump selection: construction features are dumped by name always
1048 bool isDumpByGeom = myGeometricalSelection;
1049 FeaturePtr aSelectedFeature;
1051 ResultPtr aRes = theAttrSelect->context();
1052 FeaturePtr aFeature = theAttrSelect->contextFeature();
1053 if (aRes && !aFeature)
1054 aSelectedFeature = ModelAPI_Feature::feature(aRes->data()->owner());
1055 isDumpByGeom = aSelectedFeature && aSelectedFeature->isInHistory();
1058 if (theAttrSelect->isGeometricalSelection() && aShape->shapeType() == GeomAPI_Shape::COMPOUND
1059 && theAttrSelect->context().get() && !aShape->isEqual(theAttrSelect->context()->shape())
1060 && theAttrSelect->context()->groupName() != ModelAPI_ResultPart::group()) {
1061 GeomAPI_ShapeIterator anIt(aShape);
1062 aShape = anIt.current();
1065 myDumpBuffer << "\"" << aShape->shapeTypeStr();
1066 bool aStandardDump = true;
1068 // check the selected item is a ResultPart;
1069 // in this case it is necessary to get shape with full transformation
1070 // for correct calculation of the middle point
1071 ResultPartPtr aResPart =
1072 std::dynamic_pointer_cast<ModelAPI_ResultPart>(theAttrSelect->context());
1073 if (aResPart && aShape->shapeType() == GeomAPI_Shape::COMPOUND)
1074 aShape = aResPart->shape();
1075 GeomPointPtr aMiddlePoint = aShape->middlePoint();
1076 // calculate number of features, which could be selected by the same point
1077 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttrSelect->owner());
1078 int aNbPossibleSelections = possibleSelectionsByPoint(aMiddlePoint,
1079 theAttrSelect->context(), aShape, aSelectedFeature, anOwner);
1081 // produce the index if the number of applicable features is greater than 1
1082 std::string anIndex;
1083 if (aNbPossibleSelections > 1) {
1084 std::ostringstream anOutput;
1085 anOutput << "_" << aNbPossibleSelections;
1086 anIndex = anOutput.str();
1089 myDumpBuffer << anIndex << "\", ("
1090 << aMiddlePoint->x() << ", "
1091 << aMiddlePoint->y() << ", "
1092 << aMiddlePoint->z() << ")";
1093 aStandardDump = false;
1094 } else if (myWeakNamingSelection && aShape.get() && theAttrSelect->context().get() &&
1095 aShape != theAttrSelect->context()->shape()) { // weak naming for local selection only
1096 GeomAlgoAPI_NExplode aNExplode(theAttrSelect->context()->shape(), aShape->shapeType());
1097 int anIndex = aNExplode.index(aShape);
1098 if (anIndex != 0) { // found a week-naming index, so, export it
1099 myDumpBuffer<<"\", \""<<
1100 theAttrSelect->contextName(theAttrSelect->context())<<"\", "<<anIndex;
1101 aStandardDump = false;
1105 myDumpBuffer << "\", \"" << theAttrSelect->namingName() << "\"";
1106 myDumpBuffer << ")";
1110 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1111 const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList)
1113 static const int aThreshold = 2;
1114 // if number of elements in the list if greater than a threshold,
1115 // dump it in a separate line with specific name
1116 std::string aDumped = myDumpBuffer.str();
1118 if (aDumped.empty() || theAttrSelList->size() <= aThreshold) {
1119 myDumpBuffer << "[";
1121 GeomShapePtr aShape;
1122 std::string aShapeTypeStr;
1124 bool isAdded = false;
1126 for(int anIndex = 0; anIndex < theAttrSelList->size(); ++anIndex) {
1127 AttributeSelectionPtr anAttribute = theAttrSelList->value(anIndex);
1128 aShape = anAttribute->value();
1130 ResultPtr aContext = anAttribute->context();
1132 aShape = aContext->shape();
1140 myDumpBuffer << ", ";
1144 *this << anAttribute;
1147 myDumpBuffer << "]";
1149 // clear buffer and store list "as is"
1150 myDumpBuffer.str("");
1151 *this << theAttrSelList;
1152 // save buffer and clear it again
1153 std::string aDumpedList = myDumpBuffer.str();
1154 myDumpBuffer.str("");
1155 // obtain name of list (the feature may contain several selection lists)
1156 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttrSelList->owner());
1157 std::string aListName = name(anOwner) + "_objects";
1158 std::list<AttributePtr> aSelLists =
1159 anOwner->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
1160 if (aSelLists.size() > 1) {
1162 for (std::list<AttributePtr>::iterator aSIt = aSelLists.begin();
1163 aSIt != aSelLists.end(); ++aSIt, ++anIndex)
1164 if ((*aSIt).get() == theAttrSelList.get())
1166 std::ostringstream aSStream;
1167 aSStream << aListName << "_" << anIndex;
1168 aListName = aSStream.str();
1170 // store all previous data
1171 myDumpBuffer << aListName << " = " << aDumpedList << std::endl
1172 << aDumped << aListName;
1177 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1178 const std::shared_ptr<ModelAPI_AttributeStringArray>& theArray)
1181 for(int anIndex = 0; anIndex < theArray->size(); ++anIndex) {
1185 myDumpBuffer<<"\""<<theArray->value(anIndex)<<"\"";
1193 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
1194 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&))
1196 theDumper.myDumpBuffer << theEndl;
1198 if (!theDumper.myEntitiesStack.empty()) {
1200 // all copies have been stored into stack, pop them all
1203 // Name for composite feature is dumped when all sub-entities are dumped
1204 // (see method ModelHighAPI_Dumper::processSubs).
1205 const ModelHighAPI_Dumper::LastDumpedEntity& aLastDumped = theDumper.myEntitiesStack.top();
1206 CompositeFeaturePtr aComposite =
1207 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aLastDumped.myEntity);
1209 theDumper.dumpEntitySetName();
1210 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aLastDumped.myEntity);
1212 AttributeBooleanPtr aCopyAttr = aFeature->boolean("Copy");
1213 isCopy = aCopyAttr.get() && aCopyAttr->value();
1216 } while (isCopy && !theDumper.myEntitiesStack.empty());
1219 // store all not-dumped entities first
1220 std::set<EntityPtr> aNotDumped = theDumper.myNotDumpedEntities;
1221 std::string aBufCopy = theDumper.myDumpBuffer.str();
1222 theDumper.clear(true);
1223 std::set<EntityPtr>::const_iterator anIt = aNotDumped.begin();
1224 for (; anIt != aNotDumped.end(); ++anIt) {
1225 // if the feature is composite, dump it with all subs
1226 CompositeFeaturePtr aCompFeat =
1227 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anIt);
1229 theDumper.process(aCompFeat, true);
1231 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
1232 theDumper.dumpFeature(aFeature, true);
1233 // dump the Projection feature which produces this "Copy" entity
1234 AttributeBooleanPtr aCopyAttr = aFeature->boolean("Copy");
1235 if (aCopyAttr.get() && aCopyAttr->value())
1237 const std::set<AttributePtr>& aRefs = aFeature->data()->refsToMe();
1238 std::set<AttributePtr>::iterator aRefIt = aRefs.begin();
1239 for (; aRefIt != aRefs.end(); ++aRefIt)
1240 if ((*aRefIt)->id() == "ProjectedFeature")
1241 { // process projection only
1242 FeaturePtr anOwner = ModelAPI_Feature::feature((*aRefIt)->owner());
1243 if (anOwner && !theDumper.isDumped(EntityPtr(anOwner)))
1244 theDumper.dumpFeature(anOwner, true);
1250 // avoid multiple empty lines
1251 size_t anInd = std::string::npos;
1252 while ((anInd = aBufCopy.find("\n\n\n")) != std::string::npos)
1253 aBufCopy.erase(anInd, 1);
1254 // then store currently dumped string
1255 theDumper.myFullDump << aBufCopy;
1257 // now, store all postponed features
1258 theDumper.dumpPostponed();