Salome HOME
876dccfcec3f3387a3ab058a57d705e452281cc5
[modules/hexablock.git] / src / HEXABLOCK / HexGroup.cxx
1
2 // C++ : Implementation des groupes
3
4 // Copyright (C) 2009-2023  CEA, EDF
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, or (at your option) any later version.
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 "HexXmlWriter.hxx"
25
26 BEGIN_NAMESPACE_HEXA
27
28 static const cpchar kind_name[] = { "HexaCell", "QuadCell", "EdgeCell", 
29                       "HexaNode", "QuadNode", "EdgeNode", "VertexNode" };
30
31 // ======================================================== Constructeur
32 Group::Group   (Document* dad, cpchar nom, EnumGroup grp)
33      : EltBase (dad, EL_GROUP) 
34 {
35    std::string name = std::string(nom);
36    name.erase (name.find_last_not_of (" \n\r\t" ) + 1);
37    name.erase (0, name.find_first_not_of (" \n\r\t" ));
38
39    if (NOT name.empty())
40        setName (name);
41    
42    grp_kind = grp;
43    switch (grp_kind)
44       {
45       case HexaCell : case HexaNode :
46            grp_typelt = EL_HEXA;
47            break;
48
49       case QuadCell : case QuadNode :
50            grp_typelt = EL_QUAD;
51            break;
52
53       case EdgeNode : case EdgeCell :
54            grp_typelt = EL_EDGE;
55            break;
56
57       case VertexNode : default :
58            grp_typelt = EL_VERTEX;
59       }
60 }
61 // ======================================================== addElement
62 int Group::addElement (EltBase* elt)
63 {
64    if (elt==NULL || elt->isDeleted() || grp_typelt != elt->getType())
65       return HERR;
66  
67    grp_table.push_back (elt);
68    return HOK;
69 }
70 // ======================================================== findElement
71 int Group::findElement (EltBase* elt)
72 {
73    int nbelts = grp_table.size ();
74    for (int nro=0 ; nro < nbelts ; nro++)
75        if (grp_table[nro]==elt)
76           return nro;
77        
78    return NOTHING;
79 }
80 // ======================================================== removeElement
81 int Group::removeElement (int nro)
82 {
83    int nbelts = grp_table.size ();
84
85    if (nro<0 || nro >= nbelts)
86       return HERR;
87  
88    grp_table.erase (grp_table.begin() + nro);
89    return HOK;
90 }
91 // ======================================================== removeElement
92 int Group::removeElement (EltBase* elt)
93 {
94    int nro = findElement (elt);
95    int ier = removeElement (nro);
96    return ier;
97 }
98 // ======================================================== saveXml
99 void Group::saveXml (XmlWriter* xml)
100 {
101    char buffer[16];
102    int nbelts = grp_table.size ();
103
104    xml->addMark  ("Group");
105
106    xml->openMark ("Identification");
107    xml->addAttribute ("name",  el_name);
108    xml->addAttribute ("kind",  kind_name [grp_kind]);
109    xml->closeMark ();
110
111    for (int nro=0 ; nro<nbelts ; nro++)
112        {
113        if (grp_table[nro]->isHere ())
114           {
115           xml->openMark ("Element");
116           xml->addAttribute ("id",  grp_table[nro]->getName (buffer));
117           xml->closeMark ();
118           }
119        }
120    xml->closeMark ();
121 }
122 // ======================================================== getKind
123 EnumGroup Group::getKind (cpchar kind)
124 {
125    int nbk = sizeof (kind_name) / sizeof (cpchar);
126    for (int nro=0; nro<nbk ; nro++)
127        {
128        if (Cestegal (kind, kind_name[nro]))
129           return (EnumGroup) nro;
130        }
131    return VertexNode;
132 }
133 END_NAMESPACE_HEXA