Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / ExchangePlugin / ExchangePlugin_ExportFeature.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
18 //
19
20 #include <ExchangePlugin_ExportFeature.h>
21
22 #include <algorithm>
23 #include <iterator>
24 #include <string>
25 #ifdef _DEBUG
26 #include <iostream>
27 #include <ostream>
28 #endif
29
30 #include <Config_Common.h>
31 #include <Config_PropManager.h>
32
33 #include <GeomAlgoAPI_BREPExport.h>
34 #include <GeomAlgoAPI_CompoundBuilder.h>
35 #include <GeomAlgoAPI_IGESExport.h>
36 #include <GeomAlgoAPI_STEPExport.h>
37 #include <GeomAlgoAPI_Tools.h>
38 #include <GeomAlgoAPI_XAOExport.h>
39
40 #include <GeomAPI_Shape.h>
41
42 #include <ModelAPI_AttributeSelectionList.h>
43 #include <ModelAPI_AttributeString.h>
44 #include <ModelAPI_AttributeStringArray.h>
45 #include <ModelAPI_AttributeIntArray.h>
46 #include <ModelAPI_AttributeTables.h>
47 #include <ModelAPI_Data.h>
48 #include <ModelAPI_Document.h>
49 #include <ModelAPI_Object.h>
50 #include <ModelAPI_ResultBody.h>
51 #include <ModelAPI_ResultGroup.h>
52 #include <ModelAPI_ResultField.h>
53 #include <ModelAPI_Session.h>
54 #include <ModelAPI_Validator.h>
55
56 #include <XAO_Group.hxx>
57 #include <XAO_Field.hxx>
58 #include <XAO_Xao.hxx>
59 #include <XAO_Geometry.hxx>
60
61 #include <ExchangePlugin_Tools.h>
62
63 ExchangePlugin_ExportFeature::ExchangePlugin_ExportFeature()
64 {
65 }
66
67 ExchangePlugin_ExportFeature::~ExchangePlugin_ExportFeature()
68 {
69   // TODO Auto-generated destructor stub
70 }
71
72 /*
73  * Request for initialization of data model of the feature: adding all attributes
74  */
75 void ExchangePlugin_ExportFeature::initAttributes()
76 {
77   data()->addAttribute(ExchangePlugin_ExportFeature::EXPORT_TYPE_ID(),
78     ModelAPI_AttributeString::typeId());
79   data()->addAttribute(ExchangePlugin_ExportFeature::FILE_PATH_ID(),
80     ModelAPI_AttributeString::typeId());
81   data()->addAttribute(ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID(),
82     ModelAPI_AttributeString::typeId());
83   data()->addAttribute(ExchangePlugin_ExportFeature::FILE_FORMAT_ID(),
84     ModelAPI_AttributeString::typeId());
85   data()->addAttribute(ExchangePlugin_ExportFeature::SELECTION_LIST_ID(),
86     ModelAPI_AttributeSelectionList::typeId());
87   data()->addAttribute(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID(),
88     ModelAPI_AttributeString::typeId());
89   data()->addAttribute(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID(),
90     ModelAPI_AttributeString::typeId());
91
92   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
93     ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID());
94   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
95     ExchangePlugin_ExportFeature::XAO_AUTHOR_ID());
96   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(),
97     ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID());
98 }
99
100 void ExchangePlugin_ExportFeature::attributeChanged(const std::string& theID)
101 {
102   if (theID == XAO_FILE_PATH_ID()) {
103     string(ExchangePlugin_ExportFeature::FILE_PATH_ID())->setValue(
104       string(ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID())->value());
105   }
106 }
107
108 /*
109  * Computes or recomputes the results
110  */
111 void ExchangePlugin_ExportFeature::execute()
112 {
113   AttributeStringPtr aFormatAttr =
114       this->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID());
115   std::string aFormat = aFormatAttr->value();
116
117   AttributeStringPtr aFilePathAttr =
118       this->string(ExchangePlugin_ExportFeature::FILE_PATH_ID());
119   std::string aFilePath = aFilePathAttr->value();
120   if (aFilePath.empty())
121     return;
122
123   exportFile(aFilePath, aFormat);
124 }
125
126 void ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName,
127                                               const std::string& theFormat)
128 {
129   std::string aFormatName = theFormat;
130
131   if (aFormatName.empty()) { // get default format for the extension
132     // ".brep" -> "BREP"
133     std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(theFileName);
134     if (anExtension == "BREP" || anExtension == "BRP") {
135       aFormatName = "BREP";
136     } else if (anExtension == "STEP" || anExtension == "STP") {
137       aFormatName = "STEP";
138     } else if (anExtension == "IGES" || anExtension == "IGS") {
139       aFormatName = "IGES-5.1";
140     } else if (anExtension == "XAO") {
141       aFormatName = "XAO";
142     } else {
143       aFormatName = anExtension;
144     }
145   }
146
147   if (aFormatName == "XAO") {
148     exportXAO(theFileName);
149     return;
150   }
151
152   // make shape for export from selected shapes
153   AttributeSelectionListPtr aSelectionListAttr =
154       this->selectionList(ExchangePlugin_ExportFeature::SELECTION_LIST_ID());
155   std::list<GeomShapePtr> aShapes;
156   for (int i = 0, aSize = aSelectionListAttr->size(); i < aSize; ++i) {
157     AttributeSelectionPtr anAttrSelection = aSelectionListAttr->value(i);
158     std::shared_ptr<GeomAPI_Shape> aCurShape = anAttrSelection->value();
159     if (aCurShape.get() == NULL)
160       aCurShape = anAttrSelection->context()->shape();
161     if (aCurShape.get() != NULL)
162       aShapes.push_back(aCurShape);
163   }
164
165   // Store compound if we have more than one shape.
166   std::shared_ptr<GeomAPI_Shape> aShape;
167   if(aShapes.size() == 1) {
168     aShape = aShapes.front();
169   } else {
170     aShape = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
171   }
172
173   // Perform the export
174   std::string anError;
175   bool aResult = false;
176   if (aFormatName == "BREP") {
177     aResult = BREPExport(theFileName, aFormatName, aShape, anError);
178   } else if (aFormatName == "STEP") {
179     aResult = STEPExport(theFileName, aFormatName, aShape, anError);
180   } else if (aFormatName.substr(0, 4) == "IGES") {
181     aResult = IGESExport(theFileName, aFormatName, aShape, anError);
182   } else {
183     anError = "Unsupported format: " + aFormatName;
184   }
185
186   if (!anError.empty()) {
187     setError("An error occurred while exporting " + theFileName + ": " + anError);
188     return;
189   }
190 }
191
192 /// Returns XAO string by the value from the table
193 static std::string valToString(const ModelAPI_AttributeTables::Value& theVal,
194   const ModelAPI_AttributeTables::ValueType& theType) {
195   std::ostringstream aStr; // the resulting string value
196   switch(theType) {
197   case ModelAPI_AttributeTables::BOOLEAN:
198     aStr<<(theVal.myBool ? "true" : "false");
199     break;
200   case ModelAPI_AttributeTables::INTEGER:
201     aStr<<theVal.myInt;
202     break;
203   case ModelAPI_AttributeTables::DOUBLE:
204     aStr<<theVal.myDouble;
205     break;
206   case ModelAPI_AttributeTables::STRING:
207     aStr<<theVal.myStr;
208     break;
209   }
210   return aStr.str();
211 }
212
213 void ExchangePlugin_ExportFeature::exportXAO(const std::string& theFileName)
214 {
215   try {
216
217   std::string anError;
218   XAO::Xao aXao;
219
220   // author
221
222   std::string anAuthor = string(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID())->value();
223   aXao.setAuthor(anAuthor);
224
225   // make shape for export from all results
226   std::list<GeomShapePtr> aShapes;
227   int aBodyCount = document()->size(ModelAPI_ResultBody::group());
228   for (int aBodyIndex = 0; aBodyIndex < aBodyCount; ++aBodyIndex) {
229     ResultBodyPtr aResultBody =
230         std::dynamic_pointer_cast<ModelAPI_ResultBody>(
231             document()->object(ModelAPI_ResultBody::group(), aBodyIndex));
232     if (!aResultBody.get())
233       continue;
234     aShapes.push_back(aResultBody->shape());
235   }
236   GeomShapePtr aShape = (aShapes.size() == 1)
237       ? *aShapes.begin()
238       : GeomAlgoAPI_CompoundBuilder::compound(aShapes);
239
240   SetShapeToXAO(aShape, &aXao, anError);
241
242   if (!anError.empty()) {
243     setError("An error occurred while exporting " + theFileName + ": " + anError);
244     return;
245   }
246
247   // geometry name
248
249   std::string aGeometryName = string(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID())->value();
250   aXao.getGeometry()->setName(aGeometryName);
251
252   // groups
253
254   int aGroupCount = document()->size(ModelAPI_ResultGroup::group());
255   for (int aGroupIndex = 0; aGroupIndex < aGroupCount; ++aGroupIndex) {
256     ResultGroupPtr aResultGroup =
257         std::dynamic_pointer_cast<ModelAPI_ResultGroup>(
258             document()->object(ModelAPI_ResultGroup::group(), aGroupIndex));
259
260     FeaturePtr aGroupFeature = document()->feature(aResultGroup);
261
262     AttributeSelectionListPtr aSelectionList =
263         aGroupFeature->selectionList("group_list");
264
265     // conversion of dimension
266     std::string aSelectionType = aSelectionList->selectionType();
267     std::string aDimensionString =
268       ExchangePlugin_Tools::selectionType2xaoDimension(aSelectionType);
269     XAO::Dimension aGroupDimension = XAO::XaoUtils::stringToDimension(aDimensionString);
270
271     XAO::Group* aXaoGroup = aXao.addGroup(aGroupDimension,
272                                           aResultGroup->data()->name());
273
274     for (int aSelectionIndex = 0; aSelectionIndex < aSelectionList->size(); ++aSelectionIndex) {
275       AttributeSelectionPtr aSelection = aSelectionList->value(aSelectionIndex);
276
277       // complex conversion of reference id to element index
278       int aReferenceID = aSelection->Id();
279       std::string aReferenceString = XAO::XaoUtils::intToString(aReferenceID);
280       int anElementID =
281         aXao.getGeometry()->getElementIndexByReference(aGroupDimension, aReferenceString);
282
283       aXaoGroup->add(anElementID);
284     }
285   }
286
287   // fields
288   int aFieldCount = document()->size(ModelAPI_ResultField::group());
289   for (int aFieldIndex = 0; aFieldIndex < aFieldCount; ++aFieldIndex) {
290     ResultFieldPtr aResultField =
291         std::dynamic_pointer_cast<ModelAPI_ResultField>(
292             document()->object(ModelAPI_ResultField::group(), aFieldIndex));
293
294     FeaturePtr aFieldFeature = document()->feature(aResultField);
295
296     AttributeSelectionListPtr aSelectionList =
297         aFieldFeature->selectionList("selected");
298
299     // conversion of dimension
300     std::string aSelectionType = aSelectionList->selectionType();
301     std::string aDimensionString =
302       ExchangePlugin_Tools::selectionType2xaoDimension(aSelectionType);
303     XAO::Dimension aFieldDimension = XAO::XaoUtils::stringToDimension(aDimensionString);
304     bool isWholePart = aSelectionType == "part";
305     // get tables and their type
306     std::shared_ptr<ModelAPI_AttributeTables> aTables = aFieldFeature->tables("values");
307     std::string aTypeString = ExchangePlugin_Tools::valuesType2xaoType(aTables->type());
308     XAO::Type aFieldType = XAO::XaoUtils::stringToFieldType(aTypeString);
309
310     XAO::Field* aXaoField = aXao.addField(aFieldType, aFieldDimension, aTables->columns(),
311                                           aResultField->data()->name());
312     // set components names
313     AttributeStringArrayPtr aComponents = aFieldFeature->stringArray("components_names");
314     for(int aComp = 0; aComp < aComponents->size(); aComp++) {
315       std::string aName = aComponents->value(aComp);
316       aXaoField->setComponentName(aComp, aName);
317     }
318
319     AttributeIntArrayPtr aStamps = aFieldFeature->intArray("stamps");
320     for (int aStepIndex = 0; aStepIndex < aTables->tables(); aStepIndex++) {
321       XAO::Step* aStep = aXaoField->addNewStep(aStepIndex);
322       aStep->setStep(aStepIndex);
323       int aStampIndex = aStamps->value(aStepIndex);
324       aStep->setStamp(aStampIndex);
325       int aNumElements = isWholePart ? aXaoField->countElements() : aTables->rows();
326       int aNumComps = aTables->columns();
327       std::set<int> aFilledIDs; // to fill the rest by defaults
328       // omit default values first row
329       for(int aRow = isWholePart ? 0 : 1; aRow < aNumElements; aRow++) {
330         for(int aCol = 0; aCol < aNumComps; aCol++) {
331           int anElementID = 0;
332           if (!isWholePart) {
333             // element index actually is the ID of the selection
334             AttributeSelectionPtr aSelection = aSelectionList->value(aRow - 1);
335
336             // complex conversion of reference id to element index
337             int aReferenceID = aSelection->Id();
338             std::string aReferenceString = XAO::XaoUtils::intToString(aReferenceID);
339             anElementID =
340               aXao.getGeometry()->getElementIndexByReference(aFieldDimension, aReferenceString);
341           }
342
343           ModelAPI_AttributeTables::Value aVal = aTables->value(
344             isWholePart ? 0 : aRow, aCol, aStepIndex);
345           std::string aStrVal = valToString(aVal, aTables->type());
346           aStep->setStringValue(isWholePart ? aRow : anElementID, aCol, aStrVal);
347           aFilledIDs.insert(anElementID);
348         }
349       }
350       if (!isWholePart) { // fill the rest values by default ones
351         XAO::GeometricElementList::iterator allElem = aXao.getGeometry()->begin(aFieldDimension);
352         for(; allElem != aXao.getGeometry()->end(aFieldDimension); allElem++) {
353           if (aFilledIDs.find(allElem->first) != aFilledIDs.end())
354             continue;
355           for(int aCol = 0; aCol < aNumComps; aCol++) {
356             ModelAPI_AttributeTables::Value aVal = aTables->value(0, aCol, aStepIndex); // default
357             std::string aStrVal = valToString(aVal, aTables->type());
358             aStep->setStringValue(allElem->first, aCol, aStrVal);
359           }
360         }
361       }
362     }
363   }
364
365   // exporting
366   XAOExport(theFileName, &aXao, anError);
367
368   if (!anError.empty()) {
369     setError("An error occurred while exporting " + theFileName + ": " + anError);
370     return;
371   }
372
373   } catch (XAO::XAO_Exception& e) {
374     std::string anError = e.what();
375     setError("An error occurred while exporting " + theFileName + ": " + anError);
376     return;
377   }
378 }