]> SALOME platform Git repositories - modules/shaper.git/blob - src/ExchangePlugin/ExchangePlugin_ExportFeature.cpp
Salome HOME
Make exportToXAO work even if a group or a field is not valid.
[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() && 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   int aGroupCount = document()->size(ModelAPI_ResultGroup::group());
262   for (int aGroupIndex = 0; aGroupIndex < aGroupCount; ++aGroupIndex) {
263     ResultGroupPtr aResultGroup =
264         std::dynamic_pointer_cast<ModelAPI_ResultGroup>(
265             document()->object(ModelAPI_ResultGroup::group(), aGroupIndex));
266
267     FeaturePtr aGroupFeature = document()->feature(aResultGroup);
268
269     AttributeSelectionListPtr aSelectionList =
270         aGroupFeature->selectionList("group_list");
271
272     // conversion of dimension
273     std::string aSelectionType = aSelectionList->selectionType();
274     std::string aDimensionString =
275       ExchangePlugin_Tools::selectionType2xaoDimension(aSelectionType);
276     XAO::Dimension aGroupDimension = XAO::XaoUtils::stringToDimension(aDimensionString);
277
278     XAO::Group* aXaoGroup = aXao.addGroup(aGroupDimension,
279                                           aResultGroup->data()->name());
280
281     try {
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     } catch (XAO::XAO_Exception& e) {
299       std::string msg = "An error occurred while exporting group " + aResultGroup->data()->name();
300       msg += ".\n";
301       msg += e.what();
302       msg += "\n";
303       msg += "=> skipping this group from XAO export.";
304       setError(msg);
305       aXao.removeGroup(aXaoGroup);
306     }
307   }
308
309   // fields
310   int aFieldCount = document()->size(ModelAPI_ResultField::group());
311   for (int aFieldIndex = 0; aFieldIndex < aFieldCount; ++aFieldIndex) {
312     ResultFieldPtr aResultField =
313         std::dynamic_pointer_cast<ModelAPI_ResultField>(
314             document()->object(ModelAPI_ResultField::group(), aFieldIndex));
315
316     FeaturePtr aFieldFeature = document()->feature(aResultField);
317
318     AttributeSelectionListPtr aSelectionList =
319         aFieldFeature->selectionList("selected");
320
321     // conversion of dimension
322     std::string aSelectionType = aSelectionList->selectionType();
323     std::string aDimensionString =
324       ExchangePlugin_Tools::selectionType2xaoDimension(aSelectionType);
325     XAO::Dimension aFieldDimension = XAO::XaoUtils::stringToDimension(aDimensionString);
326     bool isWholePart = aSelectionType == "part";
327     // get tables and their type
328     std::shared_ptr<ModelAPI_AttributeTables> aTables = aFieldFeature->tables("values");
329     std::string aTypeString = ExchangePlugin_Tools::valuesType2xaoType(aTables->type());
330     XAO::Type aFieldType = XAO::XaoUtils::stringToFieldType(aTypeString);
331
332     XAO::Field* aXaoField = aXao.addField(aFieldType, aFieldDimension, aTables->columns(),
333                                           aResultField->data()->name());
334
335
336     try {
337       // set components names
338       AttributeStringArrayPtr aComponents = aFieldFeature->stringArray("components_names");
339       for(int aComp = 0; aComp < aComponents->size(); aComp++) {
340         std::string aName = aComponents->value(aComp);
341         aXaoField->setComponentName(aComp, aName);
342       }
343
344       AttributeIntArrayPtr aStamps = aFieldFeature->intArray("stamps");
345       for (int aStepIndex = 0; aStepIndex < aTables->tables(); aStepIndex++) {
346         XAO::Step* aStep = aXaoField->addNewStep(aStepIndex);
347         aStep->setStep(aStepIndex);
348         int aStampIndex = aStamps->value(aStepIndex);
349         aStep->setStamp(aStampIndex);
350         int aNumElements = isWholePart ? aXaoField->countElements() : aTables->rows();
351         int aNumComps = aTables->columns();
352         std::set<int> aFilledIDs; // to fill the rest by defaults
353         // omit default values first row
354         for(int aRow = isWholePart ? 0 : 1; aRow < aNumElements; aRow++) {
355           for(int aCol = 0; aCol < aNumComps; aCol++) {
356             int anElementID = 0;
357             if (!isWholePart) {
358               // element index actually is the ID of the selection
359               AttributeSelectionPtr aSelection = aSelectionList->value(aRow - 1);
360
361               // complex conversion of reference id to element index
362               // gives bad id in case the selection is done from python script
363               // => using GeomAlgoAPI_CompoundBuilder::id instead
364               //int aReferenceID_old = aSelection->Id();
365
366               int aReferenceID = GeomAlgoAPI_CompoundBuilder::id(aShape, aSelection->value());
367
368               std::string aReferenceString = XAO::XaoUtils::intToString(aReferenceID);
369               anElementID =
370                 aXao.getGeometry()->getElementIndexByReference(aFieldDimension, aReferenceString);
371             }
372
373             ModelAPI_AttributeTables::Value aVal = aTables->value(
374               isWholePart ? 0 : aRow, aCol, aStepIndex);
375             std::string aStrVal = valToString(aVal, aTables->type());
376             aStep->setStringValue(isWholePart ? aRow : anElementID, aCol, aStrVal);
377             aFilledIDs.insert(anElementID);
378           }
379         }
380         if (!isWholePart) { // fill the rest values by default ones
381           XAO::GeometricElementList::iterator allElem = aXao.getGeometry()->begin(aFieldDimension);
382           for(; allElem != aXao.getGeometry()->end(aFieldDimension); allElem++) {
383             if (aFilledIDs.find(allElem->first) != aFilledIDs.end())
384               continue;
385             for(int aCol = 0; aCol < aNumComps; aCol++) {
386               ModelAPI_AttributeTables::Value aVal = aTables->value(0, aCol, aStepIndex); // default
387               std::string aStrVal = valToString(aVal, aTables->type());
388               aStep->setStringValue(allElem->first, aCol, aStrVal);
389             }
390           }
391         }
392       }
393     } catch (XAO::XAO_Exception& e) {
394       std::string msg = "An error occurred while exporting field " + aResultField->data()->name();
395       msg += ".\n";
396       msg += e.what();
397       msg += "\n";
398       msg += "=> skipping this field from XAO export.";
399       setError(msg);
400       aXao.removeField(aXaoField);
401     }
402   }
403
404   // exporting
405   XAOExport(theFileName, &aXao, anError);
406
407   if (!anError.empty()) {
408     setError("An error occurred while exporting " + theFileName + ": " + anError);
409     return;
410   }
411
412   } catch (XAO::XAO_Exception& e) {
413     std::string anError = e.what();
414     setError("An error occurred while exporting " + theFileName + ": " + anError);
415     return;
416   }
417 }