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_ImportFeature.h>
25 #include <Config_Common.h>
26 #include <Config_PropManager.h>
28 #include <GeomAlgoAPI_BREPImport.h>
29 #include <GeomAlgoAPI_IGESImport.h>
30 #include <GeomAlgoAPI_STEPImport.h>
31 #include <GeomAlgoAPI_Tools.h>
32 #include <GeomAlgoAPI_XAOImport.h>
33 #include <GeomAlgoAPI_STLImport.h>
35 #include <GeomAPI_Shape.h>
36 #include <GeomAPI_Face.h>
37 #include <GeomAPI_ShapeExplorer.h>
39 #include <Locale_Convert.h>
41 #include <ModelAPI_AttributeRefList.h>
42 #include <ModelAPI_AttributeSelectionList.h>
43 #include <ModelAPI_AttributeString.h>
44 #include <ModelAPI_AttributeStringArray.h>
45 #include <ModelAPI_AttributeIntArray.h>
46 #include <ModelAPI_AttributeTables.h>
47 #include <ModelAPI_AttributeBoolean.h>
48 #include <ModelAPI_AttributeInteger.h>
49 #include <ModelAPI_BodyBuilder.h>
50 #include <ModelAPI_Data.h>
51 #include <ModelAPI_Document.h>
52 #include <ModelAPI_Events.h>
53 #include <ModelAPI_Object.h>
54 #include <ModelAPI_ResultBody.h>
55 #include <ModelAPI_ResultGroup.h>
56 #include <ModelAPI_Session.h>
57 #include <ModelAPI_Validator.h>
58 #include <ModelAPI_Tools.h>
60 #include <XAO_Xao.hxx>
61 #include <XAO_Group.hxx>
62 #include <XAO_Field.hxx>
63 #include <XAO_Step.hxx>
65 #include <ExchangePlugin_Tools.h>
69 ExchangePlugin_ImportFeature::ExchangePlugin_ImportFeature()
73 ExchangePlugin_ImportFeature::~ExchangePlugin_ImportFeature()
75 // TODO Auto-generated destructor stub
79 * Request for initialization of data model of the feature: adding all attributes
81 void ExchangePlugin_ImportFeature::initAttributes()
83 data()->addAttribute(ExchangePlugin_ImportFeature::FILE_PATH_ID(),
84 ModelAPI_AttributeString::typeId());
85 AttributePtr aFeaturesAttribute =
86 data()->addAttribute(ExchangePlugin_ImportFeature::FEATURES_ID(),
87 ModelAPI_AttributeRefList::typeId());
88 data()->addAttribute(STEP_FILE_PATH_ID(), ModelAPI_AttributeString::typeId());
89 data()->addAttribute(IMPORT_TYPE_ID(), ModelAPI_AttributeString::typeId());
90 data()->addAttribute(STEP_MATERIALS_ID(), ModelAPI_AttributeBoolean::typeId());
91 data()->addAttribute(STEP_COLORS_ID(), ModelAPI_AttributeBoolean::typeId());
92 data()->addAttribute(STEP_SCALE_INTER_UNITS_ID(), ModelAPI_AttributeBoolean::typeId());
94 aFeaturesAttribute->setIsArgument(false);
96 ModelAPI_Session::get()->validators()->registerNotObligatory(
97 getKind(), ExchangePlugin_ImportFeature::FEATURES_ID());
98 ModelAPI_Session::get()->validators()->registerNotObligatory(
99 getKind(), ExchangePlugin_ImportFeature::STEP_COLORS_ID());
100 ModelAPI_Session::get()->validators()->registerNotObligatory(
101 getKind(), ExchangePlugin_ImportFeature::STEP_MATERIALS_ID());
102 ModelAPI_Session::get()->validators()->registerNotObligatory(
103 getKind(), ExchangePlugin_ImportFeature::STEP_SCALE_INTER_UNITS_ID());
104 ModelAPI_Session::get()->validators()->registerNotObligatory(
105 getKind(), ExchangePlugin_ImportFeature::STEP_FILE_PATH_ID());
106 ModelAPI_Session::get()->validators()->registerNotObligatory(
107 getKind(), ExchangePlugin_ImportFeature::FILE_PATH_ID());
111 * Computes or recomputes the results
113 void ExchangePlugin_ImportFeature::execute()
115 AttributeStringPtr aImportTypeAttr = string(ExchangePlugin_ImportFeature::IMPORT_TYPE_ID());
116 std::string aFormat = aImportTypeAttr->value();
117 AttributeStringPtr aFilePathAttr;
118 if (aFormat == "STEP" || aFormat == "STP")
120 aFilePathAttr = string(ExchangePlugin_ImportFeature::STEP_FILE_PATH_ID());
122 aFilePathAttr = string(ExchangePlugin_ImportFeature::FILE_PATH_ID());
124 std::string aFilePath = aFilePathAttr->value();
125 if (aFilePath.empty()) {
126 setError("File path is empty.");
130 importFile(aFilePath);
133 std::shared_ptr<ModelAPI_ResultBody> ExchangePlugin_ImportFeature::createResultBody(
134 std::shared_ptr<GeomAPI_Shape> theGeomShape)
136 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
137 //LoadNamingDS of the imported shape
138 loadNamingDS(theGeomShape, aResultBody);
142 void ExchangePlugin_ImportFeature::importFile(const std::string& theFileName)
144 // "*.brep" -> "BREP"
145 std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(theFileName);
147 if (anExtension == "XAO") {
148 importXAO(theFileName);
152 // Perform the import
154 std::shared_ptr<GeomAPI_Shape> aGeomShape;
156 std::map<std::wstring, std::list<std::wstring>> theMaterialShape;
158 std::string anObjectName = GeomAlgoAPI_Tools::File_Tools::name(theFileName);
159 data()->setName(Locale::Convert::toWString(anObjectName));
161 ResultBodyPtr aResult = document()->createBody(data());
163 bool anColorGroupSelected = boolean(ExchangePlugin_ImportFeature::STEP_COLORS_ID())->value();
164 bool anMaterialsGroupSelected =
165 boolean(ExchangePlugin_ImportFeature::STEP_MATERIALS_ID())->value();
167 if (anExtension == "BREP" || anExtension == "BRP") {
168 aGeomShape = BREPImport(theFileName, anExtension, anError);
169 } else if (anExtension == "STEP" || anExtension == "STP") {
170 bool anScalInterUnits =
171 boolean(ExchangePlugin_ImportFeature::STEP_SCALE_INTER_UNITS_ID())->value();
173 // Process groups/fields
174 std::shared_ptr<ModelAPI_AttributeRefList> aRefListOfGroups =
175 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(data()->attribute(FEATURES_ID()));
177 // Remove previous groups/fields stored in RefList
178 std::list<ObjectPtr> anGroupList = aRefListOfGroups->list();
179 std::list<ObjectPtr>::iterator anGroupIt = anGroupList.begin();
180 for (; anGroupIt != anGroupList.end(); ++anGroupIt) {
181 std::shared_ptr<ModelAPI_Feature> aFeature = ModelAPI_Feature::feature(*anGroupIt);
183 document()->removeFeature(aFeature);
186 aGeomShape = STEPImportAttributs(theFileName, aResult, anScalInterUnits,
187 anMaterialsGroupSelected, anColorGroupSelected,
188 theMaterialShape, anError);
189 } else if (anExtension == "IGES" || anExtension == "IGS") {
190 aGeomShape = IGESImport(theFileName, anExtension, anError);
191 } else if (anExtension == "STL") {
192 aGeomShape = STLImport(theFileName, anError);
194 anError = "Unsupported format: " + anExtension;
197 // Check if shape is valid
198 if (!anError.empty()) {
199 setError("An error occurred while importing " + theFileName + ": " + anError);
203 // Pass the results into the model
205 loadNamingDS(aGeomShape, aResult);
207 // create color group
208 if (anColorGroupSelected)
210 setColorGroups(aResult);
213 // create Materiel group
214 if (anMaterialsGroupSelected){
215 setMaterielGroup(aResult,theMaterialShape);
219 aResult->clearShapeNameAndColor();
223 void ExchangePlugin_ImportFeature::setColorGroups(
224 std::shared_ptr<ModelAPI_ResultBody> theResultBody)
226 std::vector<int> aColor;
228 std::list<std::vector<int>> aColorsRead;
230 ModelAPI_Tools::getColor(theResultBody, aColor);
231 if (!aColor.empty() ){
232 std::wstringstream aColorName;
233 aColorName <<L"Color_"<< anIndice;
234 setColorGroup(theResultBody, aColor, aColorName.str());
236 aColorsRead.push_back(aColor);
239 std::list<ResultPtr> allRes;
240 ModelAPI_Tools::allSubs(theResultBody, allRes);
241 for(std::list<ResultPtr>::iterator aRes = allRes.begin(); aRes != allRes.end(); ++aRes) {
242 ModelAPI_Tools::getColor(*aRes, aColor);
243 if (!aColor.empty() ){
244 auto it = std::find(aColorsRead.begin(), aColorsRead.end(), aColor);
245 if ( it == aColorsRead.end() ){
246 std::wstringstream aColorName;
247 aColorName<<L"Color_"<< anIndice;
248 setColorGroup(theResultBody, aColor, aColorName.str());
250 aColorsRead.push_back(aColor);
256 void ExchangePlugin_ImportFeature::setColorGroup(
257 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
258 std::vector<int> &theColor,
259 const std::wstring& theName )
261 std::vector<int> aColor;
262 std::shared_ptr<ModelAPI_Feature> aGroupFeature = addFeature("Group");
265 aGroupFeature->data()->setName(theName);
268 AttributeSelectionListPtr aSelectionList = aGroupFeature->selectionList("group_list");
270 ModelAPI_Tools::getColor(theResultBody, aColor);
271 if (!aColor.empty()){
272 if (aColor == theColor) {
273 GeomShapePtr aShape = theResultBody->shape();
274 aSelectionList->setSelectionType(aShape->shapeTypeStr());
275 aSelectionList->append(theResultBody,aShape);
278 // add element with the same color
279 std::list<ResultPtr> allRes;
280 ModelAPI_Tools::allSubs(theResultBody, allRes);
281 for(std::list<ResultPtr>::iterator aRes = allRes.begin();
282 aRes != allRes.end(); ++aRes) {
283 ModelAPI_Tools::getColor(*aRes, aColor);
284 GeomShapePtr aShape = (*aRes)->shape();
286 if (!aColor.empty()){
287 if (aRes->get() && aColor == theColor) {
288 aSelectionList->setSelectionType(aShape->shapeTypeStr());
289 aSelectionList->append(theResultBody,aShape);
294 // Create the group in the document to be able to set its color
295 ResultPtr aGroup = document()->createGroup(aGroupFeature->data());
296 aGroupFeature->setResult(aGroup);
298 ModelAPI_Tools::setColor(aGroupFeature->lastResult(),theColor);
300 if (aSelectionList->size() == 0) {
301 document()->removeFeature(aGroupFeature);
305 void ExchangePlugin_ImportFeature::setMaterielGroup(
306 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
307 std::map< std::wstring,std::list<std::wstring>>& theMaterialShape)
309 std::map< std::wstring, std::list<std::wstring>>::iterator anIt;
310 for (anIt = theMaterialShape.begin(); anIt != theMaterialShape.end(); ++anIt) {
312 std::shared_ptr<ModelAPI_Feature> aGroupFeature = addFeature("Group");
314 aGroupFeature->data()->setName((*anIt).first);
317 AttributeSelectionListPtr aSelectionList = aGroupFeature->selectionList("group_list");
319 std::list<ResultPtr> allRes;
320 ModelAPI_Tools::allSubs(theResultBody, allRes);
321 for (std::list<ResultPtr>::iterator aRes = allRes.begin(); aRes != allRes.end(); ++aRes) {
323 GeomShapePtr aShape = (*aRes)->shape();
324 for (std::list<std::wstring>::iterator aResMat = anIt->second.begin();
325 aResMat != anIt->second.end(); ++aResMat) {
326 if (aRes->get() && ((*aRes)->data()->name() == (*aResMat)))
328 aSelectionList->append(theResultBody,aShape);
329 aSelectionList->setSelectionType(aShape->shapeTypeStr());
334 if (aSelectionList->size() == 0){
335 document()->removeFeature(aGroupFeature);
340 void ExchangePlugin_ImportFeature::importXAO(const std::string& theFileName)
346 std::shared_ptr<GeomAPI_Shape> aGeomShape = XAOImport(theFileName, anError, &aXao);
348 if (!anError.empty()) {
349 setError("An error occurred while importing " + theFileName + ": " + anError);
353 XAO::Geometry* aXaoGeometry = aXao.getGeometry();
355 // use the geometry name or the file name for the feature
356 std::string aBodyName = aXaoGeometry->getName();
357 if (aBodyName.empty())
358 aBodyName = GeomAlgoAPI_Tools::File_Tools::name(theFileName);
359 data()->setName(Locale::Convert::toWString(aBodyName));
361 ResultBodyPtr aResultBody = createResultBody(aGeomShape);
362 setResult(aResultBody);
364 // Process groups/fields
365 std::shared_ptr<ModelAPI_AttributeRefList> aRefListOfGroups =
366 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(data()->attribute(FEATURES_ID()));
368 // Remove previous groups/fields stored in RefList
369 std::list<ObjectPtr> anGroupList = aRefListOfGroups->list();
370 std::list<ObjectPtr>::iterator anGroupIt = anGroupList.begin();
371 for (; anGroupIt != anGroupList.end(); ++anGroupIt) {
372 std::shared_ptr<ModelAPI_Feature> aFeature = ModelAPI_Feature::feature(*anGroupIt);
374 document()->removeFeature(aFeature);
378 for (int aGroupIndex = 0; aGroupIndex < aXao.countGroups(); ++aGroupIndex) {
379 XAO::Group* aXaoGroup = aXao.getGroup(aGroupIndex);
381 std::shared_ptr<ModelAPI_Feature> aGroupFeature = addFeature("Group");
384 if (!aXaoGroup->getName().empty())
385 aGroupFeature->data()->setName(Locale::Convert::toWString(aXaoGroup->getName()));
388 AttributeSelectionListPtr aSelectionList = aGroupFeature->selectionList("group_list");
390 // conversion of dimension
391 std::string aDimensionString = XAO::XaoUtils::dimensionToString(aXaoGroup->getDimension());
392 std::string aSelectionType =
393 ExchangePlugin_Tools::xaoDimension2selectionType(aDimensionString);
395 aSelectionList->setSelectionType(aSelectionType);
396 for (int anElementIndex = 0; anElementIndex < aXaoGroup->count(); ++anElementIndex) {
397 aSelectionList->append(aResultBody, GeomShapePtr());
398 // complex conversion of element index to reference id
399 int anElementID = aXaoGroup->get(anElementIndex);
400 std::string aReferenceString =
401 aXaoGeometry->getElementReference(aXaoGroup->getDimension(), anElementID);
402 int aReferenceID = XAO::XaoUtils::stringToInt(aReferenceString);
404 aSelectionList->value(anElementIndex)->setId(aReferenceID);
408 for (int aFieldIndex = 0; aFieldIndex < aXao.countFields(); ++aFieldIndex) {
409 XAO::Field* aXaoField = aXao.getField(aFieldIndex);
411 std::shared_ptr<ModelAPI_Feature> aFieldFeature = addFeature("Field");
414 if (!aXaoField->getName().empty())
415 aFieldFeature->data()->setName(Locale::Convert::toWString(aXaoField->getName()));
418 AttributeSelectionListPtr aSelectionList = aFieldFeature->selectionList("selected");
420 // conversion of dimension
421 std::string aDimensionString = XAO::XaoUtils::dimensionToString(aXaoField->getDimension());
422 std::string aSelectionType =
423 ExchangePlugin_Tools::xaoDimension2selectionType(aDimensionString);
424 aSelectionList->setSelectionType(aSelectionType);
425 // limitation: now in XAO fields are related to everything, so, iterate all sub-shapes to fill
426 int aCountSelected = aXaoField->countElements();
427 std::list<ResultPtr>::const_iterator aResIter = results().begin();
428 for(; aResIter != results().end() && aCountSelected; aResIter++) {
429 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aResIter);
432 // check that only results that were created before this field are used
433 FeaturePtr aResultFeature = document()->feature(aBody);
434 if (!aResultFeature.get())
436 GeomShapePtr aShape = aBody->shape();
437 if (!aShape.get() || aShape->isNull())
439 GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::shapeTypeByStr(aSelectionType));
440 for(; anExp.more(); anExp.next()) {
441 aSelectionList->append(aBody, anExp.current());
443 if (aCountSelected == 0)
448 // conversion of type
449 XAO::Type aFieldType = aXaoField->getType();
450 std::string aTypeString = XAO::XaoUtils::fieldTypeToString(aFieldType);
451 ModelAPI_AttributeTables::ValueType aType =
452 ExchangePlugin_Tools::xaoType2valuesType(aTypeString);
453 // set components names
454 AttributeStringArrayPtr aComponents = aFieldFeature->stringArray("components_names");
455 aComponents->setSize(aXaoField->countComponents());
456 for(int aComp = 0; aComp < aXaoField->countComponents(); aComp++) {
457 aComponents->setValue(aComp, aXaoField->getComponentName(aComp));
460 AttributeIntArrayPtr aStamps = aFieldFeature->intArray("stamps");
461 aStamps->setSize(aXaoField->countSteps());
462 std::shared_ptr<ModelAPI_AttributeTables> aTables = aFieldFeature->tables("values");
464 aXaoField->countElements() + 1, aXaoField->countComponents(), aXaoField->countSteps());
465 aTables->setType(aType);
467 XAO::stepIterator aStepIter = aXaoField->begin();
468 for(int aStepIndex = 0; aStepIter != aXaoField->end(); aStepIter++, aStepIndex++) {
469 aStamps->setValue(aStepIndex, (*aStepIter)->getStamp());
470 for(int aRow = 1; aRow <= aXaoField->countElements(); aRow++) {
471 for(int aCol = 0; aCol < aXaoField->countComponents(); aCol++) {
472 ModelAPI_AttributeTables::Value aVal;
473 std::string aValStr = (*aStepIter)->getStringValue(aRow - 1, aCol);
475 case ModelAPI_AttributeTables::BOOLEAN:
476 aVal.myBool = aValStr == "true";
478 case ModelAPI_AttributeTables::INTEGER:
479 aVal.myInt = atoi(aValStr.c_str());
481 case ModelAPI_AttributeTables::DOUBLE:
482 aVal.myDouble = atof(aValStr.c_str());
484 case ModelAPI_AttributeTables::STRING:
485 aVal.myStr = aValStr;
488 aTables->setValue(aVal, aRow, aCol, aStepIndex);
492 // remove everything with zero-values: zeroes are treated as defaults
493 std::set<int> aRowsToRemove;
494 for(int aRow = 1; aRow < aTables->rows(); aRow++) {
496 for(int aCol = 0; aCol < aTables->columns() && isZero; aCol++) {
497 for(int aStepIndex = 0; aStepIndex != aTables->tables() && isZero; aStepIndex++) {
498 if (aTables->valueStr(aRow, aCol, aStepIndex) != aTables->valueStr(0, aCol, aStepIndex))
503 aRowsToRemove.insert(aRow - 1); // -1 to make prepared for remove from SelectionList
505 if (!aRowsToRemove.empty()) { // move usefull rows on bottom to the up of the tables
506 // number of rows passed during going through: the current rows will
507 // be moved up for this value
508 int aRemovedPassed = 0;
509 for(int aRow = 1; aRow < aTables->rows(); aRow++) {
510 if (aRowsToRemove.find(aRow - 1) != aRowsToRemove.end()) {
512 } else if (aRemovedPassed != 0) { // copy the line up
513 for(int aCol = 0; aCol < aTables->columns(); aCol++) {
514 for(int aTable = 0; aTable != aTables->tables(); aTable++) {
516 aTables->value(aRow, aCol, aTable), aRow - aRemovedPassed, aCol, aTable);
521 aTables->setSize(aTables->rows() - aRemovedPassed, aTables->columns(), aTables->tables());
522 aSelectionList->remove(aRowsToRemove); // remove also selected elements
525 // Top avoid problems in Object Browser update: issue #1647.
526 ModelAPI_EventCreator::get()->sendReordered(
527 std::dynamic_pointer_cast<ModelAPI_Feature>(aRefListOfGroups->owner()));
530 } catch (XAO::XAO_Exception& e) {
531 std::string anError = e.what();
532 setError("An error occurred while importing " + theFileName + ": " + anError);
538 //============================================================================
539 std::shared_ptr<ModelAPI_Feature> ExchangePlugin_ImportFeature::addFeature(
542 std::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID, false);
544 data()->reflist(FEATURES_ID())->append(aNew);
545 // set as current also after it becomes sub to set correctly enabled for other subs
546 //document()->setCurrentFeature(aNew, false);
551 void ExchangePlugin_ImportFeature::removeFeature(
552 std::shared_ptr<ModelAPI_Feature> theFeature)
554 if (!data()->isValid())
556 AttributeRefListPtr aList = reflist(FEATURES_ID());
557 aList->remove(theFeature);
561 int ExchangePlugin_ImportFeature::numberOfSubs(bool /*forTree*/) const
563 return data()->reflist(FEATURES_ID())->size(true);
566 std::shared_ptr<ModelAPI_Feature> ExchangePlugin_ImportFeature::subFeature(
567 const int theIndex, bool /*forTree*/)
569 ObjectPtr anObj = data()->reflist(FEATURES_ID())->object(theIndex, false);
570 FeaturePtr aRes = std::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
575 int ExchangePlugin_ImportFeature::subFeatureId(const int theIndex) const
577 std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
578 ModelAPI_AttributeRefList>(data()->attribute(FEATURES_ID()));
579 std::list<ObjectPtr> aFeatures = aRefList->list();
580 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin();
581 int aResultIndex = 1; // number of the counted (created) features, started from 1
582 int aFeatureIndex = -1; // number of the not-empty features in the list
583 for (; anIt != aFeatures.end(); anIt++) {
586 if (aFeatureIndex == theIndex)
594 bool ExchangePlugin_ImportFeature::isSub(ObjectPtr theObject) const
596 // check is this feature of result
597 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
599 return data()->reflist(FEATURES_ID())->isInList(aFeature);
603 //============================================================================
604 void ExchangePlugin_ImportFeature::loadNamingDS(
605 std::shared_ptr<GeomAPI_Shape> theGeomShape,
606 std::shared_ptr<ModelAPI_ResultBody> theResultBody)
609 theResultBody->store(theGeomShape);
610 std::string aNameMS = "Shape";
611 theResultBody->loadFirstLevel(theGeomShape, aNameMS);