Salome HOME
Issue #529 : 4.07. Import IGES, export to BREP, STEP, IGES - Export BREP
[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 #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_AttributeString.h>
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_Data.h>
25 #include <ModelAPI_Document.h>
26 #include <ModelAPI_Object.h>
27 #include <ModelAPI_ResultBody.h>
28 #include <TCollection_AsciiString.hxx>
29 #include <TDF_Label.hxx>
30 #include <TopoDS_Shape.hxx>
31 #include <OSD_Path.hxx>
32
33 #include <algorithm>
34 #include <string>
35 #ifdef _DEBUG
36 #include <iostream>
37 #include <ostream>
38 #endif
39
40 ExchangePlugin_ExportFeature::ExchangePlugin_ExportFeature()
41 {
42 }
43
44 ExchangePlugin_ExportFeature::~ExchangePlugin_ExportFeature()
45 {
46   // TODO Auto-generated destructor stub
47 }
48
49 /*
50  * Returns the unique kind of a feature
51  */
52 const std::string& ExchangePlugin_ExportFeature::getKind()
53 {
54   return ExchangePlugin_ExportFeature::ID();
55 }
56
57 /*
58  * Request for initialization of data model of the feature: adding all attributes
59  */
60 void ExchangePlugin_ExportFeature::initAttributes()
61 {
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 aFilePathAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(
72       data()->attribute(ExchangePlugin_ExportFeature::FILE_PATH_ID()));
73   std::string aFilePath = aFilePathAttr->value();
74   if (aFilePath.empty())
75     return;
76
77   AttributeSelectionListPtr aSelectionListAttr =
78       std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(
79           data()->attribute(ExchangePlugin_ExportFeature::SELECTION_LIST_ID()));
80
81   std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
82   for ( int i = 0, aSize = aSelectionListAttr->size(); i < aSize; ++i ) {
83     std::shared_ptr<ModelAPI_AttributeSelection> anSelectionAttr = aSelectionListAttr->value(i);
84     aShapes.push_back(anSelectionAttr->value());
85   }
86   std::shared_ptr<GeomAPI_Shape> aShape =
87       GeomAlgoAPI_CompoundBuilder::compound(aShapes);
88
89   exportFile(aFilePath, aShape);
90 }
91
92 bool ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName,
93                                               std::shared_ptr<GeomAPI_Shape> theShape)
94 {
95   // retrieve the file and plugin library names
96   TCollection_AsciiString aFileName(theFileName.c_str());
97   OSD_Path aPath(aFileName);
98   TCollection_AsciiString aFormatName = aPath.Extension();
99   // ".brep" -> "BREP", TCollection_AsciiString are numbered from 1
100   aFormatName = aFormatName.SubString(2, aFormatName.Length());
101   aFormatName.UpperCase();
102
103   // Perform the export
104   TCollection_AsciiString anError;
105   TDF_Label anUnknownLabel = TDF_Label();
106
107   TopoDS_Shape aShape(theShape->impl<TopoDS_Shape>());
108   bool aResult = true;
109   if (aFormatName == "BREP") {
110     aResult = BREPExport::Export(aFileName, aFormatName, aShape, anError, anUnknownLabel);
111   } else if (aFormatName == "STEP" || aFormatName == "STP") {
112 //    aShape = STEPExport::Export(aFileName, aFormatName, anError, anUnknownLabel);
113   } else if (aFormatName == "IGES") {
114     aResult = IGESExport::Export(aFileName, aFormatName, aShape, anError, anUnknownLabel);
115   }
116
117   if ( !aResult ) {
118     const static std::string aShapeError =
119         "An error occurred while exporting " + theFileName + ": " + anError.ToCString();
120     setError(aShapeError);
121     return false;
122   }
123
124   return true;
125 }