1 // Copyright (C) 2014-2020 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "ExchangePlugin_Import.h"
21 #include "ExchangePlugin_ImportFeature.h"
23 #include <Locale_Convert.h>
25 #include <PartSetPlugin_Part.h>
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>
36 static const std::wstring THE_NEW_PART_STR(L"New Part");
38 DocumentPtr findDocument(DocumentPtr thePartSetDoc, const std::wstring& thePartName)
41 FeaturePtr aPartFeature;
42 if (thePartName == THE_NEW_PART_STR) {
44 aPartFeature = thePartSetDoc->addFeature(PartSetPlugin_Part::ID());
46 aPartFeature->execute();
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) {
61 ResultPartPtr aPartResult =
62 std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->lastResult());
64 aDoc = aPartResult->partDoc();
70 ExchangePlugin_Import::ExchangePlugin_Import()
74 ExchangePlugin_Import::~ExchangePlugin_Import()
76 // TODO Auto-generated destructor stub
80 * Request for initialization of data model of the feature: adding all attributes
82 void ExchangePlugin_Import::initAttributes()
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());
97 * Computes or recomputes the results
99 void ExchangePlugin_Import::execute()
101 AttributeStringPtr aFormatAttr =
102 this->string(ExchangePlugin_Import::IMPORT_TYPE_ID());
103 std::string aFormat = aFormatAttr->value();
105 AttributeStringPtr aFilePathAttr;
106 std::string aFilePath;
107 AttributeStringArrayPtr aPartsAttr;
108 AttributeIntegerPtr aTargetAttr;
109 if (aFormat == "STEP" || aFormat == "STP")
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());
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());
124 if (aFilePath.empty()) {
125 setError("File path is empty.");
128 SessionPtr aSession = ModelAPI_Session::get();
129 DocumentPtr aDoc = findDocument(aSession->moduleDocument(),
130 Locale::Convert::toWString(aPartsAttr->value(aTargetAttr->value())));
133 FeaturePtr aImportFeature = aDoc->addFeature(ExchangePlugin_ImportFeature::ID());
134 DataPtr aData = aImportFeature->data();
135 AttributeStringPtr aPathAttr;
136 if (aFormat == "STEP" || aFormat == "STP")
138 aPathAttr = aData->string(ExchangePlugin_ImportFeature::STEP_FILE_PATH_ID());
141 aPathAttr = aData->string(ExchangePlugin_ImportFeature::FILE_PATH_ID());
144 AttributeStringPtr aImportTypeAttr =
145 aData->string(ExchangePlugin_ImportFeature::IMPORT_TYPE_ID());
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());
154 aPathAttr->setValue(aFilePathAttr->value());
155 aImportTypeAttr->setValue(aFormat);
157 aImportFeature->execute();
162 void ExchangePlugin_Import::attributeChanged(const std::string& theID)
164 AttributeStringPtr aFilePathAttr;
165 AttributeStringArrayPtr aPartsAttr;
166 AttributeIntegerPtr aTargetAttr;
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())
172 aPartsAttr = stringArray(TARGET_PARTS_LIST_ID());
173 aTargetAttr = integer(TARGET_PART_ID());
175 updatePart(aPartsAttr, aTargetAttr);
177 aFilePathAttr = string(STEP_FILE_PATH_ID());
178 if (theID == STEP_FILE_PATH_ID() && aFilePathAttr->value().empty())
181 aPartsAttr = stringArray(STEP_TARGET_PARTS_LIST_ID());
182 aTargetAttr = integer(STEP_TARGET_PART_ID());
183 updatePart(aPartsAttr, aTargetAttr);
187 void ExchangePlugin_Import::updatePart(AttributeStringArrayPtr& thePartsAttr,
188 AttributeIntegerPtr& theTargetAttr)
190 // update the list of target parts
191 SessionPtr aSession = ModelAPI_Session::get();
192 DocumentPtr aDoc = document();
193 bool isPartSet = aDoc == aSession->moduleDocument();
195 std::list<std::wstring> anAcceptedValues;
196 anAcceptedValues.push_back(THE_NEW_PART_STR);
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());
206 if ((size_t)thePartsAttr->size() != anAcceptedValues.size())
207 theTargetAttr->setValue(0);
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));
215 // keep only the name of the current part
216 if (thePartsAttr->size() == 0) {
217 FeaturePtr aPartFeature = ModelAPI_Tools::findPartFeature(aSession->moduleDocument(), aDoc);
219 thePartsAttr->setSize(1);
220 thePartsAttr->setValue(0, Locale::Convert::toString(aPartFeature->name()));
221 theTargetAttr->setValue(0);