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
62 static bool db0 = true;
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)
227 asso_edge.push_back (edge);
228 asso_pmin.push_back (pmin);
229 asso_pmax.push_back (pmax);
230 asso_edid.push_back (subid);
232 // ====================================================== addAssociation
233 void NewShape::addAssociation (Quad* quad, int subid)
238 asso_quad.push_back (quad);
239 asso_quid.push_back (subid);
241 // ====================================================== closeShape
242 void NewShape::closeShape ()
244 geo_shape = geo_compound;
245 shape_defined = true;
247 cout << " ------------------- closeShape " << el_name << endl;
248 parseShape (geo_shape);
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],
254 nombre = asso_quad.size ();
255 for (int nro = 0 ; nro < nombre ; ++nro)
256 asso_quad[nro]->addAssociation (this, asso_quid[nro]);
259 nombre = asso_vertex.size ();
260 for (int nro = 0 ; nro < nombre ; ++nro)
262 EdgeShape* line = findEdge (asso_vxid[nro]);
263 Vertex* vertex = asso_vertex[nro];
264 if (line!=NULL && vertex!=NULL && NOT vertex->isAssociated())
266 line->getPoint (asso_param[nro], point);
267 asso_vertex[nro]->setAssociation (point);
272 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
273 // ====================================================== findVertex
274 VertexShape* NewShape::findVertex (int shid)
276 int nbre = tab_vertex.size ();
277 for (int nro=0 ; nro < nbre ; nro++)
279 VertexShape* shape = tab_vertex [nro];
280 if (shape->getIdent() == shid)
285 // ====================================================== findEdge
286 EdgeShape* NewShape::findEdge (int shid)
288 int nbre = tab_edge.size ();
289 for (int nro=0 ; nro < nbre ; nro++)
291 EdgeShape* shape = tab_edge [nro];
292 if (shape->getIdent() == shid)
297 // ====================================================== findFace
298 FaceShape* NewShape::findFace (int shid)
300 int nbre = tab_face.size ();
301 for (int nro=0 ; nro < nbre ; nro++)
303 FaceShape* shape = tab_face [nro];
304 if (shape->getIdent() == shid)
309 // ====================================================== findSubShape
310 SubShape* NewShape::findSubShape (int shid)
312 SubShape* shape = findEdge (shid);
314 shape = findVertex (shid);
316 shape = findFace (shid);
320 // ====================================================== getVertex
321 int NewShape::getVertex (int nro)
323 if (nro <0 || nro >= (int) tab_vertex.size())
326 SubShape* shape = tab_vertex [nro];
327 return shape->getIdent() ;
329 // ====================================================== getEdge
330 int NewShape::getEdge (int nro)
332 if (nro <0 || nro >= (int) tab_edge.size())
335 SubShape* shape = tab_edge [nro];
336 return shape->getIdent() ;
338 // ====================================================== getFace
339 int NewShape::getFace (int nro)
341 if (nro <0 || nro >= (int) tab_face.size())
344 SubShape* shape = tab_face [nro];
345 return shape->getIdent() ;
348 // ====================================================== getNameVertex
349 cpchar NewShape::getNameVertex (int nro)
351 if (nro <0 || nro >= (int) tab_vertex.size())
354 SubShape* shape = tab_vertex [nro];
355 return shape->getName() ;
357 // ====================================================== getNameEdge
358 cpchar NewShape::getNameEdge (int nro)
360 if (nro <0 || nro >= (int) tab_edge.size())
363 SubShape* shape = tab_edge [nro];
364 return shape->getName() ;
366 // ====================================================== getNameFace
367 cpchar NewShape::getNameFace (int nro)
369 if (nro <0 || nro >= (int) tab_face.size())
372 SubShape* shape = tab_face [nro];
373 return shape->getName() ;
375 // ====================================================== getFaceShape
376 FaceShape* NewShape::getFaceShape (int nro)
378 if (nro <0 || nro >= (int) tab_face.size())
381 FaceShape* shape = tab_face [nro];
384 // ====================================================== getEdgeShape
385 EdgeShape* NewShape::getEdgeShape (int nro)
387 if (nro <0 || nro >= (int) tab_edge.size())
390 EdgeShape* shape = tab_edge [nro];
393 // ====================================================== getVertexShape
394 VertexShape* NewShape::getVertexShape (int nro)
396 if (nro <0 || nro >= (int) tab_vertex.size())
399 VertexShape* shape = tab_vertex [nro];
402 // ====================================================== addPoint
403 int NewShape::addPoint (double* point)
406 int subid = tab_vertex.size() + 2;
408 sprintf (suffix, ":vertex_%02d", subid);
409 string name = el_name + suffix;
411 VertexShape* sub_shape = new VertexShape (this, subid, point);
412 sub_shape->setName (name);
413 tab_vertex.push_back (sub_shape);
416 // ===================================================== saveBrep
417 int NewShape::saveBrep ()
419 string filename = el_name + ".brep";
421 int ier = updateBrep ();
425 pfile fic = fopen (filename.c_str(), "w");
429 fprintf (fic, "%s\n", geo_brep.c_str());
433 // ====================================================== saveXml
434 void NewShape::saveXml (XmlWriter* xml)
436 if (sh_origin==SH_CLOUD)
438 int nbre = tab_vertex.size();
439 xml->addMark ("Cloud");
440 for (int nro=0 ; nro<nbre ; nro++)
441 tab_vertex[nro]->saveXml (xml);
447 int ya1brep = updateBrep ();
449 xml->openMark ("Shape");
450 xml->addAttribute ("id", getName (buffer));
451 xml->addAttribute ("type", sh_origin);
453 xml->addAttribute ("name", el_name);
457 clean_brep (geo_brep);
458 xml->addAttribute ("brep", geo_brep.c_str());
462 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
463 // ===================================================== parseShape
464 void NewShape::parseShape (const TopoDS_Shape& shape)
469 TopoDS_Iterator iter (shape);
470 for (; iter.More(); iter.Next())
472 TopoDS_Shape sub_shape = iter.Value();
473 parseShape (sub_shape);
478 // ===================================================== addSubShape
479 void NewShape::addSubShape (const TopoDS_Shape& shape)
485 int subid = map_shape.Add (shape);
486 int type = shape.ShapeType();
491 sprintf (chnum, ":face_%02d", subid);
492 name = el_name + chnum;
493 if (subid > sh_face_max)
495 FaceShape* sub_shape = new FaceShape (this, subid);
496 sub_shape->setName (name);
497 tab_face.push_back (sub_shape);
502 sprintf (chnum, ":edge_%02d", subid);
503 name = el_name + chnum;
504 if (subid > sh_edge_max)
506 EdgeShape* sub_shape = new EdgeShape (this, subid);
507 sub_shape->setName (name);
508 tab_edge.push_back (sub_shape);
513 sprintf (chnum, ":vertex_%02d", subid);
514 name = el_name + chnum;
515 if (subid > sh_vertex_max)
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;
524 sprintf (chnum, "type=%d, subid=%02d", type, subid);
530 sprintf (chnum, "%4d : ", sh_indice);
532 for (int nro=1; nro<sh_level; nro++) cout << " | ";
535 if (type == TopAbs_VERTEX)
537 TopoDS_Vertex gver = TopoDS::Vertex (shape);
538 gp_Pnt gpoint = BRep_Tool::Pnt (gver);
540 cout << " = (" << gpoint.X()
542 << "," << gpoint.Z() << ")";
547 // ===================================================== updateShape
548 int NewShape::updateShape ()
553 if (NOT brep_defined)
556 shape_defined = true;
557 geom_brep2shape (geo_brep, geo_shape);
558 cout << " ------------------- updateShape " << el_name << endl;
559 parseShape (geo_shape);
562 // ===================================================== updateBrep
563 int NewShape::updateBrep ()
568 if (NOT shape_defined)
572 geom_make_brep (geo_shape, geo_brep);