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