Salome HOME
Copyright update 2020
[modules/shaper.git] / src / GDMLPlugin / GDMLPlugin_Ellipsoid.cpp
1 // Copyright (C) 2014-2020  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 email : webmaster.salome@opencascade.com
18 //
19
20 #include <GDMLPlugin_Ellipsoid.h>
21
22 #include <GeomAPI_ShapeExplorer.h>
23
24 #include <ModelAPI_Data.h>
25 #include <ModelAPI_ResultBody.h>
26 #include <ModelAPI_AttributeDouble.h>
27 #include <ModelAPI_AttributeString.h>
28
29 #include <sstream>
30
31 //=================================================================================================
32 GDMLPlugin_Ellipsoid::GDMLPlugin_Ellipsoid() // Nothing to do during instantiation
33 {
34 }
35
36 //=================================================================================================
37 void GDMLPlugin_Ellipsoid::initAttributes()
38 {
39   data()->addAttribute(GDMLPlugin_Ellipsoid::AX_ID(), ModelAPI_AttributeDouble::typeId());
40   data()->addAttribute(GDMLPlugin_Ellipsoid::BY_ID(), ModelAPI_AttributeDouble::typeId());
41   data()->addAttribute(GDMLPlugin_Ellipsoid::CZ_ID(), ModelAPI_AttributeDouble::typeId());
42   data()->addAttribute(GDMLPlugin_Ellipsoid::USE_ZCUT1_ID(), ModelAPI_AttributeString::typeId());
43   data()->addAttribute(GDMLPlugin_Ellipsoid::ZCUT1_ID(), ModelAPI_AttributeDouble::typeId());
44   data()->addAttribute(GDMLPlugin_Ellipsoid::USE_ZCUT2_ID(), ModelAPI_AttributeString::typeId());
45   data()->addAttribute(GDMLPlugin_Ellipsoid::ZCUT2_ID(), ModelAPI_AttributeDouble::typeId());
46 }
47
48 //=================================================================================================
49 void GDMLPlugin_Ellipsoid::execute()
50 {
51   std::shared_ptr<GeomAlgoAPI_Ellipsoid> anEllipsoidAlgo;
52
53   double aAx = real(AX_ID())->value();
54   double aBy = real(BY_ID())->value();
55   double aCz = real(CZ_ID())->value();
56
57   std::string useZCut1 = string(USE_ZCUT1_ID())->value();
58   std::string useZCut2 = string(USE_ZCUT2_ID())->value();
59
60   double aZCut1 = 0.;
61   if (useZCut1.empty()) {
62     aZCut1 = aCz /2.;
63   } else {
64     aZCut1 = real(ZCUT1_ID())->value();
65   }
66   double aZCut2 = 0.;
67   if (useZCut2.empty()) {
68     aZCut2 = aCz /2.;
69   } else {
70     aZCut2 = real(ZCUT2_ID())->value();
71   }
72
73   anEllipsoidAlgo = std::shared_ptr<GeomAlgoAPI_Ellipsoid>(
74     new GeomAlgoAPI_Ellipsoid(aAx, aBy, aCz, aZCut1, aZCut2));
75
76   // Check with that the arguments for anEllipsoidAlgo are correct
77   if (!anEllipsoidAlgo->check()) {
78     setError(anEllipsoidAlgo->getError(), false);
79     return;
80   }
81
82   anEllipsoidAlgo->build();
83
84   // Check if the creation of the ellipsoid is correct
85   if (!anEllipsoidAlgo->isDone()) {
86     setError(anEllipsoidAlgo->getError(), false);
87     return;
88   }
89
90   // Check if the created ellipsoid is valid
91   if (!anEllipsoidAlgo->checkValid("Ellipsoid builder")) {
92     setError(anEllipsoidAlgo->getError(), false);
93     return;
94   }
95
96   int aResultIndex = 0;
97   ResultBodyPtr aResultEllipsoid = document()->createBody(data(), aResultIndex);
98   loadNamingDS(anEllipsoidAlgo, aResultEllipsoid);
99   setResult(aResultEllipsoid, aResultIndex);
100
101 }
102
103 //=================================================================================================
104 void GDMLPlugin_Ellipsoid::loadNamingDS(std::shared_ptr<GeomAlgoAPI_Ellipsoid> theEllipsoidAlgo,
105                                         std::shared_ptr<ModelAPI_ResultBody> theResultEllipsoid)
106 {
107   // Load the result
108   theResultEllipsoid->store(theEllipsoidAlgo->shape());
109
110   // Prepare the naming
111   theEllipsoidAlgo->prepareNamingFaces();
112
113   // Insert to faces
114   // Naming for faces and edges
115   std::map< std::string, std::shared_ptr<GeomAPI_Shape> > listOfFaces =
116       theEllipsoidAlgo->getCreatedFaces();
117   for (std::map< std::string, std::shared_ptr<GeomAPI_Shape> >::iterator it = listOfFaces.begin();
118        it!=listOfFaces.end();
119        ++it)
120   {
121     theResultEllipsoid->generated((*it).second, (*it).first);
122   }
123
124   // Naming vertices
125   GeomAPI_DataMapOfShapeShape aVertices;
126   int anIndex = 1;
127   for (GeomAPI_ShapeExplorer aVertExp(theEllipsoidAlgo->shape(), GeomAPI_Shape::VERTEX);
128        aVertExp.more();
129        aVertExp.next())
130   {
131     if (!aVertices.isBound(aVertExp.current())) {
132       std::ostringstream aStream;
133       aStream<<"Vertex_"<<anIndex++;
134       theResultEllipsoid->generated(aVertExp.current(), aStream.str());
135       aVertices.bind(aVertExp.current(), aVertExp.current());
136     }
137   }
138 }
139