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