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