2 // C++ : Gestion des Shapes (Hexa v5)
4 // Copyright (C) 2009-2013 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/
21 // 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>
61 static bool db = on_debug (); // == getenv ("HEXA_DB") > 0
64 // ====================================================== Constructeur
65 NewShape::NewShape (Document* dad, EnumShape type)
66 : EltBase (dad, EL_SHAPE)
69 brep_defined = shape_defined = false;
71 sh_level = sh_indice = 0;
72 sh_face_max = sh_edge_max = sh_vertex_max = 0;
76 // ====================================================== setShape
77 void NewShape::setShape (const TopoDS_Shape& shape, EnumShape type)
91 sh_level = sh_indice = 0;
92 sh_face_max = sh_edge_max = sh_vertex_max = 0;
95 cout << " ------------------- setShape " << el_name << endl;
96 parseShape (geo_shape);
98 // ====================================================== setBrep
99 void NewShape::setBrep (rcstring brep)
105 // ====================================================== getGeoShape
106 const TopoDS_Shape& NewShape::getGeoShape (int id)
108 const TopoDS_Shape& shape = map_shape.FindKey (id);
111 // ====================================================== getShape
112 const TopoDS_Shape& NewShape::getShape ()
116 // ====================================================== openShape
117 void NewShape::openShape ()
119 geo_builder.MakeCompound (geo_compound);
129 // ====================================================== addCircle
130 int NewShape::addCircle (double* milieu, double rayon, double* normale,
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]);
137 gp_Ax2 gp_axes (gp_center, gp_norm, gp_vx);
138 gp_Circ gp_circ (gp_axes, rayon);
140 TopoDS_Edge geom_circ = BRepBuilderAPI_MakeEdge(gp_circ).Edge();
141 geo_builder.Add (geo_compound, geom_circ);
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]
151 int subid = curr_subid;
155 // ====================================================== addSphere
156 int NewShape::addSphere (double* milieu, double radius)
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));
161 BRepPrimAPI_MakeSphere make_sphere (gp_axis, radius);
163 TopoDS_Shape geom_sphere = make_sphere.Face();
164 geo_builder.Add (geo_compound, geom_sphere);
167 cout << " .... AddSphere subid " << curr_subid << " : rayon=" << radius
168 << " centre = " << milieu[dir_x] << "," << milieu[dir_y]
169 << "," << milieu[dir_z] << endl;
171 int subid = curr_subid;
175 // ====================================================== transfoShape
176 int NewShape::transfoShape (Matrix& matrice, SubShape* shape)
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,
184 TopoDS_Shape shape_orig = shape->getShape ();
185 BRepBuilderAPI_Transform builder (shape_orig, transfo, Standard_True);
186 TopoDS_Shape result = builder.Shape();
188 geo_builder.Add (geo_compound, result);
190 int subid = curr_subid;
191 curr_subid += 2; // PROVISOIRE
194 // ====================================================== translateShape
195 int NewShape::translateShape (double dir[], SubShape* shape)
198 gp_Vec vecteur (dir [dir_x], dir [dir_y], dir [dir_z]);
199 transfo.SetTranslation (vecteur);
201 TopoDS_Shape shape_orig = shape->getShape ();
202 BRepBuilderAPI_Transform builder (shape_orig, transfo, Standard_True);
203 TopoDS_Shape result = builder.Shape();
205 geo_builder.Add (geo_compound, result);
207 int subid = curr_subid;
208 curr_subid += 2; // PROVISOIRE
211 // ====================================================== addAssociation
212 void NewShape::addAssociation (Vertex* vertex, int subid, double param)
217 asso_vertex.push_back (vertex);
218 asso_param .push_back (param);
219 asso_vxid .push_back (subid);
221 // ====================================================== addAssociation
222 void NewShape::addAssociation (Edge* edge, int subid, double pmin, double pmax)
229 cout << edge->getName()
230 << " = (" << edge->getVertex(0)->getName()
231 << ", " << edge->getVertex(1)->getName()
232 << ") --> subid= " << subid << " [ "
233 << pmin << ", " << pmax << "]" << endl;
236 asso_edge.push_back (edge);
237 asso_pmin.push_back (pmin);
238 asso_pmax.push_back (pmax);
239 asso_edid.push_back (subid);
241 // ====================================================== addAssociation
242 void NewShape::addAssociation (Quad* quad, int subid)
247 asso_quad.push_back (quad);
248 asso_quid.push_back (subid);
250 // ====================================================== closeShape
251 void NewShape::closeShape ()
253 geo_shape = geo_compound;
254 shape_defined = true;
256 cout << " ------------------- closeShape " << el_name << endl;
257 parseShape (geo_shape);
259 int nombre = asso_edge.size ();
260 for (int nro = 0 ; nro < nombre ; ++nro)
261 asso_edge[nro]->addAssociation (this, asso_edid[nro], asso_pmin[nro],
263 nombre = asso_quad.size ();
264 for (int nro = 0 ; nro < nombre ; ++nro)
265 asso_quad[nro]->addAssociation (this, asso_quid[nro]);
268 nombre = asso_vertex.size ();
269 for (int nro = 0 ; nro < nombre ; ++nro)
271 EdgeShape* line = findEdge (asso_vxid[nro]);
272 Vertex* vertex = asso_vertex[nro];
273 if (line!=NULL && vertex!=NULL && NOT vertex->isAssociated())
275 line->getPoint (asso_param[nro], point);
276 asso_vertex[nro]->setAssociation (point);
281 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
282 // ====================================================== findVertex
283 VertexShape* NewShape::findVertex (int shid)
285 int nbre = tab_vertex.size ();
286 for (int nro=0 ; nro < nbre ; nro++)
288 VertexShape* shape = tab_vertex [nro];
289 if (shape->getIdent() == shid)
294 // ====================================================== findVertex
295 VertexShape* NewShape::findVertex (double point[])
297 int nbre = tab_vertex.size ();
298 for (int nro=0 ; nro < nbre ; nro++)
300 VertexShape* shape = tab_vertex [nro];
301 if (shape->definedBy (point))
306 // ====================================================== findEdge
307 EdgeShape* NewShape::findEdge (int shid)
309 int nbre = tab_edge.size ();
310 for (int nro=0 ; nro < nbre ; nro++)
312 EdgeShape* shape = tab_edge [nro];
313 if (shape->getIdent() == shid)
318 // ====================================================== findEdge
319 EdgeShape* NewShape::findEdge (double p1[], double p2[])
321 int nbre = tab_edge.size ();
322 for (int nro=0 ; nro < nbre ; nro++)
324 EdgeShape* shape = tab_edge [nro];
325 if (shape->definedBy (p1, p2))
330 // ====================================================== findFace
331 FaceShape* NewShape::findFace (int shid)
333 int nbre = tab_face.size ();
334 for (int nro=0 ; nro < nbre ; nro++)
336 FaceShape* shape = tab_face [nro];
337 if (shape->getIdent() == shid)
342 // ====================================================== findSubShape
343 SubShape* NewShape::findSubShape (int shid)
345 SubShape* shape = findEdge (shid);
347 shape = findVertex (shid);
349 shape = findFace (shid);
353 // ====================================================== getVertex
354 int NewShape::getVertex (int nro)
356 if (nro <0 || nro >= (int) tab_vertex.size())
359 SubShape* shape = tab_vertex [nro];
360 return shape->getIdent() ;
362 // ====================================================== getEdge
363 int NewShape::getEdge (int nro)
365 if (nro <0 || nro >= (int) tab_edge.size())
368 SubShape* shape = tab_edge [nro];
369 return shape->getIdent() ;
371 // ====================================================== getFace
372 int NewShape::getFace (int nro)
374 if (nro <0 || nro >= (int) tab_face.size())
377 SubShape* shape = tab_face [nro];
378 return shape->getIdent() ;
381 // ====================================================== getNameVertex
382 cpchar NewShape::getNameVertex (int nro)
384 if (nro <0 || nro >= (int) tab_vertex.size())
387 SubShape* shape = tab_vertex [nro];
388 return shape->getName() ;
390 // ====================================================== getNameEdge
391 cpchar NewShape::getNameEdge (int nro)
393 if (nro <0 || nro >= (int) tab_edge.size())
396 SubShape* shape = tab_edge [nro];
397 return shape->getName() ;
399 // ====================================================== getNameFace
400 cpchar NewShape::getNameFace (int nro)
402 if (nro <0 || nro >= (int) tab_face.size())
405 SubShape* shape = tab_face [nro];
406 return shape->getName() ;
408 // ====================================================== getFaceShape
409 FaceShape* NewShape::getFaceShape (int nro)
411 if (nro <0 || nro >= (int) tab_face.size())
414 FaceShape* shape = tab_face [nro];
417 // ====================================================== getEdgeShape
418 EdgeShape* NewShape::getEdgeShape (int nro)
420 if (nro <0 || nro >= (int) tab_edge.size())
423 EdgeShape* shape = tab_edge [nro];
426 // ====================================================== getVertexShape
427 VertexShape* NewShape::getVertexShape (int nro)
429 if (nro <0 || nro >= (int) tab_vertex.size())
432 VertexShape* shape = tab_vertex [nro];
435 // ====================================================== addPoint
436 int NewShape::addPoint (double* point)
439 int subid = tab_vertex.size() + 2;
441 sprintf (suffix, ":vertex_%02d", subid);
442 string name = el_name + suffix;
444 VertexShape* sub_shape = new VertexShape (this, subid, point);
445 sub_shape->setName (name);
446 tab_vertex.push_back (sub_shape);
449 // ===================================================== saveBrep
450 int NewShape::saveBrep ()
452 string filename = el_name + ".brep";
454 int ier = updateBrep ();
458 pfile fic = fopen (filename.c_str(), "w");
462 fprintf (fic, "%s\n", geo_brep.c_str());
466 // ====================================================== saveXml
467 void NewShape::saveXml (XmlWriter* xml)
469 if (sh_origin==SH_CLOUD)
471 int nbre = tab_vertex.size();
472 xml->addMark ("Cloud");
473 for (int nro=0 ; nro<nbre ; nro++)
474 tab_vertex[nro]->saveXml (xml);
480 int ya1brep = updateBrep ();
482 xml->openMark ("Shape");
483 xml->addAttribute ("id", getName (buffer));
484 xml->addAttribute ("type", sh_origin);
486 xml->addAttribute ("name", el_name);
490 clean_brep (geo_brep);
491 xml->addAttribute ("brep", geo_brep.c_str());
495 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
496 // ===================================================== parseShape
497 void NewShape::parseShape (const TopoDS_Shape& shape)
502 TopoDS_Iterator iter (shape);
503 for (; iter.More(); iter.Next())
505 TopoDS_Shape sub_shape = iter.Value();
506 parseShape (sub_shape);
511 // ===================================================== addSubShape
512 void NewShape::addSubShape (const TopoDS_Shape& shape)
518 int subid = map_shape.Add (shape);
519 int type = shape.ShapeType();
524 sprintf (chnum, ":face_%02d", subid);
525 name = el_name + chnum;
526 if (subid > sh_face_max)
528 FaceShape* sub_shape = new FaceShape (this, subid);
529 sub_shape->setName (name);
530 tab_face.push_back (sub_shape);
535 sprintf (chnum, ":edge_%02d", subid);
536 name = el_name + chnum;
537 if (subid > sh_edge_max)
539 EdgeShape* sub_shape = new EdgeShape (this, subid);
540 sub_shape->setName (name);
541 tab_edge.push_back (sub_shape);
546 sprintf (chnum, ":vertex_%02d", subid);
547 name = el_name + chnum;
548 if (subid > sh_vertex_max)
550 VertexShape* sub_shape = new VertexShape (this, subid);
551 sub_shape->setName (name);
552 tab_vertex.push_back (sub_shape);
553 sh_vertex_max = subid;
557 sprintf (chnum, "type=%d, subid=%02d", type, subid);
563 sprintf (chnum, "%4d : ", sh_indice);
565 for (int nro=1; nro<sh_level; nro++) cout << " | ";
568 if (type == TopAbs_VERTEX)
570 TopoDS_Vertex gver = TopoDS::Vertex (shape);
571 gp_Pnt gpoint = BRep_Tool::Pnt (gver);
573 cout << " = (" << gpoint.X()
575 << "," << gpoint.Z() << ")";
580 // ===================================================== updateShape
581 int NewShape::updateShape ()
586 if (NOT brep_defined)
589 shape_defined = true;
590 geom_brep2shape (geo_brep, geo_shape);
591 cout << " ------------------- updateShape " << el_name << endl;
592 parseShape (geo_shape);
595 // ===================================================== updateBrep
596 int NewShape::updateBrep ()
601 if (NOT shape_defined)
605 geom_make_brep (geo_shape, geo_brep);