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