1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "ModelHighAPI_Dumper.h"
23 #include <Config_PropManager.h>
25 #include <GeomAPI_Pnt.h>
26 #include <GeomAPI_Dir.h>
27 #include <GeomAPI_ShapeExplorer.h>
29 #include <GeomDataAPI_Dir.h>
30 #include <GeomDataAPI_Point.h>
31 #include <GeomDataAPI_Point2D.h>
33 #include <ModelAPI_AttributeBoolean.h>
34 #include <ModelAPI_AttributeDouble.h>
35 #include <ModelAPI_AttributeIntArray.h>
36 #include <ModelAPI_AttributeInteger.h>
37 #include <ModelAPI_AttributeRefAttr.h>
38 #include <ModelAPI_AttributeRefAttrList.h>
39 #include <ModelAPI_AttributeReference.h>
40 #include <ModelAPI_AttributeRefList.h>
41 #include <ModelAPI_AttributeSelection.h>
42 #include <ModelAPI_AttributeSelectionList.h>
43 #include <ModelAPI_AttributeString.h>
44 #include <ModelAPI_AttributeStringArray.h>
45 #include <ModelAPI_CompositeFeature.h>
46 #include <ModelAPI_Document.h>
47 #include <ModelAPI_Entity.h>
48 #include <ModelAPI_Feature.h>
49 #include <ModelAPI_Folder.h>
50 #include <ModelAPI_Result.h>
51 #include <ModelAPI_ResultBody.h>
52 #include <ModelAPI_ResultConstruction.h>
53 #include <ModelAPI_ResultPart.h>
54 #include <ModelAPI_Session.h>
55 #include <ModelAPI_Tools.h>
57 #include <ModelGeomAlgo_Shape.h>
59 #include <PartSetPlugin_Part.h>
61 #include <OSD_OpenFile.hxx>
65 static int gCompositeStackDepth = 0;
67 ModelHighAPI_Dumper* ModelHighAPI_Dumper::mySelf = 0;
69 ModelHighAPI_Dumper::ModelHighAPI_Dumper()
70 : myGeometricalSelection(false)
75 void ModelHighAPI_Dumper::setInstance(ModelHighAPI_Dumper* theDumper)
81 ModelHighAPI_Dumper* ModelHighAPI_Dumper::getInstance()
86 void ModelHighAPI_Dumper::clear(bool bufferOnly)
89 myDumpBuffer << std::setprecision(16);
95 myFullDump << std::setprecision(16);
99 myFeatureCount.clear();
100 while (!myEntitiesStack.empty())
101 myEntitiesStack.pop();
104 myDumpPostponedInProgress = false;
108 void ModelHighAPI_Dumper::clearNotDumped()
110 myNotDumpedEntities.clear();
113 // Convert string to integer. If the string is not a number, return -1
114 static int toInt(const std::string& theString)
116 std::string::const_iterator aChar = theString.begin();
117 for (; aChar != theString.end(); ++aChar)
118 if (!std::isdigit(*aChar))
120 if (aChar != theString.end())
121 return -1; // not a number
122 return std::stoi(theString);
125 const std::string& ModelHighAPI_Dumper::name(const EntityPtr& theEntity,
126 bool theSaveNotDumped,
127 bool theUseEntityName)
129 EntityNameMap::const_iterator aFound = myNames.find(theEntity);
130 if (aFound != myNames.end())
131 return aFound->second.myCurrentName;
133 // entity is not found, store it
134 std::string aName, aKind;
135 bool isDefaultName = false;
136 bool isSaveNotDumped = theSaveNotDumped;
137 std::ostringstream aDefaultName;
138 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theEntity);
140 aName = aFeature->name();
141 aKind = aFeature->getKind();
143 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(theEntity);
145 aName = aFolder->data()->name();
146 aKind = ModelAPI_Folder::ID();
147 isSaveNotDumped = false;
151 ObjectPtr anObject = std::dynamic_pointer_cast<ModelAPI_Object>(theEntity);
153 DocumentPtr aDoc = anObject->document();
154 std::pair<int, int>& aNbFeatures = myFeatureCount[aDoc][aKind];
155 aNbFeatures.first += 1;
157 size_t anIndex = aName.find(aKind);
158 if (anIndex == 0 && aName[aKind.length()] == '_') { // name starts with "FeatureKind_"
159 std::string anIdStr = aName.substr(aKind.length() + 1);
160 int anId = toInt(anIdStr);
162 // Check number of already registered objects of such kind. Index of current object
163 // should be the same to identify feature's name as automatically generated.
164 if (aNbFeatures.first == anId && aNbFeatures.second < anId) {
165 // name is not user-defined
166 isDefaultName = true;
169 if (anId > aNbFeatures.second)
170 aNbFeatures.second = anId;
173 // obtain default name for the feature
174 if (theUseEntityName)
175 aDefaultName << aName;
178 NbFeaturesMap::const_iterator aFIt = myFeatureCount.begin();
179 for (; aFIt != myFeatureCount.end(); ++aFIt) {
180 std::map<std::string, std::pair<int, int> >::const_iterator aFound =
181 aFIt->second.find(aKind);
182 if (aFound != aFIt->second.end())
183 aFullIndex += aFound->second.first;
185 aDefaultName << aKind << "_" << aFullIndex;
189 myNames[theEntity] = EntityName(aDefaultName.str(), aName, isDefaultName);
191 myNotDumpedEntities.insert(theEntity);
193 // store names of results
195 saveResultNames(aFeature);
197 return myNames[theEntity].myCurrentName;
200 const std::string& ModelHighAPI_Dumper::parentName(const FeaturePtr& theEntity)
202 const std::set<AttributePtr>& aRefs = theEntity->data()->refsToMe();
203 std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
204 for (; aRefIt != aRefs.end(); ++aRefIt) {
205 CompositeFeaturePtr anOwner = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(
206 ModelAPI_Feature::feature((*aRefIt)->owner()));
208 return name(anOwner);
211 static const std::string DUMMY;
215 void ModelHighAPI_Dumper::saveResultNames(const FeaturePtr& theFeature)
217 // Default name of the feature
218 bool isFeatureDefaultName = myNames[theFeature].myIsDefault;
220 // Save only names of results which is not correspond to default feature name
221 const std::list<ResultPtr>& aResults = theFeature->results();
222 std::list<ResultPtr> allRes;
223 ModelAPI_Tools::allResults(theFeature, allRes);
224 for(std::list<ResultPtr>::iterator aRes = allRes.begin(); aRes != allRes.end(); aRes++) {
225 std::pair<std::string, bool> aName = ModelAPI_Tools::getDefaultName(*aRes);
226 std::string aDefaultName = aName.first;
227 std::string aResName = (*aRes)->data()->name();
228 bool isUserDefined = !(isFeatureDefaultName && aDefaultName == aResName);
230 EntityName(aResName, (isUserDefined ? aResName : std::string()), !isUserDefined);
234 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc,
235 const std::string& theFileName)
237 // dump top level document feature
238 static const std::string aDocName("partSet");
239 myNames[theDoc] = EntityName(aDocName, std::string(), true);
240 *this << aDocName << " = model.moduleDocument()" << std::endl;
242 // dump subfeatures and store result to file
243 return process(theDoc) && exportTo(theFileName);
246 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_Document>& theDoc)
249 std::list<ObjectPtr> anObjects = theDoc->allObjects();
250 std::list<ObjectPtr>::const_iterator anObjIt = anObjects.begin();
251 // firstly, dump all parameters
252 for (; anObjIt != anObjects.end(); ++ anObjIt) {
253 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIt);
255 dumpParameter(aFeature);
257 // dump all other features
258 for (anObjIt = anObjects.begin(); anObjIt != anObjects.end(); ++anObjIt) {
259 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anObjIt);
260 if (aCompFeat) // iteratively process composite features
261 isOk = process(aCompFeat) && isOk;
262 else if (!isDumped(EntityPtr(*anObjIt))) {
264 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(*anObjIt);
268 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIt);
269 if (aFeature) // dump common feature
270 dumpFeature(aFeature);
274 // dump folders if any
279 bool ModelHighAPI_Dumper::process(const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
282 // increase composite features stack
283 ++gCompositeStackDepth;
284 // dump composite itself
285 if (!isDumped(EntityPtr(theComposite)) || isForce)
286 dumpFeature(FeaturePtr(theComposite), isForce);
288 // sub-part is processed independently, because it provides separate document
289 if (theComposite->getKind() == PartSetPlugin_Part::ID()) {
290 // dump name of the part if it is different from default
291 if (!myEntitiesStack.empty())
294 // decrease composite features stack because we run into separate document
295 --gCompositeStackDepth;
297 ResultPartPtr aPartResult =
298 std::dynamic_pointer_cast<ModelAPI_ResultPart>(theComposite->lastResult());
301 DocumentPtr aSubDoc = aPartResult->partDoc();
304 // set name of document
305 const std::string& aPartName = myNames[theComposite].myCurrentName;
306 std::string aDocName = aPartName + "_doc";
307 myNames[aSubDoc] = EntityName(aDocName, std::string(), true);
309 // dump document in a separate line
310 *this << aDocName << " = " << aPartName << ".document()" << std::endl;
311 // dump features in the document
312 bool aRes = process(aSubDoc);
313 *this << "model.do()" << std::endl;
318 bool isOk = processSubs(theComposite);
319 // decrease composite features stack
320 --gCompositeStackDepth;
325 bool ModelHighAPI_Dumper::processSubs(
326 const std::shared_ptr<ModelAPI_CompositeFeature>& theComposite,
330 // dump all sub-features;
331 bool isSubDumped = false;
332 int aNbSubs = theComposite->numberOfSubs();
333 for (int anIndex = 0; anIndex < aNbSubs; ++anIndex) {
334 FeaturePtr aFeature = theComposite->subFeature(anIndex);
335 if (isDumped(EntityPtr(aFeature)))
339 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
340 if (aCompFeat) // iteratively process composite features
341 isOk = process(aCompFeat) && isOk;
343 dumpFeature(aFeature, true);
346 bool isDumpSetName = !myEntitiesStack.empty() &&
347 myEntitiesStack.top().myEntity == EntityPtr(theComposite);
348 bool isForceModelDo = isSubDumped && isDumpSetName &&
349 (myEntitiesStack.top().myUserName || !myEntitiesStack.top().myResults.empty());
350 // It is necessary for the sketch to create its result when complete (command "model.do()").
351 // This option is set by flat theDumpModelDo.
352 // However, nested sketches are rebuilt by parent feature, so, they do not need
353 // explicit call of "model.do()". This will be controlled by the depth of the stack.
354 if (isForceModelDo || (theDumpModelDo && gCompositeStackDepth <= 1))
355 *this << "model.do()" << std::endl;
357 // dump "setName" for composite feature
363 void ModelHighAPI_Dumper::postpone(const EntityPtr& theEntity)
366 name(theEntity, false);
367 myPostponed.push_back(theEntity);
370 void ModelHighAPI_Dumper::dumpPostponed(bool theDumpFolders)
372 if (myDumpPostponedInProgress)
375 myDumpPostponedInProgress = true;
376 // make a copy of postponed entities, because the list will be updated
377 // if some features are not able to be dumped
378 std::list<EntityPtr> aPostponedCopy = myPostponed;
381 // iterate over postponed entities and try to dump them
382 std::list<EntityPtr>::const_iterator anIt = aPostponedCopy.begin();
383 for (; anIt != aPostponedCopy.end(); ++anIt) {
384 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(*anIt);
389 myPostponed.push_back(*anIt);
392 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
394 dumpFeature(aFeature, true);
397 myDumpPostponedInProgress = false;
400 void ModelHighAPI_Dumper::dumpSubFeatureNameAndColor(const std::string theSubFeatureGet,
401 const FeaturePtr& theSubFeature)
403 name(theSubFeature, false);
404 myNames[theSubFeature] = EntityName(theSubFeatureGet, theSubFeature->name(), false);
406 // store results if they have user-defined names or colors
407 std::list<ResultPtr> aResultsWithNameOrColor;
408 const std::list<ResultPtr>& aResults = theSubFeature->results();
409 std::list<ResultPtr>::const_iterator aResIt = aResults.begin();
410 for (; aResIt != aResults.end(); ++aResIt) {
411 std::string aResName = (*aResIt)->data()->name();
412 myNames[*aResIt] = EntityName(aResName, aResName, false);
413 aResultsWithNameOrColor.push_back(*aResIt);
416 // store just dumped entity to stack
417 myEntitiesStack.push(LastDumpedEntity(theSubFeature, true, aResultsWithNameOrColor));
422 bool ModelHighAPI_Dumper::exportTo(const std::string& theFileName)
425 OSD_OpenStream(aFile, theFileName.c_str(), std::ofstream::out);
426 if (!aFile.is_open())
429 // standard header (encoding + imported modules)
430 aFile << "# -*- coding: utf-8 -*-" << std::endl << std::endl;
431 for (ModulesMap::const_iterator aModIt = myModules.begin();
432 aModIt != myModules.end(); ++aModIt) {
433 aFile << "from " << aModIt->first << " import ";
434 if (aModIt->second.empty() ||
435 aModIt->second.find(std::string()) != aModIt->second.end())
436 aFile << "*"; // import whole module
438 // import specific features
439 std::set<std::string>::const_iterator anObjIt = aModIt->second.begin();
441 for (++anObjIt; anObjIt != aModIt->second.end(); ++anObjIt)
442 aFile << ", " << *anObjIt;
446 if (!myModules.empty())
449 aFile << "from salome.shaper import model" << std::endl << std::endl;
450 aFile << "model.begin()" << std::endl;
452 // dump collected data
453 aFile << myFullDump.str();
454 aFile << myDumpBuffer.str();
457 aFile << "model.end()" << std::endl;
465 void ModelHighAPI_Dumper::importModule(const std::string& theModuleName,
466 const std::string& theObject)
468 myModules[theModuleName].insert(theObject);
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 bool ModelHighAPI_Dumper::isDefaultColor(const ResultPtr& theResult) const
570 AttributeIntArrayPtr aColor = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
571 if (!aColor || !aColor->isInitialized())
574 std::string aSection, aName, aDefault;
575 theResult->colorConfigInfo(aSection, aName, aDefault);
577 // dump current color
578 std::ostringstream aColorInfo;
579 aColorInfo << aColor->value(0) << "," << aColor->value(1) << "," << aColor->value(2);
581 return aDefault == aColorInfo.str();
584 bool ModelHighAPI_Dumper::isDefaultDeflection(const ResultPtr& theResult) const
586 AttributeDoublePtr aDeflectionAttr = theResult->data()->real(ModelAPI_Result::DEFLECTION_ID());
587 if(!aDeflectionAttr || !aDeflectionAttr->isInitialized()) {
591 double aCurrent = aDeflectionAttr->value();
592 double aDefault = -1;
594 bool isConstruction = false;
595 std::string aResultGroup = theResult->groupName();
596 if (aResultGroup == ModelAPI_ResultConstruction::group())
597 isConstruction = true;
598 else if (aResultGroup == ModelAPI_ResultBody::group()) {
599 GeomShapePtr aGeomShape = theResult->shape();
600 if (aGeomShape.get()) {
601 // if the shape could not be exploded on faces, it contains only wires, edges, and vertices
602 // correction of deviation for them should not influence to the application performance
603 GeomAPI_ShapeExplorer anExp(aGeomShape, GeomAPI_Shape::FACE);
604 isConstruction = !anExp.more();
608 aDefault = Config_PropManager::real("Visualization", "construction_deflection");
610 aDefault = Config_PropManager::real("Visualization", "body_deflection");
612 return fabs(aCurrent - aDefault) < 1.e-12;
615 bool ModelHighAPI_Dumper::isDefaultTransparency(const ResultPtr& theResult) const
617 AttributeDoublePtr anAttribute = theResult->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
618 if(!anAttribute || !anAttribute->isInitialized()) {
621 return fabs(anAttribute->value()) < 1.e-12;
624 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char theChar)
626 myDumpBuffer << theChar;
630 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const char* theString)
632 myDumpBuffer << theString;
636 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::string& theString)
638 myDumpBuffer << theString;
642 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const bool theValue)
644 myDumpBuffer << (theValue ? "True" : "False");
648 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const int theValue)
650 myDumpBuffer << theValue;
654 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const double theValue)
656 myDumpBuffer << theValue;
660 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
662 importModule("GeomAPI", "GeomAPI_Pnt");
663 myDumpBuffer << "GeomAPI_Pnt(" << thePoint->x() << ", "
664 << thePoint->y() << ", " << thePoint->z() << ")";
668 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const std::shared_ptr<GeomAPI_Dir>& theDir)
670 importModule("GeomAPI", "GeomAPI_Dir");
671 myDumpBuffer << "GeomAPI_Dir(" << theDir->x() << ", "
672 << theDir->y() << ", " << theDir->z() << ")";
676 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
677 const std::shared_ptr<GeomDataAPI_Dir>& theDir)
679 myDumpBuffer << theDir->x() << ", " << theDir->y() << ", " << theDir->z();
683 static void dumpArray(std::ostringstream& theOutput, int theSize,
684 double* theValues, std::string* theTexts)
686 for (int i = 0; i < theSize; ++i) {
689 if (theTexts[i].empty())
690 theOutput << theValues[i];
692 theOutput << "\"" << theTexts[i] << "\"";
696 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
697 const std::shared_ptr<GeomDataAPI_Point>& thePoint)
699 static const int aSize = 3;
700 double aValues[aSize] = {thePoint->x(), thePoint->y(), thePoint->z()};
701 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY(), thePoint->textZ()};
702 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
706 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
707 const std::shared_ptr<GeomDataAPI_Point2D>& thePoint)
709 static const int aSize = 2;
710 double aValues[aSize] = {thePoint->x(), thePoint->y()};
711 std::string aTexts[aSize] = {thePoint->textX(), thePoint->textY()};
712 dumpArray(myDumpBuffer, aSize, aValues, aTexts);
716 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
717 const std::shared_ptr<ModelAPI_AttributeBoolean>& theAttrBool)
719 myDumpBuffer << (theAttrBool->value() ? "True" : "False");
723 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
724 const std::shared_ptr<ModelAPI_AttributeInteger>& theAttrInt)
726 std::string aText = theAttrInt->text();
728 myDumpBuffer << theAttrInt->value();
730 myDumpBuffer << "\"" << aText << "\"";
734 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
735 const std::shared_ptr<ModelAPI_AttributeDouble>& theAttrReal)
737 std::string aText = theAttrReal->text();
739 myDumpBuffer << theAttrReal->value();
741 myDumpBuffer << "\"" << aText << "\"";
745 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
746 const std::shared_ptr<ModelAPI_AttributeString>& theAttrStr)
748 myDumpBuffer << "\"" << theAttrStr->value() << "\"";
752 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FolderPtr& theFolder)
754 myDumpBuffer << name(theFolder);
756 // add dumped folder to a stack
757 if (!myNames[theFolder].myIsDumped &&
758 (myEntitiesStack.empty() || myEntitiesStack.top().myEntity != theFolder))
759 myEntitiesStack.push(LastDumpedEntity(theFolder, !myNames[theFolder].myIsDefault));
764 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const FeaturePtr& theEntity)
766 myDumpBuffer << name(theEntity);
768 if (!myNames[theEntity].myIsDumped) {
769 bool isUserDefinedName = !myNames[theEntity].myIsDefault;
770 // store results if they have user-defined names or colors
771 std::list<ResultPtr> aResultsWithNameOrColor;
772 std::list<ResultPtr> allRes;
773 ModelAPI_Tools::allResults(theEntity, allRes);
774 for(std::list<ResultPtr>::iterator aRes = allRes.begin(); aRes != allRes.end(); aRes++) {
775 if(!myNames[*aRes].myIsDefault || !isDefaultColor(*aRes) ||
776 !isDefaultDeflection(*aRes) || !isDefaultTransparency(*aRes))
777 aResultsWithNameOrColor.push_back(*aRes);
779 // store just dumped entity to stack
780 if (myEntitiesStack.empty() || myEntitiesStack.top().myEntity != theEntity)
781 myEntitiesStack.push(
782 LastDumpedEntity(theEntity, isUserDefinedName, aResultsWithNameOrColor));
785 // remove entity from the list of not dumped items
786 myNotDumpedEntities.erase(theEntity);
790 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ResultPtr& theResult)
792 // iterate in the structure of sub-results to the parent
793 ResultPtr aCurRes = theResult;
794 FeaturePtr aFeature = ModelAPI_Feature::feature(theResult);
795 std::list<int> anIndices; // indexes of results in the parent result, starting from topmost
796 while(aCurRes.get()) {
797 ResultBodyPtr aParent = ModelAPI_Tools::bodyOwner(aCurRes);
799 anIndices.push_front(ModelAPI_Tools::bodyIndex(aCurRes));
800 } else { // index of the result in the feature
801 std::list<ResultPtr>::const_iterator aRes = aFeature->results().cbegin();
802 for(int anIndex = 0; aRes != aFeature->results().cend(); aRes++, anIndex++) {
803 if (*aRes == aCurRes) {
804 anIndices.push_front(anIndex);
812 myDumpBuffer << name(aFeature);
813 for (std::list<int>::iterator anI = anIndices.begin(); anI != anIndices.end(); anI++) {
814 if (anI == anIndices.begin()) {
816 myDumpBuffer << ".result()";
819 myDumpBuffer << ".results()[" << *anI << "]";
822 myDumpBuffer << ".subResult(" << *anI << ")";
829 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const ObjectPtr& theObject)
831 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
833 myDumpBuffer << name(aFeature);
837 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
846 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(const AttributePtr& theAttr)
848 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttr->owner());
850 std::string aWrapperPrefix, aWrapperSuffix;
851 // Check the attribute belongs to copied (in multi-translation or multi-rotation) feature.
852 // In this case we need to cast explicitly feature to appropriate type.
853 AttributeBooleanPtr isCopy = anOwner->boolean("Copy");
854 if (isCopy.get() && isCopy->value()) {
855 aWrapperPrefix = featureWrapper(anOwner) + "(";
856 aWrapperSuffix = ")";
857 importModule("SketchAPI");
860 myDumpBuffer << aWrapperPrefix << name(anOwner) << aWrapperSuffix
861 << "." << attributeGetter(anOwner, theAttr->id()) << "()";
865 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
866 const std::shared_ptr<ModelAPI_AttributeRefAttr>& theRefAttr)
868 if (theRefAttr->isObject())
869 *this << theRefAttr->object();
871 *this << theRefAttr->attr();
875 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
876 const std::shared_ptr<ModelAPI_AttributeRefAttrList>& theRefAttrList)
879 std::list<std::pair<ObjectPtr, AttributePtr> > aList = theRefAttrList->list();
880 bool isAdded = false;
881 std::list<std::pair<ObjectPtr, AttributePtr> >::const_iterator anIt = aList.begin();
882 for (; anIt != aList.end(); ++anIt) {
884 myDumpBuffer << ", ";
888 *this << anIt->first;
889 else if (anIt->second)
890 * this << anIt->second;
896 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
897 const std::shared_ptr<ModelAPI_AttributeReference>& theReference)
899 *this << theReference->value();
903 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
904 const std::shared_ptr<ModelAPI_AttributeRefList>& theRefList)
906 static const int aThreshold = 2;
907 // if number of elements in the list if greater than a threshold,
908 // dump it in a separate line with specific name
909 std::string aDumped = myDumpBuffer.str();
910 if (aDumped.empty() || theRefList->size() <= aThreshold) {
912 std::list<ObjectPtr> aList = theRefList->list();
913 bool isAdded = false;
914 std::list<ObjectPtr>::const_iterator anIt = aList.begin();
915 for (; anIt != aList.end(); ++anIt) {
917 myDumpBuffer << ", ";
925 // clear buffer and store list "as is"
926 myDumpBuffer.str("");
928 // save buffer and clear it again
929 std::string aDumpedList = myDumpBuffer.str();
930 myDumpBuffer.str("");
931 // obtain name of list
932 FeaturePtr anOwner = ModelAPI_Feature::feature(theRefList->owner());
933 std::string aListName = name(anOwner) + "_objects";
934 // store all previous data
935 myDumpBuffer << aListName << " = " << aDumpedList << std::endl
936 << aDumped << aListName;
941 static int possibleSelectionsByPoint(const GeomPointPtr& thePoint,
942 const ResultPtr& theResult,
943 const GeomShapePtr& theShape,
944 const FeaturePtr& theStartFeature,
945 const FeaturePtr& theEndFeature)
947 DocumentPtr aDoc1 = theStartFeature->document();
948 DocumentPtr aDoc2 = theEndFeature->document();
950 std::list<FeaturePtr> aFeatures = aDoc1->allFeatures();
951 if (aDoc1 != aDoc2) {
952 std::list<FeaturePtr> anAdditionalFeatures = aDoc2->allFeatures();
953 aFeatures.insert(aFeatures.end(), anAdditionalFeatures.begin(), anAdditionalFeatures.end());
956 CompositeFeaturePtr aLastCompositeFeature;
958 std::list<FeaturePtr>::const_iterator aFIt = aFeatures.begin();
959 while (aFIt != aFeatures.end() && *aFIt != theStartFeature) {
960 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFIt);
962 aLastCompositeFeature = aCompFeat;
966 // collect the list of composite features, containing the last feature;
967 // these features should be excluded from searching,
968 // because the feature cannot select sub-shapes from its parent
969 std::set<FeaturePtr> aEndFeatureParents = ModelAPI_Tools::getParents(theEndFeature);
971 int aNbPossibleSelections = 0;
972 for (; aFIt != aFeatures.end() && *aFIt != theEndFeature; ++aFIt) {
973 bool isSkipFeature = false;
974 if (aLastCompositeFeature && aLastCompositeFeature->isSub(*aFIt))
975 isSkipFeature = true;
976 CompositeFeaturePtr aCompFeat = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*aFIt);
978 ResultPartPtr aPartRes =
979 std::dynamic_pointer_cast<ModelAPI_ResultPart>(aCompFeat->firstResult());
981 aLastCompositeFeature = aCompFeat;
982 if (aEndFeatureParents.find(aCompFeat) != aEndFeatureParents.end()) {
983 // do not process the parent for the last feature,
984 // because it cannot select objects from its parent
985 isSkipFeature = true;
991 std::list<ModelGeomAlgo_Shape::SubshapeOfResult> anApproproate;
992 if (ModelGeomAlgo_Shape::findSubshapeByPoint(*aFIt, thePoint, theShape->shapeType(),
994 std::list<ModelGeomAlgo_Shape::SubshapeOfResult>::iterator anApIt = anApproproate.begin();
995 for (; anApIt != anApproproate.end(); ++anApIt) {
996 ++aNbPossibleSelections;
998 // stop if the target shape and result are found
999 GeomShapePtr aCurShape = anApIt->mySubshape;
1001 aCurShape = anApIt->myResult->shape();
1003 if (anApIt->myResult->isSame(theResult) && aCurShape->isSame(theShape))
1008 return aNbPossibleSelections;
1011 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1012 const std::shared_ptr<ModelAPI_AttributeSelection>& theAttrSelect)
1014 myDumpBuffer << "model.selection(";
1016 if(!theAttrSelect->isInitialized()) {
1017 myDumpBuffer << ")";
1021 GeomShapePtr aShape = theAttrSelect->value();
1023 aShape = theAttrSelect->context()->shape();
1027 myDumpBuffer << ")";
1031 // how to dump selection: construction features are dumped by name always
1032 bool isDumpByGeom = myGeometricalSelection;
1033 FeaturePtr aSelectedFeature;
1035 ResultPtr aRes = theAttrSelect->context();
1037 aSelectedFeature = ModelAPI_Feature::feature(aRes->data()->owner());
1038 if (aSelectedFeature)
1039 isDumpByGeom = aSelectedFeature->isInHistory();
1043 myDumpBuffer << "\"" << aShape->shapeTypeStr();
1045 // check the selected item is a ResultPart;
1046 // in this case it is necessary to get shape with full transformation
1047 // for correct calculation of the middle point
1048 ResultPartPtr aResPart =
1049 std::dynamic_pointer_cast<ModelAPI_ResultPart>(theAttrSelect->context());
1051 aShape = aResPart->shape();
1052 GeomPointPtr aMiddlePoint = aShape->middlePoint();
1053 // calculate number of features, which could be selected by the same point
1054 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttrSelect->owner());
1055 int aNbPossibleSelections = possibleSelectionsByPoint(aMiddlePoint,
1056 theAttrSelect->context(), aShape, aSelectedFeature, anOwner);
1058 // produce the index if the number of applicable features is greater than 1
1059 std::string anIndex;
1060 if (aNbPossibleSelections > 1) {
1061 std::ostringstream anOutput;
1062 anOutput << "_" << aNbPossibleSelections;
1063 anIndex = anOutput.str();
1066 myDumpBuffer << anIndex << "\", ";
1067 *this << aMiddlePoint;
1070 myDumpBuffer << "\", \"" << theAttrSelect->namingName() << "\"";
1071 myDumpBuffer << ")";
1075 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1076 const std::shared_ptr<ModelAPI_AttributeSelectionList>& theAttrSelList)
1078 static const int aThreshold = 2;
1079 // if number of elements in the list if greater than a threshold,
1080 // dump it in a separate line with specific name
1081 std::string aDumped = myDumpBuffer.str();
1083 if (aDumped.empty() || theAttrSelList->size() <= aThreshold) {
1084 myDumpBuffer << "[";
1086 GeomShapePtr aShape;
1087 std::string aShapeTypeStr;
1089 bool isAdded = false;
1091 for(int anIndex = 0; anIndex < theAttrSelList->size(); ++anIndex) {
1092 AttributeSelectionPtr anAttribute = theAttrSelList->value(anIndex);
1093 aShape = anAttribute->value();
1095 ResultPtr aContext = anAttribute->context();
1097 aShape = aContext->shape();
1105 myDumpBuffer << ", ";
1109 *this << anAttribute;
1112 myDumpBuffer << "]";
1114 // clear buffer and store list "as is"
1115 myDumpBuffer.str("");
1116 *this << theAttrSelList;
1117 // save buffer and clear it again
1118 std::string aDumpedList = myDumpBuffer.str();
1119 myDumpBuffer.str("");
1120 // obtain name of list (the feature may contain several selection lists)
1121 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttrSelList->owner());
1122 std::string aListName = name(anOwner) + "_objects";
1123 std::list<AttributePtr> aSelLists =
1124 anOwner->data()->attributes(ModelAPI_AttributeSelectionList::typeId());
1125 if (aSelLists.size() > 1) {
1127 for (std::list<AttributePtr>::iterator aSIt = aSelLists.begin();
1128 aSIt != aSelLists.end(); ++aSIt, ++anIndex)
1129 if ((*aSIt).get() == theAttrSelList.get())
1131 std::ostringstream aSStream;
1132 aSStream << aListName << "_" << anIndex;
1133 aListName = aSStream.str();
1135 // store all previous data
1136 myDumpBuffer << aListName << " = " << aDumpedList << std::endl
1137 << aDumped << aListName;
1142 ModelHighAPI_Dumper& ModelHighAPI_Dumper::operator<<(
1143 const std::shared_ptr<ModelAPI_AttributeStringArray>& theArray)
1146 for(int anIndex = 0; anIndex < theArray->size(); ++anIndex) {
1150 myDumpBuffer<<"\""<<theArray->value(anIndex)<<"\"";
1158 ModelHighAPI_Dumper& operator<<(ModelHighAPI_Dumper& theDumper,
1159 std::basic_ostream<char>& (*theEndl)(std::basic_ostream<char>&))
1161 theDumper.myDumpBuffer << theEndl;
1163 if (!theDumper.myEntitiesStack.empty()) {
1165 // all copies have been stored into stack, pop them all
1168 // Name for composite feature is dumped when all sub-entities are dumped
1169 // (see method ModelHighAPI_Dumper::processSubs).
1170 const ModelHighAPI_Dumper::LastDumpedEntity& aLastDumped = theDumper.myEntitiesStack.top();
1171 CompositeFeaturePtr aComposite =
1172 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aLastDumped.myEntity);
1174 theDumper.dumpEntitySetName();
1175 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aLastDumped.myEntity);
1177 AttributeBooleanPtr aCopyAttr = aFeature->boolean("Copy");
1178 isCopy = aCopyAttr.get() && aCopyAttr->value();
1181 } while (isCopy && !theDumper.myEntitiesStack.empty());
1184 // store all not-dumped entities first
1185 std::set<EntityPtr> aNotDumped = theDumper.myNotDumpedEntities;
1186 std::string aBufCopy = theDumper.myDumpBuffer.str();
1187 theDumper.clear(true);
1188 std::set<EntityPtr>::const_iterator anIt = aNotDumped.begin();
1189 for (; anIt != aNotDumped.end(); ++anIt) {
1190 // if the feature is composite, dump it with all subs
1191 CompositeFeaturePtr aCompFeat =
1192 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(*anIt);
1194 theDumper.process(aCompFeat, true);
1196 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
1197 theDumper.dumpFeature(aFeature, true);
1198 // dump the Projection feature which produces this "Copy" entity
1199 AttributeBooleanPtr aCopyAttr = aFeature->boolean("Copy");
1200 if (aCopyAttr.get() && aCopyAttr->value())
1202 const std::set<AttributePtr>& aRefs = aFeature->data()->refsToMe();
1203 std::set<AttributePtr>::iterator aRefIt = aRefs.begin();
1204 for (; aRefIt != aRefs.end(); ++aRefIt)
1205 if ((*aRefIt)->id() == "ProjectedFeature")
1206 { // process projection only
1207 FeaturePtr anOwner = ModelAPI_Feature::feature((*aRefIt)->owner());
1208 if (anOwner && !theDumper.isDumped(EntityPtr(anOwner)))
1209 theDumper.dumpFeature(anOwner, true);
1215 // avoid multiple empty lines
1216 size_t anInd = std::string::npos;
1217 while ((anInd = aBufCopy.find("\n\n\n")) != std::string::npos)
1218 aBufCopy.erase(anInd, 1);
1219 // then store currently dumped string
1220 theDumper.myFullDump << aBufCopy;
1222 // now, store all postponed features
1223 theDumper.dumpPostponed();