Salome HOME
Change names of some ExchangePlugin feature attributes
[modules/shaper.git] / src / ExchangePlugin / ExchangePlugin_ExportFeature.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:    ExchangePlugin_ExportFeature.cpp
4 // Created: May 14, 2015
5 // Author:  Sergey POKHODENKO
6
7 #include <ExchangePlugin_ExportFeature.h>
8
9 #include <ExchangePlugin_Tools.h>
10
11 #include <GeomAlgoAPI_BREPExport.h>
12 #include <GeomAlgoAPI_CompoundBuilder.h>
13 #include <GeomAlgoAPI_IGESExport.h>
14 #include <GeomAlgoAPI_STEPExport.h>
15 #include <GeomAlgoAPI_Tools.h>
16
17 #include <Config_Common.h>
18 #include <Config_PropManager.h>
19
20 #include <GeomAPI_Shape.h>
21
22 #include <ModelAPI_AttributeSelectionList.h>
23 #include <ModelAPI_AttributeString.h>
24 #include <ModelAPI_Data.h>
25 #include <ModelAPI_Document.h>
26 #include <ModelAPI_Object.h>
27 #include <ModelAPI_ResultBody.h>
28
29 #include <algorithm>
30 #include <iterator>
31 #include <string>
32 #ifdef _DEBUG
33 #include <iostream>
34 #include <ostream>
35 #endif
36
37 ExchangePlugin_ExportFeature::ExchangePlugin_ExportFeature()
38 {
39 }
40
41 ExchangePlugin_ExportFeature::~ExchangePlugin_ExportFeature()
42 {
43   // TODO Auto-generated destructor stub
44 }
45
46 /*
47  * Returns the unique kind of a feature
48  */
49 const std::string& ExchangePlugin_ExportFeature::getKind()
50 {
51   return ExchangePlugin_ExportFeature::ID();
52 }
53
54 /*
55  * Request for initialization of data model of the feature: adding all attributes
56  */
57 void ExchangePlugin_ExportFeature::initAttributes()
58 {
59   data()->addAttribute(ExchangePlugin_ExportFeature::FILE_PATH_ID(), ModelAPI_AttributeString::typeId());
60   data()->addAttribute(ExchangePlugin_ExportFeature::FILE_FORMAT_ID(), ModelAPI_AttributeString::typeId());
61   data()->addAttribute(ExchangePlugin_ExportFeature::SELECTION_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
62 }
63
64 /*
65  * Computes or recomputes the results
66  */
67 void ExchangePlugin_ExportFeature::execute()
68 {
69   AttributeStringPtr aFormatAttr =
70       this->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID());
71   std::string aFormat = aFormatAttr->value();
72   // Format may be empty. In this case look at extension.
73 //  if (aFormat.empty())
74 //    return;
75
76   AttributeStringPtr aFilePathAttr =
77       this->string(ExchangePlugin_ExportFeature::FILE_PATH_ID());
78   std::string aFilePath = aFilePathAttr->value();
79   if (aFilePath.empty())
80     return;
81
82   AttributeSelectionListPtr aSelectionListAttr =
83       this->selectionList(ExchangePlugin_ExportFeature::SELECTION_LIST_ID());
84   std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
85   for (int i = 0, aSize = aSelectionListAttr->size(); i < aSize; ++i) {
86     aShapes.push_back(aSelectionListAttr->value(i)->value());
87   }
88   std::shared_ptr<GeomAPI_Shape> aShape =
89       GeomAlgoAPI_CompoundBuilder::compound(aShapes);
90
91   exportFile(aFilePath, aFormat, aShape);
92 }
93
94 bool ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName,
95                                               const std::string& theFormat,
96                                               std::shared_ptr<GeomAPI_Shape> theShape)
97 {
98   // retrieve the file and plugin library names
99   std::string aFormatName = theFormat;
100
101   if (theFormat.empty()) { // look at extension
102     // ".brep" -> "BREP"
103     std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(theFileName);
104     if (anExtension == "BREP" || anExtension == "BRP") {
105       aFormatName = "BREP";
106     } else if (anExtension == "STEP" || anExtension == "STP") {
107       aFormatName = "STEP";
108     } else if (anExtension == "IGES" || anExtension == "IGS") {
109       aFormatName = "IGES-5.1";
110     } else {
111       aFormatName = anExtension;
112     }
113   }
114
115   // Perform the export
116   std::string anError;
117   bool aResult = false;
118   if (aFormatName == "BREP") {
119     aResult = BREPExport(theFileName, aFormatName, theShape, anError);
120   } else if (aFormatName == "STEP") {
121     aResult = STEPExport(theFileName, aFormatName, theShape, anError);
122   } else if (aFormatName.substr(0, 4) == "IGES") {
123     aResult = IGESExport(theFileName, aFormatName, theShape, anError);
124   } else {
125     anError = "Unsupported format " + aFormatName;
126   }
127
128   if (!aResult) {
129     std::string aShapeError =
130         "An error occurred while exporting " + theFileName + ": " + anError;
131     setError(aShapeError);
132     return false;
133   }
134
135   return true;
136 }