Salome HOME
Merge from V6_main 15/03/2013
[modules/hexablock.git] / src / HEXABLOCK / HexElements_asso.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/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HexElements.hxx"
24 #include "HexEdge.hxx"
25 #include "HexDocument.hxx"
26
27 #include "HexKasBiCylinder.hxx"
28 #include "HexKasPoint.hxx"
29 #include "HexKasLine.hxx"
30
31 #include <BRepTools.hxx>
32 #include <BRep_Builder.hxx>
33 #include <BRepAdaptor_Curve.hxx>
34 #include <BRepBuilderAPI_Transform.hxx>
35 #include <BRepBuilderAPI_MakeVertex.hxx>
36
37 #include <GCPnts_AbscissaPoint.hxx>
38
39 #include <TopoDS.hxx>
40 #include <TopoDS_Shape.hxx>
41 #include <TopoDS_Edge.hxx>
42 #include <TopoDS_Compound.hxx>
43 #include <TopoDS_Vertex.hxx>
44
45 #include <gp_Pln.hxx>
46 #include <gp_Pnt.hxx>
47 #include <gp_Dir.hxx>
48 #include <gp_Lin.hxx>
49
50 // HEXABLOCK includes
51 #include "HexVertex.hxx"
52 #include "HexOldShape.hxx"
53 #include "HexDiagnostics.hxx"
54                                     // Cercles
55 #include <GEOMImpl_CircleDriver.hxx>
56 #include <GEOMImpl_ICircle.hxx>
57
58 #include <BRepBuilderAPI_MakeEdge.hxx>
59 #include <BRep_Tool.hxx>
60
61 #include <GC_MakeCircle.hxx>
62 #include <Geom_Circle.hxx>
63
64 #include <gp_Circ.hxx>
65                                     // Sphere
66 #include <BRepPrimAPI_MakeSphere.hxx>
67                                     // Cylindre
68 #include <GEOMImpl_CylinderDriver.hxx>
69 #include <GEOMImpl_ICylinder.hxx>
70 #include <BRepPrimAPI_MakeCylinder.hxx>
71 #include <BRepAlgoAPI_Section.hxx>
72
73 BEGIN_NAMESPACE_HEXA
74
75 static bool db = false;
76
77 void clear_associations (Edge* edge);
78
79 // ---------------------------------------------------------------------
80
81 static KasLine      current_line;
82 static Shape        current_shape ("");
83
84 // ========================================================= cutAssociation
85 void Elements::cutAssociation (Shapes& tshapes, Edges& tedges, bool exist)
86 {
87    db = on_debug ();
88    char foo[18];
89    int nbedges  = tedges.size();
90    int nbshapes = tshapes.size ();
91    if (nbshapes==0)
92       return;
93
94    std::vector <KasLine>  tab_gline  (nbshapes);
95
96    Vertex* prems = tedges [0]         -> getVertex (V_AMONT);
97    Vertex* derns = tedges [nbedges-1] -> getVertex (V_AVAL);
98
99    KasPoint pnt_first, pnt_last;
100    pnt_first.definePoint (prems);
101    pnt_last .definePoint (derns);
102
103    if (exist)
104       {
105       if (pnt_first.isBad())
106          {
107          el_root->putError (W_ASSO_CUT1, prems->getName (foo));
108          return;
109          }
110       else if (pnt_last.isBad ())
111          {
112          el_root->putError (W_ASSO_CUT2, derns->getName (foo));
113          return;
114          }
115       }
116                             // ----------- Define + longueur totale
117    double  longueur = 0;
118    for (int ns = 0 ; ns<nbshapes ; ns++)
119        {
120        tab_gline[ns].defineLine (tshapes[ns]);
121        longueur += tab_gline[ns].getLength ();
122        }
123
124                             // ----------- Ordonnancement des shapes
125    double  abscisse = 0;
126    int     nslast   = 0;
127    double* coord    = pnt_first.getCoord ();
128    if (nbshapes==1)
129       {
130       tab_gline [0].setRank (0, 0, abscisse);
131       }
132    else
133       {
134
135       for (int rg = 0 ; rg<nbshapes ; rg++)
136           {
137           bool more = true;
138           for (int ns = 0 ; ns<nbshapes && more ; ns++)
139               {
140               if (tab_gline[ns].getRank()==NOTHING)
141                  {
142                  int pos = tab_gline[ns].findBound (coord);
143                  if (pos!=NOTHING)
144                     {
145                     more   = false;
146                     nslast = ns;
147                     tab_gline [ns].setRank (rg, pos, abscisse);
148                     coord = tab_gline [ns].getEnd ();
149                     }
150                  }
151               }
152                           // Pas trouve
153           if (more)
154              {
155              el_root->putError (W_ASSO_CUT3, derns->getName (foo));
156              return;
157              }
158           }
159       }
160                             // ----------- Dernier
161    if (exist)
162       {
163       coord = pnt_last.getCoord ();
164       int pos = tab_gline[nslast].findBound (coord);
165       if (pos != V_AVAL)
166          {
167          el_root->putError (W_ASSO_CUT4, derns->getName (foo));
168          return;
169          }
170       }
171                             // ----------- Associations
172    double delta = longueur / nbedges;
173    for (int ned = 0 ; ned<nbedges ; ned++)
174        {
175        if (db) cout << " ++ Association Edge nro "
176                     << ned << "/" <<nbedges << endl;
177        Edge*  edge = tedges[ned];
178        double  sm1 = ned*delta;
179        double  sm2 = sm1 + delta;
180        for (int ns = 0 ; ns<nbshapes ; ns++)
181            {
182            tab_gline[ns].associate (edge, sm1, sm2);
183            }
184
185        }
186    if (db) cout << " +++ End of Elements::cutAssociation" << endl;
187 }
188 // ====================================================== geom_define_line
189 void geom_define_line (string& brep)
190 {
191    current_shape.setBrep   (brep);
192    current_shape.setBounds (0, 1);
193    current_line.defineLine (&current_shape);
194 }
195 // ====================================================== geom_asso_point
196 void geom_asso_point (double angle, Vertex* node)
197 {
198    if (node!=NULL && node->getAssociation() == NULL)
199        current_line.assoPoint (angle, node);
200 }
201 // ====================================================== geom_asso_point
202 void geom_asso_point (Vertex* node)
203 {
204    if (node==NULL || node->getAssociation() != NULL)
205       return;
206
207    Real3 koord = { node->getX(), node->getY(), node->getZ() };
208    KasPoint  asso_point ;
209    asso_point.definePoint (koord);
210    asso_point.associate   (node);
211 }
212 // ====================================================== geom_create_circle
213 void geom_create_circle (double* milieu, double rayon, double* normale,
214                          double* base, string& brep)
215 {
216    db = on_debug ();
217    if (db) printf ("geom_create_circle c=(%g,%g,%g), r=%g\n",
218                     milieu[0], milieu[1], milieu[2], rayon);
219    if (db) printf ("    -------- base=(%g,%g,%g)\n", base[0], base[1], base[2]);
220    if (db) printf ("    -------- norm=(%g,%g,%g)\n", normale[0], normale[1],
221                                                   normale[2]);
222
223    gp_Pnt gp_center (milieu [dir_x], milieu [dir_y], milieu [dir_z]);
224    gp_Vec gp_vx     (base   [dir_x], base   [dir_y], base   [dir_z]);
225    gp_Vec gp_norm   (normale[dir_x], normale[dir_y], normale[dir_z]);
226
227    gp_Ax2  gp_axes (gp_center, gp_norm, gp_vx);
228    gp_Circ gp_circ (gp_axes,   rayon);
229
230    TopoDS_Edge    geom_circ = BRepBuilderAPI_MakeEdge(gp_circ).Edge();
231    ostringstream  stream_shape;
232    BRepTools::Write(geom_circ, stream_shape);
233    brep = stream_shape.str();
234
235    // geom_make_brep (geom_circ, brep);
236    if (NOT db)
237       return;
238                              // Impressions de mise au point
239    double umin = 0, umax = 0;
240    TopLoc_Location    loc;
241    Handle(Geom_Curve) handle = BRep_Tool::Curve (geom_circ, loc, umin, umax);
242    GeomAdaptor_Curve  AdaptCurve (handle);
243    double length = GCPnts_AbscissaPoint::Length(AdaptCurve, umin, umax);
244
245    BRepAdaptor_Curve geom_curve (geom_circ);
246
247    for (int pk=0; pk<=4; pk++)
248        {
249        GCPnts_AbscissaPoint s1 (geom_curve, pk*length/4,
250                                 geom_curve.FirstParameter());
251        double u1    = s1.Parameter ();
252        gp_Pnt point = geom_curve.Value (u1);
253        if (db)
254           printf ( " ..... pnt%d = (%g, %g, %g)\n", pk, point.X(),
255                                             point.Y(),  point.Z());
256        }
257 }
258 // ====================================================== geom_create_sphere
259 void geom_create_sphere (double* milieu, double radius, string& brep)
260 {
261    gp_Pnt gp_center (milieu [dir_x], milieu [dir_y], milieu [dir_z]);
262    gp_Ax2 gp_axis = gp_Ax2 (gp_center, gp_Dir(0,0,1), gp_Dir(1,0,0));
263
264    BRepPrimAPI_MakeSphere make_sphere (gp_axis, radius);
265
266    make_sphere.Build();
267
268    ostringstream     stream_shape;
269    TopoDS_Shape      geom_sphere = make_sphere.Face();
270    BRepTools::Write (geom_sphere, stream_shape);
271    brep = stream_shape.str();
272 }
273 // ====================================================== geom_dump_asso
274 void geom_dump_asso (Edge* edge)
275 {
276    printf (" %s dump_edge :\n",
277            "_______________________________________________________________");
278    if (edge==NULL || NOT edge->isHere ())
279       {
280       printf ("*** deleted ***)\n");
281       return;
282       }
283
284    bool db0 = db;
285    db  = false;
286    edge->printName(" = (");
287    edge->getVertex (V_AMONT)-> printName (", ");
288    edge->getVertex (V_AVAL) -> printName (")\n");
289
290    KasPoint asso_point;
291    for (int nro=0 ; nro<V_TWO ; nro++)
292        {
293        Vertex* vertex = edge->getVertex (nro);
294        vertex->printName ("");
295        printf (" = (%g, %g, %g)", vertex->getX(),  vertex->getY(),
296                                   vertex->getZ());
297
298        int ier = asso_point.definePoint (vertex);
299        if (ier==HOK)
300           {
301           double* coord = asso_point.getCoord();
302           printf (",  pnt_asso  = (%g, %g, %g)", coord[dir_x],  coord[dir_y],
303                                      coord[dir_z]);
304           }
305        printf ("\n");
306        }
307
308    KasLine asso_line;
309    const Shapes&  tshapes = edge->getAssociations ();
310    for (int nro=0 ; nro<tshapes.size() ; nro++)
311        {
312        printf ( " ------------------- Edge.tab_shape[%d] :\n", nro);
313        Shape* shape = tshapes[nro];
314        if (shape == NULL)
315           {
316           printf ( "                =  NULL\n");
317           }
318        else
319           {
320           asso_line.defineLine (shape);
321           double* deb = asso_line.getStart  ();
322           double* fin = asso_line.getEnd    ();
323           double  lg  = asso_line.getLength ();
324           printf (" Longueur = %g\n", lg);
325           printf (" Debut = %g = (%g, %g, %g)\n", shape->getStart(),
326                                                   deb[0], deb[1], deb[2]);
327           printf (" Fin   = %g = (%g, %g, %g)\n", shape->getEnd(),
328                                                   fin[0], fin[1], fin[2]);
329           }
330        }
331    db = db0;
332 }
333 // ====================================================== translate_brep
334 void translate_brep (string& brep, double dir[], string& trep)
335 {
336    gp_Trsf       transfo;
337    BRep_Builder  builder;
338    TopoDS_Shape  orig;
339
340    gp_Vec      vecteur       (dir [dir_x], dir [dir_y], dir [dir_z]);
341    transfo.SetTranslation    (vecteur);
342    istringstream stream_brep (brep);
343    BRepTools::Read           (orig, stream_brep, builder);
344
345    TopLoc_Location  loc_orig   = orig.Location();
346    gp_Trsf          trans_orig = loc_orig.Transformation();
347    TopLoc_Location  loc_result (transfo * trans_orig);
348    TopoDS_Shape     result = orig.Located (loc_result);
349
350    ostringstream stream_shape;
351    BRepTools::Write (result, stream_shape);
352    trep = stream_shape.str();
353 }
354 // ====================================================== transfo_brep
355 void transfo_brep (string& brep, Matrix* matrice, string& trep)
356 {
357    BRep_Builder  builder;
358    TopoDS_Shape  shape_orig;
359    gp_Trsf       transfo;
360
361    double             a11,a12,a13,a14, a21,a22,a23,a24, a31,a32,a33,a34;
362    matrice->getCoeff (a11,a12,a13,a14, a21,a22,a23,a24, a31,a32,a33,a34);
363    transfo.SetValues (a11,a12,a13,a14, a21,a22,a23,a24, a31,a32,a33,a34,
364                       Epsil2, Epsil2);
365
366    istringstream stream_brep (brep);
367    BRepTools::Read           (shape_orig, stream_brep, builder);
368
369    BRepBuilderAPI_Transform brep_transfo (shape_orig, transfo, Standard_True);
370    TopoDS_Shape result = brep_transfo.Shape();
371
372    ostringstream stream_shape;
373    BRepTools::Write (result, stream_shape);
374    trep = stream_shape.str();
375 }
376 // ====================================================== clear_associations
377 void clear_associations (Edge* edge)
378 {
379    edge->clearAssociation();
380    edge->getVertex(V_AMONT)->clearAssociation();
381    edge->getVertex(V_AVAL )->clearAssociation();
382 }
383 // ====================================================== set_debug_asso
384 void set_debug_asso (bool boule)
385 {
386    db = boule;
387    if (db)
388        printf (" ... Traces actives dans  HexAssoElements_asso.cxx\n");
389 }
390 // ====================================================== clean_brep
391 void clean_brep (string& brep)
392 {
393    TopoDS_Shape  shape;
394    BRep_Builder  builder;
395    istringstream stream_brep (brep);
396
397    BRepTools::Read  (shape, stream_brep, builder);
398    BRepTools::Clean (shape);
399
400    ostringstream    stream_shape;
401    BRepTools::Write (shape, stream_shape);
402    brep = stream_shape.str();
403 }
404 END_NAMESPACE_HEXA