]> SALOME platform Git repositories - modules/shaper.git/blob - src/ExchangePlugin/ExchangePlugin_ExportFeature.cpp
Salome HOME
f4e87b76ef0c6378491cc4d1b31e9a68162190c2
[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 <TopoDS_Shape.hxx>
30
31 #include <algorithm>
32 #include <iterator>
33 #include <string>
34 #ifdef _DEBUG
35 #include <iostream>
36 #include <ostream>
37 #endif
38
39 ExchangePlugin_ExportFeature::ExchangePlugin_ExportFeature()
40 {
41 }
42
43 ExchangePlugin_ExportFeature::~ExchangePlugin_ExportFeature()
44 {
45   // TODO Auto-generated destructor stub
46 }
47
48 /*
49  * Returns the unique kind of a feature
50  */
51 const std::string& ExchangePlugin_ExportFeature::getKind()
52 {
53   return ExchangePlugin_ExportFeature::ID();
54 }
55
56 /*
57  * Request for initialization of data model of the feature: adding all attributes
58  */
59 void ExchangePlugin_ExportFeature::initAttributes()
60 {
61   data()->addAttribute(ExchangePlugin_ExportFeature::FILE_FORMAT_ID(), ModelAPI_AttributeString::typeId());
62   data()->addAttribute(ExchangePlugin_ExportFeature::FILE_PATH_ID(), ModelAPI_AttributeString::typeId());
63   data()->addAttribute(ExchangePlugin_ExportFeature::SELECTION_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
64 }
65
66 /*
67  * Computes or recomputes the results
68  */
69 void ExchangePlugin_ExportFeature::execute()
70 {
71   AttributeStringPtr aFormatAttr =
72       this->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID());
73   std::string aFormat = aFormatAttr->value();
74   // Format may be empty. In this case look at extension.
75 //  if (aFormat.empty())
76 //    return;
77
78   AttributeStringPtr aFilePathAttr =
79       this->string(ExchangePlugin_ExportFeature::FILE_PATH_ID());
80   std::string aFilePath = aFilePathAttr->value();
81   if (aFilePath.empty())
82     return;
83
84   AttributeSelectionListPtr aSelectionListAttr =
85       this->selectionList(ExchangePlugin_ExportFeature::SELECTION_LIST_ID());
86   std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
87   for (int i = 0, aSize = aSelectionListAttr->size(); i < aSize; ++i) {
88     aShapes.push_back(aSelectionListAttr->value(i)->value());
89   }
90   std::shared_ptr<GeomAPI_Shape> aShape =
91       GeomAlgoAPI_CompoundBuilder::compound(aShapes);
92
93   exportFile(aFilePath, aFormat, aShape);
94 }
95
96 bool ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName,
97                                               const std::string& theFormat,
98                                               std::shared_ptr<GeomAPI_Shape> theShape)
99 {
100   // retrieve the file and plugin library names
101   std::string aFormatName = theFormat;
102
103   if (theFormat.empty()) { // look at extension
104     // ".brep" -> "BREP"
105     std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(theFileName);
106     if (anExtension == "BREP" || anExtension == "BRP") {
107       aFormatName = "BREP";
108     } else if (anExtension == "STEP" || anExtension == "STP") {
109       aFormatName = "STEP";
110     } else if (anExtension == "IGES" || anExtension == "IGS") {
111       aFormatName = "IGES-5.1";
112     } else {
113       aFormatName = anExtension;
114     }
115   }
116
117   // Perform the export
118   std::string anError;
119
120   TopoDS_Shape aShape(theShape->impl<TopoDS_Shape>());
121   bool aResult = false;
122   if (aFormatName == "BREP") {
123     aResult = BREPExport(theFileName, aFormatName, aShape, anError);
124   } else if (aFormatName == "STEP") {
125     aResult = STEPExport(theFileName, aFormatName, aShape, anError);
126   } else if (aFormatName.substr(0, 4) == "IGES") {
127     aResult = IGESExport(theFileName, aFormatName, aShape, anError);
128   } else {
129     anError = "Unsupported format " + aFormatName;
130   }
131
132   if (!aResult) {
133     std::string aShapeError =
134         "An error occurred while exporting " + theFileName + ": " + anError;
135     setError(aShapeError);
136     return false;
137   }
138
139   return true;
140 }