Salome HOME
8f2acd4383dcd474bb8e565107a62beb9364bd27
[modules/hexablock.git] / src / HEXABLOCK / HexEltBase.cxx
1
2 // C++ : Element de base 
3
4 // Copyright (C) 2009-2012  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 #include "HexEltBase.hxx"
23 #include "HexDocument.hxx"
24 #include "HexDiagnostics.hxx"
25
26 BEGIN_NAMESPACE_HEXA
27 // =================================================== Constructeur
28 EltBase::EltBase (Document* doc, EnumElt type)
29 {
30    el_root   = doc;
31    el_type   = type;
32    el_id     = 0;
33
34    el_next   = NULL;
35    el_assoc  = NULL;
36    el_status = HOK;
37    el_mark   = 0;
38
39    // EL_NONE, EL_VERTEX, EL_EDGE, EL_QUAD, EL_HEXA, EL_REMOVED
40
41    if (el_root==NULL)
42       {
43       el_name  = "NoValid";
44       setError ();
45       return;
46       }
47
48    el_id    = el_root->doc_nbr_elt [el_type];
49    el_root->doc_nbr_elt  [el_type] ++;
50    el_root->doc_last_elt [el_type] -> el_next = this;
51    el_root->doc_last_elt [el_type] = this;
52    el_root->setDeprecated (1);
53
54    char buffer [16];
55    el_name = getName (buffer);
56 }
57 // =================================================== Destructeur
58 EltBase::~EltBase ()
59 {
60    //  printf (" delete ");
61    //  dump ();
62 }
63 // =================================================== Constructeur Bis
64 // Utilise uniquement dans le constructeur de Document
65 // Creation d'un premier element fictif pour accelerer les chainages
66 EltBase::EltBase (EnumElt type)
67 {
68    el_root   = NULL;
69    el_type   = type;
70    el_id     = -1;
71
72    el_next   = NULL;
73    el_status = HOK;
74    el_mark   = 0;
75 }
76 // =================================================== remove
77 void EltBase::remove ()
78 {
79    if (el_type == EL_REMOVED)
80       return;
81
82    el_root->setDeprecated (2);
83    el_type = EL_REMOVED;
84    int nbp = el_parent.size();
85    for (int nro=0 ; nro<nbp ; nro++)
86        {
87        EltBase* elt = el_parent[nro];
88        if (elt != NULL && elt->isHere())
89            elt->remove ();
90        }           
91 }
92 // =================================================== suppress
93 void EltBase::suppress ()
94 {
95    if (el_type == EL_REMOVED)
96       return;
97
98    el_root->setDeprecated (2);
99    el_type = EL_REMOVED;
100 }
101 // ========================================================= replaceAssociation 
102 void EltBase::replaceAssociation (EltBase* orig)
103 {
104    if (   orig == NULL || orig->el_assoc == NULL
105        || orig == this || orig->el_assoc == el_assoc)
106       return;
107
108    if (el_assoc==NULL)
109       el_assoc = orig->el_assoc;
110    else 
111       el_root->hputError (W_REPL_ASSOC, this, orig);
112 }
113 // ========================================================= copyAssociation 
114 void EltBase::copyAssociation (EltBase* orig)
115 {
116    if (   orig == NULL || orig->el_assoc == NULL
117        || orig == this || orig->el_assoc == el_assoc)
118       return;
119
120    el_assoc = orig->el_assoc;
121    el_root->hputError (W_DISCO_ASSOC, orig);
122 }
123 // ========================================================= getName 
124 cpchar EltBase::getName  ()
125 {
126    return el_name.c_str() ;
127 }
128 // ========================================================= getName 
129 char* EltBase::getName  (pchar buffer)
130 {
131 // EL_NONE, EL_VERTEX, EL_EDGE, EL_QUAD, EL_HEXA, EL_REMOVED
132    sprintf (buffer, "%c%04d", ABR_TYPES[el_type], el_id);
133    return   buffer;
134 }
135 // ========================================================= printName 
136 void EltBase::printName  (cpchar sep)
137 {
138    char nom[12];
139
140    printf ("%s%s", getName(nom), sep);
141 }
142 // ========================================================= setAssociation
143 void EltBase::setAssociation (Shape* forme)
144 {
145    el_assoc = forme;
146
147    if (el_root->debug (2))
148        cout << "  Vertex " << el_name << " : setAssociation" << endl;
149 }
150 // ========================================================= addAssociation
151 int EltBase::addAssociation (Shape* forme)
152 {
153    setAssociation (forme);
154    return HOK;
155 }
156 // ========================================================= dumpRef 
157 void EltBase::dumpRef ()
158 {
159    int nbp = el_parent.size();
160    bool prems = true;
161
162    if (nbp==0)
163       {
164       printf ("\n");
165       }
166
167    for (int nro=0 ; nro<nbp ; nro++)          
168        {
169        if (el_parent[nro]->isHere ())
170           {
171           if (prems) 
172               printf ("\t isin ");
173           prems = false;
174           el_parent[nro]->printName(", ");
175           }
176        }
177
178    printf ("\n");
179 }
180 // ========================================================= addAssociation
181 bool EltBase::canBeAssociated ()
182 {
183    bool rep =   isValid() && isHere() 
184             && (el_type==EL_VERTEX || el_type==EL_EDGE || el_type==EL_QUAD);
185    return rep;
186 }
187 // ========================================================= addAssociation
188 void EltBase::setId (int ln)
189 {
190    char buffer [16];
191    bool defname = el_name == getName (buffer);
192
193    el_id     = ln;
194    int maxid = std::max (el_root->doc_nbr_elt[el_type], ln+1);
195
196    el_root->doc_nbr_elt[el_type] = maxid;
197    if (defname) 
198       el_name = getName (buffer);
199 }
200
201 END_NAMESPACE_HEXA
202