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