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>
12 #include <ExchangePlugin_Tools.h>
14 #include <GeomAlgoAPI_BREPExport.h>
15 #include <GeomAlgoAPI_STEPExport.h>
16 #include <GeomAlgoAPI_IGESExport.h>
18 #include <Config_Common.h>
19 #include <Config_PropManager.h>
21 #include <GeomAlgoAPI_CompoundBuilder.h>
23 #include <GeomAPI_Shape.h>
25 #include <ModelAPI_AttributeSelectionList.h>
26 #include <ModelAPI_AttributeString.h>
27 #include <ModelAPI_Data.h>
28 #include <ModelAPI_Document.h>
29 #include <ModelAPI_Object.h>
30 #include <ModelAPI_ResultBody.h>
32 #include <TCollection_AsciiString.hxx>
33 #include <TDF_Label.hxx>
34 #include <TopoDS_Shape.hxx>
35 #include <OSD_Path.hxx>
45 ExchangePlugin_ExportFeature::ExchangePlugin_ExportFeature()
49 ExchangePlugin_ExportFeature::~ExchangePlugin_ExportFeature()
51 // TODO Auto-generated destructor stub
55 * Returns the unique kind of a feature
57 const std::string& ExchangePlugin_ExportFeature::getKind()
59 return ExchangePlugin_ExportFeature::ID();
63 * Request for initialization of data model of the feature: adding all attributes
65 void ExchangePlugin_ExportFeature::initAttributes()
67 data()->addAttribute(ExchangePlugin_ExportFeature::FILE_FORMAT_ID(), ModelAPI_AttributeString::typeId());
68 data()->addAttribute(ExchangePlugin_ExportFeature::FILE_PATH_ID(), ModelAPI_AttributeString::typeId());
69 data()->addAttribute(ExchangePlugin_ExportFeature::SELECTION_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
73 * Computes or recomputes the results
75 void ExchangePlugin_ExportFeature::execute()
77 AttributeStringPtr aFormatAttr =
78 this->string(ExchangePlugin_ExportFeature::FILE_FORMAT_ID());
79 std::string aFormat = aFormatAttr->value();
80 // Format may be empty. In this case look at extension.
81 // if (aFormat.empty())
84 AttributeStringPtr aFilePathAttr =
85 this->string(ExchangePlugin_ExportFeature::FILE_PATH_ID());
86 std::string aFilePath = aFilePathAttr->value();
87 if (aFilePath.empty())
90 AttributeSelectionListPtr aSelectionListAttr =
91 this->selectionList(ExchangePlugin_ExportFeature::SELECTION_LIST_ID());
92 std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
93 for (int i = 0, aSize = aSelectionListAttr->size(); i < aSize; ++i) {
94 aShapes.push_back(aSelectionListAttr->value(i)->value());
96 std::shared_ptr<GeomAPI_Shape> aShape =
97 GeomAlgoAPI_CompoundBuilder::compound(aShapes);
99 exportFile(aFilePath, aFormat, aShape);
102 bool ExchangePlugin_ExportFeature::exportFile(const std::string& theFileName,
103 const std::string& theFormat,
104 std::shared_ptr<GeomAPI_Shape> theShape)
106 // retrieve the file and plugin library names
107 TCollection_AsciiString aFileName(theFileName.c_str());
108 TCollection_AsciiString aFormatName(theFormat.c_str());
110 if (theFormat.empty()) { // look at extension
111 OSD_Path aPath(aFileName);
112 TCollection_AsciiString anExtension = aPath.Extension();
113 // ".brep" -> "BREP", TCollection_AsciiString are numbered from 1
114 anExtension = anExtension.SubString(2, anExtension.Length());
115 anExtension.UpperCase();
116 if (anExtension == "BREP" || anExtension == "BRP") {
117 aFormatName = "BREP";
118 } else if (anExtension == "STEP" || anExtension == "STP") {
119 aFormatName = "STEP";
120 } else if (anExtension == "IGES" || anExtension == "IGS") {
121 aFormatName = "IGES-5.1";
123 aFormatName = anExtension;
127 // Perform the export
128 TCollection_AsciiString anError;
129 TopoDS_Shape aShape(theShape->impl<TopoDS_Shape>());
130 bool aResult = false;
131 if (aFormatName == "BREP") {
132 aResult = BREPExport::Export(aFileName, aFormatName, aShape, anError);
133 } else if (aFormatName == "STEP") {
134 aResult = STEPExport::Export(aFileName, aFormatName, aShape, anError);
135 } else if (aFormatName.SubString(1, 4) == "IGES") {
136 aResult = IGESExport::Export(aFileName, aFormatName, aShape, anError);
138 anError = TCollection_AsciiString("Unsupported format ") + aFormatName;
142 std::string aShapeError =
143 "An error occurred while exporting " + theFileName + ": " + anError.ToCString();
144 setError(aShapeError);