2 // C++ : Gestion des Shapes (Hexa v5)
4 // Copyright (C) 2009-2016 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, or (at your option) any later version.
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 "HexNewShape.hxx"
24 #include "HexSubShape.hxx"
25 #include "HexVertexShape.hxx"
26 #include "HexEdgeShape.hxx"
27 #include "HexFaceShape.hxx"
29 #include "HexQuad.hxx"
30 #include "HexEdge.hxx"
31 #include "HexMatrix.hxx"
33 #include "HexKas_functions.hxx"
35 #include "HexXmlWriter.hxx"
36 #include "HexXmlTree.hxx"
39 #include <TopoDS_Iterator.hxx>
40 #include <TopoDS_Edge.hxx>
41 #include <TopoDS_Compound.hxx>
43 #include <BRepTools.hxx>
44 #include <BRep_Builder.hxx>
45 #include <BRepBuilderAPI_MakeEdge.hxx>
46 #include <BRepPrimAPI_MakeSphere.hxx>
47 #include <BRepBuilderAPI_Transform.hxx>
49 #include <BRep_Tool.hxx>
56 #include <gp_Circ.hxx>
58 #include <Basics_OCCTVersion.hxx>
62 static bool db = on_debug (); // == getenv ("HEXA_DB") > 0
65 // ====================================================== Constructeur
66 NewShape::NewShape (Document* dad, EnumShape type)
67 : EltBase (dad, EL_SHAPE)
70 brep_defined = shape_defined = false;
72 sh_level = sh_indice = 0;
73 sh_face_max = sh_edge_max = sh_vertex_max = 0;
77 // ====================================================== setShape
78 void NewShape::setShape (const TopoDS_Shape& shape, EnumShape type)
92 sh_level = sh_indice = 0;
93 sh_face_max = sh_edge_max = sh_vertex_max = 0;
96 cout << " ------------------- setShape " << el_name << endl;
97 parseShape (geo_shape);
99 // ====================================================== setBrep
100 void NewShape::setBrep (rcstring brep)
106 // ====================================================== getGeoShape
107 const TopoDS_Shape& NewShape::getGeoShape (int id)
109 const TopoDS_Shape& shape = map_shape.FindKey (id);
112 // ====================================================== getShape
113 const TopoDS_Shape& NewShape::getShape ()
117 // ====================================================== openShape
118 void NewShape::openShape ()
120 geo_builder.MakeCompound (geo_compound);
130 // ====================================================== addCircle
131 int NewShape::addCircle (double* milieu, double rayon, double* normale,
134 gp_Pnt gp_center (milieu [dir_x], milieu [dir_y], milieu [dir_z]);
135 gp_Vec gp_vx (base [dir_x], base [dir_y], base [dir_z]);
136 gp_Vec gp_norm (normale[dir_x], normale[dir_y], normale[dir_z]);
138 gp_Ax2 gp_axes (gp_center, gp_norm, gp_vx);
139 gp_Circ gp_circ (gp_axes, rayon);
141 TopoDS_Edge geom_circ = BRepBuilderAPI_MakeEdge(gp_circ).Edge();
142 geo_builder.Add (geo_compound, geom_circ);
145 cout << " .... AddCircle subid " << curr_subid << " : rayon=" << rayon
146 << " centre = " << milieu[dir_x] << "," << milieu[dir_y]
147 << "," << milieu[dir_z]
148 << " norm = " << normale[dir_x] << "," << normale[dir_y]
149 << "," << normale[dir_z]
152 int subid = curr_subid;
156 // ====================================================== addSphere
157 int NewShape::addSphere (double* milieu, double radius)
159 gp_Pnt gp_center (milieu [dir_x], milieu [dir_y], milieu [dir_z]);
160 gp_Ax2 gp_axis (gp_center, gp_Dir(0,0,1), gp_Dir(1,0,0));
162 BRepPrimAPI_MakeSphere make_sphere (gp_axis, radius);
164 TopoDS_Shape geom_sphere = make_sphere.Face();
165 geo_builder.Add (geo_compound, geom_sphere);
168 cout << " .... AddSphere subid " << curr_subid << " : rayon=" << radius
169 << " centre = " << milieu[dir_x] << "," << milieu[dir_y]
170 << "," << milieu[dir_z] << endl;
172 int subid = curr_subid;
176 // ====================================================== transfoShape
177 int NewShape::transfoShape (Matrix& matrice, SubShape* shape)
180 double a11,a12,a13,a14, a21,a22,a23,a24, a31,a32,a33,a34;
181 matrice.getCoeff (a11,a12,a13,a14, a21,a22,a23,a24, a31,a32,a33,a34);
182 #if OCC_VERSION_LARGE > 0x06070100
183 transfo.SetValues (a11,a12,a13,a14, a21,a22,a23,a24, a31,a32,a33,a34);
185 transfo.SetValues (a11,a12,a13,a14, a21,a22,a23,a24, a31,a32,a33,a34,
189 TopoDS_Shape shape_orig = shape->getShape ();
190 BRepBuilderAPI_Transform builder (shape_orig, transfo, Standard_True);
191 TopoDS_Shape result = builder.Shape();
193 geo_builder.Add (geo_compound, result);
195 int subid = curr_subid;
196 curr_subid += 2; // PROVISOIRE
199 // ====================================================== translateShape
200 int NewShape::translateShape (double dir[], SubShape* shape)
203 gp_Vec vecteur (dir [dir_x], dir [dir_y], dir [dir_z]);
204 transfo.SetTranslation (vecteur);
206 TopoDS_Shape shape_orig = shape->getShape ();
207 BRepBuilderAPI_Transform builder (shape_orig, transfo, Standard_True);
208 TopoDS_Shape result = builder.Shape();
210 geo_builder.Add (geo_compound, result);
212 int subid = curr_subid;
213 curr_subid += 2; // PROVISOIRE
216 // ====================================================== addAssociation
217 void NewShape::addAssociation (Vertex* vertex, int subid, double param)
222 asso_vertex.push_back (vertex);
223 asso_param .push_back (param);
224 asso_vxid .push_back (subid);
226 // ====================================================== addAssociation
227 void NewShape::addAssociation (Edge* edge, int subid, double pmin, double pmax)
234 cout << edge->getName()
235 << " = (" << edge->getVertex(0)->getName()
236 << ", " << edge->getVertex(1)->getName()
237 << ") --> subid= " << subid << " [ "
238 << pmin << ", " << pmax << "]" << endl;
241 asso_edge.push_back (edge);
242 asso_pmin.push_back (pmin);
243 asso_pmax.push_back (pmax);
244 asso_edid.push_back (subid);
246 // ====================================================== addAssociation
247 void NewShape::addAssociation (Quad* quad, int subid)
252 asso_quad.push_back (quad);
253 asso_quid.push_back (subid);
255 // ====================================================== closeShape
256 void NewShape::closeShape ()
258 geo_shape = geo_compound;
259 shape_defined = true;
261 cout << " ------------------- closeShape " << el_name << endl;
262 parseShape (geo_shape);
264 int nombre = asso_edge.size ();
265 for (int nro = 0 ; nro < nombre ; ++nro)
266 asso_edge[nro]->addAssociation (this, asso_edid[nro], asso_pmin[nro],
268 nombre = asso_quad.size ();
269 for (int nro = 0 ; nro < nombre ; ++nro)
270 asso_quad[nro]->addAssociation (this, asso_quid[nro]);
273 nombre = asso_vertex.size ();
274 for (int nro = 0 ; nro < nombre ; ++nro)
276 EdgeShape* line = findEdge (asso_vxid[nro]);
277 Vertex* vertex = asso_vertex[nro];
278 if (line!=NULL && vertex!=NULL && NOT vertex->isAssociated())
280 line->getPoint (asso_param[nro], point);
281 asso_vertex[nro]->setAssociation (point);
286 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
287 // ====================================================== findVertex
288 VertexShape* NewShape::findVertex (int shid)
290 int nbre = tab_vertex.size ();
291 for (int nro=0 ; nro < nbre ; nro++)
293 VertexShape* shape = tab_vertex [nro];
294 if (shape->getIdent() == shid)
299 // ====================================================== findVertex
300 VertexShape* NewShape::findVertex (double point[])
302 int nbre = tab_vertex.size ();
303 for (int nro=0 ; nro < nbre ; nro++)
305 VertexShape* shape = tab_vertex [nro];
306 if (shape->definedBy (point))
311 // ====================================================== findEdge
312 EdgeShape* NewShape::findEdge (int shid)
314 int nbre = tab_edge.size ();
315 for (int nro=0 ; nro < nbre ; nro++)
317 EdgeShape* shape = tab_edge [nro];
318 if (shape->getIdent() == shid)
323 // ====================================================== findEdge
324 EdgeShape* NewShape::findEdge (double p1[], double p2[])
326 int nbre = tab_edge.size ();
327 for (int nro=0 ; nro < nbre ; nro++)
329 EdgeShape* shape = tab_edge [nro];
330 if (shape->definedBy (p1, p2))
335 // ====================================================== findFace
336 FaceShape* NewShape::findFace (int shid)
338 int nbre = tab_face.size ();
339 for (int nro=0 ; nro < nbre ; nro++)
341 FaceShape* shape = tab_face [nro];
342 if (shape->getIdent() == shid)
347 // ====================================================== findSubShape
348 SubShape* NewShape::findSubShape (int shid)
350 SubShape* shape = findEdge (shid);
352 shape = findVertex (shid);
354 shape = findFace (shid);
358 // ====================================================== getVertex
359 int NewShape::getVertex (int nro)
361 if (nro <0 || nro >= (int) tab_vertex.size())
364 SubShape* shape = tab_vertex [nro];
365 return shape->getIdent() ;
367 // ====================================================== getEdge
368 int NewShape::getEdge (int nro)
370 if (nro <0 || nro >= (int) tab_edge.size())
373 SubShape* shape = tab_edge [nro];
374 return shape->getIdent() ;
376 // ====================================================== getFace
377 int NewShape::getFace (int nro)
379 if (nro <0 || nro >= (int) tab_face.size())
382 SubShape* shape = tab_face [nro];
383 return shape->getIdent() ;
386 // ====================================================== getNameVertex
387 cpchar NewShape::getNameVertex (int nro)
389 if (nro <0 || nro >= (int) tab_vertex.size())
392 SubShape* shape = tab_vertex [nro];
393 return shape->getName() ;
395 // ====================================================== getNameEdge
396 cpchar NewShape::getNameEdge (int nro)
398 if (nro <0 || nro >= (int) tab_edge.size())
401 SubShape* shape = tab_edge [nro];
402 return shape->getName() ;
404 // ====================================================== getNameFace
405 cpchar NewShape::getNameFace (int nro)
407 if (nro <0 || nro >= (int) tab_face.size())
410 SubShape* shape = tab_face [nro];
411 return shape->getName() ;
413 // ====================================================== getFaceShape
414 FaceShape* NewShape::getFaceShape (int nro)
416 if (nro <0 || nro >= (int) tab_face.size())
419 FaceShape* shape = tab_face [nro];
422 // ====================================================== getEdgeShape
423 EdgeShape* NewShape::getEdgeShape (int nro)
425 if (nro <0 || nro >= (int) tab_edge.size())
428 EdgeShape* shape = tab_edge [nro];
431 // ====================================================== getVertexShape
432 VertexShape* NewShape::getVertexShape (int nro)
434 if (nro <0 || nro >= (int) tab_vertex.size())
437 VertexShape* shape = tab_vertex [nro];
440 // ====================================================== addPoint
441 int NewShape::addPoint (double* point)
444 int subid = tab_vertex.size() + 2;
446 sprintf (suffix, ":vertex_%02d", subid);
447 string name = el_name + suffix;
449 VertexShape* sub_shape = new VertexShape (this, subid, point);
450 sub_shape->setName (name);
451 tab_vertex.push_back (sub_shape);
454 // ===================================================== saveBrep
455 int NewShape::saveBrep ()
457 string filename = el_name + ".brep";
459 int ier = updateBrep ();
463 pfile fic = fopen (filename.c_str(), "w");
467 fprintf (fic, "%s\n", geo_brep.c_str());
471 // ===================================================== getBrep
472 cpchar NewShape::getBrep ()
474 int ier = updateBrep ();
477 return geo_brep.c_str();
479 // ====================================================== saveXml
480 void NewShape::saveXml (XmlWriter* xml)
482 if (sh_origin==SH_CLOUD)
484 int nbre = tab_vertex.size();
485 xml->addMark ("Cloud");
486 for (int nro=0 ; nro<nbre ; nro++)
487 tab_vertex[nro]->saveXml (xml);
493 int ya1brep = updateBrep ();
495 xml->openMark ("Shape");
496 xml->addAttribute ("id", getName (buffer));
497 xml->addAttribute ("type", sh_origin);
499 xml->addAttribute ("name", el_name);
503 clean_brep (geo_brep);
504 xml->addAttribute ("brep", geo_brep.c_str());
508 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
509 // ===================================================== parseShape
510 void NewShape::parseShape (const TopoDS_Shape& shape)
515 TopoDS_Iterator iter (shape);
516 for (; iter.More(); iter.Next())
518 TopoDS_Shape sub_shape = iter.Value();
519 parseShape (sub_shape);
524 // ===================================================== addSubShape
525 void NewShape::addSubShape (const TopoDS_Shape& shape)
531 int subid = map_shape.Add (shape);
532 int type = shape.ShapeType();
537 sprintf (chnum, ":face_%02d", subid);
538 name = el_name + chnum;
539 if (subid > sh_face_max)
541 FaceShape* sub_shape = new FaceShape (this, subid);
542 sub_shape->setName (name);
543 tab_face.push_back (sub_shape);
548 sprintf (chnum, ":edge_%02d", subid);
549 name = el_name + chnum;
550 if (subid > sh_edge_max)
552 EdgeShape* sub_shape = new EdgeShape (this, subid);
553 sub_shape->setName (name);
554 tab_edge.push_back (sub_shape);
559 sprintf (chnum, ":vertex_%02d", subid);
560 name = el_name + chnum;
561 if (subid > sh_vertex_max)
563 VertexShape* sub_shape = new VertexShape (this, subid);
564 sub_shape->setName (name);
565 tab_vertex.push_back (sub_shape);
566 sh_vertex_max = subid;
570 sprintf (chnum, "type=%d, subid=%02d", type, subid);
576 sprintf (chnum, "%4d : ", sh_indice);
578 for (int nro=1; nro<sh_level; nro++) cout << " | ";
581 if (type == TopAbs_VERTEX)
583 TopoDS_Vertex gver = TopoDS::Vertex (shape);
584 gp_Pnt gpoint = BRep_Tool::Pnt (gver);
586 cout << " = (" << gpoint.X()
588 << "," << gpoint.Z() << ")";
593 // ===================================================== updateShape
594 int NewShape::updateShape ()
599 if (NOT brep_defined)
602 shape_defined = true;
603 geom_brep2shape (geo_brep, geo_shape);
604 cout << " ------------------- updateShape " << el_name << endl;
605 parseShape (geo_shape);
608 // ===================================================== updateBrep
609 int NewShape::updateBrep ()
614 if (NOT shape_defined)
618 geom_make_brep (geo_shape, geo_brep);