2 // C++ : Gestion des Shapes (Hexa v5)
4 // Copyright (C) 2009-2012 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"
32 #include "HexKas_functions.hxx"
34 #include "HexXmlWriter.hxx"
35 #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>
48 #include <BRep_Tool.hxx>
55 #include <gp_Circ.hxx>
60 static bool db = on_debug (); // == getenv ("HEXA_DB") > 0
61 static bool db0 = true;
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 cout << " ------------------- setShape " << el_name << 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 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 cout << " .... AddSphere subid " << curr_subid << " : rayon=" << radius
167 << " centre = " << milieu[dir_x] << "," << milieu[dir_y]
168 << "," << milieu[dir_z] << endl;
170 int subid = curr_subid;
174 // ====================================================== addAssociation
175 void NewShape::addAssociation (Vertex* vertex, int subid, double param)
180 asso_vertex.push_back (vertex);
181 asso_param .push_back (param);
182 asso_vxid .push_back (subid);
184 // ====================================================== addAssociation
185 void NewShape::addAssociation (Edge* edge, int subid, double pmin, double pmax)
190 asso_edge.push_back (edge);
191 asso_pmin.push_back (pmin);
192 asso_pmax.push_back (pmax);
193 asso_edid.push_back (subid);
195 // ====================================================== addAssociation
196 void NewShape::addAssociation (Quad* quad, int subid)
201 asso_quad.push_back (quad);
202 asso_quid.push_back (subid);
204 // ====================================================== closeShape
205 void NewShape::closeShape ()
207 geo_shape = geo_compound;
208 shape_defined = true;
210 cout << " ------------------- closeShape " << el_name << endl;
211 parseShape (geo_shape);
213 int nombre = asso_edge.size ();
214 for (int nro = 0 ; nro < nombre ; ++nro)
215 asso_edge[nro]->addAssociation (this, asso_edid[nro], asso_pmin[nro],
217 nombre = asso_quad.size ();
218 for (int nro = 0 ; nro < nombre ; ++nro)
219 asso_quad[nro]->addAssociation (this, asso_quid[nro]);
222 nombre = asso_vertex.size ();
223 for (int nro = 0 ; nro < nombre ; ++nro)
225 EdgeShape* line = findEdge (asso_vxid[nro]);
226 Vertex* vertex = asso_vertex[nro];
227 if (line!=NULL && vertex!=NULL && NOT vertex->isAssociated())
229 line->getPoint (asso_param[nro], point);
230 asso_vertex[nro]->setAssociation (point);
235 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
236 // ====================================================== findVertex
237 VertexShape* NewShape::findVertex (int shid)
239 int nbre = tab_vertex.size ();
240 for (int nro=0 ; nro < nbre ; nro++)
242 VertexShape* shape = tab_vertex [nro];
243 if (shape->getIdent() == shid)
248 // ====================================================== findEdge
249 EdgeShape* NewShape::findEdge (int shid)
251 int nbre = tab_edge.size ();
252 for (int nro=0 ; nro < nbre ; nro++)
254 EdgeShape* shape = tab_edge [nro];
255 if (shape->getIdent() == shid)
260 // ====================================================== findFace
261 FaceShape* NewShape::findFace (int shid)
263 int nbre = tab_face.size ();
264 for (int nro=0 ; nro < nbre ; nro++)
266 FaceShape* shape = tab_face [nro];
267 if (shape->getIdent() == shid)
272 // ====================================================== findSubShape
273 SubShape* NewShape::findSubShape (int shid)
275 SubShape* shape = findEdge (shid);
277 shape = findVertex (shid);
279 shape = findFace (shid);
283 // ====================================================== getVertex
284 int NewShape::getVertex (int nro)
286 if (nro <0 || nro >= tab_vertex.size())
289 SubShape* shape = tab_vertex [nro];
290 return shape->getIdent() ;
292 // ====================================================== getEdge
293 int NewShape::getEdge (int nro)
295 if (nro <0 || nro >= tab_edge.size())
298 SubShape* shape = tab_edge [nro];
299 return shape->getIdent() ;
301 // ====================================================== getFace
302 int NewShape::getFace (int nro)
304 if (nro <0 || nro >= tab_face.size())
307 SubShape* shape = tab_face [nro];
308 return shape->getIdent() ;
311 // ====================================================== getNameVertex
312 cpchar NewShape::getNameVertex (int nro)
314 if (nro <0 || nro >= tab_vertex.size())
317 SubShape* shape = tab_vertex [nro];
318 return shape->getName() ;
320 // ====================================================== getNameEdge
321 cpchar NewShape::getNameEdge (int nro)
323 if (nro <0 || nro >= tab_edge.size())
326 SubShape* shape = tab_edge [nro];
327 return shape->getName() ;
329 // ====================================================== getNameFace
330 cpchar NewShape::getNameFace (int nro)
332 if (nro <0 || nro >= tab_face.size())
335 SubShape* shape = tab_face [nro];
336 return shape->getName() ;
338 // ====================================================== getFaceShape
339 FaceShape* NewShape::getFaceShape (int nro)
341 if (nro <0 || nro >= tab_face.size())
344 FaceShape* shape = tab_face [nro];
347 // ====================================================== getEdgeShape
348 EdgeShape* NewShape::getEdgeShape (int nro)
350 if (nro <0 || nro >= tab_edge.size())
353 EdgeShape* shape = tab_edge [nro];
356 // ====================================================== getVertexShape
357 VertexShape* NewShape::getVertexShape (int nro)
359 if (nro <0 || nro >= tab_vertex.size())
362 VertexShape* shape = tab_vertex [nro];
365 // ====================================================== addPoint
366 int NewShape::addPoint (double* point)
369 int subid = tab_vertex.size() + 1;
371 sprintf (suffix, ":vertex%02d", subid);
372 string name = el_name + suffix;
374 VertexShape* sub_shape = new VertexShape (this, subid, point);
375 sub_shape->setName (name);
376 tab_vertex.push_back (sub_shape);
379 // ===================================================== saveBrep
380 int NewShape::saveBrep ()
382 string filename = el_name + ".brep";
384 int ier = updateBrep ();
388 pfile fic = fopen (filename.c_str(), "w");
392 fprintf (fic, "%s\n", geo_brep.c_str());
395 // ====================================================== saveXml
396 void NewShape::saveXml (XmlWriter* xml)
398 if (sh_origin==SH_CLOUD)
400 int nbre = tab_vertex.size();
401 xml->addMark ("Cloud");
402 for (int nro=0 ; nro<nbre ; nro++)
403 tab_vertex[nro]->saveXml (xml);
409 int ya1brep = updateBrep ();
411 xml->openMark ("Shape");
412 xml->addAttribute ("id", getName (buffer));
413 xml->addAttribute ("type", sh_origin);
415 xml->addAttribute ("name", el_name);
419 clean_brep (geo_brep);
420 xml->addAttribute ("brep", geo_brep.c_str());
424 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
425 // ===================================================== parseShape
426 void NewShape::parseShape (const TopoDS_Shape& shape)
431 TopoDS_Iterator iter (shape);
432 for (; iter.More(); iter.Next())
434 TopoDS_Shape sub_shape = iter.Value();
435 parseShape (sub_shape);
440 // ===================================================== addSubShape
441 void NewShape::addSubShape (const TopoDS_Shape& shape)
447 int subid = map_shape.Add (shape);
448 int type = shape.ShapeType();
453 sprintf (chnum, ":face_%02d", subid);
454 name = el_name + chnum;
455 if (subid > sh_face_max)
457 FaceShape* sub_shape = new FaceShape (this, subid);
458 sub_shape->setName (name);
459 tab_face.push_back (sub_shape);
464 sprintf (chnum, ":edge_%02d", subid);
465 name = el_name + chnum;
466 if (subid > sh_edge_max)
468 EdgeShape* sub_shape = new EdgeShape (this, subid);
469 sub_shape->setName (name);
470 tab_edge.push_back (sub_shape);
475 sprintf (chnum, ":vertex_%02d", subid);
476 name = el_name + chnum;
477 if (subid > sh_vertex_max)
479 VertexShape* sub_shape = new VertexShape (this, subid);
480 sub_shape->setName (name);
481 tab_vertex.push_back (sub_shape);
482 sh_vertex_max = subid;
486 sprintf (chnum, "type=%d, subid=%02d", type, subid);
492 sprintf (chnum, "%4d : ", sh_indice);
494 for (int nro=1; nro<sh_level; nro++) cout << " | ";
497 if (type == TopAbs_VERTEX)
499 TopoDS_Vertex gver = TopoDS::Vertex (shape);
500 gp_Pnt gpoint = BRep_Tool::Pnt (gver);
502 cout << " = (" << gpoint.X()
504 << "," << gpoint.Z() << ")";
509 // ===================================================== updateShape
510 int NewShape::updateShape ()
515 if (NOT brep_defined)
518 shape_defined = true;
519 geom_brep2shape (geo_brep, geo_shape);
520 cout << " ------------------- updateShape " << el_name << endl;
521 parseShape (geo_shape);
524 // ===================================================== updateBrep
525 int NewShape::updateBrep ()
530 if (NOT shape_defined)
534 geom_make_brep (geo_shape, geo_brep);
541 // ====================================================== ===================
543 NewShape::NewShape (Document* dad) : EltBase (dad, EL_SHAPE) {}
544 void NewShape::setShape (const TopoDS_Shape& shape) {}
545 void NewShape::saveXml (XmlWriter* xml) {}
546 SubShape* NewShape::findSubShape (int shid) { return NULL; }
547 SubShape* NewShape::findVertex (int shid) { return NULL; }
548 SubShape* NewShape::findEdge (int shid) { return NULL; }
549 SubShape* NewShape::findFace (int shid) { return NULL; }
551 int NewShape::getVertex (int nro) { return 0 ; }
552 int NewShape::getEdge (int nro) { return 0 ; }
553 int NewShape::getFace (int nro) { return 0 ; }
555 cpchar NewShape::getNameVertex (int nro) { return "Nothing" ; }
556 cpchar NewShape::getNameEdge (int nro) { return "Nothing" ; }
557 cpchar NewShape::getNameFace (int nro) { return "Nothing" ; }