Salome HOME
Make import XAO with groups
[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_XAOImport.h>
23 #include <GeomAlgoAPI_Tools.h>
24
25 #include <GeomAPI_Shape.h>
26
27 #include <ModelAPI_AttributeString.h>
28 #include <ModelAPI_AttributeSelectionList.h>
29 #include <ModelAPI_BodyBuilder.h>
30 #include <ModelAPI_Data.h>
31 #include <ModelAPI_Document.h>
32 #include <ModelAPI_Object.h>
33 #include <ModelAPI_ResultBody.h>
34 #include <ModelAPI_ResultGroup.h>
35
36 #include <XAO_Xao.hxx>
37 #include <XAO_Group.hxx>
38
39 ExchangePlugin_ImportFeature::ExchangePlugin_ImportFeature()
40 {
41 }
42
43 ExchangePlugin_ImportFeature::~ExchangePlugin_ImportFeature()
44 {
45   // TODO Auto-generated destructor stub
46 }
47
48 /*
49  * Returns the unique kind of a feature
50  */
51 const std::string& ExchangePlugin_ImportFeature::getKind()
52 {
53   return ExchangePlugin_ImportFeature::ID();
54 }
55
56 /*
57  * Request for initialization of data model of the feature: adding all attributes
58  */
59 void ExchangePlugin_ImportFeature::initAttributes()
60 {
61   data()->addAttribute(ExchangePlugin_ImportFeature::FILE_PATH_ID(), ModelAPI_AttributeString::typeId());
62 }
63
64 /*
65  * Computes or recomputes the results
66  */
67 void ExchangePlugin_ImportFeature::execute()
68 {
69   AttributeStringPtr aFilePathAttr =
70       this->string(ExchangePlugin_ImportFeature::FILE_PATH_ID());
71   std::string aFilePath = aFilePathAttr->value();
72   if (aFilePath.empty())
73     return;
74
75   importFile(aFilePath);
76 }
77
78 bool ExchangePlugin_ImportFeature::importFile(const std::string& theFileName)
79 {
80   // retrieve the file and plugin library names
81   // ".brep" -> "BREP"
82   std::string anExtension = GeomAlgoAPI_Tools::File_Tools::extension(theFileName);
83
84   // Perform the import
85   std::string anError;
86
87   std::shared_ptr<GeomAPI_Shape> aGeomShape;
88   std::shared_ptr<XAO::Xao> aXao;
89   if (anExtension == "BREP" || anExtension == "BRP") {
90     aGeomShape = BREPImport(theFileName, anExtension, anError);
91   } else if (anExtension == "STEP" || anExtension == "STP") {
92     aGeomShape = STEPImport(theFileName, anExtension, anError);
93   } else if (anExtension == "IGES" || anExtension == "IGS") {
94     aGeomShape = IGESImport(theFileName, anExtension, anError);
95   } else if (anExtension == "XAO") {
96     std::shared_ptr<XAO::Xao> aTmpXao(new XAO::Xao);
97     aGeomShape = XAOImport(theFileName, anExtension, anError, aTmpXao.get());
98     if (!aGeomShape->isNull())
99       aXao = aTmpXao;
100   }
101
102   // Check if shape is valid
103   if (aGeomShape->isNull()) {
104     const static std::string aShapeError =
105       "An error occurred while importing " + theFileName + ": " + anError;
106     setError(aShapeError);
107     return false;
108   }
109
110   // Pass the results into the model
111   std::string anObjectName = GeomAlgoAPI_Tools::File_Tools::name(theFileName);
112   data()->setName(anObjectName);
113   std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
114
115   //LoadNamingDS of the imported shape
116   loadNamingDS(aGeomShape, aResultBody);
117
118   setResult(aResultBody);
119
120   if (aXao.get()) {
121     XAO::Geometry* aXaoGeometry = aXao->getGeometry();
122
123     // Creates group results
124     for (int aGroupIndex = 0; aGroupIndex < aXao->countGroups(); ++aGroupIndex) {
125       XAO::Group* aXaoGroup = aXao->getGroup(aGroupIndex);
126
127       std::shared_ptr<ModelAPI_Feature> aGroupFeature = document()->addFeature("Group", false);
128       if (aGroupFeature) {
129         if (!aXaoGroup->getName().empty())
130           aGroupFeature->data()->setName(aXaoGroup->getName());
131         AttributeSelectionListPtr aSelectionList = aGroupFeature->selectionList("group_list");
132         aSelectionList->setSelectionType(XAO::XaoUtils::dimensionToString(aXaoGroup->getDimension()));
133
134         for (int anElementIndex = 0; anElementIndex < aXaoGroup->count(); ++anElementIndex) {
135           aSelectionList->append(aResultBody, GeomShapePtr());
136           int anElementID = aXaoGroup->get(anElementIndex);
137           std::string aReferenceString =
138               aXaoGeometry->getElementReference(aXaoGroup->getDimension(), anElementID);
139           int aReferenceID = XAO::XaoUtils::stringToInt(aReferenceString);
140           aSelectionList->value(anElementIndex)->setId(aReferenceID);
141         }
142
143         document()->setCurrentFeature(aGroupFeature, true);
144       }
145     }
146   }
147
148   return true;
149 }
150
151 //============================================================================
152 void ExchangePlugin_ImportFeature::loadNamingDS(
153     std::shared_ptr<GeomAPI_Shape> theGeomShape,
154     std::shared_ptr<ModelAPI_ResultBody> theResultBody)
155 {
156   //load result
157   theResultBody->store(theGeomShape);
158
159   int aTag(1);
160   std::string aNameMS = "Shape";
161   theResultBody->loadFirstLevel(theGeomShape, aNameMS, aTag);
162 }