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>
68 ExchangePlugin_ImportFeature::ExchangePlugin_ImportFeature()
72 ExchangePlugin_ImportFeature::~ExchangePlugin_ImportFeature()
74 // TODO Auto-generated destructor stub
78 * Request for initialization of data model of the feature: adding all attributes
80 void ExchangePlugin_ImportFeature::initAttributes()
82 data()->addAttribute(ExchangePlugin_ImportFeature::FILE_PATH_ID(),
83 ModelAPI_AttributeString::typeId());
84 AttributePtr aFeaturesAttribute =
85 data()->addAttribute(ExchangePlugin_ImportFeature::FEATURES_ID(),
86 ModelAPI_AttributeRefList::typeId());
87 data()->addAttribute(STEP_FILE_PATH_ID(), ModelAPI_AttributeString::typeId());
88 data()->addAttribute(IMPORT_TYPE_ID(), ModelAPI_AttributeString::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());
93 aFeaturesAttribute->setIsArgument(false);
95 ModelAPI_Session::get()->validators()->registerNotObligatory(
96 getKind(), ExchangePlugin_ImportFeature::FEATURES_ID());
97 ModelAPI_Session::get()->validators()->registerNotObligatory(
98 getKind(), ExchangePlugin_ImportFeature::STEP_COLORS_ID());
99 ModelAPI_Session::get()->validators()->registerNotObligatory(
100 getKind(), ExchangePlugin_ImportFeature::STEP_MATERIALS_ID());
101 ModelAPI_Session::get()->validators()->registerNotObligatory(
102 getKind(), ExchangePlugin_ImportFeature::STEP_SCALE_INTER_UNITS_ID());
103 ModelAPI_Session::get()->validators()->registerNotObligatory(
104 getKind(), ExchangePlugin_ImportFeature::STEP_FILE_PATH_ID());
105 ModelAPI_Session::get()->validators()->registerNotObligatory(
106 getKind(), ExchangePlugin_ImportFeature::FILE_PATH_ID());
110 * Computes or recomputes the results
112 void ExchangePlugin_ImportFeature::execute()
114 AttributeStringPtr aImportTypeAttr = string(ExchangePlugin_ImportFeature::IMPORT_TYPE_ID());
115 std::string aFormat = aImportTypeAttr->value();
116 AttributeStringPtr aFilePathAttr;
117 if (aFormat == "STEP" || aFormat == "STP")
119 aFilePathAttr = string(ExchangePlugin_ImportFeature::STEP_FILE_PATH_ID());
121 aFilePathAttr = string(ExchangePlugin_ImportFeature::FILE_PATH_ID());
123 std::string aFilePath = aFilePathAttr->value();
124 if (aFilePath.empty()) {
125 setError("File path is empty.");
129 importFile(aFilePath);
132 std::shared_ptr<ModelAPI_ResultBody> ExchangePlugin_ImportFeature::createResultBody(
133 std::shared_ptr<GeomAPI_Shape> theGeomShape)
135 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
136 //LoadNamingDS of the imported shape
137 loadNamingDS(theGeomShape, aResultBody);
141 void ExchangePlugin_ImportFeature::importFile(const std::string& theFileName)
143 // "*.brep" -> "BREP"
144 std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(theFileName);
146 if (anExtension == "XAO") {
147 importXAO(theFileName);
151 // Perform the import
153 std::shared_ptr<GeomAPI_Shape> aGeomShape;
155 std::map<std::wstring, std::list<std::wstring>> theMaterialShape;
157 std::string anObjectName = GeomAlgoAPI_Tools::File_Tools::name(theFileName);
158 data()->setName(Locale::Convert::toWString(anObjectName));
160 ResultBodyPtr aResult = document()->createBody(data());
162 bool anColorGroupSelected = boolean(ExchangePlugin_ImportFeature::STEP_COLORS_ID())->value();
163 bool anMaterialsGroupSelected =
164 boolean(ExchangePlugin_ImportFeature::STEP_MATERIALS_ID())->value();
166 if (anExtension == "BREP" || anExtension == "BRP") {
167 aGeomShape = BREPImport(theFileName, anExtension, anError);
168 } else if (anExtension == "STEP" || anExtension == "STP") {
169 bool anScalInterUnits =
170 boolean(ExchangePlugin_ImportFeature::STEP_SCALE_INTER_UNITS_ID())->value();
172 // Process groups/fields
173 std::shared_ptr<ModelAPI_AttributeRefList> aRefListOfGroups =
174 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(data()->attribute(FEATURES_ID()));
176 // Remove previous groups/fields stored in RefList
177 std::list<ObjectPtr> anGroupList = aRefListOfGroups->list();
178 std::list<ObjectPtr>::iterator anGroupIt = anGroupList.begin();
179 for (; anGroupIt != anGroupList.end(); ++anGroupIt) {
180 std::shared_ptr<ModelAPI_Feature> aFeature = ModelAPI_Feature::feature(*anGroupIt);
182 document()->removeFeature(aFeature);
185 aGeomShape = STEPImportAttributs(theFileName, aResult, anScalInterUnits,
186 anMaterialsGroupSelected, anColorGroupSelected,
187 theMaterialShape, anError);
188 } else if (anExtension == "IGES" || anExtension == "IGS") {
189 aGeomShape = IGESImport(theFileName, anExtension, anError);
191 anError = "Unsupported format: " + anExtension;
194 // Check if shape is valid
195 if (!anError.empty()) {
196 setError("An error occurred while importing " + theFileName + ": " + anError);
200 // Pass the results into the model
202 loadNamingDS(aGeomShape, aResult);
204 // create color group
205 if (anColorGroupSelected)
207 setColorGroups(aResult);
210 // create Materiel group
211 if (anMaterialsGroupSelected){
212 setMaterielGroup(aResult,theMaterialShape);
216 aResult->clearShapeNameAndColor();
220 void ExchangePlugin_ImportFeature::setColorGroups(
221 std::shared_ptr<ModelAPI_ResultBody> theResultBody)
223 std::vector<int> aColor;
225 std::list<std::vector<int>> aColorsRead;
227 ModelAPI_Tools::getColor(theResultBody, aColor);
228 if (!aColor.empty() ){
229 std::wstringstream aColorName;
230 aColorName <<L"Color_"<< anIndice;
231 setColorGroup(theResultBody, aColor, aColorName.str());
233 aColorsRead.push_back(aColor);
236 std::list<ResultPtr> allRes;
237 ModelAPI_Tools::allSubs(theResultBody, allRes);
238 for(std::list<ResultPtr>::iterator aRes = allRes.begin(); aRes != allRes.end(); ++aRes) {
239 ModelAPI_Tools::getColor(*aRes, aColor);
240 if (!aColor.empty() ){
241 auto it = std::find(aColorsRead.begin(), aColorsRead.end(), aColor);
242 if ( it == aColorsRead.end() ){
243 std::wstringstream aColorName;
244 aColorName<<L"Color_"<< anIndice;
245 setColorGroup(theResultBody, aColor, aColorName.str());
247 aColorsRead.push_back(aColor);
253 void ExchangePlugin_ImportFeature::setColorGroup(
254 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
255 std::vector<int> &theColor,
256 const std::wstring& theName )
258 std::vector<int> aColor;
259 std::shared_ptr<ModelAPI_Feature> aGroupFeature = addFeature("Group");
262 aGroupFeature->data()->setName(theName);
265 AttributeSelectionListPtr aSelectionList = aGroupFeature->selectionList("group_list");
267 ModelAPI_Tools::getColor(theResultBody, aColor);
268 if (!aColor.empty()){
269 if (aColor == theColor) {
270 GeomShapePtr aShape = theResultBody->shape();
271 aSelectionList->setSelectionType(aShape->shapeTypeStr());
272 aSelectionList->append(theResultBody,aShape);
275 // add element with the same color
276 std::list<ResultPtr> allRes;
277 ModelAPI_Tools::allSubs(theResultBody, allRes);
278 for(std::list<ResultPtr>::iterator aRes = allRes.begin();
279 aRes != allRes.end(); ++aRes) {
280 ModelAPI_Tools::getColor(*aRes, aColor);
281 GeomShapePtr aShape = (*aRes)->shape();
283 if (!aColor.empty()){
284 if (aRes->get() && aColor == theColor) {
285 aSelectionList->setSelectionType(aShape->shapeTypeStr());
286 aSelectionList->append(theResultBody,aShape);
291 // Create the group in the document to be able to set its color
292 ResultPtr aGroup = document()->createGroup(aGroupFeature->data());
293 aGroupFeature->setResult(aGroup);
295 ModelAPI_Tools::setColor(aGroupFeature->lastResult(),theColor);
297 if (aSelectionList->size() == 0) {
298 document()->removeFeature(aGroupFeature);
302 void ExchangePlugin_ImportFeature::setMaterielGroup(
303 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
304 std::map< std::wstring,std::list<std::wstring>>& theMaterialShape)
306 std::map< std::wstring, std::list<std::wstring>>::iterator anIt;
307 for (anIt = theMaterialShape.begin(); anIt != theMaterialShape.end(); ++anIt) {
309 std::shared_ptr<ModelAPI_Feature> aGroupFeature = addFeature("Group");
311 aGroupFeature->data()->setName((*anIt).first);
314 AttributeSelectionListPtr aSelectionList = aGroupFeature->selectionList("group_list");
316 std::list<ResultPtr> allRes;
317 ModelAPI_Tools::allSubs(theResultBody, allRes);
318 for (std::list<ResultPtr>::iterator aRes = allRes.begin(); aRes != allRes.end(); ++aRes) {
320 GeomShapePtr aShape = (*aRes)->shape();
321 for (std::list<std::wstring>::iterator aResMat = anIt->second.begin();
322 aResMat != anIt->second.end(); ++aResMat) {
323 if (aRes->get() && ((*aRes)->data()->name() == (*aResMat)))
325 aSelectionList->append(theResultBody,aShape);
326 aSelectionList->setSelectionType(aShape->shapeTypeStr());
331 if (aSelectionList->size() == 0){
332 document()->removeFeature(aGroupFeature);
337 void ExchangePlugin_ImportFeature::importXAO(const std::string& theFileName)
343 std::shared_ptr<GeomAPI_Shape> aGeomShape = XAOImport(theFileName, anError, &aXao);
345 if (!anError.empty()) {
346 setError("An error occurred while importing " + theFileName + ": " + anError);
350 XAO::Geometry* aXaoGeometry = aXao.getGeometry();
352 // use the geometry name or the file name for the feature
353 std::string aBodyName = aXaoGeometry->getName();
354 if (aBodyName.empty())
355 aBodyName = GeomAlgoAPI_Tools::File_Tools::name(theFileName);
356 data()->setName(Locale::Convert::toWString(aBodyName));
358 ResultBodyPtr aResultBody = createResultBody(aGeomShape);
359 setResult(aResultBody);
361 // Process groups/fields
362 std::shared_ptr<ModelAPI_AttributeRefList> aRefListOfGroups =
363 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(data()->attribute(FEATURES_ID()));
365 // Remove previous groups/fields stored in RefList
366 std::list<ObjectPtr> anGroupList = aRefListOfGroups->list();
367 std::list<ObjectPtr>::iterator anGroupIt = anGroupList.begin();
368 for (; anGroupIt != anGroupList.end(); ++anGroupIt) {
369 std::shared_ptr<ModelAPI_Feature> aFeature = ModelAPI_Feature::feature(*anGroupIt);
371 document()->removeFeature(aFeature);
375 for (int aGroupIndex = 0; aGroupIndex < aXao.countGroups(); ++aGroupIndex) {
376 XAO::Group* aXaoGroup = aXao.getGroup(aGroupIndex);
378 std::shared_ptr<ModelAPI_Feature> aGroupFeature = addFeature("Group");
381 if (!aXaoGroup->getName().empty())
382 aGroupFeature->data()->setName(Locale::Convert::toWString(aXaoGroup->getName()));
385 AttributeSelectionListPtr aSelectionList = aGroupFeature->selectionList("group_list");
387 // conversion of dimension
388 std::string aDimensionString = XAO::XaoUtils::dimensionToString(aXaoGroup->getDimension());
389 std::string aSelectionType =
390 ExchangePlugin_Tools::xaoDimension2selectionType(aDimensionString);
392 aSelectionList->setSelectionType(aSelectionType);
393 for (int anElementIndex = 0; anElementIndex < aXaoGroup->count(); ++anElementIndex) {
394 aSelectionList->append(aResultBody, GeomShapePtr());
395 // complex conversion of element index to reference id
396 int anElementID = aXaoGroup->get(anElementIndex);
397 std::string aReferenceString =
398 aXaoGeometry->getElementReference(aXaoGroup->getDimension(), anElementID);
399 int aReferenceID = XAO::XaoUtils::stringToInt(aReferenceString);
401 aSelectionList->value(anElementIndex)->setId(aReferenceID);
405 for (int aFieldIndex = 0; aFieldIndex < aXao.countFields(); ++aFieldIndex) {
406 XAO::Field* aXaoField = aXao.getField(aFieldIndex);
408 std::shared_ptr<ModelAPI_Feature> aFieldFeature = addFeature("Field");
411 if (!aXaoField->getName().empty())
412 aFieldFeature->data()->setName(Locale::Convert::toWString(aXaoField->getName()));
415 AttributeSelectionListPtr aSelectionList = aFieldFeature->selectionList("selected");
417 // conversion of dimension
418 std::string aDimensionString = XAO::XaoUtils::dimensionToString(aXaoField->getDimension());
419 std::string aSelectionType =
420 ExchangePlugin_Tools::xaoDimension2selectionType(aDimensionString);
421 aSelectionList->setSelectionType(aSelectionType);
422 // limitation: now in XAO fields are related to everything, so, iterate all sub-shapes to fill
423 int aCountSelected = aXaoField->countElements();
424 std::list<ResultPtr>::const_iterator aResIter = results().begin();
425 for(; aResIter != results().end() && aCountSelected; aResIter++) {
426 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aResIter);
429 // check that only results that were created before this field are used
430 FeaturePtr aResultFeature = document()->feature(aBody);
431 if (!aResultFeature.get())
433 GeomShapePtr aShape = aBody->shape();
434 if (!aShape.get() || aShape->isNull())
436 GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::shapeTypeByStr(aSelectionType));
437 for(; anExp.more(); anExp.next()) {
438 aSelectionList->append(aBody, anExp.current());
440 if (aCountSelected == 0)
445 // conversion of type
446 XAO::Type aFieldType = aXaoField->getType();
447 std::string aTypeString = XAO::XaoUtils::fieldTypeToString(aFieldType);
448 ModelAPI_AttributeTables::ValueType aType =
449 ExchangePlugin_Tools::xaoType2valuesType(aTypeString);
450 // set components names
451 AttributeStringArrayPtr aComponents = aFieldFeature->stringArray("components_names");
452 aComponents->setSize(aXaoField->countComponents());
453 for(int aComp = 0; aComp < aXaoField->countComponents(); aComp++) {
454 aComponents->setValue(aComp, aXaoField->getComponentName(aComp));
457 AttributeIntArrayPtr aStamps = aFieldFeature->intArray("stamps");
458 aStamps->setSize(aXaoField->countSteps());
459 std::shared_ptr<ModelAPI_AttributeTables> aTables = aFieldFeature->tables("values");
461 aXaoField->countElements() + 1, aXaoField->countComponents(), aXaoField->countSteps());
462 aTables->setType(aType);
464 XAO::stepIterator aStepIter = aXaoField->begin();
465 for(int aStepIndex = 0; aStepIter != aXaoField->end(); aStepIter++, aStepIndex++) {
466 aStamps->setValue(aStepIndex, (*aStepIter)->getStamp());
467 for(int aRow = 1; aRow <= aXaoField->countElements(); aRow++) {
468 for(int aCol = 0; aCol < aXaoField->countComponents(); aCol++) {
469 ModelAPI_AttributeTables::Value aVal;
470 std::string aValStr = (*aStepIter)->getStringValue(aRow - 1, aCol);
472 case ModelAPI_AttributeTables::BOOLEAN:
473 aVal.myBool = aValStr == "true";
475 case ModelAPI_AttributeTables::INTEGER:
476 aVal.myInt = atoi(aValStr.c_str());
478 case ModelAPI_AttributeTables::DOUBLE:
479 aVal.myDouble = atof(aValStr.c_str());
481 case ModelAPI_AttributeTables::STRING:
482 aVal.myStr = aValStr;
485 aTables->setValue(aVal, aRow, aCol, aStepIndex);
489 // remove everything with zero-values: zeroes are treated as defaults
490 std::set<int> aRowsToRemove;
491 for(int aRow = 1; aRow < aTables->rows(); aRow++) {
493 for(int aCol = 0; aCol < aTables->columns() && isZero; aCol++) {
494 for(int aStepIndex = 0; aStepIndex != aTables->tables() && isZero; aStepIndex++) {
495 if (aTables->valueStr(aRow, aCol, aStepIndex) != aTables->valueStr(0, aCol, aStepIndex))
500 aRowsToRemove.insert(aRow - 1); // -1 to make prepared for remove from SelectionList
502 if (!aRowsToRemove.empty()) { // move usefull rows on bottom to the up of the tables
503 // number of rows passed during going through: the current rows will
504 // be moved up for this value
505 int aRemovedPassed = 0;
506 for(int aRow = 1; aRow < aTables->rows(); aRow++) {
507 if (aRowsToRemove.find(aRow - 1) != aRowsToRemove.end()) {
509 } else if (aRemovedPassed != 0) { // copy the line up
510 for(int aCol = 0; aCol < aTables->columns(); aCol++) {
511 for(int aTable = 0; aTable != aTables->tables(); aTable++) {
513 aTables->value(aRow, aCol, aTable), aRow - aRemovedPassed, aCol, aTable);
518 aTables->setSize(aTables->rows() - aRemovedPassed, aTables->columns(), aTables->tables());
519 aSelectionList->remove(aRowsToRemove); // remove also selected elements
522 // Top avoid problems in Object Browser update: issue #1647.
523 ModelAPI_EventCreator::get()->sendReordered(
524 std::dynamic_pointer_cast<ModelAPI_Feature>(aRefListOfGroups->owner()));
527 } catch (XAO::XAO_Exception& e) {
528 std::string anError = e.what();
529 setError("An error occurred while importing " + theFileName + ": " + anError);
535 //============================================================================
536 std::shared_ptr<ModelAPI_Feature> ExchangePlugin_ImportFeature::addFeature(
539 std::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID, false);
541 data()->reflist(FEATURES_ID())->append(aNew);
542 // set as current also after it becomes sub to set correctly enabled for other subs
543 //document()->setCurrentFeature(aNew, false);
548 void ExchangePlugin_ImportFeature::removeFeature(
549 std::shared_ptr<ModelAPI_Feature> theFeature)
551 if (!data()->isValid())
553 AttributeRefListPtr aList = reflist(FEATURES_ID());
554 aList->remove(theFeature);
558 int ExchangePlugin_ImportFeature::numberOfSubs(bool /*forTree*/) const
560 return data()->reflist(FEATURES_ID())->size(true);
563 std::shared_ptr<ModelAPI_Feature> ExchangePlugin_ImportFeature::subFeature(
564 const int theIndex, bool /*forTree*/)
566 ObjectPtr anObj = data()->reflist(FEATURES_ID())->object(theIndex, false);
567 FeaturePtr aRes = std::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
572 int ExchangePlugin_ImportFeature::subFeatureId(const int theIndex) const
574 std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
575 ModelAPI_AttributeRefList>(data()->attribute(FEATURES_ID()));
576 std::list<ObjectPtr> aFeatures = aRefList->list();
577 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin();
578 int aResultIndex = 1; // number of the counted (created) features, started from 1
579 int aFeatureIndex = -1; // number of the not-empty features in the list
580 for (; anIt != aFeatures.end(); anIt++) {
583 if (aFeatureIndex == theIndex)
591 bool ExchangePlugin_ImportFeature::isSub(ObjectPtr theObject) const
593 // check is this feature of result
594 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
596 return data()->reflist(FEATURES_ID())->isInList(aFeature);
600 //============================================================================
601 void ExchangePlugin_ImportFeature::loadNamingDS(
602 std::shared_ptr<GeomAPI_Shape> theGeomShape,
603 std::shared_ptr<ModelAPI_ResultBody> theResultBody)
606 theResultBody->store(theGeomShape);
607 std::string aNameMS = "Shape";
608 theResultBody->loadFirstLevel(theGeomShape, aNameMS);