Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom into Dev_1.1.0
[modules/shaper.git] / src / ExchangePlugin / ExchangePlugin_ImportFeature.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 /*
4  * ExchangePlugin_ImportFeature.cpp
5  *
6  *  Created on: Aug 28, 2014
7  *      Author: sbh
8  */
9
10 #include <ExchangePlugin_ImportFeature.h>
11 #include <GeomAlgoAPI_BREPImport.h>
12 #include <GeomAlgoAPI_STEPImport.h>
13
14 #include <GeomAPI_Shape.h>
15 #include <Config_Common.h>
16 #include <Config_PropManager.h>
17
18 #include <ModelAPI_AttributeString.h>
19 #include <ModelAPI_Data.h>
20 #include <ModelAPI_Document.h>
21 #include <ModelAPI_Object.h>
22 #include <ModelAPI_ResultBody.h>
23 #include <TCollection_AsciiString.hxx>
24 #include <TDF_Label.hxx>
25 #include <TopoDS_Shape.hxx>
26 #include <OSD_Path.hxx>
27
28 #include <algorithm>
29 #include <string>
30 #ifdef _DEBUG
31 #include <iostream>
32 #include <ostream>
33 #endif
34
35 ExchangePlugin_ImportFeature::ExchangePlugin_ImportFeature()
36 {
37 }
38
39 ExchangePlugin_ImportFeature::~ExchangePlugin_ImportFeature()
40 {
41   // TODO Auto-generated destructor stub
42 }
43
44 /*
45  * Returns the unique kind of a feature
46  */
47 const std::string& ExchangePlugin_ImportFeature::getKind()
48 {
49   return ExchangePlugin_ImportFeature::ID();
50 }
51
52 /*
53  * Request for initialization of data model of the feature: adding all attributes
54  */
55 void ExchangePlugin_ImportFeature::initAttributes()
56 {
57   data()->addAttribute(ExchangePlugin_ImportFeature::FILE_PATH_ID(), ModelAPI_AttributeString::type());
58 }
59
60 /*
61  * Computes or recomputes the results
62  */
63 void ExchangePlugin_ImportFeature::execute()
64 {
65   AttributeStringPtr aFilePathAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(
66       data()->attribute(ExchangePlugin_ImportFeature::FILE_PATH_ID()));
67   std::string aFilePath = aFilePathAttr->value();
68   if(aFilePath.empty())
69     return;
70   importFile(aFilePath);
71 }
72
73 void ExchangePlugin_ImportFeature::customisePresentation(AISObjectPtr thePrs)
74 {
75   std::vector<int> aRGB = Config_PropManager::color("Visualization", "import_feature_color",
76                                                     IMPORTED_FEATURE_COLOR);
77   thePrs->setColor(aRGB[0], aRGB[1], aRGB[2]);
78   thePrs->redisplay();
79 }
80
81 bool ExchangePlugin_ImportFeature::importFile(const std::string& theFileName)
82 {
83   // retrieve the file and plugin library names
84   TCollection_AsciiString aFileName (theFileName.c_str());
85   OSD_Path aPath(aFileName);
86   TCollection_AsciiString aFormatName = aPath.Extension();
87   // ".brep" -> "BREP", TCollection_AsciiString are numbered from 1
88   aFormatName = aFormatName.SubString(2, aFormatName.Length());
89   aFormatName.UpperCase();
90
91   // Perform the import
92   TCollection_AsciiString anError;
93   TDF_Label anUnknownLabel = TDF_Label();
94
95   TopoDS_Shape aShape;
96   if (aFormatName == "BREP") {
97     aShape = BREPImport::Import(aFileName, aFormatName, anError, anUnknownLabel);
98   } else if (aFormatName == "STEP") {
99     aShape = STEPImport::Import(aFileName, aFormatName, anError, anUnknownLabel);
100   }
101    // Check if shape is valid
102   if ( aShape.IsNull() ) {
103      const static std::string aShapeError = 
104        "An error occurred while importing " + theFileName + ": " + anError.ToCString();
105      setError(aShapeError);
106      return false;
107    }
108
109   // Pass the results into the model
110   std::string anObjectName = aPath.Name().ToCString();
111   data()->setName(anObjectName);
112   std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
113   std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
114   aGeomShape->setImpl(new TopoDS_Shape(aShape));
115
116   //LoadNamingDS of the imported shape
117   loadNamingDS(aGeomShape, aResultBody);
118
119   setResult(aResultBody);
120
121   return true;
122 }
123
124 //============================================================================
125 void ExchangePlugin_ImportFeature::loadNamingDS(
126                                     std::shared_ptr<GeomAPI_Shape> theGeomShape, 
127                                              std::shared_ptr<ModelAPI_ResultBody> theResultBody)
128 {  
129   //load result
130   theResultBody->store(theGeomShape);
131  
132   int aTag(1);
133   std::string aNameMS = "Shape";
134   theResultBody->loadFirstLevel(theGeomShape, aNameMS, aTag);
135   //std::string aNameDE = "DiscEdges";
136   //theResultBody->loadDisconnectedEdges(theGeomShape, aNameDE, aTag);
137   //std::string aNameDV = "DiscVertexes";
138   //theResultBody->loadDisconnectedVertexes(theGeomShape, aNameDV, aTag); 
139 }