Salome HOME
Salome's 7.5.0 import BREP/STEP methods extraction
[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 <ExchangePlugin_BREPImport.h>
12 #include <ExchangePlugin_STEPImport.h>
13
14 #include <GeomAPI_Shape.h>
15 #include <Config_Common.h>
16 #include <ModelAPI_AttributeString.h>
17 #include <ModelAPI_Data.h>
18 #include <ModelAPI_Document.h>
19 #include <ModelAPI_Object.h>
20 #include <ModelAPI_ResultBody.h>
21 #include <TCollection_AsciiString.hxx>
22 #include <TDF_Label.hxx>
23 #include <TopoDS_Shape.hxx>
24 #include <OSD_Path.hxx>
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::type());
56 }
57
58 /*
59  * Computes or recomputes the results
60  */
61 void ExchangePlugin_ImportFeature::execute()
62 {
63   AttributeStringPtr aFilePathAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(
64       data()->attribute(ExchangePlugin_ImportFeature::FILE_PATH_ID()));
65   std::string aFilePath = aFilePathAttr->value();
66   if(aFilePath.empty())
67     return;
68   importFile(aFilePath);
69 }
70
71 bool ExchangePlugin_ImportFeature::importFile(const std::string& theFileName)
72 {
73   // retrieve the file and plugin library names
74   TCollection_AsciiString aFileName (theFileName.c_str());
75   OSD_Path aPath(aFileName);
76   TCollection_AsciiString aFormatName = aPath.Extension();
77   // ".brep" -> "BREP", TCollection_AsciiString are numbered from 1
78   aFormatName = aFormatName.SubString(2, aFormatName.Length());
79   aFormatName.UpperCase();
80
81   // Perform the import
82   TCollection_AsciiString anError;
83   TDF_Label anUnknownLabel = TDF_Label();
84
85   TopoDS_Shape aShape;
86   if (aFormatName == "BREP") {
87     aShape = BREPImport::Import(aFileName, aFormatName, anError, anUnknownLabel);
88   } else if (aFormatName == "STEP") {
89     aShape = STEPImport::Import(aFileName, aFormatName, anError, anUnknownLabel);
90   }
91    // Check if shape is valid
92   if ( aShape.IsNull() ) {
93      const static std::string aShapeError = 
94        "An error occurred while importing " + theFileName + ": " + anError.ToCString();
95      setError(aShapeError);
96      return false;
97    }
98
99   // Pass the results into the model
100   std::string anObjectName = aPath.Name().ToCString();
101   data()->setName(anObjectName);
102   std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
103   std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
104   aGeomShape->setImpl(new TopoDS_Shape(aShape));
105
106   //LoadNamingDS of the imported shape
107   loadNamingDS(aGeomShape, aResultBody);
108
109   setResult(aResultBody);
110
111   return true;
112 }
113
114 //============================================================================
115 void ExchangePlugin_ImportFeature::loadNamingDS(
116                                     std::shared_ptr<GeomAPI_Shape> theGeomShape, 
117                                              std::shared_ptr<ModelAPI_ResultBody> theResultBody)
118 {  
119   //load result
120   theResultBody->store(theGeomShape);
121  
122   int aTag(1);
123   std::string aNameMS = "Shape";
124   theResultBody->loadFirstLevel(theGeomShape, aNameMS, aTag);
125   std::string aNameDE = "DiscEdges";
126   theResultBody->loadDisconnectedEdges(theGeomShape, aNameDE, aTag);
127   std::string aNameDV = "DiscVertexes";
128   theResultBody->loadDisconnectedVertexes(theGeomShape, aNameDV, aTag); 
129 }