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