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