Salome HOME
Merge remote-tracking branch 'remotes/origin/EDF_2020_Lot2'
[modules/shaper.git] / src / ExchangePlugin / ExchangePlugin_Import.cpp
1 // Copyright (C) 2014-2020  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "ExchangePlugin_Import.h"
21 #include "ExchangePlugin_ImportFeature.h"
22
23 #include <Locale_Convert.h>
24
25 #include <PartSetPlugin_Part.h>
26
27 #include <ModelAPI_AttributeString.h>
28 #include <ModelAPI_AttributeInteger.h>
29 #include <ModelAPI_AttributeStringArray.h>
30 #include <ModelAPI_Session.h>
31 #include <ModelAPI_ResultPart.h>
32 #include <ModelAPI_Tools.h>
33
34
35 static const std::wstring THE_NEW_PART_STR(L"New Part");
36
37 DocumentPtr findDocument(DocumentPtr thePartSetDoc, const std::wstring& thePartName)
38 {
39   DocumentPtr aDoc;
40   FeaturePtr aPartFeature;
41   if (thePartName == THE_NEW_PART_STR) {
42     // create new part
43     aPartFeature = thePartSetDoc->addFeature(PartSetPlugin_Part::ID());
44     if (aPartFeature)
45       aPartFeature->execute();
46   }
47   else {
48     // find existing part by its name
49     std::list<FeaturePtr> aSubFeatures = thePartSetDoc->allFeatures();
50     for (std::list<FeaturePtr>::iterator aFIt = aSubFeatures.begin();
51       aFIt != aSubFeatures.end(); ++aFIt) {
52       if ((*aFIt)->getKind() == PartSetPlugin_Part::ID() && (*aFIt)->name() == thePartName) {
53         aPartFeature = *aFIt;
54         break;
55       }
56     }
57   }
58
59   if (aPartFeature) {
60     ResultPartPtr aPartResult =
61       std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->lastResult());
62     if (aPartResult)
63       aDoc = aPartResult->partDoc();
64   }
65   return aDoc;
66 }
67
68
69 ExchangePlugin_Import::ExchangePlugin_Import()
70 {
71 }
72
73 ExchangePlugin_Import::~ExchangePlugin_Import()
74 {
75   // TODO Auto-generated destructor stub
76 }
77
78 /*
79  * Request for initialization of data model of the feature: adding all attributes
80  */
81 void ExchangePlugin_Import::initAttributes()
82 {
83   data()->addAttribute(FILE_PATH_ID(), ModelAPI_AttributeString::typeId());
84   data()->addAttribute(TARGET_PART_ID(), ModelAPI_AttributeInteger::typeId());
85   data()->addAttribute(TARGET_PARTS_LIST_ID(), ModelAPI_AttributeStringArray::typeId());
86 }
87
88 /*
89  * Computes or recomputes the results
90  */
91 void ExchangePlugin_Import::execute()
92 {
93   AttributeStringPtr aFilePathAttr = string(ExchangePlugin_Import::FILE_PATH_ID());
94   std::string aFilePath = aFilePathAttr->value();
95   if (aFilePath.empty()) {
96     setError("File path is empty.");
97     return;
98   }
99
100   // get the document where to import
101   AttributeStringArrayPtr aPartsAttr = stringArray(TARGET_PARTS_LIST_ID());
102   AttributeIntegerPtr aTargetAttr = integer(TARGET_PART_ID());
103   SessionPtr aSession = ModelAPI_Session::get();
104   DocumentPtr aDoc =
105     findDocument(aSession->moduleDocument(),
106       Locale::Convert::toWString(aPartsAttr->value(aTargetAttr->value())));
107
108   if (aDoc.get()) {
109     FeaturePtr aImportFeature = aDoc->addFeature(ExchangePlugin_ImportFeature::ID());
110     DataPtr aData = aImportFeature->data();
111     AttributeStringPtr aPathAttr = aData->string(ExchangePlugin_ImportFeature::FILE_PATH_ID());
112     aPathAttr->setValue(aFilePathAttr->value());
113     aImportFeature->execute();
114   }
115 }
116
117
118 void ExchangePlugin_Import::attributeChanged(const std::string& theID)
119 {
120   if (theID == FILE_PATH_ID()) {
121     AttributeStringPtr aFilePathAttr = string(FILE_PATH_ID());
122     if (aFilePathAttr->value().empty())
123       return;
124
125     AttributeStringArrayPtr aPartsAttr = stringArray(TARGET_PARTS_LIST_ID());
126     AttributeIntegerPtr aTargetAttr = integer(TARGET_PART_ID());
127
128     // update the list of target parts
129     SessionPtr aSession = ModelAPI_Session::get();
130     DocumentPtr aDoc = document();
131     bool isPartSet = aDoc == aSession->moduleDocument();
132     if (isPartSet) {
133       std::list<std::wstring> anAcceptedValues;
134       anAcceptedValues.push_back(THE_NEW_PART_STR);
135
136       // append names of all parts
137       std::list<FeaturePtr> aSubFeatures = aDoc->allFeatures();
138       for (std::list<FeaturePtr>::iterator aFIt = aSubFeatures.begin();
139         aFIt != aSubFeatures.end(); ++aFIt) {
140         if ((*aFIt)->getKind() == PartSetPlugin_Part::ID())
141           anAcceptedValues.push_back((*aFIt)->name());
142       }
143
144       if ((size_t)aPartsAttr->size() != anAcceptedValues.size())
145         aTargetAttr->setValue(0);
146
147       aPartsAttr->setSize((int)anAcceptedValues.size());
148       std::list<std::wstring>::iterator anIt = anAcceptedValues.begin();
149       for (int anInd = 0; anIt != anAcceptedValues.end(); ++anIt, ++anInd)
150         aPartsAttr->setValue(anInd, Locale::Convert::toString(*anIt));
151     }
152     else {
153       // keep only the name of the current part
154       if (aPartsAttr->size() == 0) {
155         FeaturePtr aPartFeature = ModelAPI_Tools::findPartFeature(aSession->moduleDocument(), aDoc);
156
157         aPartsAttr->setSize(1);
158         aPartsAttr->setValue(0, Locale::Convert::toString(aPartFeature->name()));
159         aTargetAttr->setValue(0);
160       }
161     }
162   }
163 }