Salome HOME
Copyright update 2022
[modules/shaper.git] / src / ExchangePlugin / ExchangePlugin_Import.cpp
1 // Copyright (C) 2014-2022  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_AttributeBoolean.h>
31 #include <ModelAPI_Session.h>
32 #include <ModelAPI_ResultPart.h>
33 #include <ModelAPI_Tools.h>
34 #include <ModelAPI_Validator.h>
35
36
37 static const std::wstring THE_NEW_PART_STR(L"New Part");
38
39 DocumentPtr findDocument(DocumentPtr thePartSetDoc, const std::wstring& thePartName)
40 {
41   DocumentPtr aDoc;
42   FeaturePtr aPartFeature;
43   if (thePartName == THE_NEW_PART_STR) {
44     // create new part
45     aPartFeature = thePartSetDoc->addFeature(PartSetPlugin_Part::ID());
46     if (aPartFeature)
47       aPartFeature->execute();
48   }
49   else {
50     // find existing part by its name
51     std::list<FeaturePtr> aSubFeatures = thePartSetDoc->allFeatures();
52     for (std::list<FeaturePtr>::iterator aFIt = aSubFeatures.begin();
53       aFIt != aSubFeatures.end(); ++aFIt) {
54       if ((*aFIt)->getKind() == PartSetPlugin_Part::ID() && (*aFIt)->name() == thePartName) {
55         aPartFeature = *aFIt;
56         break;
57       }
58     }
59   }
60
61   if (aPartFeature) {
62     ResultPartPtr aPartResult =
63       std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->lastResult());
64     if (aPartResult)
65       aDoc = aPartResult->partDoc();
66   }
67   return aDoc;
68 }
69
70
71 /*
72  * Request for initialization of data model of the feature: adding all attributes
73  */
74 void ExchangePlugin_ImportBase::initAttributes()
75 {
76   data()->addAttribute(FILE_PATH_ID(), ModelAPI_AttributeString::typeId());
77   data()->addAttribute(TARGET_PART_ID(), ModelAPI_AttributeInteger::typeId());
78   data()->addAttribute(TARGET_PARTS_LIST_ID(), ModelAPI_AttributeStringArray::typeId());
79 }
80
81 void ExchangePlugin_Import::initAttributes()
82 {
83   ExchangePlugin_ImportBase::initAttributes();
84
85   data()->addAttribute(STEP_FILE_PATH_ID(), ModelAPI_AttributeString::typeId());
86   data()->addAttribute(IMPORT_TYPE_ID(), ModelAPI_AttributeString::typeId());
87   data()->addAttribute(STEP_TARGET_PART_ID(), ModelAPI_AttributeInteger::typeId());
88   data()->addAttribute(STEP_TARGET_PARTS_LIST_ID(), ModelAPI_AttributeStringArray::typeId());
89   data()->addAttribute(STEP_MATERIALS_ID(), ModelAPI_AttributeBoolean::typeId());
90   data()->addAttribute(STEP_COLORS_ID(), ModelAPI_AttributeBoolean::typeId());
91   data()->addAttribute(STEP_SCALE_INTER_UNITS_ID(), ModelAPI_AttributeBoolean::typeId());
92
93 #ifndef HAVE_SALOME
94   ModelAPI_Session::get()->validators()->registerNotObligatory(
95     getKind(), ExchangePlugin_Import::IMPORT_TYPE_ID());
96   ModelAPI_Session::get()->validators()->registerNotObligatory(
97     getKind(), ExchangePlugin_Import::STEP_FILE_PATH_ID());
98   ModelAPI_Session::get()->validators()->registerNotObligatory(
99     getKind(), ExchangePlugin_Import::STEP_TARGET_PART_ID());
100   ModelAPI_Session::get()->validators()->registerNotObligatory(
101     getKind(), ExchangePlugin_Import::STEP_TARGET_PARTS_LIST_ID());
102   ModelAPI_Session::get()->validators()->registerNotObligatory(
103     getKind(), ExchangePlugin_Import::STEP_MATERIALS_ID());
104   ModelAPI_Session::get()->validators()->registerNotObligatory(
105     getKind(), ExchangePlugin_Import::STEP_COLORS_ID());
106   ModelAPI_Session::get()->validators()->registerNotObligatory(
107     getKind(), ExchangePlugin_Import::STEP_SCALE_INTER_UNITS_ID());
108 #endif
109 }
110
111 /*
112  * Computes or recomputes the results
113  */
114
115 void ExchangePlugin_Import::execute()
116 {
117   AttributeStringPtr aFormatAttr =
118       this->string(ExchangePlugin_Import::IMPORT_TYPE_ID());
119   std::string aFormat = aFormatAttr->value();
120   AttributeStringPtr aFilePathAttr;
121   std::string aFilePath;
122   AttributeStringArrayPtr aPartsAttr;
123   AttributeIntegerPtr aTargetAttr;
124   if (aFormat == "STEP" || aFormat == "STP")
125   {
126     aFilePathAttr = string(ExchangePlugin_Import::STEP_FILE_PATH_ID());
127     aFilePath = aFilePathAttr->value();
128     // get the document where to import
129     aPartsAttr = stringArray(STEP_TARGET_PARTS_LIST_ID());
130     aTargetAttr = integer(STEP_TARGET_PART_ID());
131   }else{
132     aFilePathAttr = string(ExchangePlugin_Import::FILE_PATH_ID());
133     aFilePath = aFilePathAttr->value();
134     // get the document where to import
135     aPartsAttr = stringArray(TARGET_PARTS_LIST_ID());
136     aTargetAttr = integer(TARGET_PART_ID());
137   }
138
139   if (aFilePath.empty()) {
140       setError("File path is empty.");
141       return;
142   }
143   SessionPtr aSession = ModelAPI_Session::get();
144   DocumentPtr aDoc = findDocument(aSession->moduleDocument(),
145       Locale::Convert::toWString(aPartsAttr->value(aTargetAttr->value())));
146
147   if (aDoc.get()) {
148     FeaturePtr aImportFeature = aDoc->addFeature(ExchangePlugin_ImportFeature::ID());
149     DataPtr aData = aImportFeature->data();
150     AttributeStringPtr aPathAttr;
151     if (aFormat == "STEP" || aFormat == "STP")
152     {
153       aPathAttr = aData->string(ExchangePlugin_ImportFeature::STEP_FILE_PATH_ID());
154     }else
155     {
156       aPathAttr = aData->string(ExchangePlugin_ImportFeature::FILE_PATH_ID());
157     }
158
159     AttributeStringPtr aImportTypeAttr =
160         aData->string(ExchangePlugin_ImportFeature::IMPORT_TYPE_ID());
161
162     aData->boolean(ExchangePlugin_ImportFeature::STEP_MATERIALS_ID())
163         ->setValue(boolean(ExchangePlugin_Import::STEP_MATERIALS_ID())->value());
164     aData->boolean(ExchangePlugin_ImportFeature::STEP_COLORS_ID())
165         ->setValue(boolean(ExchangePlugin_Import::STEP_COLORS_ID())->value());
166     aData->boolean(ExchangePlugin_ImportFeature::STEP_SCALE_INTER_UNITS_ID())
167         ->setValue(boolean(ExchangePlugin_Import::STEP_SCALE_INTER_UNITS_ID())->value());
168
169     aPathAttr->setValue(aFilePathAttr->value());
170     aImportTypeAttr->setValue(aFormat);
171     aImportFeature->execute();
172   }
173 }
174
175 void ExchangePlugin_Import_Image::execute()
176 {
177   AttributeStringPtr aFilePathAttr = string(ExchangePlugin_ImportBase::FILE_PATH_ID());
178   std::string aFilePath = aFilePathAttr->value();
179   if (aFilePath.empty()) {
180     setError("File path is empty.");
181     return;
182   }
183
184   // get the document where to import
185   AttributeStringArrayPtr aPartsAttr = stringArray(TARGET_PARTS_LIST_ID());
186   AttributeIntegerPtr aTargetAttr = integer(TARGET_PART_ID());
187   SessionPtr aSession = ModelAPI_Session::get();
188   DocumentPtr aDoc = findDocument(aSession->moduleDocument(),
189       Locale::Convert::toWString(aPartsAttr->value(aTargetAttr->value())));
190
191   if (aDoc.get()) {
192     FeaturePtr aImportFeature = aDoc->addFeature(ExchangePlugin_Import_ImageFeature::ID());
193     DataPtr aData = aImportFeature->data();
194     AttributeStringPtr aPathAttr =
195         aData->string(ExchangePlugin_Import_ImageFeature::FILE_PATH_ID());
196     aPathAttr->setValue(aFilePathAttr->value());
197     aImportFeature->execute();
198   }
199 }
200 void ExchangePlugin_Import::attributeChanged(const std::string& theID)
201 {
202   AttributeStringPtr aFilePathAttr;
203   AttributeStringArrayPtr aPartsAttr;
204   AttributeIntegerPtr aTargetAttr;
205
206   if (theID == FILE_PATH_ID() ||theID == STEP_FILE_PATH_ID() ) {
207     aFilePathAttr = string(FILE_PATH_ID());
208     if (theID == FILE_PATH_ID() && aFilePathAttr->value().empty())
209       return;
210     aPartsAttr = stringArray(TARGET_PARTS_LIST_ID());
211     aTargetAttr = integer(TARGET_PART_ID());
212
213     updatePart(aPartsAttr, aTargetAttr);
214
215     aFilePathAttr = string(STEP_FILE_PATH_ID());
216     if (theID == STEP_FILE_PATH_ID() && aFilePathAttr->value().empty())
217       return;
218
219     aPartsAttr = stringArray(STEP_TARGET_PARTS_LIST_ID());
220     aTargetAttr = integer(STEP_TARGET_PART_ID());
221     updatePart(aPartsAttr, aTargetAttr);
222    }
223 }
224
225 void ExchangePlugin_Import_Image::attributeChanged(const std::string& theID)
226 {
227   if (theID == FILE_PATH_ID()) {
228     AttributeStringPtr aFilePathAttr = string(FILE_PATH_ID());
229     if (aFilePathAttr->value().empty())
230       return;
231
232     AttributeStringArrayPtr aPartsAttr = stringArray(TARGET_PARTS_LIST_ID());
233     AttributeIntegerPtr aTargetAttr = integer(TARGET_PART_ID());
234     updatePart(aPartsAttr, aTargetAttr);
235   }
236 }
237
238 void ExchangePlugin_ImportBase::updatePart(AttributeStringArrayPtr& aPartsAttr,
239                                        AttributeIntegerPtr& aTargetAttr)
240 {
241
242   // update the list of target parts
243   SessionPtr aSession = ModelAPI_Session::get();
244   DocumentPtr aDoc = document();
245   bool isPartSet = aDoc == aSession->moduleDocument();
246   if (isPartSet) {
247     std::list<std::wstring> anAcceptedValues;
248     anAcceptedValues.push_back(THE_NEW_PART_STR);
249
250     // append names of all parts
251     std::list<FeaturePtr> aSubFeatures = aDoc->allFeatures();
252     for (std::list<FeaturePtr>::iterator aFIt = aSubFeatures.begin();
253          aFIt != aSubFeatures.end(); ++aFIt) {
254       if ((*aFIt)->getKind() == PartSetPlugin_Part::ID())
255         anAcceptedValues.push_back((*aFIt)->name());
256     }
257
258     if ((size_t)aPartsAttr->size() != anAcceptedValues.size())
259       aTargetAttr->setValue(0);
260
261     aPartsAttr->setSize((int)anAcceptedValues.size());
262     std::list<std::wstring>::iterator anIt = anAcceptedValues.begin();
263     for (int anInd = 0; anIt != anAcceptedValues.end(); ++anIt, ++anInd)
264       aPartsAttr->setValue(anInd, Locale::Convert::toString(*anIt));
265   }
266   else {
267     // keep only the name of the current part
268     if (aPartsAttr->size() == 0) {
269       FeaturePtr aPartFeature = ModelAPI_Tools::findPartFeature(aSession->moduleDocument(), aDoc);
270
271       aPartsAttr->setSize(1);
272       aPartsAttr->setValue(0, Locale::Convert::toString(aPartFeature->name()));
273       aTargetAttr->setValue(0);
274     }
275   }
276 }