Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/hexablock.git] / src / HEXABLOCK / HexDocument_asso.cxx
1
2 // C++ : Associatiosn dans le document
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 "HexDocument.hxx"
23 #include "HexEdge.hxx"
24 #include "HexDiagnostics.hxx"
25
26
27 BEGIN_NAMESPACE_HEXA
28
29 static bool db = false;
30
31 int vertexInLine (Vertex* mfirst, Edges& mline, vector<int> &tsens);
32 int associateShapes (Edges& mline, int msens[], Shape* gstart, Shapes& gline, 
33                     double pstart, double pend, bool closed, bool inv=false);
34
35 // ====================================================== associateOpenedLine
36 int Document::associateOpenedLine (Edge*  mstart, Edges&  mline, Shape* gstart, 
37                                    double pstart, Shapes& gline, double pend)
38 {
39    int ier = associateLine (NULL, mstart, mline, gstart, pstart, gline, pend);
40    return ier;
41 }
42 // ====================================================== associateClosedLine
43 int Document::associateClosedLine (Vertex* vfirst, Edge*  mstart, Edges& mline, 
44                          Shape*  gstart, double pstart, bool inv, Shapes& gline)
45 {
46    if (vfirst == NULL)
47       {
48       putError (W_ASSO_LINE3);
49       return HERR;
50       }
51
52    int ier = associateLine (vfirst, mstart, mline, gstart, pstart, gline, 
53                             1.0, inv);
54    return ier;
55 }
56 // ====================================================== associateClosedLine
57 int Document::associateLine (Vertex* vfirst, Edge*  mstart, Edges& mline, 
58                     Shape*  gstart, double pstart, Shapes& gline, double pend,
59                     bool inv)
60 {
61    db = on_debug ();
62    char buffer [16], cnum [8];
63    int  nbseg  = mline.size ();
64    bool closed = vfirst != NULL;
65
66    if (mstart == NULL)
67       {
68       putError (W_ASSO_LINE4);
69       return HERR;
70       }
71                 // Contour ferme : 
72                 // Le vertex de depart n'appartient pas a l'edge de depart
73    int istart = mstart->index (vfirst);
74    if (closed && istart == NOTHING) 
75       {
76       putError (W_ASSO_LINE2, vfirst->getName (buffer));
77       return HERR;
78       }
79
80    if (db)
81       {
82       PutName (vfirst);
83       printf ("  . : ");
84       mstart->printName (" = (");
85       mstart->getVertex(V_AMONT)->printName (", ");
86       mstart->getVertex(V_AVAL) ->printName (")\n");
87
88       for (int nro=0 ; nro<nbseg ; nro++)
89           {
90           printf (" %2d : ", nro);
91           mline[nro]->printName(" = (");
92           mline[nro]->getVertex(V_AMONT)->printName(", ");
93           mline[nro]->getVertex(V_AVAL )->printName(")\n");
94           }
95       }
96
97
98    for (int ns = 0 ; ns < nbseg ; ns++)
99        {
100        if (mline[ns] == NULL)
101           {
102           sprintf (cnum, "%d", ns);
103           putError (W_ASSO_LINE5, cnum);
104           return HERR;
105           }
106        }
107
108    vector <int> tab_sens (nbseg, V_TWO);
109    int     sens  = V_AMONT;
110    int     nedge = NOTHING;
111    Vertex* pnode = mstart->getVertex (sens);
112
113    if (nbseg==0)
114       {
115       nedge = 0;
116       }
117    else if (NOT closed)
118       {
119       pnode = mstart->getVertex (sens);
120       nedge = vertexInLine (pnode, mline, tab_sens);
121       if (nedge == NOTHING) 
122          {
123          sens  = V_AVAL;
124          pnode = mstart->getVertex (sens);
125          nedge = vertexInLine (pnode, mline, tab_sens);
126          }
127       }
128         // Closed : on recherche ou se trouve le 2e vertex de mstart
129    else
130       {
131       sens  = 1-istart;
132       pnode = mstart->getVertex (sens);
133       nedge = vertexInLine (pnode, mline, tab_sens);
134       }
135
136    HexDisplay (nbseg);
137
138    if (nedge == NOTHING) 
139       {
140       putError (W_ASSO_LINE, mstart->getName (buffer));
141       return HERR;
142       }
143
144    Edges        les_edges;
145    vector <int> les_orig; 
146    les_edges.push_back (mstart);
147    les_orig .push_back (1-sens);
148    HexDisplay (nedge);
149    cout << " ........................................ Marque 1" << endl;
150    if (nbseg>0) 
151       {
152       les_edges.push_back (mline[nedge]);
153       les_orig. push_back (tab_sens[nedge]);
154       }
155
156    cout << " ........................................ Marque 2" << endl;
157    if (db) 
158       printf (" mstart  [%d] = mline[%d][%d] = %s\n", sens, nedge, 
159                                   tab_sens [nedge], pnode->getName(buffer));
160
161    cout << " ........................................ Marque 3" << endl;
162    for (int ns = 1 ; ns < nbseg ; ns++)
163        {
164        Vertex* pnode = mline[nedge]->getVertex (1-tab_sens [nedge]);
165        int nro = vertexInLine (pnode, mline, tab_sens);
166        if (nro == NOTHING) 
167           {
168           sprintf (cnum, "%d", nedge);
169           putError (W_ASSO_LINE2, cnum, mline[nedge]->getName (buffer));
170           return HERR;
171           }
172        if (db) 
173           printf (" mline[%d][%d] = mline[%d][%d] = %s\n", nedge, 
174                1-tab_sens [nedge], nro, tab_sens [nro], pnode->getName(buffer));
175        nedge = nro;
176        les_edges.push_back (mline    [nedge]);
177        les_orig. push_back (tab_sens [nedge]);
178        }
179
180    /********************
181    if (db)
182       {
183       printf (" ... gstart = 0x%x, pstart=%g\n", gstart, pstart);
184       for (int ns = 0 ; ns < gline.size() ; ns++)
185           {
186           printf (" ... gline[%d] = 0x%x\n", ns, gline[ns]);
187           }
188        }
189    **************************/
190     
191    if (closed)
192       {
193       Vertex* tete  = les_edges [0]    ->getVertex (les_orig[0]);
194       Vertex* queue = les_edges [nbseg]->getVertex (1-les_orig[nbseg]);
195       if (tete != queue)
196          {
197          HexDump (tete);
198          HexDump (queue);
199          HexDump (les_edges [0]);
200          HexDump (les_edges [nbseg]);
201          putError (W_ASSO_LINE6);
202          return HERR;
203          }
204       }
205    
206    int ier = associateShapes (les_edges, &les_orig[0], gstart, gline, pstart, 
207                               pend, closed, inv);
208    return ier;
209 }
210 // ====================================================== vertexInLine
211 int vertexInLine (Vertex* mfirst, Edges& mline, vector<int> &tsens)
212 {
213    int nbseg = mline.size ();
214
215    for (int ned = 0 ; ned<nbseg ; ned++)
216        {
217        if (tsens [ned] == V_TWO)
218           for (int ns=V_AMONT; ns<=V_AVAL ; ns++)
219               {
220               if (mline[ned]->getVertex (ns) == mfirst)
221                  {
222                  tsens [ned] = ns;
223                  return ned; 
224                  }
225               }
226        }
227  
228    return NOTHING;
229 }
230 // ====================================================== vertexInLine
231 void Document::clearAssociation ()
232 {
233    for (int nat=EL_VERTEX ; nat<=EL_QUAD ; nat++)
234        {
235        for (EltBase* elt = doc_first_elt[nat]->next (); elt!=NULL;
236                      elt = elt->next())
237            clear_association (elt);
238        }
239 }
240
241 END_NAMESPACE_HEXA