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