Salome HOME
Working with folders which can not be shown empty
[modules/shaper.git] / src / ExchangePlugin / ExchangePlugin_ImportFeature.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:    ExchangePlugin_ImportFeature.cpp
4 // Created: Aug 28, 2014
5 // Author:  Sergey BELASH
6
7 #include <ExchangePlugin_ImportFeature.h>
8
9 #include <GeomAlgoAPI_BREPImport.h>
10 #include <GeomAlgoAPI_IGESImport.h>
11 #include <GeomAlgoAPI_STEPImport.h>
12 #include <GeomAlgoAPI_Tools.h>
13
14 #include <GeomAPI_Shape.h>
15
16 #include <Config_Common.h>
17 #include <Config_PropManager.h>
18
19 #include <ModelAPI_AttributeString.h>
20 #include <ModelAPI_Data.h>
21 #include <ModelAPI_Document.h>
22 #include <ModelAPI_Object.h>
23 #include <ModelAPI_ResultBody.h>
24
25 #include <algorithm>
26 #include <string>
27 #ifdef _DEBUG
28 #include <iostream>
29 #include <ostream>
30 #endif
31
32 ExchangePlugin_ImportFeature::ExchangePlugin_ImportFeature()
33 {
34 }
35
36 ExchangePlugin_ImportFeature::~ExchangePlugin_ImportFeature()
37 {
38   // TODO Auto-generated destructor stub
39 }
40
41 /*
42  * Returns the unique kind of a feature
43  */
44 const std::string& ExchangePlugin_ImportFeature::getKind()
45 {
46   return ExchangePlugin_ImportFeature::ID();
47 }
48
49 /*
50  * Request for initialization of data model of the feature: adding all attributes
51  */
52 void ExchangePlugin_ImportFeature::initAttributes()
53 {
54   data()->addAttribute(ExchangePlugin_ImportFeature::FILE_PATH_ID(), ModelAPI_AttributeString::typeId());
55 }
56
57 /*
58  * Computes or recomputes the results
59  */
60 void ExchangePlugin_ImportFeature::execute()
61 {
62   AttributeStringPtr aFilePathAttr =
63       this->string(ExchangePlugin_ImportFeature::FILE_PATH_ID());
64   std::string aFilePath = aFilePathAttr->value();
65   if (aFilePath.empty())
66     return;
67
68   importFile(aFilePath);
69 }
70
71 bool ExchangePlugin_ImportFeature::importFile(const std::string& theFileName)
72 {
73   // retrieve the file and plugin library names
74   // ".brep" -> "BREP"
75   std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(theFileName);
76
77   // Perform the import
78   std::string anError;
79
80   std::shared_ptr<GeomAPI_Shape> aGeomShape;
81   if (anExtension == "BREP" || anExtension == "BRP") {
82     aGeomShape = BREPImport(theFileName, anExtension, anError);
83   } else if (anExtension == "STEP" || anExtension == "STP") {
84     aGeomShape = STEPImport(theFileName, anExtension, anError);
85   } else if (anExtension == "IGES" || anExtension == "IGS") {
86     aGeomShape = IGESImport(theFileName, anExtension, anError);
87   }
88    // Check if shape is valid
89   if ( aGeomShape->isNull() ) {
90     const static std::string aShapeError =
91       "An error occurred while importing " + theFileName + ": " + anError;
92     setError(aShapeError);
93     return false;
94   }
95
96   // Pass the results into the model
97   std::string anObjectName = GeomAlgoAPI_Tools::File_Tools::name(theFileName);
98   data()->setName(anObjectName);
99   std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
100
101   //LoadNamingDS of the imported shape
102   loadNamingDS(aGeomShape, aResultBody);
103
104   setResult(aResultBody);
105
106   return true;
107 }
108
109 //============================================================================
110 void ExchangePlugin_ImportFeature::loadNamingDS(
111     std::shared_ptr<GeomAPI_Shape> theGeomShape,
112     std::shared_ptr<ModelAPI_ResultBody> theResultBody)
113 {
114   //load result
115   theResultBody->store(theGeomShape);
116
117   int aTag(1);
118   std::string aNameMS = "Shape";
119   theResultBody->loadFirstLevel(theGeomShape, aNameMS, aTag);
120 }