Salome HOME
Fix naming bug for the vertices.
[modules/shaper.git] / src / GDMLPlugin / GDMLPlugin_Ellipsoid.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <GDMLPlugin_Ellipsoid.h>
22
23 #include <GeomAPI_ShapeExplorer.h>
24
25 #include <ModelAPI_Data.h>
26 #include <ModelAPI_ResultBody.h>
27 #include <ModelAPI_AttributeDouble.h>
28 #include <ModelAPI_AttributeString.h>
29
30 #include <sstream>
31
32 //=================================================================================================
33 GDMLPlugin_Ellipsoid::GDMLPlugin_Ellipsoid() // Nothing to do during instantiation
34 {
35 }
36
37 //=================================================================================================
38 void GDMLPlugin_Ellipsoid::initAttributes()
39 {
40   data()->addAttribute(GDMLPlugin_Ellipsoid::AX_ID(), ModelAPI_AttributeDouble::typeId());
41   data()->addAttribute(GDMLPlugin_Ellipsoid::BY_ID(), ModelAPI_AttributeDouble::typeId());
42   data()->addAttribute(GDMLPlugin_Ellipsoid::CZ_ID(), ModelAPI_AttributeDouble::typeId());
43   data()->addAttribute(GDMLPlugin_Ellipsoid::USE_ZCUT1_ID(), ModelAPI_AttributeString::typeId());
44   data()->addAttribute(GDMLPlugin_Ellipsoid::ZCUT1_ID(), ModelAPI_AttributeDouble::typeId());
45   data()->addAttribute(GDMLPlugin_Ellipsoid::USE_ZCUT2_ID(), ModelAPI_AttributeString::typeId());
46   data()->addAttribute(GDMLPlugin_Ellipsoid::ZCUT2_ID(), ModelAPI_AttributeDouble::typeId());
47 }
48
49 //=================================================================================================
50 void GDMLPlugin_Ellipsoid::execute()
51 {
52   std::shared_ptr<GeomAlgoAPI_Ellipsoid> anEllipsoidAlgo;
53
54   double aAx = real(AX_ID())->value();
55   double aBy = real(BY_ID())->value();
56   double aCz = real(CZ_ID())->value();
57
58   std::string useZCut1 = string(USE_ZCUT1_ID())->value();
59   std::string useZCut2 = string(USE_ZCUT2_ID())->value();
60
61   double aZCut1 = 0.;
62   if (useZCut1.empty()) {
63     aZCut1 = aCz /2.;
64   } else {
65     aZCut1 = real(ZCUT1_ID())->value();
66   }
67   double aZCut2 = 0.;
68   if (useZCut2.empty()) {
69     aZCut2 = aCz /2.;
70   } else {
71     aZCut2 = real(ZCUT2_ID())->value();
72   }
73
74   anEllipsoidAlgo = std::shared_ptr<GeomAlgoAPI_Ellipsoid>(
75     new GeomAlgoAPI_Ellipsoid(aAx, aBy, aCz, aZCut1, aZCut2));
76
77   // Check with that the arguments for anEllipsoidAlgo are correct
78   if (!anEllipsoidAlgo->check()) {
79     setError(anEllipsoidAlgo->getError(), false);
80     return;
81   }
82
83   anEllipsoidAlgo->build();
84
85   // Check if the creation of the ellipsoid is correct
86   if (!anEllipsoidAlgo->isDone()) {
87     setError(anEllipsoidAlgo->getError(), false);
88     return;
89   }
90
91   // Check if the created ellipsoid is valid
92   if (!anEllipsoidAlgo->checkValid("Ellipsoid builder")) {
93     setError(anEllipsoidAlgo->getError(), false);
94     return;
95   }
96
97   int aResultIndex = 0;
98   ResultBodyPtr aResultEllipsoid = document()->createBody(data(), aResultIndex);
99   loadNamingDS(anEllipsoidAlgo, aResultEllipsoid);
100   setResult(aResultEllipsoid, aResultIndex);
101
102 }
103
104 //=================================================================================================
105 void GDMLPlugin_Ellipsoid::loadNamingDS(std::shared_ptr<GeomAlgoAPI_Ellipsoid> theEllipsoidAlgo,
106                                         std::shared_ptr<ModelAPI_ResultBody> theResultEllipsoid)
107 {
108   // Load the result
109   theResultEllipsoid->store(theEllipsoidAlgo->shape());
110
111   // Prepare the naming
112   theEllipsoidAlgo->prepareNamingFaces();
113
114   // Insert to faces
115   // Naming for faces and edges
116   int num = 1;
117   std::map< std::string, std::shared_ptr<GeomAPI_Shape> > listOfFaces =
118       theEllipsoidAlgo->getCreatedFaces();
119   for (std::map< std::string, std::shared_ptr<GeomAPI_Shape> >::iterator
120        it=listOfFaces.begin(); it!=listOfFaces.end(); ++it) {
121     std::shared_ptr<GeomAPI_Shape> aFace = (*it).second;
122     theResultEllipsoid->generated(aFace, (*it).first, num++);
123   }
124
125   // Naming vertices
126   GeomAPI_DataMapOfShapeShape aVertices;
127   GeomAPI_ShapeExplorer aVertExp(theEllipsoidAlgo->shape(), GeomAPI_Shape::VERTEX);
128   for(int anIndex = 1; aVertExp.more(); aVertExp.next()) {
129     if (!aVertices.isBound(aVertExp.current())) {
130       std::ostringstream aStream;
131       aStream<<"Vertex_"<<anIndex++;
132       theResultEllipsoid->generated(aVertExp.current(), aStream.str(), num++);
133       aVertices.bind(aVertExp.current(), aVertExp.current());
134     }
135   }
136 }
137