1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <ExchangePlugin_ExportFeature.h>
31 #include <Config_Common.h>
32 #include <Config_PropManager.h>
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>
41 #include <GeomAPI_Shape.h>
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>
57 #include <XAO_Group.hxx>
58 #include <XAO_Field.hxx>
59 #include <XAO_Xao.hxx>
60 #include <XAO_Geometry.hxx>
62 #include <ExchangePlugin_Tools.h>
64 ExchangePlugin_ExportFeature::ExchangePlugin_ExportFeature()
68 ExchangePlugin_ExportFeature::~ExchangePlugin_ExportFeature()
70 // TODO Auto-generated destructor stub
74 * Request for initialization of data model of the feature: adding all attributes
76 void ExchangePlugin_ExportFeature::initAttributes()
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());
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());
101 void ExchangePlugin_ExportFeature::attributeChanged(const std::string& theID)
103 if (theID == XAO_FILE_PATH_ID()) {
104 string(ExchangePlugin_ExportFeature::FILE_PATH_ID())->setValue(
105 string(ExchangePlugin_ExportFeature::XAO_FILE_PATH_ID())->value());
110 * Computes or recomputes the results
112 void ExchangePlugin_ExportFeature::execute()
114 AttributeStringPtr aFormatAttr =
115 this->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID());
116 std::string aFormat = aFormatAttr->value();
118 AttributeStringPtr aFilePathAttr =
119 this->string(ExchangePlugin_ExportFeature::FILE_PATH_ID());
120 std::string aFilePath = aFilePathAttr->value();
121 if (aFilePath.empty())
124 exportFile(aFilePath, aFormat);
127 void ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName,
128 const std::string& theFormat)
130 std::string aFormatName = theFormat;
132 if (aFormatName.empty()) { // get default format for the extension
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") {
144 aFormatName = anExtension;
148 if (aFormatName == "XAO") {
149 exportXAO(theFileName);
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);
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();
171 aShape = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
174 // Perform the export
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);
184 anError = "Unsupported format: " + aFormatName;
187 if (!anError.empty()) {
188 setError("An error occurred while exporting " + theFileName + ": " + anError);
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
198 case ModelAPI_AttributeTables::BOOLEAN:
199 aStr<<(theVal.myBool ? "true" : "false");
201 case ModelAPI_AttributeTables::INTEGER:
204 case ModelAPI_AttributeTables::DOUBLE:
205 aStr<<theVal.myDouble;
207 case ModelAPI_AttributeTables::STRING:
214 void ExchangePlugin_ExportFeature::exportXAO(const std::string& theFileName)
223 std::string anAuthor = string(ExchangePlugin_ExportFeature::XAO_AUTHOR_ID())->value();
224 aXao.setAuthor(anAuthor);
226 // make shape for export from all results
227 std::list<GeomShapePtr> aShapes;
228 int aBodyCount = document()->size(ModelAPI_ResultBody::group());
229 for (int aBodyIndex = 0; aBodyIndex < aBodyCount; ++aBodyIndex) {
230 ResultBodyPtr aResultBody =
231 std::dynamic_pointer_cast<ModelAPI_ResultBody>(
232 document()->object(ModelAPI_ResultBody::group(), aBodyIndex));
233 if (!aResultBody.get())
235 aShapes.push_back(aResultBody->shape());
237 GeomShapePtr aShape = (aShapes.size() == 1)
239 : GeomAlgoAPI_CompoundBuilder::compound(aShapes);
241 SetShapeToXAO(aShape, &aXao, anError);
243 if (!anError.empty()) {
244 setError("An error occurred while exporting " + theFileName + ": " + anError);
250 std::string aGeometryName = string(ExchangePlugin_ExportFeature::XAO_GEOMETRY_NAME_ID())->value();
251 aXao.getGeometry()->setName(aGeometryName);
255 int aGroupCount = document()->size(ModelAPI_ResultGroup::group());
256 for (int aGroupIndex = 0; aGroupIndex < aGroupCount; ++aGroupIndex) {
257 ResultGroupPtr aResultGroup =
258 std::dynamic_pointer_cast<ModelAPI_ResultGroup>(
259 document()->object(ModelAPI_ResultGroup::group(), aGroupIndex));
261 FeaturePtr aGroupFeature = document()->feature(aResultGroup);
263 AttributeSelectionListPtr aSelectionList =
264 aGroupFeature->selectionList("group_list");
266 // conversion of dimension
267 std::string aSelectionType = aSelectionList->selectionType();
268 std::string aDimensionString =
269 ExchangePlugin_Tools::selectionType2xaoDimension(aSelectionType);
270 XAO::Dimension aGroupDimension = XAO::XaoUtils::stringToDimension(aDimensionString);
272 XAO::Group* aXaoGroup = aXao.addGroup(aGroupDimension,
273 aResultGroup->data()->name());
275 for (int aSelectionIndex = 0; aSelectionIndex < aSelectionList->size(); ++aSelectionIndex) {
276 AttributeSelectionPtr aSelection = aSelectionList->value(aSelectionIndex);
278 // complex conversion of reference id to element index
279 int aReferenceID = aSelection->Id();
280 std::string aReferenceString = XAO::XaoUtils::intToString(aReferenceID);
282 aXao.getGeometry()->getElementIndexByReference(aGroupDimension, aReferenceString);
284 aXaoGroup->add(anElementID);
289 int aFieldCount = document()->size(ModelAPI_ResultField::group());
290 for (int aFieldIndex = 0; aFieldIndex < aFieldCount; ++aFieldIndex) {
291 ResultFieldPtr aResultField =
292 std::dynamic_pointer_cast<ModelAPI_ResultField>(
293 document()->object(ModelAPI_ResultField::group(), aFieldIndex));
295 FeaturePtr aFieldFeature = document()->feature(aResultField);
297 AttributeSelectionListPtr aSelectionList =
298 aFieldFeature->selectionList("selected");
300 // conversion of dimension
301 std::string aSelectionType = aSelectionList->selectionType();
302 std::string aDimensionString =
303 ExchangePlugin_Tools::selectionType2xaoDimension(aSelectionType);
304 XAO::Dimension aFieldDimension = XAO::XaoUtils::stringToDimension(aDimensionString);
305 bool isWholePart = aSelectionType == "part";
306 // get tables and their type
307 std::shared_ptr<ModelAPI_AttributeTables> aTables = aFieldFeature->tables("values");
308 std::string aTypeString = ExchangePlugin_Tools::valuesType2xaoType(aTables->type());
309 XAO::Type aFieldType = XAO::XaoUtils::stringToFieldType(aTypeString);
311 XAO::Field* aXaoField = aXao.addField(aFieldType, aFieldDimension, aTables->columns(),
312 aResultField->data()->name());
313 // set components names
314 AttributeStringArrayPtr aComponents = aFieldFeature->stringArray("components_names");
315 for(int aComp = 0; aComp < aComponents->size(); aComp++) {
316 std::string aName = aComponents->value(aComp);
317 aXaoField->setComponentName(aComp, aName);
320 AttributeIntArrayPtr aStamps = aFieldFeature->intArray("stamps");
321 for (int aStepIndex = 0; aStepIndex < aTables->tables(); aStepIndex++) {
322 XAO::Step* aStep = aXaoField->addNewStep(aStepIndex);
323 aStep->setStep(aStepIndex);
324 int aStampIndex = aStamps->value(aStepIndex);
325 aStep->setStamp(aStampIndex);
326 int aNumElements = isWholePart ? aXaoField->countElements() : aTables->rows();
327 int aNumComps = aTables->columns();
328 std::set<int> aFilledIDs; // to fill the rest by defaults
329 // omit default values first row
330 for(int aRow = isWholePart ? 0 : 1; aRow < aNumElements; aRow++) {
331 for(int aCol = 0; aCol < aNumComps; aCol++) {
334 // element index actually is the ID of the selection
335 AttributeSelectionPtr aSelection = aSelectionList->value(aRow - 1);
337 // complex conversion of reference id to element index
338 int aReferenceID = aSelection->Id();
339 std::string aReferenceString = XAO::XaoUtils::intToString(aReferenceID);
341 aXao.getGeometry()->getElementIndexByReference(aFieldDimension, aReferenceString);
344 ModelAPI_AttributeTables::Value aVal = aTables->value(
345 isWholePart ? 0 : aRow, aCol, aStepIndex);
346 std::string aStrVal = valToString(aVal, aTables->type());
347 aStep->setStringValue(isWholePart ? aRow : anElementID, aCol, aStrVal);
348 aFilledIDs.insert(anElementID);
351 if (!isWholePart) { // fill the rest values by default ones
352 XAO::GeometricElementList::iterator allElem = aXao.getGeometry()->begin(aFieldDimension);
353 for(; allElem != aXao.getGeometry()->end(aFieldDimension); allElem++) {
354 if (aFilledIDs.find(allElem->first) != aFilledIDs.end())
356 for(int aCol = 0; aCol < aNumComps; aCol++) {
357 ModelAPI_AttributeTables::Value aVal = aTables->value(0, aCol, aStepIndex); // default
358 std::string aStrVal = valToString(aVal, aTables->type());
359 aStep->setStringValue(allElem->first, aCol, aStrVal);
367 XAOExport(theFileName, &aXao, anError);
369 if (!anError.empty()) {
370 setError("An error occurred while exporting " + theFileName + ": " + anError);
374 } catch (XAO::XAO_Exception& e) {
375 std::string anError = e.what();
376 setError("An error occurred while exporting " + theFileName + ": " + anError);