Salome HOME
82c9ba6c183a36197742e1afc3fb9ea0d54bfb51
[modules/hexablock.git] / src / HEXABLOCK / HexBiCylinderShape.cxx
1 //
2 // CC++ : Interface Cascade de la classe Elements
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/
21 // or email : webmaster.salome@opencascade.com
22 //
23 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
24
25 #include "HexBiCylinderShape.hxx"
26
27 #include "HexDocument.hxx"
28 #include "HexNewShape.hxx"
29 #include "HexEdgeShape.hxx"
30
31 #include "HexVertex.hxx"
32 #include "HexEdge.hxx"
33
34                                     // Cylindre
35 #include <BRepPrimAPI_MakeCylinder.hxx>
36 #include <BRepAlgoAPI_Section.hxx>
37
38 BEGIN_NAMESPACE_HEXA
39
40 static bool db = false;
41 int BiCylinderShape::nbr_intersections = 0;
42
43 // ====================================================== Constructeur
44 BiCylinderShape::BiCylinderShape (Document* doc)
45 {
46    bi_docu  = doc;
47    bi_shape = NULL;
48    no_inter = true;
49 }
50 // ====================================================== defineCyls
51 int BiCylinderShape::defineCyls (double* borig,  double* bnorm, double* bbase,
52                               double  brayon, double  bhaut,
53                               double* sorig,  double* snorm, double* sbase,
54                               double  srayon, double  shaut)
55 {
56                     // --------------------------- Preparation
57    gp_Pnt gpb_orig (borig [dir_x], borig [dir_y], borig [dir_z]);
58    gp_Vec gpb_norm (bnorm [dir_x], bnorm [dir_y], bnorm [dir_z]);
59    gp_Vec gpb_vx   (bbase [dir_x], bbase [dir_y], bbase [dir_z]);
60    gp_Ax2 gpb_axes (gpb_orig, gpb_norm, gpb_vx);
61 // gp_Ax2  gp_axes (gp_center, gp_norm, gp_vx);
62
63    BRepPrimAPI_MakeCylinder make_bcyl (gpb_axes, brayon, bhaut);
64    make_bcyl.Build();
65
66    gp_Pnt gps_orig (sorig [dir_x], sorig [dir_y], sorig [dir_z]);
67    gp_Vec gps_vx   (sbase [dir_x], sbase [dir_y], sbase [dir_z]);
68    gp_Vec gps_norm (snorm [dir_x], snorm [dir_y], snorm [dir_z]);
69    gp_Ax2 gps_axes (gps_orig, gps_norm, gpb_vx);
70
71    BRepPrimAPI_MakeCylinder make_scyl (gps_axes, srayon, shaut);
72    make_scyl.Build();
73
74    if (NOT make_bcyl.IsDone())
75       {
76       printf ("defineCyls : Can' build big cylinder\n");
77       return HERR;
78       }
79    else if (NOT make_scyl.IsDone())
80       {
81       printf ("defineCyls : Can' build small cylinder\n");
82       return HERR;
83       }
84                     // --------------------------- Intersection
85
86    TopoDS_Shape cyl_big   = make_bcyl.Shape();
87    TopoDS_Shape cyl_small = make_scyl.Shape();
88
89    BRepAlgoAPI_Section make_inter (cyl_big, cyl_small, false);
90    make_inter.Approximation (true);
91    make_inter.Build();
92
93    if (NOT make_inter.IsDone())
94       {
95       printf ("defineCyls : No intersection\n");
96       return HERR;
97       }
98
99    TopoDS_Shape cyl_inter = make_inter.Shape();
100    // geom_make_brep (cyl_inter, brep);
101
102                     // --------------------------- Recuperation
103    char name [16];
104    nbr_intersections ++;
105    sprintf (name, "inter_cyl%d", nbr_intersections);
106    bi_shape = bi_docu->addShape (name, SH_INTER);
107    bi_shape->setShape (cyl_inter, SH_INTER);
108    bi_shape->saveBrep ();        // PROVISOIRE
109
110    return HOK;
111 }
112 // ====================================================== anaVertex
113 // === Trouve le(s) ligne(s) contenant ce vertex et le(s) parametre(s)
114 int BiCylinderShape::anaVertex (Vertex* node, int* tline, double* tpara)
115 {
116    Real3  point;
117    node->getPoint (point);
118    int nbsol   = 0;
119    int nblines = bi_shape->countEdge ();
120
121    for (int nl=0 ; nl<nblines ; nl++)
122        {
123        EdgeShape* line  = bi_shape->getEdgeShape (nl);
124        double     param = line->getParam (point);
125        if (db) cout << " ... getParam " << node->getName()
126                     << ", point=(" << point[0] << ", " << point[1]
127                     << ", " << point[2]
128                     << "), nl=" << nl << ", param=" << param << endl;
129        if (param>=0)
130           {
131           if (nbsol>=2)
132              return nbsol;
133
134           tline [nbsol] = nl;
135           tpara [nbsol] = param;
136           nbsol ++;
137           }
138        }
139
140    if (nbsol==1)
141       {
142       if (tpara[0]<=Epsil)
143          {
144          nbsol ++;
145          tpara[1] = 1.0;
146          tline[1] = tline[0]-1;
147          if (tline[1] <0) tline[1] = nblines-1;
148          }
149       else if (tpara[0]>=UnEpsil)
150          {
151          nbsol ++;
152          tpara[1] = 0;
153          tline[1] = tline[0]+1;
154          if (tline[1] >= nblines) tline[1] = 0;
155          }
156       }
157
158    return nbsol;
159 }
160 // ====================================================== associate
161 // ==== On suppose une orientation correcte
162 int BiCylinderShape::associate (Edge* edge)
163 {
164
165    double tparam1 [V_TWO], tparam2 [V_TWO];
166    int    tline1  [V_TWO], tline2  [V_TWO];
167    int sol1 = anaVertex (edge->getVertex(V_AMONT), tline1, tparam1);
168    int sol2 = anaVertex (edge->getVertex(V_AVAL),  tline2, tparam2);
169
170    if (sol1==0 || sol2==0)
171       return HERR;
172
173    edge->clearAssociation ();
174                                 // Ligne commune ?
175    for (int ns1=0 ; ns1<sol1 ; ns1++)
176        {
177        int nlig  = tline1[ns1];
178        for (int ns2=0 ; ns2<sol2 ; ns2++)
179            {
180            if (tline2[ns2] == nlig)
181               {
182               EdgeShape* line = bi_shape->getEdgeShape (nlig);
183               double   param1 = tparam1 [ns1];
184               double   param2 = tparam2 [ns2];
185               if (param1 >= 1.0-Epsil && param1 <= 1.0+Epsil)
186                   param1  = 0.0;
187               if (param2 >= -Epsil && param2 <= Epsil)
188                   param2  = 1.0;
189               if (param1 < param2)
190                   {
191                   associate (edge, line, param1, param2, V_TWO);
192                   }
193               else
194                      // Le debut de la ligne (fermee) est entre les 2 vertex)
195                   {
196                   associate (edge, line, param1, 1, V_AMONT);
197                   associate (edge, line, 0, param2, V_AVAL);
198                   }
199               return HOK;
200               }
201            }
202        }
203
204
205    EdgeShape* line1 = bi_shape->getEdgeShape (tline1[0]);
206    EdgeShape* line2 = bi_shape->getEdgeShape (tline2[0]);
207
208    associate (edge, line1, tparam1[0], 1, V_AMONT);
209    associate (edge, line2, 0, tparam2[0], V_AVAL);
210    return HOK;
211 }
212 // ====================================================== associate
213 // ==== On suppose une orientation correcte
214 int BiCylinderShape::associate (Edge* edge, EdgeShape* line, 
215                                 double para1, double para2, int extrem)
216 {
217    edge->addAssociation (line, para1, para2);
218
219    return HOK;                    // PROVISOIRE
220    Real3   point;
221    Vertex* vertex;
222
223    if (extrem != V_AVAL)
224       {
225       line->getPoint   (para1, point);
226       vertex =  edge->getVertex (V_AMONT);
227       vertex->setAssociation (point);
228       }
229
230    if (extrem != V_AMONT)
231       {
232       line->getPoint   (para2, point);
233       vertex =  edge->getVertex (V_AVAL);
234       vertex->setAssociation (point);
235       }
236 }
237 END_NAMESPACE_HEXA