Salome HOME
Issue #1932: error when dump study with not loaded documents
[modules/shaper.git] / src / ExchangePlugin / ExchangePlugin_Dump.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:    ExchangePlugin_ExportFeature.cpp
4 // Created: May 14, 2015
5 // Author:  Sergey POKHODENKO
6
7 #include <ExchangePlugin_Dump.h>
8
9 #include <ModelAPI_AttributeString.h>
10 #include <ModelAPI_Document.h>
11 #include <ModelAPI_Session.h>
12 #include <ModelAPI_ResultPart.h>
13
14 #include <ModelHighAPI_Dumper.h>
15
16 #include <Config_ModuleReader.h>
17
18
19 ExchangePlugin_Dump::ExchangePlugin_Dump()
20 {
21 }
22
23 ExchangePlugin_Dump::~ExchangePlugin_Dump()
24 {
25 }
26
27 void ExchangePlugin_Dump::initAttributes()
28 {
29   data()->addAttribute(ExchangePlugin_Dump::FILE_PATH_ID(), ModelAPI_AttributeString::typeId());
30   data()->addAttribute(ExchangePlugin_Dump::FILE_FORMAT_ID(), ModelAPI_AttributeString::typeId());
31 }
32
33 void ExchangePlugin_Dump::execute()
34 {
35   AttributeStringPtr aFilePathAttr =
36       this->string(ExchangePlugin_Dump::FILE_PATH_ID());
37   std::string aFilePath = aFilePathAttr->value();
38   if (aFilePath.empty())
39     return;
40
41   dump(aFilePath);
42 }
43
44 void ExchangePlugin_Dump::dump(const std::string& theFileName)
45 {
46   // load DumpAssistant from Python side
47   Config_ModuleReader::loadScript("salome.shaper.model.dump");
48
49   ModelHighAPI_Dumper* aDumper = ModelHighAPI_Dumper::getInstance();
50   aDumper->clear();
51   DocumentPtr aDoc = ModelAPI_Session::get()->moduleDocument();
52
53   std::list<FeaturePtr> aFeatures = aDoc->allFeatures();
54   for(std::list<FeaturePtr>::const_iterator aFeatIt = aFeatures.begin();
55       aFeatIt != aFeatures.end();
56       ++aFeatIt) {
57     ResultPartPtr aResultPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>((*aFeatIt)->firstResult());
58     if(aResultPart.get()) {
59       if(!aResultPart->isActivated()) {
60         setError("Error: Not all parts are loaded. Can not dump.");
61         return;
62       }
63     }
64   }
65
66   if (!aDumper || !aDumper->process(aDoc, theFileName))
67     setError("An error occured while dumping to " + theFileName);
68 }