]> SALOME platform Git repositories - modules/hexablock.git/blob - src/HEXABLOCK/HexNewShape.cxx
Salome HOME
Hexa6 : Mise ajour des sources
[modules/hexablock.git] / src / HEXABLOCK / HexNewShape.cxx
1
2 // C++ : Gestion des Shapes (Hexa v5)
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 #include "HexNewShape.hxx"
24 #include "HexSubShape.hxx"
25 #include "HexVertexShape.hxx"
26 #include "HexEdgeShape.hxx"
27 #include "HexFaceShape.hxx"
28
29 #include "HexQuad.hxx"
30 #include "HexEdge.hxx"
31 #include "HexMatrix.hxx"
32
33 #include "HexKas_functions.hxx"
34
35 #include "HexXmlWriter.hxx"
36 #include "HexXmlTree.hxx"
37
38 #include <TopoDS.hxx>
39 #include <TopoDS_Iterator.hxx>
40 #include <TopoDS_Edge.hxx>
41 #include <TopoDS_Compound.hxx>
42
43 #include <BRepTools.hxx>
44 #include <BRep_Builder.hxx>
45 #include <BRepBuilderAPI_MakeEdge.hxx>
46 #include <BRepPrimAPI_MakeSphere.hxx>
47 #include <BRepBuilderAPI_Transform.hxx>
48
49 #include <BRep_Tool.hxx>
50
51
52 #include <gp_Pnt.hxx>
53 #include <gp_Pln.hxx>
54 #include <gp_Dir.hxx>
55 #include <gp_Lin.hxx>
56 #include <gp_Circ.hxx>
57
58
59 BEGIN_NAMESPACE_HEXA
60
61 static bool db  = on_debug ();  // == getenv ("HEXA_DB") > 0
62 static bool db0 = true;
63
64 // ====================================================== Constructeur
65 NewShape::NewShape (Document* dad, EnumShape type)
66         : EltBase  (dad, EL_SHAPE)
67 {
68    sh_origin    =  type;
69    brep_defined =  shape_defined = false;
70
71    sh_level     = sh_indice      = 0;
72    sh_face_max  = sh_edge_max    = sh_vertex_max = 0;
73
74    curr_subid = 2;
75 }
76 // ====================================================== setShape
77 void NewShape::setShape (const TopoDS_Shape& shape, EnumShape type)
78 {
79    sh_origin = type;
80    geo_shape = shape;
81    geo_brep  = "";
82
83    shape_defined = true;
84    brep_defined  = false;
85
86    map_shape .Clear ();
87    tab_face  .clear ();
88    tab_edge  .clear ();
89    tab_vertex.clear ();
90
91    sh_level    = sh_indice   = 0;
92    sh_face_max = sh_edge_max = sh_vertex_max = 0;
93
94    if (db)
95       cout <<  " ------------------- setShape " << el_name << endl;
96    parseShape (geo_shape);
97 }
98 // ====================================================== setBrep
99 void NewShape::setBrep (rcstring brep)
100 {
101    geo_brep     = brep;
102    brep_defined = true;
103    updateShape ();
104 }
105 // ====================================================== getGeoShape
106 const TopoDS_Shape& NewShape::getGeoShape (int id)
107 {
108    const TopoDS_Shape& shape = map_shape.FindKey (id);
109    return shape;
110 }
111 // ====================================================== getShape
112 const TopoDS_Shape& NewShape::getShape ()
113 {
114    return geo_shape;
115 }
116 // ====================================================== openShape
117 void NewShape::openShape ()
118 {
119    geo_builder.MakeCompound (geo_compound);
120    curr_subid = 2;
121
122    asso_edge.clear ();
123    asso_pmin.clear ();
124    asso_pmax.clear ();
125    asso_edid.clear ();
126    asso_quad.clear ();
127    asso_quid.clear ();
128 }
129 // ====================================================== addCircle
130 int NewShape::addCircle (double* milieu, double rayon, double* normale,
131                          double* base)
132 {
133    gp_Pnt gp_center (milieu [dir_x], milieu [dir_y], milieu [dir_z]);
134    gp_Vec gp_vx     (base   [dir_x], base   [dir_y], base   [dir_z]);
135    gp_Vec gp_norm   (normale[dir_x], normale[dir_y], normale[dir_z]);
136
137    gp_Ax2  gp_axes (gp_center, gp_norm, gp_vx);
138    gp_Circ gp_circ (gp_axes,   rayon);
139
140    TopoDS_Edge  geom_circ = BRepBuilderAPI_MakeEdge(gp_circ).Edge();
141    geo_builder.Add (geo_compound, geom_circ);
142
143    if (db)
144    cout << " .... AddCircle subid " << curr_subid << " : rayon=" << rayon
145         << " centre = " << milieu[dir_x] << "," << milieu[dir_y]
146         << "," << milieu[dir_z]
147         << " norm = " << normale[dir_x] << "," << normale[dir_y]
148         << "," << normale[dir_z]
149         << endl;
150
151    int  subid  = curr_subid;
152    curr_subid += 2;
153    return subid;
154 }
155 // ====================================================== addSphere
156 int NewShape::addSphere (double* milieu, double radius)
157 {
158    gp_Pnt  gp_center (milieu [dir_x], milieu [dir_y], milieu [dir_z]);
159    gp_Ax2  gp_axis (gp_center, gp_Dir(0,0,1), gp_Dir(1,0,0));
160
161    BRepPrimAPI_MakeSphere make_sphere (gp_axis, radius);
162    make_sphere.Build();
163    TopoDS_Shape   geom_sphere = make_sphere.Face();
164    geo_builder.Add (geo_compound, geom_sphere);
165
166    if (db)
167    cout << " .... AddSphere subid " << curr_subid << " : rayon=" << radius
168         << " centre = " << milieu[dir_x] << "," << milieu[dir_y]
169         << "," << milieu[dir_z] << endl;
170
171    int  subid  = curr_subid;
172    curr_subid += 7;
173    return subid;
174 }
175 // ====================================================== transfoShape
176 int NewShape::transfoShape (Matrix& matrice, SubShape* shape)
177 {
178    gp_Trsf transfo;
179    double             a11,a12,a13,a14, a21,a22,a23,a24, a31,a32,a33,a34;
180    matrice.getCoeff  (a11,a12,a13,a14, a21,a22,a23,a24, a31,a32,a33,a34);
181    transfo.SetValues (a11,a12,a13,a14, a21,a22,a23,a24, a31,a32,a33,a34,
182                       Epsil2, Epsil2);
183
184    TopoDS_Shape shape_orig = shape->getShape ();
185    BRepBuilderAPI_Transform builder (shape_orig, transfo, Standard_True);
186    TopoDS_Shape result = builder.Shape();
187
188    geo_builder.Add (geo_compound, result);
189
190    int  subid  = curr_subid;
191    curr_subid += 2;                       // PROVISOIRE
192    return subid;
193 }
194 // ====================================================== translateShape
195 int NewShape::translateShape (double dir[], SubShape* shape)
196 {
197    gp_Trsf transfo;
198    gp_Vec  vecteur        (dir [dir_x], dir [dir_y], dir [dir_z]);
199    transfo.SetTranslation (vecteur);
200
201    TopoDS_Shape shape_orig = shape->getShape ();
202    BRepBuilderAPI_Transform builder (shape_orig, transfo, Standard_True);
203    TopoDS_Shape result = builder.Shape();
204
205    geo_builder.Add (geo_compound, result);
206
207    int  subid  = curr_subid;
208    curr_subid += 2;                       // PROVISOIRE
209    return subid;
210 }
211 // ====================================================== addAssociation
212 void NewShape::addAssociation (Vertex* vertex, int subid, double param)
213 {
214    if (vertex==NULL)
215       return;
216
217    asso_vertex.push_back (vertex);
218    asso_param .push_back (param);
219    asso_vxid  .push_back (subid);
220 }
221 // ====================================================== addAssociation
222 void NewShape::addAssociation (Edge* edge, int subid, double pmin, double pmax)
223 {
224    if (edge==NULL)
225       return;
226
227    asso_edge.push_back (edge);
228    asso_pmin.push_back (pmin);
229    asso_pmax.push_back (pmax);
230    asso_edid.push_back (subid);
231 }
232 // ====================================================== addAssociation
233 void NewShape::addAssociation (Quad* quad, int subid)
234 {
235    if (quad==NULL)
236       return;
237
238    asso_quad.push_back (quad);
239    asso_quid.push_back (subid);
240 }
241 // ====================================================== closeShape
242 void NewShape::closeShape ()
243 {
244    geo_shape     = geo_compound;
245    shape_defined = true;
246    if (db)
247    cout <<  " ------------------- closeShape " << el_name << endl;
248    parseShape (geo_shape);
249
250    int nombre = asso_edge.size ();
251    for (int nro = 0 ; nro < nombre ; ++nro)
252        asso_edge[nro]->addAssociation (this, asso_edid[nro], asso_pmin[nro],
253                                                              asso_pmax[nro]);
254    nombre = asso_quad.size ();
255    for (int nro = 0 ; nro < nombre ; ++nro)
256        asso_quad[nro]->addAssociation (this, asso_quid[nro]);
257
258    Real3 point;
259    nombre = asso_vertex.size ();
260    for (int nro = 0 ; nro < nombre ; ++nro)
261        {
262        EdgeShape* line   = findEdge (asso_vxid[nro]);
263        Vertex*    vertex = asso_vertex[nro];
264        if (line!=NULL && vertex!=NULL && NOT vertex->isAssociated())
265           {
266           line->getPoint (asso_param[nro], point);
267           asso_vertex[nro]->setAssociation (point);
268           }
269        }
270
271 }
272 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
273 // ====================================================== findVertex
274 VertexShape* NewShape::findVertex (int shid)
275 {
276    int nbre = tab_vertex.size ();
277    for (int nro=0 ; nro < nbre ; nro++)
278        {
279        VertexShape* shape = tab_vertex [nro];
280        if (shape->getIdent() == shid)
281           return shape;
282        }
283    return NULL;
284 }
285 // ====================================================== findEdge
286 EdgeShape* NewShape::findEdge (int shid)
287 {
288    int nbre = tab_edge.size ();
289    for (int nro=0 ; nro < nbre ; nro++)
290        {
291        EdgeShape* shape = tab_edge [nro];
292        if (shape->getIdent() == shid)
293           return shape;
294        }
295    return NULL;
296 }
297 // ====================================================== findFace
298 FaceShape* NewShape::findFace (int shid)
299 {
300    int nbre = tab_face.size ();
301    for (int nro=0 ; nro < nbre ; nro++)
302        {
303        FaceShape* shape = tab_face [nro];
304        if (shape->getIdent() == shid)
305           return shape;
306        }
307    return NULL;
308 }
309 // ====================================================== findSubShape
310 SubShape* NewShape::findSubShape (int shid)
311 {
312    SubShape* shape = findEdge (shid);
313    if (shape==NULL)
314        shape = findVertex (shid);
315    if (shape==NULL)
316        shape = findFace (shid);
317
318    return shape;
319 }
320 // ====================================================== getVertex
321 int NewShape::getVertex (int nro)
322 {
323    if (nro <0 || nro >= (int) tab_vertex.size())
324       return 0;
325
326    SubShape* shape = tab_vertex [nro];
327    return shape->getIdent() ;
328 }
329 // ====================================================== getEdge
330 int NewShape::getEdge   (int nro)
331 {
332    if (nro <0 || nro >= (int) tab_edge.size())
333       return 0;
334
335    SubShape* shape = tab_edge [nro];
336    return shape->getIdent() ;
337 }
338 // ====================================================== getFace
339 int NewShape::getFace   (int nro)
340 {
341    if (nro <0 || nro >= (int) tab_face.size())
342       return 0;
343
344    SubShape* shape = tab_face [nro];
345    return shape->getIdent() ;
346 }
347
348 // ====================================================== getNameVertex
349 cpchar NewShape::getNameVertex  (int nro)
350 {
351    if (nro <0 || nro >= (int) tab_vertex.size())
352       return "?";
353
354    SubShape* shape = tab_vertex [nro];
355    return shape->getName() ;
356 }
357 // ====================================================== getNameEdge
358 cpchar NewShape::getNameEdge    (int nro)
359 {
360    if (nro <0 || nro >= (int) tab_edge.size())
361       return "?";
362
363    SubShape* shape = tab_edge [nro];
364    return shape->getName() ;
365 }
366 // ====================================================== getNameFace
367 cpchar NewShape::getNameFace    (int nro)
368 {
369    if (nro <0 || nro >= (int) tab_face.size())
370       return "?";
371
372    SubShape* shape = tab_face [nro];
373    return shape->getName() ;
374 }
375 // ====================================================== getFaceShape
376 FaceShape* NewShape::getFaceShape (int nro)
377 {
378    if (nro <0 || nro >= (int) tab_face.size())
379       return 0;
380
381    FaceShape* shape = tab_face [nro];
382    return shape;
383 }
384 // ====================================================== getEdgeShape
385 EdgeShape* NewShape::getEdgeShape (int nro)
386 {
387    if (nro <0 || nro >= (int) tab_edge.size())
388       return 0;
389
390    EdgeShape* shape = tab_edge [nro];
391    return shape;
392 }
393 // ====================================================== getVertexShape
394 VertexShape* NewShape::getVertexShape (int nro)
395 {
396    if (nro <0 || nro >= (int) tab_vertex.size())
397       return 0;
398
399    VertexShape* shape = tab_vertex [nro];
400    return shape;
401 }
402 // ====================================================== addPoint
403 int NewShape::addPoint (double* point)
404 {
405    char suffix [32];
406    int  subid = tab_vertex.size() + 2;
407
408    sprintf (suffix, ":vertex_%02d", subid);
409    string name = el_name + suffix;
410
411    VertexShape* sub_shape = new VertexShape (this, subid, point);
412    sub_shape->setName (name);
413    tab_vertex.push_back (sub_shape);
414    return subid;
415 }
416 // ===================================================== saveBrep
417 int NewShape::saveBrep ()
418 {
419    string filename = el_name + ".brep";
420
421    int ier   = updateBrep ();
422    if (ier != HOK)
423       return ier;
424
425    pfile fic = fopen (filename.c_str(), "w");
426    if (fic==NULL)
427       return HERR;
428
429    fprintf (fic, "%s\n", geo_brep.c_str());
430    fclose  (fic);
431    return HOK;
432 }
433 // ====================================================== saveXml
434 void NewShape::saveXml (XmlWriter* xml)
435 {
436    if (sh_origin==SH_CLOUD)
437       {
438       int nbre = tab_vertex.size();
439       xml->addMark ("Cloud");
440       for (int nro=0 ; nro<nbre ; nro++)
441           tab_vertex[nro]->saveXml (xml);
442       xml->closeMark ();
443       return;
444       }
445
446    char buffer [32];
447    int  ya1brep = updateBrep ();
448
449    xml->openMark     ("Shape");
450    xml->addAttribute ("id",    getName (buffer));
451    xml->addAttribute ("type",  sh_origin);
452    if (el_name!=buffer)
453       xml->addAttribute ("name", el_name);
454
455    if (ya1brep == HOK)
456       {
457       clean_brep (geo_brep);
458       xml->addAttribute ("brep",  geo_brep.c_str());
459       }
460    xml->closeMark ();
461 }
462 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
463 // ===================================================== parseShape
464 void NewShape::parseShape (const TopoDS_Shape& shape)
465 {
466    push_level ();
467    addSubShape (shape);
468
469    TopoDS_Iterator iter (shape);
470    for (; iter.More(); iter.Next())
471        {
472        TopoDS_Shape sub_shape = iter.Value();
473        parseShape (sub_shape);
474        }
475
476    pop_level ();
477 }
478 // ===================================================== addSubShape
479 void NewShape::addSubShape (const TopoDS_Shape& shape)
480 {
481    char chnum [32];
482    string name;
483    sh_indice ++;
484
485    int subid = map_shape.Add (shape);
486    int type  = shape.ShapeType();
487
488    switch (type)
489       {
490       case TopAbs_FACE :
491            sprintf (chnum, ":face_%02d", subid);
492            name = el_name + chnum;
493            if (subid > sh_face_max)
494               {
495               FaceShape* sub_shape = new FaceShape (this, subid);
496               sub_shape->setName (name);
497               tab_face.push_back (sub_shape);
498               sh_face_max = subid;
499               }
500            break;
501       case TopAbs_EDGE  :
502            sprintf (chnum, ":edge_%02d", subid);
503            name = el_name + chnum;
504            if (subid > sh_edge_max)
505               {
506               EdgeShape* sub_shape = new EdgeShape (this, subid);
507               sub_shape->setName (name);
508               tab_edge.push_back (sub_shape);
509               sh_edge_max = subid;
510               }
511            break;
512       case TopAbs_VERTEX :
513            sprintf (chnum, ":vertex_%02d", subid);
514            name = el_name + chnum;
515            if (subid > sh_vertex_max)
516               {
517               VertexShape* sub_shape = new VertexShape (this, subid);
518               sub_shape->setName (name);
519               tab_vertex.push_back (sub_shape);
520               sh_vertex_max = subid;
521               }
522            break;
523       default : ;
524            sprintf (chnum, "type=%d, subid=%02d", type, subid);
525            name   = chnum;
526       }
527
528    if (db0)
529       {
530       sprintf (chnum, "%4d : ", sh_indice);
531       cout <<  chnum;
532       for (int nro=1; nro<sh_level; nro++) cout << " | ";
533       cout <<  name;
534
535       if (type == TopAbs_VERTEX)
536          {
537          TopoDS_Vertex gver = TopoDS::Vertex (shape);
538          gp_Pnt      gpoint = BRep_Tool::Pnt (gver);
539
540          cout << " = (" << gpoint.X()
541               << ","    << gpoint.Y()
542               << ","    << gpoint.Z() << ")";
543          }
544       cout << endl;
545       }
546 }
547 // ===================================================== updateShape
548 int NewShape::updateShape ()
549 {
550    if (shape_defined)
551       return HOK;
552
553    if (NOT brep_defined)
554       return HERR;
555
556    shape_defined = true;
557    geom_brep2shape (geo_brep, geo_shape);
558    cout <<  " ------------------- updateShape " << el_name << endl;
559    parseShape (geo_shape);
560    return HOK;
561 }
562 // ===================================================== updateBrep
563 int NewShape::updateBrep ()
564 {
565    if (brep_defined)
566       return HOK;
567
568    if (NOT shape_defined)
569       return HERR;
570
571    brep_defined = true;
572    geom_make_brep (geo_shape, geo_brep);
573    return HOK;
574 }
575 END_NAMESPACE_HEXA