]> SALOME platform Git repositories - modules/shaper.git/blob - src/ExchangePlugin/ExchangePlugin_ImportFeature.cpp
Salome HOME
f1592b5b5779580fb7634ae49503f0468af20a65
[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 void ExchangePlugin_ImportFeature::loadNamingDS(
206     std::shared_ptr<GeomAPI_Shape> theGeomShape,
207     std::shared_ptr<ModelAPI_ResultBody> theResultBody)
208 {
209   //load result
210   theResultBody->store(theGeomShape);
211
212   int aTag(1);
213   std::string aNameMS = "Shape";
214   theResultBody->loadFirstLevel(theGeomShape, aNameMS, aTag);
215 }