1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: ExchangePlugin_ImportFeature.cpp
4 // Created: Aug 28, 2014
5 // Author: Sergey BELASH
7 #include <ExchangePlugin_ImportFeature.h>
16 #include <Config_Common.h>
17 #include <Config_PropManager.h>
19 #include <GeomAlgoAPI_BREPImport.h>
20 #include <GeomAlgoAPI_IGESImport.h>
21 #include <GeomAlgoAPI_STEPImport.h>
22 #include <GeomAlgoAPI_Tools.h>
23 #include <GeomAlgoAPI_XAOImport.h>
25 #include <GeomAPI_Shape.h>
27 #include <ModelAPI_AttributeRefList.h>
28 #include <ModelAPI_AttributeSelectionList.h>
29 #include <ModelAPI_AttributeString.h>
30 #include <ModelAPI_BodyBuilder.h>
31 #include <ModelAPI_Data.h>
32 #include <ModelAPI_Document.h>
33 #include <ModelAPI_Events.h>
34 #include <ModelAPI_Object.h>
35 #include <ModelAPI_ResultBody.h>
36 #include <ModelAPI_ResultGroup.h>
37 #include <ModelAPI_Session.h>
38 #include <ModelAPI_Validator.h>
40 #include <XAO_Xao.hxx>
41 #include <XAO_Group.hxx>
43 ExchangePlugin_ImportFeature::ExchangePlugin_ImportFeature()
47 ExchangePlugin_ImportFeature::~ExchangePlugin_ImportFeature()
49 // TODO Auto-generated destructor stub
53 * Request for initialization of data model of the feature: adding all attributes
55 void ExchangePlugin_ImportFeature::initAttributes()
57 data()->addAttribute(ExchangePlugin_ImportFeature::FILE_PATH_ID(), ModelAPI_AttributeString::typeId());
58 data()->addAttribute(ExchangePlugin_ImportFeature::GROUP_LIST_ID(), ModelAPI_AttributeRefList::typeId());
60 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), ExchangePlugin_ImportFeature::GROUP_LIST_ID());
64 * Computes or recomputes the results
66 void ExchangePlugin_ImportFeature::execute()
68 AttributeStringPtr aFilePathAttr = string(ExchangePlugin_ImportFeature::FILE_PATH_ID());
69 std::string aFilePath = aFilePathAttr->value();
70 if (aFilePath.empty()) {
71 setError("File path is empty.");
75 importFile(aFilePath);
78 std::shared_ptr<ModelAPI_ResultBody> ExchangePlugin_ImportFeature::createResultBody(
79 std::shared_ptr<GeomAPI_Shape> aGeomShape)
81 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
82 //LoadNamingDS of the imported shape
83 loadNamingDS(aGeomShape, aResultBody);
87 void ExchangePlugin_ImportFeature::importFile(const std::string& theFileName)
90 std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(theFileName);
92 if (anExtension == "XAO") {
93 importXAO(theFileName);
99 std::shared_ptr<GeomAPI_Shape> aGeomShape;
100 if (anExtension == "BREP" || anExtension == "BRP") {
101 aGeomShape = BREPImport(theFileName, anExtension, anError);
102 } else if (anExtension == "STEP" || anExtension == "STP") {
103 aGeomShape = STEPImport(theFileName, anExtension, anError);
104 } else if (anExtension == "IGES" || anExtension == "IGS") {
105 aGeomShape = IGESImport(theFileName, anExtension, anError);
107 anError = "Unsupported format: " + anExtension;
110 // Check if shape is valid
111 if (!anError.empty()) {
112 setError("An error occurred while importing " + theFileName + ": " + anError);
116 // Pass the results into the model
117 std::string anObjectName = GeomAlgoAPI_Tools::File_Tools::name(theFileName);
118 data()->setName(anObjectName);
120 setResult(createResultBody(aGeomShape));
123 void ExchangePlugin_ImportFeature::importXAO(const std::string& theFileName)
128 std::shared_ptr<GeomAPI_Shape> aGeomShape = XAOImport(theFileName, anError, &aXao);
130 if (!anError.empty()) {
131 setError("An error occurred while importing " + theFileName + ": " + anError);
135 XAO::Geometry* aXaoGeometry = aXao.getGeometry();
137 // use the geometry name or the file name for the feature
138 std::string aBodyName = aXaoGeometry->getName().empty()
139 ? GeomAlgoAPI_Tools::File_Tools::name(theFileName)
140 : aXaoGeometry->getName();
141 data()->setName(aBodyName);
143 ResultBodyPtr aResultBody = createResultBody(aGeomShape);
144 setResult(aResultBody);
147 AttributeRefListPtr aRefListOfGroups = reflist(ExchangePlugin_ImportFeature::GROUP_LIST_ID());
149 // Remove previous groups stored in RefList
150 std::list<ObjectPtr> anGroupList = aRefListOfGroups->list();
151 std::list<ObjectPtr>::iterator anGroupIt = anGroupList.begin();
152 for (; anGroupIt != anGroupList.end(); ++anGroupIt) {
153 std::shared_ptr<ModelAPI_Feature> aFeature =
154 std::dynamic_pointer_cast<ModelAPI_Feature>(*anGroupIt);
156 document()->removeFeature(aFeature);
160 for (int aGroupIndex = 0; aGroupIndex < aXao.countGroups(); ++aGroupIndex) {
161 XAO::Group* aXaoGroup = aXao.getGroup(aGroupIndex);
163 std::shared_ptr<ModelAPI_Feature> aGroupFeature = document()->addFeature("Group", false);
166 if (!aXaoGroup->getName().empty())
167 aGroupFeature->data()->setName(aXaoGroup->getName());
170 AttributeSelectionListPtr aSelectionList = aGroupFeature->selectionList("group_list");
171 aSelectionList->setSelectionType(XAO::XaoUtils::dimensionToString(aXaoGroup->getDimension()));
172 for (int anElementIndex = 0; anElementIndex < aXaoGroup->count(); ++anElementIndex) {
173 aSelectionList->append(aResultBody, GeomShapePtr());
174 // complex conversion of element index to reference id
175 int anElementID = aXaoGroup->get(anElementIndex);
176 std::string aReferenceString =
177 aXaoGeometry->getElementReference(aXaoGroup->getDimension(), anElementID);
178 int aReferenceID = XAO::XaoUtils::stringToInt(aReferenceString);
180 aSelectionList->value(anElementIndex)->setId(aReferenceID);
183 aRefListOfGroups->append(aGroupFeature);
185 document()->setCurrentFeature(aGroupFeature, true);
189 //============================================================================
190 void ExchangePlugin_ImportFeature::loadNamingDS(
191 std::shared_ptr<GeomAPI_Shape> theGeomShape,
192 std::shared_ptr<ModelAPI_ResultBody> theResultBody)
195 theResultBody->store(theGeomShape);
198 std::string aNameMS = "Shape";
199 theResultBody->loadFirstLevel(theGeomShape, aNameMS, aTag);