2 // CC++ : Interface Cascade de la classe Elements
4 // Copyright (C) 2009-2012 CEA/DEN, EDF R&D
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "HexElements.hxx"
24 #include "HexEdge.hxx"
25 #include "HexDocument.hxx"
28 #include "HexKasBiCylinder.hxx"
29 #include "HexKasPoint.hxx"
30 #include "HexKasLine.hxx"
32 #include <BRepTools.hxx>
33 #include <BRep_Builder.hxx>
34 #include <BRepAdaptor_Curve.hxx>
35 #include <BRepBuilderAPI_Transform.hxx>
36 #include <BRepBuilderAPI_MakeVertex.hxx>
38 #include <GCPnts_AbscissaPoint.hxx>
41 #include <TopoDS_Shape.hxx>
42 #include <TopoDS_Edge.hxx>
43 #include <TopoDS_Compound.hxx>
44 #include <TopoDS_Vertex.hxx>
52 #include "HexVertex.hxx"
53 #include "HexOldShape.hxx"
54 #include "HexDiagnostics.hxx"
56 #include <GEOMImpl_CircleDriver.hxx>
57 #include <GEOMImpl_ICircle.hxx>
59 #include <BRepBuilderAPI_MakeEdge.hxx>
60 #include <BRep_Tool.hxx>
62 #include <GC_MakeCircle.hxx>
63 #include <Geom_Circle.hxx>
65 #include <gp_Circ.hxx>
67 #include <BRepPrimAPI_MakeSphere.hxx>
69 #include <GEOMImpl_CylinderDriver.hxx>
70 #include <GEOMImpl_ICylinder.hxx>
71 #include <BRepPrimAPI_MakeCylinder.hxx>
72 #include <BRepAlgoAPI_Section.hxx>
76 static bool db = false;
78 void clear_associations (Edge* edge);
80 // ---------------------------------------------------------------------
82 static KasLine current_line;
83 static Shape current_shape ("");
85 // ========================================================= cutAssociation
86 void Elements::cutAssociation (Shapes& tshapes, Edges& tedges, bool exist)
90 int nbedges = tedges.size();
91 int nbshapes = tshapes.size ();
95 std::vector <KasLine> tab_gline (nbshapes);
97 Vertex* prems = tedges [0] -> getVertex (V_AMONT);
98 Vertex* derns = tedges [nbedges-1] -> getVertex (V_AVAL);
100 KasPoint pnt_first, pnt_last;
101 pnt_first.definePoint (prems);
102 pnt_last .definePoint (derns);
106 if (pnt_first.isBad())
108 el_root->putError (W_ASSO_CUT1, prems->getName (foo));
111 else if (pnt_last.isBad ())
113 el_root->putError (W_ASSO_CUT2, derns->getName (foo));
117 // ----------- Define + longueur totale
119 for (int ns = 0 ; ns<nbshapes ; ns++)
121 tab_gline[ns].defineLine (tshapes[ns]);
122 longueur += tab_gline[ns].getLength ();
125 // ----------- Ordonnancement des shapes
128 double* coord = pnt_first.getCoord ();
131 tab_gline [0].setRank (0, 0, abscisse);
136 for (int rg = 0 ; rg<nbshapes ; rg++)
139 for (int ns = 0 ; ns<nbshapes && more ; ns++)
141 if (tab_gline[ns].getRank()==NOTHING)
143 int pos = tab_gline[ns].findBound (coord);
148 tab_gline [ns].setRank (rg, pos, abscisse);
149 coord = tab_gline [ns].getEnd ();
156 el_root->putError (W_ASSO_CUT3, derns->getName (foo));
161 // ----------- Dernier
164 coord = pnt_last.getCoord ();
165 int pos = tab_gline[nslast].findBound (coord);
168 el_root->putError (W_ASSO_CUT4, derns->getName (foo));
172 // ----------- Associations
173 double delta = longueur / nbedges;
174 for (int ned = 0 ; ned<nbedges ; ned++)
176 if (db) cout << " ++ Association Edge nro "
177 << ned << "/" <<nbedges << endl;
178 Edge* edge = tedges[ned];
179 double sm1 = ned*delta;
180 double sm2 = sm1 + delta;
181 for (int ns = 0 ; ns<nbshapes ; ns++)
183 tab_gline[ns].associate (edge, sm1, sm2);
187 if (db) cout << " +++ End of Elements::cutAssociation" << endl;
189 // ====================================================== geom_define_line
190 void geom_define_line (string& brep)
192 current_shape.setBrep (brep);
193 current_shape.setBounds (0, 1);
194 current_line.defineLine (¤t_shape);
196 // ====================================================== geom_asso_point
197 void geom_asso_point (double angle, Vertex* node)
199 if (node!=NULL && node->getAssociation() == NULL)
200 current_line.assoPoint (angle, node);
202 // ====================================================== geom_asso_point
203 void geom_asso_point (Vertex* node)
205 if (node==NULL || node->getAssociation() != NULL)
208 Real3 koord = { node->getX(), node->getY(), node->getZ() };
209 KasPoint asso_point ;
210 asso_point.definePoint (koord);
211 asso_point.associate (node);
213 // ====================================================== geom_create_circle
214 void geom_create_circle (double* milieu, double rayon, double* normale,
215 double* base, string& brep)
218 if (db) printf ("geom_create_circle c=(%g,%g,%g), r=%g\n",
219 milieu[0], milieu[1], milieu[2], rayon);
220 if (db) printf (" -------- base=(%g,%g,%g)\n", base[0], base[1], base[2]);
221 if (db) printf (" -------- norm=(%g,%g,%g)\n", normale[0], normale[1],
224 gp_Pnt gp_center (milieu [dir_x], milieu [dir_y], milieu [dir_z]);
225 gp_Vec gp_vx (base [dir_x], base [dir_y], base [dir_z]);
226 gp_Vec gp_norm (normale[dir_x], normale[dir_y], normale[dir_z]);
228 gp_Ax2 gp_axes (gp_center, gp_norm, gp_vx);
229 gp_Circ gp_circ (gp_axes, rayon);
231 TopoDS_Edge geom_circ = BRepBuilderAPI_MakeEdge(gp_circ).Edge();
232 ostringstream stream_shape;
233 BRepTools::Write(geom_circ, stream_shape);
234 brep = stream_shape.str();
236 // geom_make_brep (geom_circ, brep);
239 // Impressions de mise au point
240 double umin = 0, umax = 0;
242 Handle(Geom_Curve) handle = BRep_Tool::Curve (geom_circ, loc, umin, umax);
243 GeomAdaptor_Curve AdaptCurve (handle);
244 double length = GCPnts_AbscissaPoint::Length(AdaptCurve, umin, umax);
246 BRepAdaptor_Curve geom_curve (geom_circ);
248 for (int pk=0; pk<=4; pk++)
250 GCPnts_AbscissaPoint s1 (geom_curve, pk*length/4,
251 geom_curve.FirstParameter());
252 double u1 = s1.Parameter ();
253 gp_Pnt point = geom_curve.Value (u1);
255 printf ( " ..... pnt%d = (%g, %g, %g)\n", pk, point.X(),
256 point.Y(), point.Z());
259 // ====================================================== geom_create_sphere
260 void geom_create_sphere (double* milieu, double radius, string& brep)
262 gp_Pnt gp_center (milieu [dir_x], milieu [dir_y], milieu [dir_z]);
263 gp_Ax2 gp_axis = gp_Ax2 (gp_center, gp_Dir(0,0,1), gp_Dir(1,0,0));
265 BRepPrimAPI_MakeSphere make_sphere (gp_axis, radius);
269 ostringstream stream_shape;
270 TopoDS_Shape geom_sphere = make_sphere.Face();
271 BRepTools::Write (geom_sphere, stream_shape);
272 brep = stream_shape.str();
274 // ====================================================== geom_dump_asso
275 void geom_dump_asso (Edge* edge)
277 printf (" %s dump_edge :\n",
278 "_______________________________________________________________");
279 if (edge==NULL || NOT edge->isHere ())
281 printf ("*** deleted ***)\n");
287 edge->printName(" = (");
288 edge->getVertex (V_AMONT)-> printName (", ");
289 edge->getVertex (V_AVAL) -> printName (")\n");
292 for (int nro=0 ; nro<V_TWO ; nro++)
294 Vertex* vertex = edge->getVertex (nro);
295 vertex->printName ("");
296 printf (" = (%g, %g, %g)", vertex->getX(), vertex->getY(),
299 int ier = asso_point.definePoint (vertex);
302 double* coord = asso_point.getCoord();
303 printf (", pnt_asso = (%g, %g, %g)", coord[dir_x], coord[dir_y],
310 const Shapes& tshapes = edge->getAssociations ();
311 for (int nro=0 ; nro<tshapes.size() ; nro++)
313 printf ( " ------------------- Edge.tab_shape[%d] :\n", nro);
314 Shape* shape = tshapes[nro];
317 printf ( " = NULL\n");
321 asso_line.defineLine (shape);
322 double* deb = asso_line.getStart ();
323 double* fin = asso_line.getEnd ();
324 double lg = asso_line.getLength ();
325 printf (" Longueur = %g\n", lg);
326 printf (" Debut = %g = (%g, %g, %g)\n", shape->getStart(),
327 deb[0], deb[1], deb[2]);
328 printf (" Fin = %g = (%g, %g, %g)\n", shape->getEnd(),
329 fin[0], fin[1], fin[2]);
334 // ====================================================== translate_brep
335 void translate_brep (string& brep, double dir[], string& trep)
338 BRep_Builder builder;
341 gp_Vec vecteur (dir [dir_x], dir [dir_y], dir [dir_z]);
342 transfo.SetTranslation (vecteur);
343 istringstream stream_brep (brep);
344 BRepTools::Read (orig, stream_brep, builder);
346 TopLoc_Location loc_orig = orig.Location();
347 gp_Trsf trans_orig = loc_orig.Transformation();
348 TopLoc_Location loc_result (transfo * trans_orig);
349 TopoDS_Shape result = orig.Located (loc_result);
351 ostringstream stream_shape;
352 BRepTools::Write (result, stream_shape);
353 trep = stream_shape.str();
355 // ====================================================== transfo_brep
356 void transfo_brep (string& brep, Matrix* matrice, string& trep)
358 BRep_Builder builder;
359 TopoDS_Shape shape_orig;
362 double a11,a12,a13,a14, a21,a22,a23,a24, a31,a32,a33,a34;
363 matrice->getCoeff (a11,a12,a13,a14, a21,a22,a23,a24, a31,a32,a33,a34);
364 transfo.SetValues (a11,a12,a13,a14, a21,a22,a23,a24, a31,a32,a33,a34,
367 istringstream stream_brep (brep);
368 BRepTools::Read (shape_orig, stream_brep, builder);
370 BRepBuilderAPI_Transform brep_transfo (shape_orig, transfo, Standard_True);
371 TopoDS_Shape result = brep_transfo.Shape();
373 ostringstream stream_shape;
374 BRepTools::Write (result, stream_shape);
375 trep = stream_shape.str();
377 // ====================================================== clear_associations
378 void clear_associations (Edge* edge)
380 edge->clearAssociation();
381 edge->getVertex(V_AMONT)->clearAssociation();
382 edge->getVertex(V_AVAL )->clearAssociation();
384 // ====================================================== associateShapes
385 int associateShapes (Edges& mline, int msens[], Shape* gstart, Shapes& gline,
386 double pstart, double pend, bool closed, bool inv)
391 cout << "____________________________________________"
392 << " associateShapes" << endl;
393 PutData (gline.size());
400 int nbshapes = gline.size ();
401 int nblines = nbshapes + 1;
403 vector <KasLine*> buff_line (nblines); // car nblines != 0
404 vector <KasLine*> geom_line (nblines);
406 // -------- Bufferisation des shapes
407 for (int ns=0 ; ns<nbshapes ; ns++)
408 buff_line [ns] = new KasLine (gline [ns], 0.0, 1.0);
410 // -------- Premiere ligne
411 int sdepart = NOTHING;
412 double* extrem = NULL;
417 if (pstart > UnEpsil)
422 else if (pstart < Epsil)
427 else if (nbshapes==1)
430 KasLine* prems = geom_line[0] = new KasLine (gstart, pstart, pfin0);
432 // Ligne fermee : sens impose par l'utilisateur
435 sdepart = inv ? V_AVAL : V_AMONT;
437 // PolyLigne ouverte : trouver le sens
441 extrem = prems -> getEnd();
442 for (int ns=V_AMONT ; ns<=V_AVAL && sdepart==NOTHING ; ns++)
444 for (int nb=0 ; nb<nbshapes && sdepart==NOTHING ; nb++)
446 if (buff_line[nb]->findBound(extrem) != NOTHING)
449 extrem = prems -> getStart();
452 if (sdepart==NOTHING)
454 printf (" ***************** Erreur dans associateShapes\n");
455 printf (" ***************** La ligne ouverte est interrompue\n");
459 // Une seule ligne ouverte : depend de pstart et pend
460 else if (pstart < pend)
471 prems -> setRank (0, sdepart, abscisse);
472 extrem = prems -> getEnd();
473 // -------- Rangement des autres lignes
474 for (int nl=1 ; nl<nblines ; nl++)
477 for (int nb=0 ; more && nb<nbshapes ; nb++)
479 KasLine* ligne = buff_line[nb];
480 int sens = ligne==NULL ? NOTHING : ligne->findBound (extrem);
483 // Derniere ligne si ouverte
484 if (NOT closed && nl==nblines-1)
486 ligne -> setBounds (0, pend);
488 ligne -> setRank (nl, sens, abscisse);
489 buff_line [nb] = NULL;
490 geom_line [nl] = ligne;
491 extrem = ligne->getEnd();
497 printf (" ***************** Erreur dans KasLine\n");
501 if (closed && pstart > Epsil)
503 KasLine* ligne = new KasLine (gstart, 0, pstart);
504 ligne->setRank (nblines, sdepart, abscisse);
505 geom_line.push_back (ligne);
508 int ntlines = geom_line.size();
509 int nbedges = mline.size ();
512 cout << "=============================================================="
515 for (int ned=0 ; ned<nbedges ; ned++)
516 clear_associations (mline[ned]);
517 // -------- Associations
518 double delta = abscisse / nbedges;
519 for (int ned=0 ; ned<nbedges ; ned++)
521 double sm1 = ned * delta;
522 double sm2 = sm1 + delta;
523 for (int ns = 0 ; ns<ntlines ; ns++)
524 geom_line[ns]->associate (mline[ned], sm1, sm2, msens[ned]);
526 // -------- Liberation
527 for (int nl=1 ; nl<ntlines ; nl++)
528 delete geom_line [nl];
532 // ====================================================== set_debug_asso
533 void set_debug_asso (bool boule)
537 printf (" ... Traces actives dans HexAssoElements_asso.cxx\n");
539 // ====================================================== clean_brep
540 void clean_brep (string& brep)
543 BRep_Builder builder;
544 istringstream stream_brep (brep);
546 BRepTools::Read (shape, stream_brep, builder);
547 BRepTools::Clean (shape);
549 ostringstream stream_shape;
550 BRepTools::Write (shape, stream_shape);
551 brep = stream_shape.str();
555 // ------------------------------------------------------------------------
556 #else // #ifndef NO_CASCADE
557 // ------------------------------------------------------------------------
561 // ====================================================== geom_create_circle
562 void geom_create_circle (double* milieu, double rayon, double* normale,
563 double* base, string& brep)
566 sprintf (buffer, "(Cercle c=(%g,%g,%g), r=%g",
567 milieu[0], milieu[1], milieu[2], rayon);
570 // ====================================================== geom_create_sphere
571 void geom_create_sphere (double* milieu, double radius, string& brep)
575 // ========================================================= cutAssociation
576 void Elements::cutAssociation (Shapes& tshapes, Edges& tedges, bool exist)
579 // ====================================================== geom_define_line
580 void geom_define_line (string& brep)
583 // ====================================================== geom_dump_asso
584 void geom_dump_asso (Edge* edge)
587 // ====================================================== geom_asso_point
588 void geom_asso_point (double angle, Vertex* node)
591 // ====================================================== geom_asso_point
592 void geom_asso_point (Vertex* node)
595 // ====================================================== translate_brep
596 void translate_brep (string& orig, double dir[], string& result)
600 // ====================================================== transfo_brep
601 void transfo_brep (string& brep, Matrix* matrice, string& trep)
605 // ====================================================== dump_edge
606 void dump_edge (Edge* edge)
609 // ====================================================== associateShapes
610 int associateShapes (Edges& mline, int msens[], Shape* gstart, Shapes& gline,
611 double pstart, double pend, bool closed, bool inv)
615 // ====================================================== set_debug_asso
616 void set_debug_asso (bool boule)
619 // ====================================================== geom_create_cylcyl
620 int geom_create_cylcyl (double* borig, double* bnorm, double* bbase,
621 double bray, double bhaut,
622 double* sorig, double* snorm, double* sbase,
623 double sray, double shaut)
627 // ====================================================== geom_asso_cylcyl
628 int geom_asso_cylcyl (Edge* edge)
632 // ====================================================== clean_brep
633 void clean_brep (string& brep)