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