1 // Copyright (C) 2014-2022 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>
34 #include <GeomAlgoAPI_ImageImport.h>
36 #include <GeomAPI_Shape.h>
37 #include <GeomAPI_Face.h>
38 #include <GeomAPI_ShapeExplorer.h>
39 #include <GeomAPI_ShapeIterator.h>
41 #include <Locale_Convert.h>
43 #include <ModelAPI_AttributeRefList.h>
44 #include <ModelAPI_AttributeSelectionList.h>
45 #include <ModelAPI_AttributeString.h>
46 #include <ModelAPI_AttributeStringArray.h>
47 #include <ModelAPI_AttributeIntArray.h>
48 #include <ModelAPI_AttributeImage.h>
49 #include <ModelAPI_AttributeTables.h>
50 #include <ModelAPI_AttributeBoolean.h>
51 #include <ModelAPI_AttributeInteger.h>
52 #include <ModelAPI_BodyBuilder.h>
53 #include <ModelAPI_Data.h>
54 #include <ModelAPI_Document.h>
55 #include <ModelAPI_Events.h>
56 #include <ModelAPI_Object.h>
57 #include <ModelAPI_ResultBody.h>
58 #include <ModelAPI_ResultGroup.h>
59 #include <ModelAPI_Session.h>
60 #include <ModelAPI_Validator.h>
61 #include <ModelAPI_Tools.h>
63 #include <XAO_Xao.hxx>
64 #include <XAO_Group.hxx>
65 #include <XAO_Field.hxx>
66 #include <XAO_Step.hxx>
68 #include <ExchangePlugin_Tools.h>
73 * Request for initialization of data model of the feature: adding all attributes
75 void ExchangePlugin_ImportFeatureBase::initAttributes()
77 data()->addAttribute(ExchangePlugin_ImportFeatureBase::FILE_PATH_ID(),
78 ModelAPI_AttributeString::typeId());
79 AttributePtr aFeaturesAttribute =
80 data()->addAttribute(ExchangePlugin_ImportFeatureBase::FEATURES_ID(),
81 ModelAPI_AttributeRefList::typeId());
82 aFeaturesAttribute->setIsArgument(false);
84 ModelAPI_Session::get()->validators()->registerNotObligatory(
85 getKind(), ExchangePlugin_ImportFeatureBase::FEATURES_ID());
88 void ExchangePlugin_ImportFeature::initAttributes()
90 ExchangePlugin_ImportFeatureBase::initAttributes();
92 data()->addAttribute(STEP_FILE_PATH_ID(), ModelAPI_AttributeString::typeId());
93 data()->addAttribute(IMPORT_TYPE_ID(), ModelAPI_AttributeString::typeId());
94 data()->addAttribute(STEP_MATERIALS_ID(), ModelAPI_AttributeBoolean::typeId());
95 data()->addAttribute(STEP_COLORS_ID(), ModelAPI_AttributeBoolean::typeId());
96 data()->addAttribute(STEP_SCALE_INTER_UNITS_ID(), ModelAPI_AttributeBoolean::typeId());
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());
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 void ExchangePlugin_Import_ImageFeature::execute()
134 AttributeStringPtr aFilePathAttr = string(ExchangePlugin_Import_ImageFeature::FILE_PATH_ID());
135 std::string aFilePath = aFilePathAttr->value();
136 if (aFilePath.empty()) {
137 setError("File path is empty.");
140 importFile(aFilePath);
143 std::shared_ptr<ModelAPI_ResultBody> ExchangePlugin_ImportFeatureBase::createResultBody(
144 std::shared_ptr<GeomAPI_Shape> aGeomShape)
146 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
147 //LoadNamingDS of the imported shape
148 loadNamingDS(aGeomShape, aResultBody);
152 void ExchangePlugin_ImportFeature::importFile(const std::string& theFileName)
154 // "*.brep" -> "BREP"
155 std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(theFileName);
157 if (anExtension == "XAO") {
158 importXAO(theFileName);
162 // Perform the import
164 std::shared_ptr<GeomAPI_Shape> aGeomShape;
165 std::map<std::wstring, std::list<std::wstring>> theMaterialShape;
167 std::string anObjectName = GeomAlgoAPI_Tools::File_Tools::name(theFileName);
168 data()->setName(Locale::Convert::toWString(anObjectName));
170 ResultBodyPtr aResult = document()->createBody(data());
171 bool anColorGroupSelected = false, anMaterialsGroupSelected = false;
172 if (anExtension == "BREP" || anExtension == "BRP") {
173 aGeomShape = BREPImport(theFileName, anExtension, anError);
174 } else if (anExtension == "STEP" || anExtension == "STP") {
175 bool anScalInterUnits = boolean(STEP_SCALE_INTER_UNITS_ID())->value();
176 anColorGroupSelected = boolean(STEP_COLORS_ID())->value();
177 anMaterialsGroupSelected = boolean(STEP_MATERIALS_ID())->value();
179 // Process groups/fields
180 AttributeRefListPtr aRefListOfGroups = reflist(FEATURES_ID());
182 // Remove previous groups/fields stored in RefList
183 std::list<ObjectPtr> anGroupList = aRefListOfGroups->list();
184 std::list<ObjectPtr>::iterator anGroupIt = anGroupList.begin();
185 for (; anGroupIt != anGroupList.end(); ++anGroupIt) {
186 std::shared_ptr<ModelAPI_Feature> aFeature = ModelAPI_Feature::feature(*anGroupIt);
188 document()->removeFeature(aFeature);
191 aGeomShape = STEPImportAttributs(theFileName, aResult, anScalInterUnits,
192 anMaterialsGroupSelected, anColorGroupSelected,
193 theMaterialShape, anError);
194 } else if (anExtension == "IGES" || anExtension == "IGS") {
195 aGeomShape = IGESImport(theFileName, anExtension, anError);
196 } else if (anExtension == "STL") {
197 aGeomShape = STLImport(theFileName, anError);
199 anError = "Unsupported format: " + anExtension;
202 // Check if shape is valid
203 if (!anError.empty()) {
204 setError("An error occurred while importing " + theFileName + ": " + anError);
208 // Pass the results into the model
209 loadNamingDS(aGeomShape, aResult);
211 // create color group
212 if (anColorGroupSelected)
214 setColorGroups(aResult);
217 // create Material group
218 if (anMaterialsGroupSelected){
219 setMaterielGroup(aResult,theMaterialShape);
223 aResult->clearShapeNameAndColor();
227 void ExchangePlugin_ImportFeature::setColorGroups(
228 std::shared_ptr<ModelAPI_ResultBody> theResultBody)
230 std::vector<int> aColor;
232 std::list<std::vector<int>> aColorsRead;
234 ModelAPI_Tools::getColor(theResultBody, aColor);
235 if (!aColor.empty() ){
236 std::wstringstream aColorName;
237 aColorName <<L"Color_"<< anIndice;
238 setColorGroup(theResultBody, aColor, aColorName.str());
240 aColorsRead.push_back(aColor);
243 std::list<ResultPtr> allRes;
244 ModelAPI_Tools::allSubs(theResultBody, allRes);
245 for(std::list<ResultPtr>::iterator aRes = allRes.begin(); aRes != allRes.end(); ++aRes) {
246 ModelAPI_Tools::getColor(*aRes, aColor);
247 if (!aColor.empty() ){
248 auto it = std::find(aColorsRead.begin(), aColorsRead.end(), aColor);
249 if ( it == aColorsRead.end() ){
250 std::wstringstream aColorName;
251 aColorName<<L"Color_"<< anIndice;
252 setColorGroup(theResultBody, aColor, aColorName.str());
254 aColorsRead.push_back(aColor);
260 void ExchangePlugin_ImportFeature::setColorGroup(
261 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
262 std::vector<int> &theColor,
263 const std::wstring& theName )
265 std::vector<int> aColor;
266 std::shared_ptr<ModelAPI_Feature> aGroupFeature = addFeature("Group");
269 aGroupFeature->data()->setName(theName);
272 AttributeSelectionListPtr aSelectionList = aGroupFeature->selectionList("group_list");
274 ModelAPI_Tools::getColor(theResultBody, aColor);
275 if (!aColor.empty()){
276 if (aColor == theColor) {
277 GeomShapePtr aShape = theResultBody->shape();
278 aSelectionList->setSelectionType(aShape->shapeTypeStr());
279 aSelectionList->append(theResultBody,aShape);
282 // add element with the same color
283 std::list<ResultPtr> allRes;
284 ModelAPI_Tools::allSubs(theResultBody, allRes);
285 for(std::list<ResultPtr>::iterator aRes = allRes.begin();
286 aRes != allRes.end(); ++aRes) {
287 ModelAPI_Tools::getColor(*aRes, aColor);
288 GeomShapePtr aShape = (*aRes)->shape();
290 if (!aColor.empty()){
291 if (aRes->get() && aColor == theColor) {
292 if (aShape->isCompound() || aShape->isCompSolid()) {
293 GeomAPI_ShapeIterator anIt(aShape);
294 for (; anIt.more(); anIt.next()) {
295 aSelectionList->setSelectionType(anIt.current()->shapeTypeStr());
296 aSelectionList->append(theResultBody,anIt.current());
299 aSelectionList->setSelectionType(aShape->shapeTypeStr());
300 aSelectionList->append(theResultBody,aShape);
306 // Create the group in the document to be able to set its color
307 ResultPtr aGroup = document()->createGroup(aGroupFeature->data());
308 aGroupFeature->setResult(aGroup);
310 ModelAPI_Tools::setColor(aGroupFeature->lastResult(),theColor);
312 if (aSelectionList->size() == 0) {
313 document()->removeFeature(aGroupFeature);
317 void ExchangePlugin_ImportFeature::setMaterielGroup(
318 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
319 std::map< std::wstring,std::list<std::wstring>>& theMaterialShape)
321 std::map< std::wstring, std::list<std::wstring>>::iterator anIt;
322 for (anIt = theMaterialShape.begin(); anIt != theMaterialShape.end(); ++anIt) {
324 std::shared_ptr<ModelAPI_Feature> aGroupFeature = addFeature("Group");
326 aGroupFeature->data()->setName((*anIt).first);
329 AttributeSelectionListPtr aSelectionList = aGroupFeature->selectionList("group_list");
331 std::list<ResultPtr> allRes;
332 ModelAPI_Tools::allSubs(theResultBody, allRes);
333 for (std::list<ResultPtr>::iterator aRes = allRes.begin(); aRes != allRes.end(); ++aRes) {
335 GeomShapePtr aShape = (*aRes)->shape();
336 for (std::list<std::wstring>::iterator aResMat = anIt->second.begin();
337 aResMat != anIt->second.end(); ++aResMat) {
338 if (aRes->get() && ((*aRes)->data()->name() == (*aResMat)))
340 if (aShape->isCompound() || aShape->isCompSolid()) {
341 GeomAPI_ShapeIterator aShapeIt(aShape);
342 for (; aShapeIt.more(); aShapeIt.next()) {
343 aSelectionList->setSelectionType(aShapeIt.current()->shapeTypeStr());
344 aSelectionList->append(theResultBody, aShapeIt.current());
347 aSelectionList->setSelectionType(aShape->shapeTypeStr());
348 aSelectionList->append(theResultBody,aShape);
354 if (aSelectionList->size() == 0){
355 document()->removeFeature(aGroupFeature);
360 void ExchangePlugin_ImportFeature::importXAO(const std::string& theFileName)
366 std::shared_ptr<GeomAPI_Shape> aGeomShape = XAOImport(theFileName, anError, &aXao);
368 if (!anError.empty()) {
369 setError("An error occurred while importing " + theFileName + ": " + anError);
373 XAO::Geometry* aXaoGeometry = aXao.getGeometry();
375 // use the geometry name or the file name for the feature
376 std::string aBodyName = aXaoGeometry->getName();
377 if (aBodyName.empty())
378 aBodyName = GeomAlgoAPI_Tools::File_Tools::name(theFileName);
379 data()->setName(Locale::Convert::toWString(aBodyName));
381 ResultBodyPtr aResultBody = createResultBody(aGeomShape);
382 setResult(aResultBody);
384 // Process groups/fields
385 std::shared_ptr<ModelAPI_AttributeRefList> aRefListOfGroups =
386 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(data()->attribute(FEATURES_ID()));
388 // Remove previous groups/fields stored in RefList
389 std::list<ObjectPtr> anGroupList = aRefListOfGroups->list();
390 std::list<ObjectPtr>::iterator anGroupIt = anGroupList.begin();
391 for (; anGroupIt != anGroupList.end(); ++anGroupIt) {
392 std::shared_ptr<ModelAPI_Feature> aFeature = ModelAPI_Feature::feature(*anGroupIt);
394 document()->removeFeature(aFeature);
398 for (int aGroupIndex = 0; aGroupIndex < aXao.countGroups(); ++aGroupIndex) {
399 XAO::Group* aXaoGroup = aXao.getGroup(aGroupIndex);
401 std::shared_ptr<ModelAPI_Feature> aGroupFeature = addFeature("Group");
404 if (!aXaoGroup->getName().empty())
405 aGroupFeature->data()->setName(Locale::Convert::toWString(aXaoGroup->getName()));
408 AttributeSelectionListPtr aSelectionList = aGroupFeature->selectionList("group_list");
410 // conversion of dimension
411 std::string aDimensionString = XAO::XaoUtils::dimensionToString(aXaoGroup->getDimension());
412 std::string aSelectionType =
413 ExchangePlugin_Tools::xaoDimension2selectionType(aDimensionString);
415 aSelectionList->setSelectionType(aSelectionType);
416 for (int anElementIndex = 0; anElementIndex < aXaoGroup->count(); ++anElementIndex) {
417 aSelectionList->append(aResultBody, GeomShapePtr());
418 // complex conversion of element index to reference id
419 int anElementID = aXaoGroup->get(anElementIndex);
420 std::string aReferenceString =
421 aXaoGeometry->getElementReference(aXaoGroup->getDimension(), anElementID);
422 int aReferenceID = XAO::XaoUtils::stringToInt(aReferenceString);
424 aSelectionList->value(anElementIndex)->setId(aReferenceID);
428 for (int aFieldIndex = 0; aFieldIndex < aXao.countFields(); ++aFieldIndex) {
429 XAO::Field* aXaoField = aXao.getField(aFieldIndex);
431 std::shared_ptr<ModelAPI_Feature> aFieldFeature = addFeature("Field");
434 if (!aXaoField->getName().empty())
435 aFieldFeature->data()->setName(Locale::Convert::toWString(aXaoField->getName()));
438 AttributeSelectionListPtr aSelectionList = aFieldFeature->selectionList("selected");
440 // conversion of dimension
441 std::string aDimensionString = XAO::XaoUtils::dimensionToString(aXaoField->getDimension());
442 std::string aSelectionType =
443 ExchangePlugin_Tools::xaoDimension2selectionType(aDimensionString);
444 aSelectionList->setSelectionType(aSelectionType);
445 // limitation: now in XAO fields are related to everything, so, iterate all sub-shapes to fill
446 int aCountSelected = aXaoField->countElements();
447 std::list<ResultPtr>::const_iterator aResIter = results().begin();
448 for(; aResIter != results().end() && aCountSelected; aResIter++) {
449 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aResIter);
452 // check that only results that were created before this field are used
453 FeaturePtr aResultFeature = document()->feature(aBody);
454 if (!aResultFeature.get())
456 GeomShapePtr aShape = aBody->shape();
457 if (!aShape.get() || aShape->isNull())
459 GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::shapeTypeByStr(aSelectionType));
460 for(; anExp.more(); anExp.next()) {
461 aSelectionList->append(aBody, anExp.current());
463 if (aCountSelected == 0)
468 // conversion of type
469 XAO::Type aFieldType = aXaoField->getType();
470 std::string aTypeString = XAO::XaoUtils::fieldTypeToString(aFieldType);
471 ModelAPI_AttributeTables::ValueType aType =
472 ExchangePlugin_Tools::xaoType2valuesType(aTypeString);
473 // set components names
474 AttributeStringArrayPtr aComponents = aFieldFeature->stringArray("components_names");
475 aComponents->setSize(aXaoField->countComponents());
476 for(int aComp = 0; aComp < aXaoField->countComponents(); aComp++) {
477 aComponents->setValue(aComp, aXaoField->getComponentName(aComp));
480 AttributeIntArrayPtr aStamps = aFieldFeature->intArray("stamps");
481 aStamps->setSize(aXaoField->countSteps());
482 std::shared_ptr<ModelAPI_AttributeTables> aTables = aFieldFeature->tables("values");
484 aXaoField->countElements() + 1, aXaoField->countComponents(), aXaoField->countSteps());
485 aTables->setType(aType);
487 XAO::stepIterator aStepIter = aXaoField->begin();
488 for(int aStepIndex = 0; aStepIter != aXaoField->end(); aStepIter++, aStepIndex++) {
489 aStamps->setValue(aStepIndex, (*aStepIter)->getStamp());
490 for(int aRow = 1; aRow <= aXaoField->countElements(); aRow++) {
491 for(int aCol = 0; aCol < aXaoField->countComponents(); aCol++) {
492 ModelAPI_AttributeTables::Value aVal;
493 std::string aValStr = (*aStepIter)->getStringValue(aRow - 1, aCol);
495 case ModelAPI_AttributeTables::BOOLEAN:
496 aVal.myBool = aValStr == "true";
498 case ModelAPI_AttributeTables::INTEGER:
499 aVal.myInt = atoi(aValStr.c_str());
501 case ModelAPI_AttributeTables::DOUBLE:
502 aVal.myDouble = atof(aValStr.c_str());
504 case ModelAPI_AttributeTables::STRING:
505 aVal.myStr = aValStr;
508 aTables->setValue(aVal, aRow, aCol, aStepIndex);
512 // remove everything with zero-values: zeroes are treated as defaults
513 std::set<int> aRowsToRemove;
514 for(int aRow = 1; aRow < aTables->rows(); aRow++) {
516 for(int aCol = 0; aCol < aTables->columns() && isZero; aCol++) {
517 for(int aStepIndex = 0; aStepIndex != aTables->tables() && isZero; aStepIndex++) {
518 if (aTables->valueStr(aRow, aCol, aStepIndex) != aTables->valueStr(0, aCol, aStepIndex))
523 aRowsToRemove.insert(aRow - 1); // -1 to make prepared for remove from SelectionList
525 if (!aRowsToRemove.empty()) { // move usefull rows on bottom to the up of the tables
526 // number of rows passed during going through: the current rows will
527 // be moved up for this value
528 int aRemovedPassed = 0;
529 for(int aRow = 1; aRow < aTables->rows(); aRow++) {
530 if (aRowsToRemove.find(aRow - 1) != aRowsToRemove.end()) {
532 } else if (aRemovedPassed != 0) { // copy the line up
533 for(int aCol = 0; aCol < aTables->columns(); aCol++) {
534 for(int aTable = 0; aTable != aTables->tables(); aTable++) {
536 aTables->value(aRow, aCol, aTable), aRow - aRemovedPassed, aCol, aTable);
541 aTables->setSize(aTables->rows() - aRemovedPassed, aTables->columns(), aTables->tables());
542 aSelectionList->remove(aRowsToRemove); // remove also selected elements
545 // Top avoid problems in Object Browser update: issue #1647.
546 ModelAPI_EventCreator::get()->sendReordered(
547 std::dynamic_pointer_cast<ModelAPI_Feature>(aRefListOfGroups->owner()));
550 } catch (XAO::XAO_Exception& e) {
551 std::string anError = e.what();
552 setError("An error occurred while importing " + theFileName + ": " + anError);
558 //============================================================================
559 std::shared_ptr<ModelAPI_Feature> ExchangePlugin_ImportFeatureBase::addFeature(
562 std::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID, false);
564 data()->reflist(FEATURES_ID())->append(aNew);
565 // set as current also after it becomes sub to set correctly enabled for other subs
566 //document()->setCurrentFeature(aNew, false);
571 void ExchangePlugin_ImportFeatureBase::removeFeature(
572 std::shared_ptr<ModelAPI_Feature> theFeature)
574 if (!data()->isValid())
576 AttributeRefListPtr aList = reflist(FEATURES_ID());
577 aList->remove(theFeature);
581 int ExchangePlugin_ImportFeatureBase::numberOfSubs(bool /*forTree*/) const
583 return data()->reflist(FEATURES_ID())->size(true);
586 std::shared_ptr<ModelAPI_Feature> ExchangePlugin_ImportFeatureBase::subFeature(
587 const int theIndex, bool /*forTree*/)
589 ObjectPtr anObj = data()->reflist(FEATURES_ID())->object(theIndex, false);
590 FeaturePtr aRes = std::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
595 int ExchangePlugin_ImportFeatureBase::subFeatureId(const int theIndex) const
597 std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
598 ModelAPI_AttributeRefList>(data()->attribute(FEATURES_ID()));
599 std::list<ObjectPtr> aFeatures = aRefList->list();
600 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin();
601 int aResultIndex = 1; // number of the counted (created) features, started from 1
602 int aFeatureIndex = -1; // number of the not-empty features in the list
603 for (; anIt != aFeatures.end(); anIt++) {
606 if (aFeatureIndex == theIndex)
614 bool ExchangePlugin_ImportFeatureBase::isSub(ObjectPtr theObject) const
616 // check is this feature of result
617 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
619 return data()->reflist(FEATURES_ID())->isInList(aFeature);
623 //============================================================================
624 void ExchangePlugin_ImportFeatureBase::loadNamingDS(
625 std::shared_ptr<GeomAPI_Shape> theGeomShape,
626 std::shared_ptr<ModelAPI_ResultBody> theResultBody)
629 theResultBody->store(theGeomShape);
631 // to store color of higher-level shape
632 std::wstring aName = theResultBody->findShapeName(theGeomShape);
635 std::vector<int> aColor = theResultBody->findShapeColor(aName);
637 ModelAPI_Tools::setColor(theResultBody, aColor);
640 std::string aNameMS = "Shape";
641 theResultBody->loadFirstLevel(theGeomShape, aNameMS);
644 void ExchangePlugin_Import_ImageFeature::importFile(const std::string& theFileName)
646 std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(theFileName);
649 if (anExtension == "PNG" || anExtension == "GIF" ||
650 anExtension == "TIFF" || anExtension == "JPE" ||
651 anExtension == "JPEG" || anExtension == "JPG" ||
652 anExtension == "BMP" || anExtension == "PPM"
654 // Perform the import
655 QPixmap px (theFileName.c_str());
656 int aWidth = px.width();
657 int aHeight = px.height();
658 if (aWidth < 1 || aHeight < 1) {
659 setError("An error occurred while importing " + theFileName + ": invalid image");
663 std::shared_ptr<GeomAPI_Shape> aGeomShape = ImageImport(aWidth, aHeight, anError);
665 // Check if shape is valid
666 if (!anError.empty()) {
667 setError("An error occurred while importing " + theFileName + ": " + anError);
671 // Pass the results into the model
672 std::string anObjectName = GeomAlgoAPI_Tools::File_Tools::name(theFileName);
673 data()->setName(Locale::Convert::toWString(anObjectName));
675 auto resultBody = createResultBody(aGeomShape);
677 // Store image in result body attribute
678 AttributeImagePtr anImageAttr = resultBody->data()->image(ModelAPI_ResultBody::IMAGE_ID());
679 if (anImageAttr.get() != NULL) {
680 QImage aQImage = px.toImage();
681 const uchar* aImageBytes = aQImage.bits();
682 std::list<unsigned char> aByteArray (aImageBytes, aImageBytes + aQImage.sizeInBytes());
683 anImageAttr->setTexture(aWidth, aHeight, aByteArray, anExtension);
686 setResult(resultBody);
689 anError = "Unsupported format: " + anExtension;
690 setError("An error occurred while importing " + theFileName + ": " + anError);