1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
4 * ExchangePlugin_ExportFeature.cpp
6 * Created on: May 14, 2015
10 #include <ExchangePlugin_ExportFeature.h>
11 #include <GeomAlgoAPI_BREPExport.h>
12 #include <GeomAlgoAPI_STEPExport.h>
13 #include <GeomAlgoAPI_IGESExport.h>
15 #include <Config_Common.h>
16 #include <Config_PropManager.h>
18 #include <GeomAlgoAPI_CompoundBuilder.h>
20 #include <GeomAPI_Shape.h>
22 #include <ModelAPI_AttributeString.h>
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_Data.h>
25 #include <ModelAPI_Document.h>
26 #include <ModelAPI_Object.h>
27 #include <ModelAPI_ResultBody.h>
28 #include <TCollection_AsciiString.hxx>
29 #include <TDF_Label.hxx>
30 #include <TopoDS_Shape.hxx>
31 #include <OSD_Path.hxx>
40 ExchangePlugin_ExportFeature::ExchangePlugin_ExportFeature()
44 ExchangePlugin_ExportFeature::~ExchangePlugin_ExportFeature()
46 // TODO Auto-generated destructor stub
50 * Returns the unique kind of a feature
52 const std::string& ExchangePlugin_ExportFeature::getKind()
54 return ExchangePlugin_ExportFeature::ID();
58 * Request for initialization of data model of the feature: adding all attributes
60 void ExchangePlugin_ExportFeature::initAttributes()
62 data()->addAttribute(ExchangePlugin_ExportFeature::FILE_PATH_ID(), ModelAPI_AttributeString::typeId());
63 data()->addAttribute(ExchangePlugin_ExportFeature::SELECTION_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
67 * Computes or recomputes the results
69 void ExchangePlugin_ExportFeature::execute()
71 AttributeStringPtr aFilePathAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(
72 data()->attribute(ExchangePlugin_ExportFeature::FILE_PATH_ID()));
73 std::string aFilePath = aFilePathAttr->value();
74 if (aFilePath.empty())
77 AttributeSelectionListPtr aSelectionListAttr =
78 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(
79 data()->attribute(ExchangePlugin_ExportFeature::SELECTION_LIST_ID()));
81 std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
82 for ( int i = 0, aSize = aSelectionListAttr->size(); i < aSize; ++i ) {
83 std::shared_ptr<ModelAPI_AttributeSelection> anSelectionAttr = aSelectionListAttr->value(i);
84 aShapes.push_back(anSelectionAttr->value());
86 std::shared_ptr<GeomAPI_Shape> aShape =
87 GeomAlgoAPI_CompoundBuilder::compound(aShapes);
89 exportFile(aFilePath, aShape);
92 bool ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName,
93 std::shared_ptr<GeomAPI_Shape> theShape)
95 // retrieve the file and plugin library names
96 TCollection_AsciiString aFileName(theFileName.c_str());
97 OSD_Path aPath(aFileName);
98 TCollection_AsciiString aFormatName = aPath.Extension();
99 // ".brep" -> "BREP", TCollection_AsciiString are numbered from 1
100 aFormatName = aFormatName.SubString(2, aFormatName.Length());
101 aFormatName.UpperCase();
103 // Perform the export
104 TCollection_AsciiString anError;
105 TDF_Label anUnknownLabel = TDF_Label();
107 TopoDS_Shape aShape(theShape->impl<TopoDS_Shape>());
109 if (aFormatName == "BREP") {
110 aResult = BREPExport::Export(aFileName, aFormatName, aShape, anError, anUnknownLabel);
111 } else if (aFormatName == "STEP" || aFormatName == "STP") {
112 aResult = STEPExport::Export(aFileName, aFormatName, aShape, anError, anUnknownLabel);
113 } else if (aFormatName == "IGES") {
114 aResult = IGESExport::Export(aFileName, aFormatName, aShape, anError, anUnknownLabel);
118 const static std::string aShapeError =
119 "An error occurred while exporting " + theFileName + ": " + anError.ToCString();
120 setError(aShapeError);