2 // C++ : Gestion des Shapes (Hexa v5)
4 // Copyright (C) 2009-2019 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>
60 static bool db = on_debug (); // == getenv ("HEXA_DB") > 0
63 // ====================================================== Constructeur
64 NewShape::NewShape (Document* dad, EnumShape type)
65 : EltBase (dad, EL_SHAPE)
68 brep_defined = shape_defined = false;
70 sh_level = sh_indice = 0;
71 sh_face_max = sh_edge_max = sh_vertex_max = 0;
75 // ====================================================== setShape
76 void NewShape::setShape (const TopoDS_Shape& shape, EnumShape type)
90 sh_level = sh_indice = 0;
91 sh_face_max = sh_edge_max = sh_vertex_max = 0;
94 std::cout << " ------------------- setShape " << el_name << std::endl;
95 parseShape (geo_shape);
97 // ====================================================== setBrep
98 void NewShape::setBrep (rcstring brep)
104 // ====================================================== getGeoShape
105 const TopoDS_Shape& NewShape::getGeoShape (int id)
107 const TopoDS_Shape& shape = map_shape.FindKey (id);
110 // ====================================================== getShape
111 const TopoDS_Shape& NewShape::getShape ()
115 // ====================================================== openShape
116 void NewShape::openShape ()
118 geo_builder.MakeCompound (geo_compound);
128 // ====================================================== addCircle
129 int NewShape::addCircle (double* milieu, double rayon, double* normale,
132 gp_Pnt gp_center (milieu [dir_x], milieu [dir_y], milieu [dir_z]);
133 gp_Vec gp_vx (base [dir_x], base [dir_y], base [dir_z]);
134 gp_Vec gp_norm (normale[dir_x], normale[dir_y], normale[dir_z]);
136 gp_Ax2 gp_axes (gp_center, gp_norm, gp_vx);
137 gp_Circ gp_circ (gp_axes, rayon);
139 TopoDS_Edge geom_circ = BRepBuilderAPI_MakeEdge(gp_circ).Edge();
140 geo_builder.Add (geo_compound, geom_circ);
143 std::cout << " .... AddCircle subid " << curr_subid << " : rayon=" << rayon
144 << " centre = " << milieu[dir_x] << "," << milieu[dir_y]
145 << "," << milieu[dir_z]
146 << " norm = " << normale[dir_x] << "," << normale[dir_y]
147 << "," << normale[dir_z]
150 int subid = curr_subid;
154 // ====================================================== addSphere
155 int NewShape::addSphere (double* milieu, double radius)
157 gp_Pnt gp_center (milieu [dir_x], milieu [dir_y], milieu [dir_z]);
158 gp_Ax2 gp_axis (gp_center, gp_Dir(0,0,1), gp_Dir(1,0,0));
160 BRepPrimAPI_MakeSphere make_sphere (gp_axis, radius);
162 TopoDS_Shape geom_sphere = make_sphere.Face();
163 geo_builder.Add (geo_compound, geom_sphere);
166 std::cout << " .... AddSphere subid " << curr_subid << " : rayon=" << radius
167 << " centre = " << milieu[dir_x] << "," << milieu[dir_y]
168 << "," << milieu[dir_z] << std::endl;
170 int subid = curr_subid;
174 // ====================================================== transfoShape
175 int NewShape::transfoShape (Matrix& matrice, SubShape* shape)
178 double a11,a12,a13,a14, a21,a22,a23,a24, a31,a32,a33,a34;
179 matrice.getCoeff (a11,a12,a13,a14, a21,a22,a23,a24, a31,a32,a33,a34);
180 transfo.SetValues (a11,a12,a13,a14, a21,a22,a23,a24, a31,a32,a33,a34);
182 TopoDS_Shape shape_orig = shape->getShape ();
183 BRepBuilderAPI_Transform builder (shape_orig, transfo, Standard_True);
184 TopoDS_Shape result = builder.Shape();
186 geo_builder.Add (geo_compound, result);
188 int subid = curr_subid;
189 curr_subid += 2; // PROVISOIRE
192 // ====================================================== translateShape
193 int NewShape::translateShape (double dir[], SubShape* shape)
196 gp_Vec vecteur (dir [dir_x], dir [dir_y], dir [dir_z]);
197 transfo.SetTranslation (vecteur);
199 TopoDS_Shape shape_orig = shape->getShape ();
200 BRepBuilderAPI_Transform builder (shape_orig, transfo, Standard_True);
201 TopoDS_Shape result = builder.Shape();
203 geo_builder.Add (geo_compound, result);
205 int subid = curr_subid;
206 curr_subid += 2; // PROVISOIRE
209 // ====================================================== addAssociation
210 void NewShape::addAssociation (Vertex* vertex, int subid, double param)
215 asso_vertex.push_back (vertex);
216 asso_param .push_back (param);
217 asso_vxid .push_back (subid);
219 // ====================================================== addAssociation
220 void NewShape::addAssociation (Edge* edge, int subid, double pmin, double pmax)
227 std::cout << edge->getName()
228 << " = (" << edge->getVertex(0)->getName()
229 << ", " << edge->getVertex(1)->getName()
230 << ") --> subid= " << subid << " [ "
231 << pmin << ", " << pmax << "]" << std::endl;
234 asso_edge.push_back (edge);
235 asso_pmin.push_back (pmin);
236 asso_pmax.push_back (pmax);
237 asso_edid.push_back (subid);
239 // ====================================================== addAssociation
240 void NewShape::addAssociation (Quad* quad, int subid)
245 asso_quad.push_back (quad);
246 asso_quid.push_back (subid);
248 // ====================================================== closeShape
249 void NewShape::closeShape ()
251 geo_shape = geo_compound;
252 shape_defined = true;
254 std::cout << " ------------------- closeShape " << el_name << std::endl;
255 parseShape (geo_shape);
257 int nombre = asso_edge.size ();
258 for (int nro = 0 ; nro < nombre ; ++nro)
259 asso_edge[nro]->addAssociation (this, asso_edid[nro], asso_pmin[nro],
261 nombre = asso_quad.size ();
262 for (int nro = 0 ; nro < nombre ; ++nro)
263 asso_quad[nro]->addAssociation (this, asso_quid[nro]);
266 nombre = asso_vertex.size ();
267 for (int nro = 0 ; nro < nombre ; ++nro)
269 EdgeShape* line = findEdge (asso_vxid[nro]);
270 Vertex* vertex = asso_vertex[nro];
271 if (line!=NULL && vertex!=NULL && NOT vertex->isAssociated())
273 line->getPoint (asso_param[nro], point);
274 asso_vertex[nro]->setAssociation (point);
279 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
280 // ====================================================== findVertex
281 VertexShape* NewShape::findVertex (int shid)
283 int nbre = tab_vertex.size ();
284 for (int nro=0 ; nro < nbre ; nro++)
286 VertexShape* shape = tab_vertex [nro];
287 if (shape->getIdent() == shid)
292 // ====================================================== findVertex
293 VertexShape* NewShape::findVertex (double point[])
295 int nbre = tab_vertex.size ();
296 for (int nro=0 ; nro < nbre ; nro++)
298 VertexShape* shape = tab_vertex [nro];
299 if (shape->definedBy (point))
304 // ====================================================== findEdge
305 EdgeShape* NewShape::findEdge (int shid)
307 int nbre = tab_edge.size ();
308 for (int nro=0 ; nro < nbre ; nro++)
310 EdgeShape* shape = tab_edge [nro];
311 if (shape->getIdent() == shid)
316 // ====================================================== findEdge
317 EdgeShape* NewShape::findEdge (double p1[], double p2[])
319 int nbre = tab_edge.size ();
320 for (int nro=0 ; nro < nbre ; nro++)
322 EdgeShape* shape = tab_edge [nro];
323 if (shape->definedBy (p1, p2))
328 // ====================================================== findFace
329 FaceShape* NewShape::findFace (int shid)
331 int nbre = tab_face.size ();
332 for (int nro=0 ; nro < nbre ; nro++)
334 FaceShape* shape = tab_face [nro];
335 if (shape->getIdent() == shid)
340 // ====================================================== findSubShape
341 SubShape* NewShape::findSubShape (int shid)
343 SubShape* shape = findEdge (shid);
345 shape = findVertex (shid);
347 shape = findFace (shid);
351 // ====================================================== getVertex
352 int NewShape::getVertex (int nro)
354 if (nro <0 || nro >= (int) tab_vertex.size())
357 SubShape* shape = tab_vertex [nro];
358 return shape->getIdent() ;
360 // ====================================================== getEdge
361 int NewShape::getEdge (int nro)
363 if (nro <0 || nro >= (int) tab_edge.size())
366 SubShape* shape = tab_edge [nro];
367 return shape->getIdent() ;
369 // ====================================================== getFace
370 int NewShape::getFace (int nro)
372 if (nro <0 || nro >= (int) tab_face.size())
375 SubShape* shape = tab_face [nro];
376 return shape->getIdent() ;
379 // ====================================================== getNameVertex
380 cpchar NewShape::getNameVertex (int nro)
382 if (nro <0 || nro >= (int) tab_vertex.size())
385 SubShape* shape = tab_vertex [nro];
386 return shape->getName() ;
388 // ====================================================== getNameEdge
389 cpchar NewShape::getNameEdge (int nro)
391 if (nro <0 || nro >= (int) tab_edge.size())
394 SubShape* shape = tab_edge [nro];
395 return shape->getName() ;
397 // ====================================================== getNameFace
398 cpchar NewShape::getNameFace (int nro)
400 if (nro <0 || nro >= (int) tab_face.size())
403 SubShape* shape = tab_face [nro];
404 return shape->getName() ;
406 // ====================================================== getFaceShape
407 FaceShape* NewShape::getFaceShape (int nro)
409 if (nro <0 || nro >= (int) tab_face.size())
412 FaceShape* shape = tab_face [nro];
415 // ====================================================== getEdgeShape
416 EdgeShape* NewShape::getEdgeShape (int nro)
418 if (nro <0 || nro >= (int) tab_edge.size())
421 EdgeShape* shape = tab_edge [nro];
424 // ====================================================== getVertexShape
425 VertexShape* NewShape::getVertexShape (int nro)
427 if (nro <0 || nro >= (int) tab_vertex.size())
430 VertexShape* shape = tab_vertex [nro];
433 // ====================================================== addPoint
434 int NewShape::addPoint (double* point)
437 int subid = tab_vertex.size() + 2;
439 sprintf (suffix, ":vertex_%02d", subid);
440 std::string name = el_name + suffix;
442 VertexShape* sub_shape = new VertexShape (this, subid, point);
443 sub_shape->setName (name);
444 tab_vertex.push_back (sub_shape);
447 // ===================================================== saveBrep
448 int NewShape::saveBrep ()
450 std::string filename = el_name + ".brep";
452 int ier = updateBrep ();
456 pfile fic = fopen (filename.c_str(), "w");
460 fprintf (fic, "%s\n", geo_brep.c_str());
464 // ===================================================== getBrep
465 cpchar NewShape::getBrep ()
467 int ier = updateBrep ();
470 return geo_brep.c_str();
472 // ====================================================== saveXml
473 void NewShape::saveXml (XmlWriter* xml)
475 if (sh_origin==SH_CLOUD)
477 int nbre = tab_vertex.size();
478 xml->addMark ("Cloud");
479 for (int nro=0 ; nro<nbre ; nro++)
480 tab_vertex[nro]->saveXml (xml);
486 int ya1brep = updateBrep ();
488 xml->openMark ("Shape");
489 xml->addAttribute ("id", getName (buffer));
490 xml->addAttribute ("type", sh_origin);
492 xml->addAttribute ("name", el_name);
496 clean_brep (geo_brep);
497 xml->addAttribute ("brep", geo_brep.c_str());
501 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
502 // ===================================================== parseShape
503 void NewShape::parseShape (const TopoDS_Shape& shape)
508 TopoDS_Iterator iter (shape);
509 for (; iter.More(); iter.Next())
511 TopoDS_Shape sub_shape = iter.Value();
512 parseShape (sub_shape);
517 // ===================================================== addSubShape
518 void NewShape::addSubShape (const TopoDS_Shape& shape)
524 int subid = map_shape.Add (shape);
525 int type = shape.ShapeType();
530 sprintf (chnum, ":face_%02d", subid);
531 name = el_name + chnum;
532 if (subid > sh_face_max)
534 FaceShape* sub_shape = new FaceShape (this, subid);
535 sub_shape->setName (name);
536 tab_face.push_back (sub_shape);
541 sprintf (chnum, ":edge_%02d", subid);
542 name = el_name + chnum;
543 if (subid > sh_edge_max)
545 EdgeShape* sub_shape = new EdgeShape (this, subid);
546 sub_shape->setName (name);
547 tab_edge.push_back (sub_shape);
552 sprintf (chnum, ":vertex_%02d", subid);
553 name = el_name + chnum;
554 if (subid > sh_vertex_max)
556 VertexShape* sub_shape = new VertexShape (this, subid);
557 sub_shape->setName (name);
558 tab_vertex.push_back (sub_shape);
559 sh_vertex_max = subid;
563 sprintf (chnum, "type=%d, subid=%02d", type, subid);
569 sprintf (chnum, "%4d : ", sh_indice);
571 for (int nro=1; nro<sh_level; nro++) std::cout << " | ";
574 if (type == TopAbs_VERTEX)
576 TopoDS_Vertex gver = TopoDS::Vertex (shape);
577 gp_Pnt gpoint = BRep_Tool::Pnt (gver);
579 std::cout << " = (" << gpoint.X()
581 << "," << gpoint.Z() << ")";
583 std::cout << std::endl;
586 // ===================================================== updateShape
587 int NewShape::updateShape ()
592 if (NOT brep_defined)
595 shape_defined = true;
596 geom_brep2shape (geo_brep, geo_shape);
597 std::cout << " ------------------- updateShape " << el_name << std::endl;
598 parseShape (geo_shape);
601 // ===================================================== updateBrep
602 int NewShape::updateBrep ()
607 if (NOT shape_defined)
611 geom_make_brep (geo_shape, geo_brep);