Salome HOME
Issue #608: Usage of OCCT in interface -- Wrap classes by SWIG
[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 <TopoDS_Shape.hxx>
26
27 #include <algorithm>
28 #include <string>
29 #ifdef _DEBUG
30 #include <iostream>
31 #include <ostream>
32 #endif
33
34 ExchangePlugin_ImportFeature::ExchangePlugin_ImportFeature()
35 {
36 }
37
38 ExchangePlugin_ImportFeature::~ExchangePlugin_ImportFeature()
39 {
40   // TODO Auto-generated destructor stub
41 }
42
43 /*
44  * Returns the unique kind of a feature
45  */
46 const std::string& ExchangePlugin_ImportFeature::getKind()
47 {
48   return ExchangePlugin_ImportFeature::ID();
49 }
50
51 /*
52  * Request for initialization of data model of the feature: adding all attributes
53  */
54 void ExchangePlugin_ImportFeature::initAttributes()
55 {
56   data()->addAttribute(ExchangePlugin_ImportFeature::FILE_PATH_ID(), ModelAPI_AttributeString::typeId());
57 }
58
59 /*
60  * Computes or recomputes the results
61  */
62 void ExchangePlugin_ImportFeature::execute()
63 {
64   AttributeStringPtr aFilePathAttr =
65       this->string(ExchangePlugin_ImportFeature::FILE_PATH_ID());
66   std::string aFilePath = aFilePathAttr->value();
67   if (aFilePath.empty())
68     return;
69
70   importFile(aFilePath);
71 }
72
73 bool ExchangePlugin_ImportFeature::importFile(const std::string& theFileName)
74 {
75   // retrieve the file and plugin library names
76   // ".brep" -> "BREP"
77   std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(theFileName);
78
79   // Perform the import
80   std::string anError;
81
82   TopoDS_Shape aShape;
83   if (anExtension == "BREP" || anExtension == "BRP") {
84     aShape = BREPImport(theFileName, anExtension, anError);
85   } else if (anExtension == "STEP" || anExtension == "STP") {
86     aShape = STEPImport(theFileName, anExtension, anError);
87   } else if (anExtension == "IGES" || anExtension == "IGS") {
88     aShape = IGESImport(theFileName, anExtension, anError);
89   }
90    // Check if shape is valid
91   if ( aShape.IsNull() ) {
92     const static std::string aShapeError =
93       "An error occurred while importing " + theFileName + ": " + anError;
94     setError(aShapeError);
95     return false;
96   }
97
98   // Pass the results into the model
99   std::string anObjectName = GeomAlgoAPI_Tools::File_Tools::name(theFileName);
100   data()->setName(anObjectName);
101   std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
102   std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
103   aGeomShape->setImpl(new TopoDS_Shape(aShape));
104
105   //LoadNamingDS of the imported shape
106   loadNamingDS(aGeomShape, aResultBody);
107
108   setResult(aResultBody);
109
110   return true;
111 }
112
113 //============================================================================
114 void ExchangePlugin_ImportFeature::loadNamingDS(
115     std::shared_ptr<GeomAPI_Shape> theGeomShape,
116     std::shared_ptr<ModelAPI_ResultBody> theResultBody)
117 {
118   //load result
119   theResultBody->store(theGeomShape);
120
121   int aTag(1);
122   std::string aNameMS = "Shape";
123   theResultBody->loadFirstLevel(theGeomShape, aNameMS, aTag);
124 }