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