Salome HOME
e55c4efd9e2eeb0d86c2783e898c950cc28d76cf
[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(),
56                        ModelAPI_AttributeString::typeId());
57   AttributePtr aFeaturesAttribute =
58     data()->addAttribute(ExchangePlugin_ImportFeature::FEATURES_ID(),
59                          ModelAPI_AttributeRefList::typeId());
60   aFeaturesAttribute->setIsArgument(false);
61
62   ModelAPI_Session::get()->validators()->registerNotObligatory(
63       getKind(), ExchangePlugin_ImportFeature::FEATURES_ID());
64 }
65
66 /*
67  * Computes or recomputes the results
68  */
69 void ExchangePlugin_ImportFeature::execute()
70 {
71   AttributeStringPtr aFilePathAttr = string(ExchangePlugin_ImportFeature::FILE_PATH_ID());
72   std::string aFilePath = aFilePathAttr->value();
73   if (aFilePath.empty()) {
74     setError("File path is empty.");
75     return;
76   }
77
78   importFile(aFilePath);
79 }
80
81 std::shared_ptr<ModelAPI_ResultBody> ExchangePlugin_ImportFeature::createResultBody(
82     std::shared_ptr<GeomAPI_Shape> aGeomShape)
83 {
84   std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
85   //LoadNamingDS of the imported shape
86   loadNamingDS(aGeomShape, aResultBody);
87   return aResultBody;
88 }
89
90 void ExchangePlugin_ImportFeature::importFile(const std::string& theFileName)
91 {
92   // "*.brep" -> "BREP"
93   std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(theFileName);
94
95   if (anExtension == "XAO") {
96     importXAO(theFileName);
97     return;
98   }
99
100   // Perform the import
101   std::string anError;
102   std::shared_ptr<GeomAPI_Shape> aGeomShape;
103   if (anExtension == "BREP" || anExtension == "BRP") {
104     aGeomShape = BREPImport(theFileName, anExtension, anError);
105   } else if (anExtension == "STEP" || anExtension == "STP") {
106     aGeomShape = STEPImport(theFileName, anExtension, anError);
107   } else if (anExtension == "IGES" || anExtension == "IGS") {
108     aGeomShape = IGESImport(theFileName, anExtension, anError);
109   } else {
110     anError = "Unsupported format: " + anExtension;
111   }
112
113   // Check if shape is valid
114   if (!anError.empty()) {
115     setError("An error occurred while importing " + theFileName + ": " + anError);
116     return;
117   }
118
119   // Pass the results into the model
120   std::string anObjectName = GeomAlgoAPI_Tools::File_Tools::name(theFileName);
121   data()->setName(anObjectName);
122
123   setResult(createResultBody(aGeomShape));
124 }
125
126 void ExchangePlugin_ImportFeature::importXAO(const std::string& theFileName)
127 {
128   try {
129   std::string anError;
130
131   XAO::Xao aXao;
132   std::shared_ptr<GeomAPI_Shape> aGeomShape = XAOImport(theFileName, anError, &aXao);
133
134   if (!anError.empty()) {
135     setError("An error occurred while importing " + theFileName + ": " + anError);
136     return;
137   }
138
139   XAO::Geometry* aXaoGeometry = aXao.getGeometry();
140
141   // use the geometry name or the file name for the feature
142   std::string aBodyName = aXaoGeometry->getName();
143   if (aBodyName.empty())
144     aBodyName = GeomAlgoAPI_Tools::File_Tools::name(theFileName);
145   data()->setName(aBodyName);
146
147   ResultBodyPtr aResultBody = createResultBody(aGeomShape);
148   setResult(aResultBody);
149
150   // Process groups
151   std::shared_ptr<ModelAPI_AttributeRefList> aRefListOfGroups =
152       std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(data()->attribute(FEATURES_ID()));
153
154   // Remove previous groups stored in RefList
155   std::list<ObjectPtr> anGroupList = aRefListOfGroups->list();
156   std::list<ObjectPtr>::iterator anGroupIt = anGroupList.begin();
157   for (; anGroupIt != anGroupList.end(); ++anGroupIt) {
158     std::shared_ptr<ModelAPI_Feature> aFeature = ModelAPI_Feature::feature(*anGroupIt);
159     if (aFeature)
160       document()->removeFeature(aFeature);
161   }
162
163   // Create new groups
164   for (int aGroupIndex = 0; aGroupIndex < aXao.countGroups(); ++aGroupIndex) {
165     XAO::Group* aXaoGroup = aXao.getGroup(aGroupIndex);
166
167     std::shared_ptr<ModelAPI_Feature> aGroupFeature = addFeature("Group");
168
169     // group name
170     if (!aXaoGroup->getName().empty())
171       aGroupFeature->data()->setName(aXaoGroup->getName());
172
173     // fill selection
174     AttributeSelectionListPtr aSelectionList = aGroupFeature->selectionList("group_list");
175
176     // conversion of dimension
177     XAO::Dimension aGroupDimension = aXaoGroup->getDimension();
178     std::string aDimensionString = XAO::XaoUtils::dimensionToString(aXaoGroup->getDimension());
179     std::string aSelectionType = ExchangePlugin_Tools::xaoDimension2selectionType(aDimensionString);
180
181     aSelectionList->setSelectionType(aSelectionType);
182     for (int anElementIndex = 0; anElementIndex < aXaoGroup->count(); ++anElementIndex) {
183       aSelectionList->append(aResultBody, GeomShapePtr());
184       // complex conversion of element index to reference id
185       int anElementID = aXaoGroup->get(anElementIndex);
186       std::string aReferenceString =
187           aXaoGeometry->getElementReference(aXaoGroup->getDimension(), anElementID);
188       int aReferenceID = XAO::XaoUtils::stringToInt(aReferenceString);
189
190       aSelectionList->value(anElementIndex)->setId(aReferenceID);
191     }
192   }
193   // Top avoid problems in Object Browser update: issue #1647.
194   ModelAPI_EventCreator::get()->sendReordered(
195     std::dynamic_pointer_cast<ModelAPI_Feature>(aRefListOfGroups->owner()));
196
197   } catch (XAO::XAO_Exception& e) {
198     std::string anError = e.what();
199     setError("An error occurred while importing " + theFileName + ": " + anError);
200     return;
201   }
202 }
203
204 //============================================================================
205 std::shared_ptr<ModelAPI_Feature> ExchangePlugin_ImportFeature::addFeature(
206     std::string theID)
207 {
208   std::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID, false);
209   if (aNew)
210     data()->reflist(FEATURES_ID())->append(aNew);
211   // set as current also after it becomes sub to set correctly enabled for other subs
212   //document()->setCurrentFeature(aNew, false);
213   return aNew;
214 }
215
216 void ExchangePlugin_ImportFeature::removeFeature(
217     std::shared_ptr<ModelAPI_Feature> theFeature)
218 {
219   if (!data()->isValid())
220     return;
221   AttributeRefListPtr aList = reflist(FEATURES_ID());
222   aList->remove(theFeature);
223 }
224
225 int ExchangePlugin_ImportFeature::numberOfSubs(bool forTree) const
226 {
227   return data()->reflist(FEATURES_ID())->size(true);
228 }
229
230 std::shared_ptr<ModelAPI_Feature> ExchangePlugin_ImportFeature::subFeature(
231     const int theIndex, bool forTree)
232 {
233   ObjectPtr anObj = data()->reflist(FEATURES_ID())->object(theIndex, false);
234   FeaturePtr aRes = std::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
235   return aRes;
236 }
237
238 int ExchangePlugin_ImportFeature::subFeatureId(const int theIndex) const
239 {
240   std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
241       ModelAPI_AttributeRefList>(data()->attribute(FEATURES_ID()));
242   std::list<ObjectPtr> aFeatures = aRefList->list();
243   std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin();
244   int aResultIndex = 1; // number of the counted (created) features, started from 1
245   int aFeatureIndex = -1; // number of the not-empty features in the list
246   for (; anIt != aFeatures.end(); anIt++) {
247     if (anIt->get())
248       aFeatureIndex++;
249     if (aFeatureIndex == theIndex)
250       break;
251     aResultIndex++;
252   }
253   return aResultIndex;
254 }
255
256 bool ExchangePlugin_ImportFeature::isSub(ObjectPtr theObject) const
257 {
258   // check is this feature of result
259   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
260   if (aFeature)
261     return data()->reflist(FEATURES_ID())->isInList(aFeature);
262   return false;
263 }
264
265 //============================================================================
266 void ExchangePlugin_ImportFeature::loadNamingDS(
267     std::shared_ptr<GeomAPI_Shape> theGeomShape,
268     std::shared_ptr<ModelAPI_ResultBody> theResultBody)
269 {
270   //load result
271   theResultBody->store(theGeomShape);
272
273   int aTag(1);
274   std::string aNameMS = "Shape";
275   theResultBody->loadFirstLevel(theGeomShape, aNameMS, aTag);
276 }