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>
34 #include <GeomAPI_Shape.h>
35 #include <GeomAPI_Face.h>
36 #include <GeomAPI_ShapeExplorer.h>
38 #include <Locale_Convert.h>
40 #include <ModelAPI_AttributeRefList.h>
41 #include <ModelAPI_AttributeSelectionList.h>
42 #include <ModelAPI_AttributeString.h>
43 #include <ModelAPI_AttributeStringArray.h>
44 #include <ModelAPI_AttributeIntArray.h>
45 #include <ModelAPI_AttributeTables.h>
46 #include <ModelAPI_AttributeBoolean.h>
47 #include <ModelAPI_AttributeInteger.h>
48 #include <ModelAPI_BodyBuilder.h>
49 #include <ModelAPI_Data.h>
50 #include <ModelAPI_Document.h>
51 #include <ModelAPI_Events.h>
52 #include <ModelAPI_Object.h>
53 #include <ModelAPI_ResultBody.h>
54 #include <ModelAPI_ResultGroup.h>
55 #include <ModelAPI_Session.h>
56 #include <ModelAPI_Validator.h>
57 #include <ModelAPI_Tools.h>
59 #include <XAO_Xao.hxx>
60 #include <XAO_Group.hxx>
61 #include <XAO_Field.hxx>
62 #include <XAO_Step.hxx>
64 #include <ExchangePlugin_Tools.h>
66 #include <TopExp_Explorer.hxx>
67 #include <OSD_Exception.hxx>
70 ExchangePlugin_ImportFeature::ExchangePlugin_ImportFeature()
74 ExchangePlugin_ImportFeature::~ExchangePlugin_ImportFeature()
76 // TODO Auto-generated destructor stub
80 * Request for initialization of data model of the feature: adding all attributes
82 void ExchangePlugin_ImportFeature::initAttributes()
84 data()->addAttribute(ExchangePlugin_ImportFeature::FILE_PATH_ID(),
85 ModelAPI_AttributeString::typeId());
86 AttributePtr aFeaturesAttribute =
87 data()->addAttribute(ExchangePlugin_ImportFeature::FEATURES_ID(),
88 ModelAPI_AttributeRefList::typeId());
89 data()->addAttribute(STEP_FILE_PATH_ID(), ModelAPI_AttributeString::typeId());
90 data()->addAttribute(IMPORT_TYPE_ID(), ModelAPI_AttributeString::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());
95 aFeaturesAttribute->setIsArgument(false);
97 ModelAPI_Session::get()->validators()->registerNotObligatory(
98 getKind(), ExchangePlugin_ImportFeature::FEATURES_ID());
99 ModelAPI_Session::get()->validators()->registerNotObligatory(
100 getKind(), ExchangePlugin_ImportFeature::STEP_COLORS_ID());
101 ModelAPI_Session::get()->validators()->registerNotObligatory(
102 getKind(), ExchangePlugin_ImportFeature::STEP_MATERIALS_ID());
103 ModelAPI_Session::get()->validators()->registerNotObligatory(
104 getKind(), ExchangePlugin_ImportFeature::STEP_SCALE_INTER_UNITS_ID());
105 ModelAPI_Session::get()->validators()->registerNotObligatory(
106 getKind(), ExchangePlugin_ImportFeature::STEP_FILE_PATH_ID());
107 ModelAPI_Session::get()->validators()->registerNotObligatory(
108 getKind(), ExchangePlugin_ImportFeature::FILE_PATH_ID());
112 * Computes or recomputes the results
114 void ExchangePlugin_ImportFeature::execute()
116 AttributeStringPtr aImportTypeAttr = string(ExchangePlugin_ImportFeature::IMPORT_TYPE_ID());
117 std::string aFormat = aImportTypeAttr->value();
118 AttributeStringPtr aFilePathAttr;
119 if (aFormat == "STEP" || aFormat == "STP")
121 aFilePathAttr = string(ExchangePlugin_ImportFeature::STEP_FILE_PATH_ID());
123 aFilePathAttr = string(ExchangePlugin_ImportFeature::FILE_PATH_ID());
125 std::string aFilePath = aFilePathAttr->value();
126 if (aFilePath.empty()) {
127 setError("File path is empty.");
131 importFile(aFilePath);
134 std::shared_ptr<ModelAPI_ResultBody> ExchangePlugin_ImportFeature::createResultBody(
135 std::shared_ptr<GeomAPI_Shape> theGeomShape)
137 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
138 //LoadNamingDS of the imported shape
139 loadNamingDS(theGeomShape, aResultBody);
143 void ExchangePlugin_ImportFeature::importFile(const std::string& theFileName)
145 // "*.brep" -> "BREP"
146 std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(theFileName);
148 if (anExtension == "XAO") {
149 importXAO(theFileName);
153 // Perform the import
155 std::shared_ptr<GeomAPI_Shape> aGeomShape;
157 std::map< std::wstring, std::list<std::wstring>> theMaterialShape;
159 std::string anObjectName = GeomAlgoAPI_Tools::File_Tools::name(theFileName);
160 data()->setName(Locale::Convert::toWString(anObjectName));
162 ResultBodyPtr aResult = document()->createBody(data());
164 bool anColorGroupSelected = boolean(ExchangePlugin_ImportFeature::STEP_COLORS_ID())->value();
165 bool anMaterialsGroupSelected =
166 boolean(ExchangePlugin_ImportFeature::STEP_MATERIALS_ID())->value();
168 if (anExtension == "BREP" || anExtension == "BRP") {
169 aGeomShape = BREPImport(theFileName, anExtension, anError);
170 } else if (anExtension == "STEP" || anExtension == "STP") {
171 bool anScalInterUnits =
172 boolean(ExchangePlugin_ImportFeature::STEP_SCALE_INTER_UNITS_ID())->value();
176 aResult->clearShapeNameAndColor();
177 // Process groups/fields
178 std::shared_ptr<ModelAPI_AttributeRefList> aRefListOfGroups =
179 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(data()->attribute(FEATURES_ID()));
181 // Remove previous groups/fields stored in RefList
182 std::list<ObjectPtr> anGroupList = aRefListOfGroups->list();
183 std::list<ObjectPtr>::iterator anGroupIt = anGroupList.begin();
184 for (; anGroupIt != anGroupList.end(); ++anGroupIt) {
185 std::shared_ptr<ModelAPI_Feature> aFeature = ModelAPI_Feature::feature(*anGroupIt);
187 document()->removeFeature(aFeature);
190 aGeomShape = STEPImportAttributs(theFileName, aResult, anScalInterUnits,
191 anMaterialsGroupSelected, anColorGroupSelected,
192 theMaterialShape,anError);
195 catch (OSD_Exception& e) {
196 //Try to load STEP file without colors...
197 aGeomShape = STEPImport(theFileName, anExtension,anScalInterUnits,anError);
199 } else if (anExtension == "IGES" || anExtension == "IGS") {
200 aGeomShape = IGESImport(theFileName, anExtension, anError);
202 anError = "Unsupported format: " + anExtension;
205 // Check if shape is valid
206 if (!anError.empty()) {
207 setError("An error occurred while importing " + theFileName + ": " + anError);
211 // Pass the results into the model
213 loadNamingDS(aGeomShape, aResult);
215 // create color group
216 if (anColorGroupSelected)
218 setColorGroups(aResult);
221 // create Materiel group
222 if (anMaterialsGroupSelected){
223 setMaterielGroup(aResult,theMaterialShape);
230 void ExchangePlugin_ImportFeature::setColorGroups(
231 std::shared_ptr<ModelAPI_ResultBody> theResultBody)
233 std::vector<int> aColor;
235 std::list< std::vector<int> > aColorsRead;
237 ModelAPI_Tools::getColor(theResultBody, aColor);
238 if (!aColor.empty() ){
239 std::wstringstream aColorName;
240 aColorName <<L"Color_"<< anIndice;
241 setColorGroup( theResultBody, aColor, aColorName.str());
243 aColorsRead.push_back(aColor);
246 std::list<ResultPtr> allRes;
247 ModelAPI_Tools::allSubs(theResultBody, allRes);
248 for(std::list<ResultPtr>::iterator aRes = allRes.begin(); aRes != allRes.end(); ++aRes) {
249 ModelAPI_Tools::getColor(*aRes, aColor);
250 if (!aColor.empty() ){
251 auto it = std::find(aColorsRead.begin(), aColorsRead.end(), aColor);
252 if ( it == aColorsRead.end() ){
253 std::wstringstream aColorName;
254 aColorName<<L"Color_"<< anIndice;
255 setColorGroup(theResultBody, aColor, aColorName.str());
257 aColorsRead.push_back(aColor);
263 void ExchangePlugin_ImportFeature::setColorGroup(
264 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
265 std::vector<int> &theColor,
266 const std::wstring& theName )
268 std::vector<int> aColor;
269 std::shared_ptr<ModelAPI_Feature> aGroupFeature = addFeature("Group");
272 aGroupFeature->data()->setName(theName);
275 AttributeSelectionListPtr aSelectionList = aGroupFeature->selectionList("group_list");
277 ModelAPI_Tools::getColor(theResultBody, aColor);
278 if (!aColor.empty() ){
279 if (aColor == theColor ) {
280 GeomShapePtr aShape = theResultBody->shape();
281 aSelectionList->setSelectionType(aShape->shapeTypeStr() );
282 aSelectionList->append(theResultBody,aShape);
285 // add element with the same color
286 std::list<ResultPtr> allRes;
287 ModelAPI_Tools::allSubs(theResultBody, allRes);
288 for(std::list<ResultPtr>::iterator aRes = allRes.begin();
289 aRes != allRes.end(); ++aRes) {
290 ModelAPI_Tools::getColor(*aRes, aColor);
291 GeomShapePtr aShape = (*aRes)->shape();
293 if (!aColor.empty() ){
294 if (aRes->get() && aColor == theColor ) {
295 aSelectionList->setSelectionType(aShape->shapeTypeStr() );
296 aSelectionList->append(theResultBody,aShape);
301 // Create the group in the document to be able to set its color
302 ResultPtr aGroup = document()->createGroup(aGroupFeature->data());
303 aGroupFeature->setResult(aGroup);
305 ModelAPI_Tools::setColor(aGroupFeature->lastResult(),theColor);
307 if (aSelectionList->size() == 0 ){
308 document()->removeFeature(aGroupFeature);
312 void ExchangePlugin_ImportFeature::setMaterielGroup(
313 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
314 std::map< std::wstring,std::list<std::wstring>>& theMaterialShape)
316 std::map< std::wstring, std::list<std::wstring>>::iterator anIt;
317 for( anIt = theMaterialShape.begin(); anIt != theMaterialShape.end(); ++anIt) {
319 std::shared_ptr<ModelAPI_Feature> aGroupFeature = addFeature("Group");
321 aGroupFeature->data()->setName((*anIt).first);
324 AttributeSelectionListPtr aSelectionList = aGroupFeature->selectionList("group_list");
325 GeomShapePtr aShape = theResultBody->shape();
327 std::list<ResultPtr> allRes;
328 ModelAPI_Tools::allSubs(theResultBody, allRes);
329 for(std::list<ResultPtr>::iterator aRes = allRes.begin(); aRes != allRes.end(); ++aRes) {
331 GeomShapePtr aShape = (*aRes)->shape();
332 for(std::list<std::wstring>::iterator aResMat = anIt->second.begin();
333 aResMat != anIt->second.end(); ++aResMat) {
334 if (aRes->get() && ((*aRes)->data()->name() == (*aResMat)))
336 aSelectionList->append(theResultBody,aShape);
337 aSelectionList->setSelectionType(aShape->shapeTypeStr() );
342 if (aSelectionList->size() == 0){
343 document()->removeFeature(aGroupFeature);
348 void ExchangePlugin_ImportFeature::importXAO(const std::string& theFileName)
354 std::shared_ptr<GeomAPI_Shape> aGeomShape = XAOImport(theFileName, anError, &aXao);
356 if (!anError.empty()) {
357 setError("An error occurred while importing " + theFileName + ": " + anError);
361 XAO::Geometry* aXaoGeometry = aXao.getGeometry();
363 // use the geometry name or the file name for the feature
364 std::string aBodyName = aXaoGeometry->getName();
365 if (aBodyName.empty())
366 aBodyName = GeomAlgoAPI_Tools::File_Tools::name(theFileName);
367 data()->setName(Locale::Convert::toWString(aBodyName));
369 ResultBodyPtr aResultBody = createResultBody(aGeomShape);
370 setResult(aResultBody);
372 // Process groups/fields
373 std::shared_ptr<ModelAPI_AttributeRefList> aRefListOfGroups =
374 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(data()->attribute(FEATURES_ID()));
376 // Remove previous groups/fields stored in RefList
377 std::list<ObjectPtr> anGroupList = aRefListOfGroups->list();
378 std::list<ObjectPtr>::iterator anGroupIt = anGroupList.begin();
379 for (; anGroupIt != anGroupList.end(); ++anGroupIt) {
380 std::shared_ptr<ModelAPI_Feature> aFeature = ModelAPI_Feature::feature(*anGroupIt);
382 document()->removeFeature(aFeature);
386 for (int aGroupIndex = 0; aGroupIndex < aXao.countGroups(); ++aGroupIndex) {
387 XAO::Group* aXaoGroup = aXao.getGroup(aGroupIndex);
389 std::shared_ptr<ModelAPI_Feature> aGroupFeature = addFeature("Group");
392 if (!aXaoGroup->getName().empty())
393 aGroupFeature->data()->setName(Locale::Convert::toWString(aXaoGroup->getName()));
396 AttributeSelectionListPtr aSelectionList = aGroupFeature->selectionList("group_list");
398 // conversion of dimension
399 std::string aDimensionString = XAO::XaoUtils::dimensionToString(aXaoGroup->getDimension());
400 std::string aSelectionType =
401 ExchangePlugin_Tools::xaoDimension2selectionType(aDimensionString);
403 aSelectionList->setSelectionType(aSelectionType);
404 for (int anElementIndex = 0; anElementIndex < aXaoGroup->count(); ++anElementIndex) {
405 aSelectionList->append(aResultBody, GeomShapePtr());
406 // complex conversion of element index to reference id
407 int anElementID = aXaoGroup->get(anElementIndex);
408 std::string aReferenceString =
409 aXaoGeometry->getElementReference(aXaoGroup->getDimension(), anElementID);
410 int aReferenceID = XAO::XaoUtils::stringToInt(aReferenceString);
412 aSelectionList->value(anElementIndex)->setId(aReferenceID);
416 for (int aFieldIndex = 0; aFieldIndex < aXao.countFields(); ++aFieldIndex) {
417 XAO::Field* aXaoField = aXao.getField(aFieldIndex);
419 std::shared_ptr<ModelAPI_Feature> aFieldFeature = addFeature("Field");
422 if (!aXaoField->getName().empty())
423 aFieldFeature->data()->setName(Locale::Convert::toWString(aXaoField->getName()));
426 AttributeSelectionListPtr aSelectionList = aFieldFeature->selectionList("selected");
428 // conversion of dimension
429 std::string aDimensionString = XAO::XaoUtils::dimensionToString(aXaoField->getDimension());
430 std::string aSelectionType =
431 ExchangePlugin_Tools::xaoDimension2selectionType(aDimensionString);
432 aSelectionList->setSelectionType(aSelectionType);
433 // limitation: now in XAO fields are related to everything, so, iterate all sub-shapes to fill
434 int aCountSelected = aXaoField->countElements();
435 std::list<ResultPtr>::const_iterator aResIter = results().begin();
436 for(; aResIter != results().end() && aCountSelected; aResIter++) {
437 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aResIter);
440 // check that only results that were created before this field are used
441 FeaturePtr aResultFeature = document()->feature(aBody);
442 if (!aResultFeature.get())
444 GeomShapePtr aShape = aBody->shape();
445 if (!aShape.get() || aShape->isNull())
447 GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::shapeTypeByStr(aSelectionType));
448 for(; anExp.more(); anExp.next()) {
449 aSelectionList->append(aBody, anExp.current());
451 if (aCountSelected == 0)
456 // conversion of type
457 XAO::Type aFieldType = aXaoField->getType();
458 std::string aTypeString = XAO::XaoUtils::fieldTypeToString(aFieldType);
459 ModelAPI_AttributeTables::ValueType aType =
460 ExchangePlugin_Tools::xaoType2valuesType(aTypeString);
461 // set components names
462 AttributeStringArrayPtr aComponents = aFieldFeature->stringArray("components_names");
463 aComponents->setSize(aXaoField->countComponents());
464 for(int aComp = 0; aComp < aXaoField->countComponents(); aComp++) {
465 aComponents->setValue(aComp, aXaoField->getComponentName(aComp));
468 AttributeIntArrayPtr aStamps = aFieldFeature->intArray("stamps");
469 aStamps->setSize(aXaoField->countSteps());
470 std::shared_ptr<ModelAPI_AttributeTables> aTables = aFieldFeature->tables("values");
472 aXaoField->countElements() + 1, aXaoField->countComponents(), aXaoField->countSteps());
473 aTables->setType(aType);
475 XAO::stepIterator aStepIter = aXaoField->begin();
476 for(int aStepIndex = 0; aStepIter != aXaoField->end(); aStepIter++, aStepIndex++) {
477 aStamps->setValue(aStepIndex, (*aStepIter)->getStamp());
478 for(int aRow = 1; aRow <= aXaoField->countElements(); aRow++) {
479 for(int aCol = 0; aCol < aXaoField->countComponents(); aCol++) {
480 ModelAPI_AttributeTables::Value aVal;
481 std::string aValStr = (*aStepIter)->getStringValue(aRow - 1, aCol);
483 case ModelAPI_AttributeTables::BOOLEAN:
484 aVal.myBool = aValStr == "true";
486 case ModelAPI_AttributeTables::INTEGER:
487 aVal.myInt = atoi(aValStr.c_str());
489 case ModelAPI_AttributeTables::DOUBLE:
490 aVal.myDouble = atof(aValStr.c_str());
492 case ModelAPI_AttributeTables::STRING:
493 aVal.myStr = aValStr;
496 aTables->setValue(aVal, aRow, aCol, aStepIndex);
500 // remove everything with zero-values: zeroes are treated as defaults
501 std::set<int> aRowsToRemove;
502 for(int aRow = 1; aRow < aTables->rows(); aRow++) {
504 for(int aCol = 0; aCol < aTables->columns() && isZero; aCol++) {
505 for(int aStepIndex = 0; aStepIndex != aTables->tables() && isZero; aStepIndex++) {
506 if (aTables->valueStr(aRow, aCol, aStepIndex) != aTables->valueStr(0, aCol, aStepIndex))
511 aRowsToRemove.insert(aRow - 1); // -1 to make prepared for remove from SelectionList
513 if (!aRowsToRemove.empty()) { // move usefull rows on bottom to the up of the tables
514 // number of rows passed during going through: the current rows will
515 // be moved up for this value
516 int aRemovedPassed = 0;
517 for(int aRow = 1; aRow < aTables->rows(); aRow++) {
518 if (aRowsToRemove.find(aRow - 1) != aRowsToRemove.end()) {
520 } else if (aRemovedPassed != 0) { // copy the line up
521 for(int aCol = 0; aCol < aTables->columns(); aCol++) {
522 for(int aTable = 0; aTable != aTables->tables(); aTable++) {
524 aTables->value(aRow, aCol, aTable), aRow - aRemovedPassed, aCol, aTable);
529 aTables->setSize(aTables->rows() - aRemovedPassed, aTables->columns(), aTables->tables());
530 aSelectionList->remove(aRowsToRemove); // remove also selected elements
533 // Top avoid problems in Object Browser update: issue #1647.
534 ModelAPI_EventCreator::get()->sendReordered(
535 std::dynamic_pointer_cast<ModelAPI_Feature>(aRefListOfGroups->owner()));
538 } catch (XAO::XAO_Exception& e) {
539 std::string anError = e.what();
540 setError("An error occurred while importing " + theFileName + ": " + anError);
546 //============================================================================
547 std::shared_ptr<ModelAPI_Feature> ExchangePlugin_ImportFeature::addFeature(
550 std::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID, false);
552 data()->reflist(FEATURES_ID())->append(aNew);
553 // set as current also after it becomes sub to set correctly enabled for other subs
554 //document()->setCurrentFeature(aNew, false);
559 void ExchangePlugin_ImportFeature::removeFeature(
560 std::shared_ptr<ModelAPI_Feature> theFeature)
562 if (!data()->isValid())
564 AttributeRefListPtr aList = reflist(FEATURES_ID());
565 aList->remove(theFeature);
569 int ExchangePlugin_ImportFeature::numberOfSubs(bool /*forTree*/) const
571 return data()->reflist(FEATURES_ID())->size(true);
574 std::shared_ptr<ModelAPI_Feature> ExchangePlugin_ImportFeature::subFeature(
575 const int theIndex, bool /*forTree*/)
577 ObjectPtr anObj = data()->reflist(FEATURES_ID())->object(theIndex, false);
578 FeaturePtr aRes = std::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
583 int ExchangePlugin_ImportFeature::subFeatureId(const int theIndex) const
585 std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
586 ModelAPI_AttributeRefList>(data()->attribute(FEATURES_ID()));
587 std::list<ObjectPtr> aFeatures = aRefList->list();
588 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin();
589 int aResultIndex = 1; // number of the counted (created) features, started from 1
590 int aFeatureIndex = -1; // number of the not-empty features in the list
591 for (; anIt != aFeatures.end(); anIt++) {
594 if (aFeatureIndex == theIndex)
602 bool ExchangePlugin_ImportFeature::isSub(ObjectPtr theObject) const
604 // check is this feature of result
605 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
607 return data()->reflist(FEATURES_ID())->isInList(aFeature);
611 //============================================================================
612 void ExchangePlugin_ImportFeature::loadNamingDS(
613 std::shared_ptr<GeomAPI_Shape> theGeomShape,
614 std::shared_ptr<ModelAPI_ResultBody> theResultBody)
617 theResultBody->store(theGeomShape);
618 std::string aNameMS = "Shape";
619 theResultBody->loadFirstLevel(theGeomShape, aNameMS);