Salome HOME
Meet the coding style (line length <= 100)
[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   int aFeaturesNb = aDoc->size(ModelAPI_Feature::group());
54   if(aFeaturesNb > 1) {
55     FeaturePtr aLastFeature =
56         ModelAPI_Feature::feature(aDoc->object(ModelAPI_Feature::group(), aFeaturesNb - 1));
57     if(aDoc->currentFeature(true) != aLastFeature) {
58         setError("Dump cannot be done. Please move the history line to the end before dumping.");
59         return;
60     }
61   }
62
63   DocumentPtr anActiveDoc = ModelAPI_Session::get()->activeDocument();
64   aFeaturesNb = anActiveDoc->size(ModelAPI_Feature::group());
65   if(aFeaturesNb > 1) {
66     FeaturePtr aLastFeature =
67         ModelAPI_Feature::feature(anActiveDoc->object(ModelAPI_Feature::group(), aFeaturesNb - 1));
68     if(anActiveDoc->currentFeature(true) != aLastFeature) {
69         setError("Dump cannot be done. Please move the history line to the end before dumping.");
70         return;
71     }
72   }
73
74   std::list<FeaturePtr> aFeatures = aDoc->allFeatures();
75   for(std::list<FeaturePtr>::const_iterator aFeatIt = aFeatures.begin();
76       aFeatIt != aFeatures.end();
77       ++aFeatIt) {
78     ResultPartPtr aResultPart =
79       std::dynamic_pointer_cast<ModelAPI_ResultPart>((*aFeatIt)->firstResult());
80     if(aResultPart.get()) {
81       if(!aResultPart->isActivated()) {
82         setError("Error: Not all parts are loaded. Can not dump.");
83         return;
84       }
85     }
86   }
87
88   if (!aDumper || !aDumper->process(aDoc, theFileName))
89     setError("An error occured while dumping to " + theFileName);
90 }