Salome HOME
Make XAO export
[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 <algorithm>
10 #include <iterator>
11 #include <string>
12 #ifdef _DEBUG
13 #include <iostream>
14 #include <ostream>
15 #endif
16
17 #include <Config_Common.h>
18 #include <Config_PropManager.h>
19
20 #include <GeomAlgoAPI_BREPExport.h>
21 #include <GeomAlgoAPI_CompoundBuilder.h>
22 #include <GeomAlgoAPI_IGESExport.h>
23 #include <GeomAlgoAPI_STEPExport.h>
24 #include <GeomAlgoAPI_Tools.h>
25 #include <GeomAlgoAPI_XAOExport.h>
26
27 #include <GeomAPI_Shape.h>
28
29 #include <ModelAPI_AttributeSelectionList.h>
30 #include <ModelAPI_AttributeString.h>
31 #include <ModelAPI_Data.h>
32 #include <ModelAPI_Document.h>
33 #include <ModelAPI_Object.h>
34 #include <ModelAPI_ResultBody.h>
35 #include <ModelAPI_Session.h>
36 #include <ModelAPI_Validator.h>
37
38 #include <XAO_Xao.hxx>
39
40 #include <ExchangePlugin_Tools.h>
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  * Request for initialization of data model of the feature: adding all attributes
53  */
54 void ExchangePlugin_ExportFeature::initAttributes()
55 {
56   data()->addAttribute(ExchangePlugin_ExportFeature::EXPORT_TYPE_ID(), ModelAPI_AttributeString::typeId());
57   data()->addAttribute(ExchangePlugin_ExportFeature::FILE_PATH_ID(), ModelAPI_AttributeString::typeId());
58   data()->addAttribute(ExchangePlugin_ExportFeature::FILE_FORMAT_ID(), ModelAPI_AttributeString::typeId());
59   data()->addAttribute(ExchangePlugin_ExportFeature::SELECTION_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
60   data()->addAttribute(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID(), ModelAPI_AttributeString::typeId());
61
62   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), ExchangePlugin_ExportFeature::XAO_AUTHOR_ID());
63 }
64
65 /*
66  * Computes or recomputes the results
67  */
68 void ExchangePlugin_ExportFeature::execute()
69 {
70   AttributeStringPtr aFormatAttr =
71       this->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID());
72   std::string aFormat = aFormatAttr->value();
73   // Format may be empty. In this case look at extension.
74 //  if (aFormat.empty())
75 //    return;
76
77   AttributeStringPtr aFilePathAttr =
78       this->string(ExchangePlugin_ExportFeature::FILE_PATH_ID());
79   std::string aFilePath = aFilePathAttr->value();
80   if (aFilePath.empty())
81     return;
82
83   AttributeSelectionListPtr aSelectionListAttr =
84       this->selectionList(ExchangePlugin_ExportFeature::SELECTION_LIST_ID());
85   std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
86   for (int i = 0, aSize = aSelectionListAttr->size(); i < aSize; ++i) {
87     AttributeSelectionPtr anAttrSelection = aSelectionListAttr->value(i);
88     std::shared_ptr<GeomAPI_Shape> aCurShape = anAttrSelection->value();
89     if (aCurShape.get() == NULL)
90       aCurShape = anAttrSelection->context()->shape();
91     if (aCurShape.get() != NULL)
92       aShapes.push_back(aCurShape);
93   }
94
95   // Store compound if we have more than one shape.
96   std::shared_ptr<GeomAPI_Shape> aShape;
97   if(aShapes.size() == 1) {
98     aShape = aShapes.front();
99   } else {
100     aShape = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
101   }
102
103   exportFile(aFilePath, aFormat, aShape);
104 }
105
106 void ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName,
107                                               const std::string& theFormat,
108                                               std::shared_ptr<GeomAPI_Shape> theShape)
109 {
110   std::string aFormatName = theFormat;
111
112   if (aFormatName.empty()) { // get default format for the extension
113     // ".brep" -> "BREP"
114     std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(theFileName);
115     if (anExtension == "BREP" || anExtension == "BRP") {
116       aFormatName = "BREP";
117     } else if (anExtension == "STEP" || anExtension == "STP") {
118       aFormatName = "STEP";
119     } else if (anExtension == "IGES" || anExtension == "IGS") {
120       aFormatName = "IGES-5.1";
121     } else if (anExtension == "XAO") {
122       aFormatName = "XAO";
123     } else {
124       aFormatName = anExtension;
125     }
126   }
127
128   if (aFormatName == "XAO") {
129     exportXAO(theFileName, theShape);
130     return;
131   }
132
133   // Perform the export
134   std::string anError;
135   bool aResult = false;
136   if (aFormatName == "BREP") {
137     aResult = BREPExport(theFileName, aFormatName, theShape, anError);
138   } else if (aFormatName == "STEP") {
139     aResult = STEPExport(theFileName, aFormatName, theShape, anError);
140   } else if (aFormatName.substr(0, 4) == "IGES") {
141     aResult = IGESExport(theFileName, aFormatName, theShape, anError);
142   } else {
143     anError = "Unsupported format: " + aFormatName;
144   }
145
146   if (!anError.empty()) {
147     setError("An error occurred while exporting " + theFileName + ": " + anError);
148     return;
149   }
150 }
151
152 void ExchangePlugin_ExportFeature::exportXAO(const std::string& theFileName,
153                                              std::shared_ptr<GeomAPI_Shape> theShape)
154 {
155   std::string anAuthor = string(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID())->value();
156
157   XAO::Xao aXao(anAuthor, "1.0");
158
159   std::string anError;
160   XAOExport(theFileName, theShape, &aXao, anError);
161
162   if (!anError.empty()) {
163     setError("An error occurred while exporting " + theFileName + ": " + anError);
164     return;
165   }
166 }
167