Salome HOME
0871dcaf6aa8e0d3c83c28aaef3bfbc187211c1b
[modules/hexablock.git] / src / HEXABLOCK / HexGroup.cxx
1
2 // C++ : Implementation des groupes
3
4 // Copyright (C) 2009-2013  CEA/DEN, EDF R&D
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HexGroup.hxx"
24 #include "HexEltBase.hxx"
25 #include "HexXmlWriter.hxx"
26
27 BEGIN_NAMESPACE_HEXA
28
29 int Group::last_grp_id = 0;
30 static const cpchar kind_name[] = { "HexaCell", "QuadCell", "EdgeCell", 
31                   "HexaNode", "QuadNode", "EdgeNode", "VertexNode" };
32
33 // ======================================================== Constructeur
34 Group::Group (cpchar nom, EnumGroup grp)  
35 {
36    grp_id = last_grp_id++;
37
38    //Initialisation du nom du groupe: un nom par défaut est donné s'il n'est pas fourni
39    std::string _nom = std::string(nom);
40    _nom.erase (_nom.find_last_not_of (" \n\r\t" ) + 1);
41    _nom.erase (0, _nom.find_first_not_of (" \n\r\t" ));
42    if (!_nom.empty())
43         grp_name = _nom;
44    else {
45         char buffer [16];
46         sprintf (buffer, "g%04d", grp_id);
47         grp_name = std::string(buffer);
48    }
49    
50    grp_kind = grp;
51
52    switch (grp_kind)
53       {
54       case HexaCell : case HexaNode :
55            grp_typelt = EL_HEXA;
56            break;
57
58       case QuadCell : case QuadNode :
59            grp_typelt = EL_QUAD;
60            break;
61
62       case EdgeNode : case EdgeCell :
63            grp_typelt = EL_EDGE;
64            break;
65
66       case VertexNode : default :
67            grp_typelt = EL_VERTEX;
68       }
69 }
70 // ======================================================== addElement
71 int Group::addElement (EltBase* elt)
72 {
73    if (elt==NULL || elt->isDeleted() || grp_typelt != elt->getType())
74       return HERR;
75  
76    grp_table.push_back (elt);
77    return HOK;
78 }
79 // ======================================================== findElement
80 int Group::findElement (EltBase* elt)
81 {
82    int nbelts = grp_table.size ();
83    for (int nro=0 ; nro < nbelts ; nro++)
84        if (grp_table[nro]==elt)
85           return nro;
86        
87    return NOTHING;
88 }
89 // ======================================================== removeElement
90 int Group::removeElement (int nro)
91 {
92    int nbelts = grp_table.size ();
93
94    if (nro<0 || nro >= nbelts)
95       return HERR;
96  
97    grp_table.erase (grp_table.begin() + nro);
98    return HOK;
99 }
100 // ======================================================== removeElement
101 int Group::removeElement (EltBase* elt)
102 {
103    int nro = findElement (elt);
104    int ier = removeElement (nro);
105    return ier;
106 }
107 // ======================================================== saveXml
108 void Group::saveXml (XmlWriter* xml)
109 {
110    char buffer[16];
111    int nbelts = grp_table.size ();
112
113    xml->addMark  ("Group");
114
115    xml->openMark ("Identification");
116    xml->addAttribute ("name",  grp_name);
117    xml->addAttribute ("kind",  kind_name [grp_kind]);
118    xml->closeMark ();
119
120    for (int nro=0 ; nro<nbelts ; nro++)
121        {
122        if (grp_table[nro]->isHere ())
123           {
124           xml->openMark ("Element");
125           xml->addAttribute ("id",  grp_table[nro]->getName (buffer));
126           xml->closeMark ();
127           }
128        }
129    xml->closeMark ();
130 }
131
132 // ========================================================= getNextName
133 char* Group::getNextName  (pchar buffer)
134 {
135    sprintf (buffer, "g%04d", last_grp_id);
136    return   buffer;
137 }
138
139 // ======================================================== getKind
140 EnumGroup Group::getKind (cpchar kind)
141 {
142    int nbk = sizeof (kind_name) / sizeof (cpchar);
143    for (int nro=0; nro<nbk ; nro++)
144        {
145        if (Cestegal (kind, kind_name[nro]))
146           return (EnumGroup) nro;
147        }
148    return VertexNode;
149 }
150 END_NAMESPACE_HEXA