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_XAOImport.h>
23 #include <GeomAlgoAPI_Tools.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 = "Cann't read files with extension: " + 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();
136 data()->setName(aXaoGeometry->getName());
138 std::shared_ptr<ModelAPI_ResultBody> aResultBody = createResultBody(aGeomShape);
139 aResultBody->data()->setName(aXaoGeometry->getName());
140 setResult(aResultBody);
143 AttributeRefListPtr aRefListOfGroups = reflist(ExchangePlugin_ImportFeature::GROUP_LIST_ID());
145 // Remove previous groups stored in RefList
146 std::list<ObjectPtr> anGroupList = aRefListOfGroups->list();
147 std::list<ObjectPtr>::iterator anGroupIt = anGroupList.begin();
148 for (; anGroupIt != anGroupList.end(); ++anGroupIt) {
149 std::shared_ptr<ModelAPI_Feature> aFeature =
150 std::dynamic_pointer_cast<ModelAPI_Feature>(*anGroupIt);
152 document()->removeFeature(aFeature);
156 for (int aGroupIndex = 0; aGroupIndex < aXao.countGroups(); ++aGroupIndex) {
157 XAO::Group* aXaoGroup = aXao.getGroup(aGroupIndex);
159 std::shared_ptr<ModelAPI_Feature> aGroupFeature = document()->addFeature("Group", false);
162 if (!aXaoGroup->getName().empty())
163 aGroupFeature->data()->setName(aXaoGroup->getName());
166 AttributeSelectionListPtr aSelectionList = aGroupFeature->selectionList("group_list");
167 aSelectionList->setSelectionType(XAO::XaoUtils::dimensionToString(aXaoGroup->getDimension()));
168 for (int anElementIndex = 0; anElementIndex < aXaoGroup->count(); ++anElementIndex) {
169 aSelectionList->append(aResultBody, GeomShapePtr());
170 // complex conversion of element index to reference id
171 int anElementID = aXaoGroup->get(anElementIndex);
172 std::string aReferenceString =
173 aXaoGeometry->getElementReference(aXaoGroup->getDimension(), anElementID);
174 int aReferenceID = XAO::XaoUtils::stringToInt(aReferenceString);
176 aSelectionList->value(anElementIndex)->setId(aReferenceID);
179 aRefListOfGroups->append(aGroupFeature);
181 document()->setCurrentFeature(aGroupFeature, true);
185 //============================================================================
186 void ExchangePlugin_ImportFeature::loadNamingDS(
187 std::shared_ptr<GeomAPI_Shape> theGeomShape,
188 std::shared_ptr<ModelAPI_ResultBody> theResultBody)
191 theResultBody->store(theGeomShape);
194 std::string aNameMS = "Shape";
195 theResultBody->loadFirstLevel(theGeomShape, aNameMS, aTag);