Salome HOME
Make export XAO with groups
[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_ResultGroup.h>
36 #include <ModelAPI_Session.h>
37 #include <ModelAPI_Validator.h>
38
39 #include <XAO_Group.hxx>
40 #include <XAO_Xao.hxx>
41
42 #include <ExchangePlugin_Tools.h>
43
44 ExchangePlugin_ExportFeature::ExchangePlugin_ExportFeature()
45 {
46 }
47
48 ExchangePlugin_ExportFeature::~ExchangePlugin_ExportFeature()
49 {
50   // TODO Auto-generated destructor stub
51 }
52
53 /*
54  * Request for initialization of data model of the feature: adding all attributes
55  */
56 void ExchangePlugin_ExportFeature::initAttributes()
57 {
58   data()->addAttribute(ExchangePlugin_ExportFeature::EXPORT_TYPE_ID(), ModelAPI_AttributeString::typeId());
59   data()->addAttribute(ExchangePlugin_ExportFeature::FILE_PATH_ID(), ModelAPI_AttributeString::typeId());
60   data()->addAttribute(ExchangePlugin_ExportFeature::FILE_FORMAT_ID(), ModelAPI_AttributeString::typeId());
61   data()->addAttribute(ExchangePlugin_ExportFeature::SELECTION_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
62   data()->addAttribute(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID(), ModelAPI_AttributeString::typeId());
63
64   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), ExchangePlugin_ExportFeature::SELECTION_LIST_ID());
65   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), ExchangePlugin_ExportFeature::XAO_AUTHOR_ID());
66 }
67
68 /*
69  * Computes or recomputes the results
70  */
71 void ExchangePlugin_ExportFeature::execute()
72 {
73   AttributeStringPtr aFormatAttr =
74       this->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID());
75   std::string aFormat = aFormatAttr->value();
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   exportFile(aFilePath, aFormat);
84 }
85
86 void ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName,
87                                               const std::string& theFormat)
88 {
89   std::string aFormatName = theFormat;
90
91   if (aFormatName.empty()) { // get default format for the extension
92     // ".brep" -> "BREP"
93     std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(theFileName);
94     if (anExtension == "BREP" || anExtension == "BRP") {
95       aFormatName = "BREP";
96     } else if (anExtension == "STEP" || anExtension == "STP") {
97       aFormatName = "STEP";
98     } else if (anExtension == "IGES" || anExtension == "IGS") {
99       aFormatName = "IGES-5.1";
100     } else if (anExtension == "XAO") {
101       aFormatName = "XAO";
102     } else {
103       aFormatName = anExtension;
104     }
105   }
106
107   if (aFormatName == "XAO") {
108     exportXAO(theFileName);
109     return;
110   }
111
112   // make shape for export from selected shapes
113   AttributeSelectionListPtr aSelectionListAttr =
114       this->selectionList(ExchangePlugin_ExportFeature::SELECTION_LIST_ID());
115   std::list<GeomShapePtr> aShapes;
116   for (int i = 0, aSize = aSelectionListAttr->size(); i < aSize; ++i) {
117     AttributeSelectionPtr anAttrSelection = aSelectionListAttr->value(i);
118     std::shared_ptr<GeomAPI_Shape> aCurShape = anAttrSelection->value();
119     if (aCurShape.get() == NULL)
120       aCurShape = anAttrSelection->context()->shape();
121     if (aCurShape.get() != NULL)
122       aShapes.push_back(aCurShape);
123   }
124
125   // Store compound if we have more than one shape.
126   std::shared_ptr<GeomAPI_Shape> aShape;
127   if(aShapes.size() == 1) {
128     aShape = aShapes.front();
129   } else {
130     aShape = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
131   }
132
133   // Perform the export
134   std::string anError;
135   bool aResult = false;
136   if (aFormatName == "BREP") {
137     aResult = BREPExport(theFileName, aFormatName, aShape, anError);
138   } else if (aFormatName == "STEP") {
139     aResult = STEPExport(theFileName, aFormatName, aShape, anError);
140   } else if (aFormatName.substr(0, 4) == "IGES") {
141     aResult = IGESExport(theFileName, aFormatName, aShape, 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 {
154   try {
155
156   std::string anError;
157   XAO::Xao aXao;
158
159   // author
160
161   std::string anAuthor = string(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID())->value();
162   aXao.setAuthor(anAuthor);
163
164   // make shape for export from all results
165   std::list<GeomShapePtr> aShapes;
166   int aBodyCount = document()->size(ModelAPI_ResultBody::group());
167   for (int aBodyIndex = 0; aBodyIndex < aBodyCount; ++aBodyIndex) {
168     ResultBodyPtr aResultBody =
169         std::dynamic_pointer_cast<ModelAPI_ResultBody>(
170             document()->object(ModelAPI_ResultBody::group(), aBodyIndex));
171     if (!aResultBody.get())
172       continue;
173     aShapes.push_back(aResultBody->shape());
174   }
175   GeomShapePtr aShape = (aShapes.size() == 1)
176       ? *aShapes.begin()
177       : GeomAlgoAPI_CompoundBuilder::compound(aShapes);
178
179   SetShapeToXAO(aShape, &aXao, anError);
180
181   if (!anError.empty()) {
182     setError("An error occurred while exporting " + theFileName + ": " + anError);
183     return;
184   }
185
186   // groups
187
188   int aGroupCount = document()->size(ModelAPI_ResultGroup::group());
189   for (int aGroupIndex = 0; aGroupIndex < aGroupCount; ++aGroupIndex) {
190     ResultGroupPtr aResultGroup =
191         std::dynamic_pointer_cast<ModelAPI_ResultGroup>(
192             document()->object(ModelAPI_ResultGroup::group(), aGroupIndex));
193
194     FeaturePtr aGroupFeature = document()->feature(aResultGroup);
195
196     AttributeSelectionListPtr aSelectionList =
197         aGroupFeature->selectionList("group_list");
198
199     // conversion of dimension
200     std::string aSelectionType = aSelectionList->selectionType();
201     std::string aDimensionString = ExchangePlugin_Tools::selectionType2xaoDimension(aSelectionType);
202     XAO::Dimension aGroupDimension = XAO::XaoUtils::stringToDimension(aDimensionString);
203
204     XAO::Group* aXaoGroup = aXao.addGroup(aGroupDimension,
205                                           aResultGroup->data()->name());
206
207     for (int aSelectionIndex = 0; aSelectionIndex < aSelectionList->size(); ++aSelectionIndex) {
208       AttributeSelectionPtr aSelection = aSelectionList->value(aSelectionIndex);
209
210       // complex conversion of reference id to element index
211       int aReferenceID = aSelection->Id();
212       std::string aReferenceString = XAO::XaoUtils::intToString(aReferenceID);
213       int anElementID = aXao.getGeometry()->getElementIndexByReference(aGroupDimension, aReferenceString);
214
215       aXaoGroup->add(anElementID);
216     }
217   }
218
219   // exporting
220
221   XAOExport(theFileName, &aXao, anError);
222
223   if (!anError.empty()) {
224     setError("An error occurred while exporting " + theFileName + ": " + anError);
225     return;
226   }
227
228   } catch (XAO::XAO_Exception& e) {
229     std::string anError = e.what();
230     setError("An error occurred while importing " + theFileName + ": " + anError);
231     return;
232   }
233 }