1 // Copyright (C) 2014-2023 CEA, EDF
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());
97 data()->addAttribute(MEMORY_BUFFER_ID(), ModelAPI_AttributeString::typeId());
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());
109 ModelAPI_Session::get()->validators()->registerNotObligatory(
110 getKind(), ExchangePlugin_ImportFeature::MEMORY_BUFFER_ID());
113 bool ExchangePlugin_ImportFeature::isEditable()
115 AttributeStringPtr aImportTypeAttr = string(ExchangePlugin_ImportFeature::IMPORT_TYPE_ID());
116 std::string aFormat = aImportTypeAttr->value();
118 if (aFormat == "XAOMem")
119 // If the shape is imported from memory buffer, do not allow to edit the feature
126 * Computes or recomputes the results
128 void ExchangePlugin_ImportFeature::execute()
130 AttributeStringPtr aImportTypeAttr = string(ExchangePlugin_ImportFeature::IMPORT_TYPE_ID());
131 std::string aFormat = aImportTypeAttr->value();
133 if (aFormat == "XAOMem") {
134 // Import from memory buffer
135 AttributeStringPtr aMemoryBuffAttr =
136 string(ExchangePlugin_ImportFeature::MEMORY_BUFFER_ID());
137 std::string aMemoryBuff = aMemoryBuffAttr->value();
138 if (aMemoryBuff.empty()) {
139 setError("Memory buffer is empty.");
142 importXAO("", aMemoryBuff, true);
146 AttributeStringPtr aFilePathAttr;
147 if (aFormat == "STEP" || aFormat == "STP") {
148 aFilePathAttr = string(ExchangePlugin_ImportFeature::STEP_FILE_PATH_ID());
151 aFilePathAttr = string(ExchangePlugin_ImportFeature::FILE_PATH_ID());
153 std::string aFilePath = aFilePathAttr->value();
154 if (aFilePath.empty()) {
155 setError("File path is empty.");
158 importFile(aFilePath);
162 void ExchangePlugin_Import_ImageFeature::execute()
164 AttributeStringPtr aFilePathAttr = string(ExchangePlugin_Import_ImageFeature::FILE_PATH_ID());
165 std::string aFilePath = aFilePathAttr->value();
166 if (aFilePath.empty()) {
167 setError("File path is empty.");
170 importFile(aFilePath);
173 std::shared_ptr<ModelAPI_ResultBody> ExchangePlugin_ImportFeatureBase::createResultBody(
174 std::shared_ptr<GeomAPI_Shape> aGeomShape)
176 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
177 //LoadNamingDS of the imported shape
178 loadNamingDS(aGeomShape, aResultBody);
182 void ExchangePlugin_ImportFeature::importFile(const std::string& theFileName)
184 // "*.brep" -> "BREP"
185 std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(theFileName);
187 if (anExtension == "XAO") {
188 importXAO(theFileName);
192 // Perform the import
194 std::shared_ptr<GeomAPI_Shape> aGeomShape;
195 std::map<std::wstring, std::list<std::wstring>> theMaterialShape;
197 std::string anObjectName = GeomAlgoAPI_Tools::File_Tools::name(theFileName);
198 data()->setName(Locale::Convert::toWString(anObjectName));
200 ResultBodyPtr aResult = document()->createBody(data());
201 bool anColorGroupSelected = false, anMaterialsGroupSelected = false;
202 if (anExtension == "BREP" || anExtension == "BRP") {
203 aGeomShape = BREPImport(theFileName, anExtension, anError);
204 } else if (anExtension == "STEP" || anExtension == "STP") {
205 bool anScalInterUnits = boolean(STEP_SCALE_INTER_UNITS_ID())->value();
206 anColorGroupSelected = boolean(STEP_COLORS_ID())->value();
207 anMaterialsGroupSelected = boolean(STEP_MATERIALS_ID())->value();
209 // Process groups/fields
210 AttributeRefListPtr aRefListOfGroups = reflist(FEATURES_ID());
212 // Remove previous groups/fields stored in RefList
213 std::list<ObjectPtr> anGroupList = aRefListOfGroups->list();
214 std::list<ObjectPtr>::iterator anGroupIt = anGroupList.begin();
215 for (; anGroupIt != anGroupList.end(); ++anGroupIt) {
216 std::shared_ptr<ModelAPI_Feature> aFeature = ModelAPI_Feature::feature(*anGroupIt);
218 document()->removeFeature(aFeature);
221 aGeomShape = STEPImportAttributs(theFileName, aResult, anScalInterUnits,
222 anMaterialsGroupSelected, anColorGroupSelected,
223 theMaterialShape, anError);
224 } else if (anExtension == "IGES" || anExtension == "IGS") {
225 aGeomShape = IGESImport(theFileName, anExtension, anError);
226 } else if (anExtension == "STL") {
227 aGeomShape = STLImport(theFileName, anError);
229 anError = "Unsupported format: " + anExtension;
232 // Check if shape is valid
233 if (!anError.empty()) {
234 setError("An error occurred while importing " + theFileName + ": " + anError);
238 // Pass the results into the model
239 loadNamingDS(aGeomShape, aResult);
241 // create color group
242 if (anColorGroupSelected)
244 setColorGroups(aResult);
247 // create Material group
248 if (anMaterialsGroupSelected){
249 setMaterielGroup(aResult,theMaterialShape);
253 aResult->clearShapeNameAndColor();
257 void ExchangePlugin_ImportFeature::setColorGroups(
258 std::shared_ptr<ModelAPI_ResultBody> theResultBody)
260 std::vector<int> aColor;
262 std::list<std::vector<int>> aColorsRead;
264 ModelAPI_Tools::getColor(theResultBody, aColor);
265 if (!aColor.empty() ){
266 std::wstringstream aColorName;
267 aColorName <<L"Color_"<< anIndice;
268 setColorGroup(theResultBody, aColor, aColorName.str());
270 aColorsRead.push_back(aColor);
273 std::list<ResultPtr> allRes;
274 ModelAPI_Tools::allSubs(theResultBody, allRes);
275 for(std::list<ResultPtr>::iterator aRes = allRes.begin(); aRes != allRes.end(); ++aRes) {
276 ModelAPI_Tools::getColor(*aRes, aColor);
277 if (!aColor.empty() ){
278 auto it = std::find(aColorsRead.begin(), aColorsRead.end(), aColor);
279 if ( it == aColorsRead.end() ){
280 std::wstringstream aColorName;
281 aColorName<<L"Color_"<< anIndice;
282 setColorGroup(theResultBody, aColor, aColorName.str());
284 aColorsRead.push_back(aColor);
290 void ExchangePlugin_ImportFeature::setColorGroup(
291 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
292 std::vector<int> &theColor,
293 const std::wstring& theName )
295 std::vector<int> aColor;
296 std::shared_ptr<ModelAPI_Feature> aGroupFeature = addFeature("Group");
299 aGroupFeature->data()->setName(theName);
302 AttributeSelectionListPtr aSelectionList = aGroupFeature->selectionList("group_list");
304 ModelAPI_Tools::getColor(theResultBody, aColor);
305 if (!aColor.empty()){
306 if (aColor == theColor) {
307 GeomShapePtr aShape = theResultBody->shape();
308 aSelectionList->setSelectionType(aShape->shapeTypeStr());
309 aSelectionList->append(theResultBody,aShape);
312 // add element with the same color
313 std::list<ResultPtr> allRes;
314 ModelAPI_Tools::allSubs(theResultBody, allRes);
315 for(std::list<ResultPtr>::iterator aRes = allRes.begin();
316 aRes != allRes.end(); ++aRes) {
317 ModelAPI_Tools::getColor(*aRes, aColor);
318 GeomShapePtr aShape = (*aRes)->shape();
320 if (!aColor.empty()){
321 if (aRes->get() && aColor == theColor) {
322 if (aShape->isCompound() || aShape->isCompSolid()) {
323 GeomAPI_ShapeIterator anIt(aShape);
324 for (; anIt.more(); anIt.next()) {
325 aSelectionList->setSelectionType(anIt.current()->shapeTypeStr());
326 aSelectionList->append(theResultBody,anIt.current());
329 aSelectionList->setSelectionType(aShape->shapeTypeStr());
330 aSelectionList->append(theResultBody,aShape);
336 // Create the group in the document to be able to set its color
337 ResultPtr aGroup = document()->createGroup(aGroupFeature->data());
338 aGroupFeature->setResult(aGroup);
340 ModelAPI_Tools::setColor(aGroupFeature->lastResult(),theColor);
342 if (aSelectionList->size() == 0) {
343 document()->removeFeature(aGroupFeature);
347 void ExchangePlugin_ImportFeature::setMaterielGroup(
348 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
349 std::map< std::wstring,std::list<std::wstring>>& theMaterialShape)
351 std::map< std::wstring, std::list<std::wstring>>::iterator anIt;
352 for (anIt = theMaterialShape.begin(); anIt != theMaterialShape.end(); ++anIt) {
354 std::shared_ptr<ModelAPI_Feature> aGroupFeature = addFeature("Group");
356 aGroupFeature->data()->setName((*anIt).first);
359 AttributeSelectionListPtr aSelectionList = aGroupFeature->selectionList("group_list");
361 std::list<ResultPtr> allRes;
362 ModelAPI_Tools::allSubs(theResultBody, allRes);
363 for (std::list<ResultPtr>::iterator aRes = allRes.begin(); aRes != allRes.end(); ++aRes) {
365 GeomShapePtr aShape = (*aRes)->shape();
366 for (std::list<std::wstring>::iterator aResMat = anIt->second.begin();
367 aResMat != anIt->second.end(); ++aResMat) {
368 if (aRes->get() && ((*aRes)->data()->name() == (*aResMat)))
370 if (aShape->isCompound() || aShape->isCompSolid()) {
371 GeomAPI_ShapeIterator aShapeIt(aShape);
372 for (; aShapeIt.more(); aShapeIt.next()) {
373 aSelectionList->setSelectionType(aShapeIt.current()->shapeTypeStr());
374 aSelectionList->append(theResultBody, aShapeIt.current());
377 aSelectionList->setSelectionType(aShape->shapeTypeStr());
378 aSelectionList->append(theResultBody,aShape);
384 if (aSelectionList->size() == 0){
385 document()->removeFeature(aGroupFeature);
390 void ExchangePlugin_ImportFeature::importXAO(const std::string& theFileName,
391 const std::string& theMemoryBuff,
392 const bool isMemoryImport)
394 std::string aDataSource = theFileName;
400 std::shared_ptr<GeomAPI_Shape> aGeomShape;
401 if (isMemoryImport) {
402 aGeomShape = XAOImportMem(theMemoryBuff, anError, &aXao);
403 aDataSource = "memory buffer";
406 aGeomShape = XAOImport(theFileName, anError, &aXao);
408 if (!anError.empty()) {
409 setError("An error occurred while importing " + aDataSource + ": " + anError);
413 XAO::Geometry* aXaoGeometry = aXao.getGeometry();
415 // use the geometry name or the file name for the feature
416 std::string aBodyName = aXaoGeometry->getName();
417 if (aBodyName.empty()) {
419 aBodyName = "ImportXAOMem";
421 aBodyName = GeomAlgoAPI_Tools::File_Tools::name(theFileName);
423 data()->setName(Locale::Convert::toWString(aBodyName));
425 ResultBodyPtr aResultBody = createResultBody(aGeomShape);
426 setResult(aResultBody);
428 // Process groups/fields
429 std::shared_ptr<ModelAPI_AttributeRefList> aRefListOfGroups =
430 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(data()->attribute(FEATURES_ID()));
432 // Remove previous groups/fields stored in RefList
433 std::list<ObjectPtr> anGroupList = aRefListOfGroups->list();
434 std::list<ObjectPtr>::iterator anGroupIt = anGroupList.begin();
435 for (; anGroupIt != anGroupList.end(); ++anGroupIt) {
436 std::shared_ptr<ModelAPI_Feature> aFeature = ModelAPI_Feature::feature(*anGroupIt);
438 document()->removeFeature(aFeature);
442 for (int aGroupIndex = 0; aGroupIndex < aXao.countGroups(); ++aGroupIndex) {
443 XAO::Group* aXaoGroup = aXao.getGroup(aGroupIndex);
445 std::shared_ptr<ModelAPI_Feature> aGroupFeature = addFeature("Group");
448 if (!aXaoGroup->getName().empty())
449 aGroupFeature->data()->setName(Locale::Convert::toWString(aXaoGroup->getName()));
452 AttributeSelectionListPtr aSelectionList = aGroupFeature->selectionList("group_list");
454 // conversion of dimension
455 std::string aDimensionString = XAO::XaoUtils::dimensionToString(aXaoGroup->getDimension());
456 std::string aSelectionType =
457 ExchangePlugin_Tools::xaoDimension2selectionType(aDimensionString);
459 aSelectionList->setSelectionType(aSelectionType);
460 for (int anElementIndex = 0; anElementIndex < aXaoGroup->count(); ++anElementIndex) {
461 aSelectionList->append(aResultBody, GeomShapePtr());
462 // complex conversion of element index to reference id
463 int anElementID = aXaoGroup->get(anElementIndex);
464 std::string aReferenceString =
465 aXaoGeometry->getElementReference(aXaoGroup->getDimension(), anElementID);
466 int aReferenceID = XAO::XaoUtils::stringToInt(aReferenceString);
468 aSelectionList->value(anElementIndex)->setId(aReferenceID);
472 for (int aFieldIndex = 0; aFieldIndex < aXao.countFields(); ++aFieldIndex) {
473 XAO::Field* aXaoField = aXao.getField(aFieldIndex);
475 std::shared_ptr<ModelAPI_Feature> aFieldFeature = addFeature("Field");
478 if (!aXaoField->getName().empty())
479 aFieldFeature->data()->setName(Locale::Convert::toWString(aXaoField->getName()));
482 AttributeSelectionListPtr aSelectionList = aFieldFeature->selectionList("selected");
484 // conversion of dimension
485 std::string aDimensionString = XAO::XaoUtils::dimensionToString(aXaoField->getDimension());
486 std::string aSelectionType =
487 ExchangePlugin_Tools::xaoDimension2selectionType(aDimensionString);
488 aSelectionList->setSelectionType(aSelectionType);
489 // limitation: now in XAO fields are related to everything, so, iterate all sub-shapes to fill
490 int aCountSelected = aXaoField->countElements();
491 std::list<ResultPtr>::const_iterator aResIter = results().begin();
492 for(; aResIter != results().end() && aCountSelected; aResIter++) {
493 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(*aResIter);
496 // check that only results that were created before this field are used
497 FeaturePtr aResultFeature = document()->feature(aBody);
498 if (!aResultFeature.get())
500 GeomShapePtr aShape = aBody->shape();
501 if (!aShape.get() || aShape->isNull())
503 GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::shapeTypeByStr(aSelectionType));
504 for(; anExp.more(); anExp.next()) {
505 aSelectionList->append(aBody, anExp.current());
507 if (aCountSelected == 0)
512 // conversion of type
513 XAO::Type aFieldType = aXaoField->getType();
514 std::string aTypeString = XAO::XaoUtils::fieldTypeToString(aFieldType);
515 ModelAPI_AttributeTables::ValueType aType =
516 ExchangePlugin_Tools::xaoType2valuesType(aTypeString);
517 // set components names
518 AttributeStringArrayPtr aComponents = aFieldFeature->stringArray("components_names");
519 aComponents->setSize(aXaoField->countComponents());
520 for(int aComp = 0; aComp < aXaoField->countComponents(); aComp++) {
521 aComponents->setValue(aComp, aXaoField->getComponentName(aComp));
524 AttributeIntArrayPtr aStamps = aFieldFeature->intArray("stamps");
525 aStamps->setSize(aXaoField->countSteps());
526 std::shared_ptr<ModelAPI_AttributeTables> aTables = aFieldFeature->tables("values");
528 aXaoField->countElements() + 1, aXaoField->countComponents(), aXaoField->countSteps());
529 aTables->setType(aType);
531 XAO::stepIterator aStepIter = aXaoField->begin();
532 for(int aStepIndex = 0; aStepIter != aXaoField->end(); aStepIter++, aStepIndex++) {
533 aStamps->setValue(aStepIndex, (*aStepIter)->getStamp());
534 for(int aRow = 1; aRow <= aXaoField->countElements(); aRow++) {
535 for(int aCol = 0; aCol < aXaoField->countComponents(); aCol++) {
536 ModelAPI_AttributeTables::Value aVal;
537 std::string aValStr = (*aStepIter)->getStringValue(aRow - 1, aCol);
539 case ModelAPI_AttributeTables::BOOLEAN:
540 aVal.myBool = aValStr == "true";
542 case ModelAPI_AttributeTables::INTEGER:
543 aVal.myInt = atoi(aValStr.c_str());
545 case ModelAPI_AttributeTables::DOUBLE:
546 aVal.myDouble = atof(aValStr.c_str());
548 case ModelAPI_AttributeTables::STRING:
549 aVal.myStr = aValStr;
552 aTables->setValue(aVal, aRow, aCol, aStepIndex);
556 // remove everything with zero-values: zeroes are treated as defaults
557 std::set<int> aRowsToRemove;
558 for(int aRow = 1; aRow < aTables->rows(); aRow++) {
560 for(int aCol = 0; aCol < aTables->columns() && isZero; aCol++) {
561 for(int aStepIndex = 0; aStepIndex != aTables->tables() && isZero; aStepIndex++) {
562 if (aTables->valueStr(aRow, aCol, aStepIndex) != aTables->valueStr(0, aCol, aStepIndex))
567 aRowsToRemove.insert(aRow - 1); // -1 to make prepared for remove from SelectionList
569 if (!aRowsToRemove.empty()) { // move usefull rows on bottom to the up of the tables
570 // number of rows passed during going through: the current rows will
571 // be moved up for this value
572 int aRemovedPassed = 0;
573 for(int aRow = 1; aRow < aTables->rows(); aRow++) {
574 if (aRowsToRemove.find(aRow - 1) != aRowsToRemove.end()) {
576 } else if (aRemovedPassed != 0) { // copy the line up
577 for(int aCol = 0; aCol < aTables->columns(); aCol++) {
578 for(int aTable = 0; aTable != aTables->tables(); aTable++) {
580 aTables->value(aRow, aCol, aTable), aRow - aRemovedPassed, aCol, aTable);
585 aTables->setSize(aTables->rows() - aRemovedPassed, aTables->columns(), aTables->tables());
586 aSelectionList->remove(aRowsToRemove); // remove also selected elements
589 // Top avoid problems in Object Browser update: issue #1647.
590 ModelAPI_EventCreator::get()->sendReordered(
591 std::dynamic_pointer_cast<ModelAPI_Feature>(aRefListOfGroups->owner()));
594 } catch (XAO::XAO_Exception& e) {
595 std::string anError = e.what();
596 setError("An error occurred while importing " + aDataSource + ": " + anError);
602 //============================================================================
603 std::shared_ptr<ModelAPI_Feature> ExchangePlugin_ImportFeatureBase::addFeature(
606 std::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID, false);
608 data()->reflist(FEATURES_ID())->append(aNew);
609 // set as current also after it becomes sub to set correctly enabled for other subs
610 //document()->setCurrentFeature(aNew, false);
615 void ExchangePlugin_ImportFeatureBase::removeFeature(
616 std::shared_ptr<ModelAPI_Feature> theFeature)
618 if (!data()->isValid())
620 AttributeRefListPtr aList = reflist(FEATURES_ID());
621 aList->remove(theFeature);
625 int ExchangePlugin_ImportFeatureBase::numberOfSubs(bool /*forTree*/) const
627 return data()->reflist(FEATURES_ID())->size(true);
630 std::shared_ptr<ModelAPI_Feature> ExchangePlugin_ImportFeatureBase::subFeature(
631 const int theIndex, bool /*forTree*/)
633 ObjectPtr anObj = data()->reflist(FEATURES_ID())->object(theIndex, false);
634 FeaturePtr aRes = std::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
639 int ExchangePlugin_ImportFeatureBase::subFeatureId(const int theIndex) const
641 std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
642 ModelAPI_AttributeRefList>(data()->attribute(FEATURES_ID()));
643 std::list<ObjectPtr> aFeatures = aRefList->list();
644 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin();
645 int aResultIndex = 1; // number of the counted (created) features, started from 1
646 int aFeatureIndex = -1; // number of the not-empty features in the list
647 for (; anIt != aFeatures.end(); anIt++) {
650 if (aFeatureIndex == theIndex)
658 bool ExchangePlugin_ImportFeatureBase::isSub(ObjectPtr theObject) const
660 // check is this feature of result
661 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
663 return data()->reflist(FEATURES_ID())->isInList(aFeature);
667 //============================================================================
668 void ExchangePlugin_ImportFeatureBase::loadNamingDS(
669 std::shared_ptr<GeomAPI_Shape> theGeomShape,
670 std::shared_ptr<ModelAPI_ResultBody> theResultBody)
673 theResultBody->store(theGeomShape);
675 // to store color of higher-level shape
676 std::wstring aName = theResultBody->findShapeName(theGeomShape);
679 std::vector<int> aColor = theResultBody->findShapeColor(aName);
681 ModelAPI_Tools::setColor(theResultBody, aColor);
684 std::string aNameMS = "Shape";
685 theResultBody->loadFirstLevel(theGeomShape, aNameMS);
688 void ExchangePlugin_Import_ImageFeature::importFile(const std::string& theFileName)
690 std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(theFileName);
693 if (anExtension == "PNG" || anExtension == "GIF" ||
694 anExtension == "TIFF" || anExtension == "JPE" ||
695 anExtension == "JPEG" || anExtension == "JPG" ||
696 anExtension == "BMP" || anExtension == "PPM"
698 // Perform the import
699 QPixmap px (theFileName.c_str());
700 int aWidth = px.width();
701 int aHeight = px.height();
702 if (aWidth < 1 || aHeight < 1) {
703 setError("An error occurred while importing " + theFileName + ": invalid image");
707 std::shared_ptr<GeomAPI_Shape> aGeomShape = ImageImport(aWidth, aHeight, anError);
709 // Check if shape is valid
710 if (!anError.empty()) {
711 setError("An error occurred while importing " + theFileName + ": " + anError);
715 // Pass the results into the model
716 std::string anObjectName = GeomAlgoAPI_Tools::File_Tools::name(theFileName);
717 data()->setName(Locale::Convert::toWString(anObjectName));
719 auto resultBody = createResultBody(aGeomShape);
721 // Store image in result body attribute
722 AttributeImagePtr anImageAttr = resultBody->data()->image(ModelAPI_ResultBody::IMAGE_ID());
723 if (anImageAttr.get() != NULL) {
724 QImage aQImage = px.toImage();
725 const uchar* aImageBytes = aQImage.bits();
726 std::list<unsigned char> aByteArray (aImageBytes, aImageBytes + aQImage.sizeInBytes());
727 anImageAttr->setTexture(aWidth, aHeight, aByteArray, anExtension);
730 setResult(resultBody);
733 anError = "Unsupported format: " + anExtension;
734 setError("An error occurred while importing " + theFileName + ": " + anError);