]> SALOME platform Git repositories - modules/shaper.git/blob - src/ExchangePlugin/ExchangePlugin_ImportFeature.cpp
Salome HOME
4107115ce918ce816768289e9e340a3442d3bf73
[modules/shaper.git] / src / ExchangePlugin / ExchangePlugin_ImportFeature.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:    ExchangePlugin_ImportFeature.cpp
4 // Created: Aug 28, 2014
5 // Authors:  Sergey BELASH, Sergey POKHODENKO
6
7 #include <ExchangePlugin_ImportFeature.h>
8
9 #include <algorithm>
10 #include <string>
11
12 #include <Config_Common.h>
13 #include <Config_PropManager.h>
14
15 #include <GeomAlgoAPI_BREPImport.h>
16 #include <GeomAlgoAPI_IGESImport.h>
17 #include <GeomAlgoAPI_STEPImport.h>
18 #include <GeomAlgoAPI_Tools.h>
19 #include <GeomAlgoAPI_XAOImport.h>
20
21 #include <GeomAPI_Shape.h>
22
23 #include <ModelAPI_AttributeRefList.h>
24 #include <ModelAPI_AttributeSelectionList.h>
25 #include <ModelAPI_AttributeString.h>
26 #include <ModelAPI_BodyBuilder.h>
27 #include <ModelAPI_Data.h>
28 #include <ModelAPI_Document.h>
29 #include <ModelAPI_Events.h>
30 #include <ModelAPI_Object.h>
31 #include <ModelAPI_ResultBody.h>
32 #include <ModelAPI_ResultGroup.h>
33 #include <ModelAPI_Session.h>
34 #include <ModelAPI_Validator.h>
35
36 #include <XAO_Xao.hxx>
37 #include <XAO_Group.hxx>
38
39 #include <ExchangePlugin_Tools.h>
40
41 ExchangePlugin_ImportFeature::ExchangePlugin_ImportFeature()
42 {
43 }
44
45 ExchangePlugin_ImportFeature::~ExchangePlugin_ImportFeature()
46 {
47   // TODO Auto-generated destructor stub
48 }
49
50 /*
51  * Request for initialization of data model of the feature: adding all attributes
52  */
53 void ExchangePlugin_ImportFeature::initAttributes()
54 {
55   data()->addAttribute(ExchangePlugin_ImportFeature::FILE_PATH_ID(), ModelAPI_AttributeString::typeId());
56   AttributePtr aFeaturesAttribute = data()->addAttribute(ExchangePlugin_ImportFeature::FEATURES_ID(), ModelAPI_AttributeRefList::typeId());
57   aFeaturesAttribute->setIsArgument(false);
58
59   ModelAPI_Session::get()->validators()->registerNotObligatory(
60       getKind(), ExchangePlugin_ImportFeature::FEATURES_ID());
61 }
62
63 /*
64  * Computes or recomputes the results
65  */
66 void ExchangePlugin_ImportFeature::execute()
67 {
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.");
72     return;
73   }
74
75   importFile(aFilePath);
76 }
77
78 std::shared_ptr<ModelAPI_ResultBody> ExchangePlugin_ImportFeature::createResultBody(
79     std::shared_ptr<GeomAPI_Shape> aGeomShape)
80 {
81   std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
82   //LoadNamingDS of the imported shape
83   loadNamingDS(aGeomShape, aResultBody);
84   return aResultBody;
85 }
86
87 void ExchangePlugin_ImportFeature::importFile(const std::string& theFileName)
88 {
89   // "*.brep" -> "BREP"
90   std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(theFileName);
91
92   if (anExtension == "XAO") {
93     importXAO(theFileName);
94     return;
95   }
96
97   // Perform the import
98   std::string anError;
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);
106   } else {
107     anError = "Unsupported format: " + anExtension;
108   }
109
110   // Check if shape is valid
111   if (!anError.empty()) {
112     setError("An error occurred while importing " + theFileName + ": " + anError);
113     return;
114   }
115
116   // Pass the results into the model
117   std::string anObjectName = GeomAlgoAPI_Tools::File_Tools::name(theFileName);
118   data()->setName(anObjectName);
119
120   setResult(createResultBody(aGeomShape));
121 }
122
123 void ExchangePlugin_ImportFeature::importXAO(const std::string& theFileName)
124 {
125   try {
126   std::string anError;
127
128   XAO::Xao aXao;
129   std::shared_ptr<GeomAPI_Shape> aGeomShape = XAOImport(theFileName, anError, &aXao);
130
131   if (!anError.empty()) {
132     setError("An error occurred while importing " + theFileName + ": " + anError);
133     return;
134   }
135
136   XAO::Geometry* aXaoGeometry = aXao.getGeometry();
137
138   // use the geometry name or the file name for the feature
139   std::string aBodyName = aXaoGeometry->getName();
140   if (aBodyName.empty())
141     aBodyName = GeomAlgoAPI_Tools::File_Tools::name(theFileName);
142   data()->setName(aBodyName);
143
144   ResultBodyPtr aResultBody = createResultBody(aGeomShape);
145   setResult(aResultBody);
146
147   // Process groups
148   std::shared_ptr<ModelAPI_AttributeRefList> aRefListOfGroups =
149       std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(data()->attribute(FEATURES_ID()));
150
151   // Remove previous groups stored in RefList
152   std::list<ObjectPtr> anGroupList = aRefListOfGroups->list();
153   std::list<ObjectPtr>::iterator anGroupIt = anGroupList.begin();
154   for (; anGroupIt != anGroupList.end(); ++anGroupIt) {
155     std::shared_ptr<ModelAPI_Feature> aFeature = ModelAPI_Feature::feature(*anGroupIt);
156     if (aFeature)
157       document()->removeFeature(aFeature);
158   }
159
160   // Create new groups
161   for (int aGroupIndex = 0; aGroupIndex < aXao.countGroups(); ++aGroupIndex) {
162     XAO::Group* aXaoGroup = aXao.getGroup(aGroupIndex);
163
164     std::shared_ptr<ModelAPI_Feature> aGroupFeature = addFeature("Group");
165
166     // group name
167     if (!aXaoGroup->getName().empty())
168       aGroupFeature->data()->setName(aXaoGroup->getName());
169
170     // fill selection
171     AttributeSelectionListPtr aSelectionList = aGroupFeature->selectionList("group_list");
172
173     // conversion of dimension
174     XAO::Dimension aGroupDimension = aXaoGroup->getDimension();
175     std::string aDimensionString = XAO::XaoUtils::dimensionToString(aXaoGroup->getDimension());
176     std::string aSelectionType = ExchangePlugin_Tools::xaoDimension2selectionType(aDimensionString);
177
178     aSelectionList->setSelectionType(aSelectionType);
179     for (int anElementIndex = 0; anElementIndex < aXaoGroup->count(); ++anElementIndex) {
180       aSelectionList->append(aResultBody, GeomShapePtr());
181       // complex conversion of element index to reference id
182       int anElementID = aXaoGroup->get(anElementIndex);
183       std::string aReferenceString =
184           aXaoGeometry->getElementReference(aXaoGroup->getDimension(), anElementID);
185       int aReferenceID = XAO::XaoUtils::stringToInt(aReferenceString);
186
187       aSelectionList->value(anElementIndex)->setId(aReferenceID);
188     }
189   }
190   // Top avoid problems in Object Browser update: issue #1647.
191   ModelAPI_EventCreator::get()->sendReordered(
192     std::dynamic_pointer_cast<ModelAPI_Feature>(aRefListOfGroups->owner()));
193
194   } catch (XAO::XAO_Exception& e) {
195     std::string anError = e.what();
196     setError("An error occurred while importing " + theFileName + ": " + anError);
197     return;
198   }
199 }
200
201 //============================================================================
202 std::shared_ptr<ModelAPI_Feature> ExchangePlugin_ImportFeature::addFeature(
203     std::string theID)
204 {
205   std::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID, false);
206   if (aNew)
207     data()->reflist(FEATURES_ID())->append(aNew);
208   // set as current also after it becomes sub to set correctly enabled for other subs
209   //document()->setCurrentFeature(aNew, false);
210   return aNew;
211 }
212
213 void ExchangePlugin_ImportFeature::removeFeature(
214     std::shared_ptr<ModelAPI_Feature> theFeature)
215 {
216   if (!data()->isValid())
217     return;
218   AttributeRefListPtr aList = reflist(FEATURES_ID());
219   aList->remove(theFeature);
220 }
221
222 int ExchangePlugin_ImportFeature::numberOfSubs(bool forTree) const
223 {
224   return data()->reflist(FEATURES_ID())->size(true);
225 }
226
227 std::shared_ptr<ModelAPI_Feature> ExchangePlugin_ImportFeature::subFeature(
228     const int theIndex, bool forTree)
229 {
230   ObjectPtr anObj = data()->reflist(FEATURES_ID())->object(theIndex, false);
231   FeaturePtr aRes = std::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
232   return aRes;
233 }
234
235 int ExchangePlugin_ImportFeature::subFeatureId(const int theIndex) const
236 {
237   std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
238       ModelAPI_AttributeRefList>(data()->attribute(FEATURES_ID()));
239   std::list<ObjectPtr> aFeatures = aRefList->list();
240   std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin();
241   int aResultIndex = 1; // number of the counted (created) features, started from 1
242   int aFeatureIndex = -1; // number of the not-empty features in the list
243   for (; anIt != aFeatures.end(); anIt++) {
244     if (anIt->get())
245       aFeatureIndex++;
246     if (aFeatureIndex == theIndex)
247       break;
248     aResultIndex++;
249   }
250   return aResultIndex;
251 }
252
253 bool ExchangePlugin_ImportFeature::isSub(ObjectPtr theObject) const
254 {
255   // check is this feature of result
256   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
257   if (aFeature)
258     return data()->reflist(FEATURES_ID())->isInList(aFeature);
259   return false;
260 }
261
262 //============================================================================
263 void ExchangePlugin_ImportFeature::loadNamingDS(
264     std::shared_ptr<GeomAPI_Shape> theGeomShape,
265     std::shared_ptr<ModelAPI_ResultBody> theResultBody)
266 {
267   //load result
268   theResultBody->store(theGeomShape);
269
270   int aTag(1);
271   std::string aNameMS = "Shape";
272   theResultBody->loadFirstLevel(theGeomShape, aNameMS, aTag);
273 }