]> SALOME platform Git repositories - modules/shaper.git/blob - src/ExchangePlugin/ExchangePlugin_ImportFeature.cpp
Salome HOME
8e50157e647fc1966aac1aaa37f2bf75f579c951
[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 // Author:  Sergey BELASH
6
7 #include <ExchangePlugin_ImportFeature.h>
8
9 #include <algorithm>
10 #include <string>
11 #ifdef _DEBUG
12 #include <iostream>
13 #include <ostream>
14 #endif
15
16 #include <Config_Common.h>
17 #include <Config_PropManager.h>
18
19 #include <GeomAlgoAPI_BREPImport.h>
20 #include <GeomAlgoAPI_IGESImport.h>
21 #include <GeomAlgoAPI_STEPImport.h>
22 #include <GeomAlgoAPI_Tools.h>
23 #include <GeomAlgoAPI_XAOImport.h>
24
25 #include <GeomAPI_Shape.h>
26
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>
39
40 #include <XAO_Xao.hxx>
41 #include <XAO_Group.hxx>
42
43 ExchangePlugin_ImportFeature::ExchangePlugin_ImportFeature()
44 {
45 }
46
47 ExchangePlugin_ImportFeature::~ExchangePlugin_ImportFeature()
48 {
49   // TODO Auto-generated destructor stub
50 }
51
52 /*
53  * Request for initialization of data model of the feature: adding all attributes
54  */
55 void ExchangePlugin_ImportFeature::initAttributes()
56 {
57   data()->addAttribute(ExchangePlugin_ImportFeature::FILE_PATH_ID(), ModelAPI_AttributeString::typeId());
58   data()->addAttribute(ExchangePlugin_ImportFeature::GROUP_LIST_ID(), ModelAPI_AttributeRefList::typeId());
59
60   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), ExchangePlugin_ImportFeature::GROUP_LIST_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   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().empty()
139       ? GeomAlgoAPI_Tools::File_Tools::name(theFileName)
140       : aXaoGeometry->getName();
141   data()->setName(aBodyName);
142
143   ResultBodyPtr aResultBody = createResultBody(aGeomShape);
144   setResult(aResultBody);
145
146   // Process groups
147   AttributeRefListPtr aRefListOfGroups = reflist(ExchangePlugin_ImportFeature::GROUP_LIST_ID());
148
149   // Remove previous groups stored in RefList
150   std::list<ObjectPtr> anGroupList = aRefListOfGroups->list();
151   std::list<ObjectPtr>::iterator anGroupIt = anGroupList.begin();
152   for (; anGroupIt != anGroupList.end(); ++anGroupIt) {
153     std::shared_ptr<ModelAPI_Feature> aFeature =
154         std::dynamic_pointer_cast<ModelAPI_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 = document()->addFeature("Group", false);
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     aSelectionList->setSelectionType(XAO::XaoUtils::dimensionToString(aXaoGroup->getDimension()));
172     for (int anElementIndex = 0; anElementIndex < aXaoGroup->count(); ++anElementIndex) {
173       aSelectionList->append(aResultBody, GeomShapePtr());
174       // complex conversion of element index to reference id
175       int anElementID = aXaoGroup->get(anElementIndex);
176       std::string aReferenceString =
177           aXaoGeometry->getElementReference(aXaoGroup->getDimension(), anElementID);
178       int aReferenceID = XAO::XaoUtils::stringToInt(aReferenceString);
179
180       aSelectionList->value(anElementIndex)->setId(aReferenceID);
181     }
182
183     aRefListOfGroups->append(aGroupFeature);
184
185     document()->setCurrentFeature(aGroupFeature, true);
186   }
187 }
188
189 //============================================================================
190 void ExchangePlugin_ImportFeature::loadNamingDS(
191     std::shared_ptr<GeomAPI_Shape> theGeomShape,
192     std::shared_ptr<ModelAPI_ResultBody> theResultBody)
193 {
194   //load result
195   theResultBody->store(theGeomShape);
196
197   int aTag(1);
198   std::string aNameMS = "Shape";
199   theResultBody->loadFirstLevel(theGeomShape, aNameMS, aTag);
200 }