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 // ====================================================== findEdge
295 EdgeShape* NewShape::findEdge (int shid)
297 int nbre = tab_edge.size ();
298 for (int nro=0 ; nro < nbre ; nro++)
300 EdgeShape* shape = tab_edge [nro];
301 if (shape->getIdent() == shid)
306 // ====================================================== findFace
307 FaceShape* NewShape::findFace (int shid)
309 int nbre = tab_face.size ();
310 for (int nro=0 ; nro < nbre ; nro++)
312 FaceShape* shape = tab_face [nro];
313 if (shape->getIdent() == shid)
318 // ====================================================== findSubShape
319 SubShape* NewShape::findSubShape (int shid)
321 SubShape* shape = findEdge (shid);
323 shape = findVertex (shid);
325 shape = findFace (shid);
329 // ====================================================== getVertex
330 int NewShape::getVertex (int nro)
332 if (nro <0 || nro >= (int) tab_vertex.size())
335 SubShape* shape = tab_vertex [nro];
336 return shape->getIdent() ;
338 // ====================================================== getEdge
339 int NewShape::getEdge (int nro)
341 if (nro <0 || nro >= (int) tab_edge.size())
344 SubShape* shape = tab_edge [nro];
345 return shape->getIdent() ;
347 // ====================================================== getFace
348 int NewShape::getFace (int nro)
350 if (nro <0 || nro >= (int) tab_face.size())
353 SubShape* shape = tab_face [nro];
354 return shape->getIdent() ;
357 // ====================================================== getNameVertex
358 cpchar NewShape::getNameVertex (int nro)
360 if (nro <0 || nro >= (int) tab_vertex.size())
363 SubShape* shape = tab_vertex [nro];
364 return shape->getName() ;
366 // ====================================================== getNameEdge
367 cpchar NewShape::getNameEdge (int nro)
369 if (nro <0 || nro >= (int) tab_edge.size())
372 SubShape* shape = tab_edge [nro];
373 return shape->getName() ;
375 // ====================================================== getNameFace
376 cpchar NewShape::getNameFace (int nro)
378 if (nro <0 || nro >= (int) tab_face.size())
381 SubShape* shape = tab_face [nro];
382 return shape->getName() ;
384 // ====================================================== getFaceShape
385 FaceShape* NewShape::getFaceShape (int nro)
387 if (nro <0 || nro >= (int) tab_face.size())
390 FaceShape* shape = tab_face [nro];
393 // ====================================================== getEdgeShape
394 EdgeShape* NewShape::getEdgeShape (int nro)
396 if (nro <0 || nro >= (int) tab_edge.size())
399 EdgeShape* shape = tab_edge [nro];
402 // ====================================================== getVertexShape
403 VertexShape* NewShape::getVertexShape (int nro)
405 if (nro <0 || nro >= (int) tab_vertex.size())
408 VertexShape* shape = tab_vertex [nro];
411 // ====================================================== addPoint
412 int NewShape::addPoint (double* point)
415 int subid = tab_vertex.size() + 2;
417 sprintf (suffix, ":vertex_%02d", subid);
418 string name = el_name + suffix;
420 VertexShape* sub_shape = new VertexShape (this, subid, point);
421 sub_shape->setName (name);
422 tab_vertex.push_back (sub_shape);
425 // ===================================================== saveBrep
426 int NewShape::saveBrep ()
428 string filename = el_name + ".brep";
430 int ier = updateBrep ();
434 pfile fic = fopen (filename.c_str(), "w");
438 fprintf (fic, "%s\n", geo_brep.c_str());
442 // ====================================================== saveXml
443 void NewShape::saveXml (XmlWriter* xml)
445 if (sh_origin==SH_CLOUD)
447 int nbre = tab_vertex.size();
448 xml->addMark ("Cloud");
449 for (int nro=0 ; nro<nbre ; nro++)
450 tab_vertex[nro]->saveXml (xml);
456 int ya1brep = updateBrep ();
458 xml->openMark ("Shape");
459 xml->addAttribute ("id", getName (buffer));
460 xml->addAttribute ("type", sh_origin);
462 xml->addAttribute ("name", el_name);
466 clean_brep (geo_brep);
467 xml->addAttribute ("brep", geo_brep.c_str());
471 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
472 // ===================================================== parseShape
473 void NewShape::parseShape (const TopoDS_Shape& shape)
478 TopoDS_Iterator iter (shape);
479 for (; iter.More(); iter.Next())
481 TopoDS_Shape sub_shape = iter.Value();
482 parseShape (sub_shape);
487 // ===================================================== addSubShape
488 void NewShape::addSubShape (const TopoDS_Shape& shape)
494 int subid = map_shape.Add (shape);
495 int type = shape.ShapeType();
500 sprintf (chnum, ":face_%02d", subid);
501 name = el_name + chnum;
502 if (subid > sh_face_max)
504 FaceShape* sub_shape = new FaceShape (this, subid);
505 sub_shape->setName (name);
506 tab_face.push_back (sub_shape);
511 sprintf (chnum, ":edge_%02d", subid);
512 name = el_name + chnum;
513 if (subid > sh_edge_max)
515 EdgeShape* sub_shape = new EdgeShape (this, subid);
516 sub_shape->setName (name);
517 tab_edge.push_back (sub_shape);
522 sprintf (chnum, ":vertex_%02d", subid);
523 name = el_name + chnum;
524 if (subid > sh_vertex_max)
526 VertexShape* sub_shape = new VertexShape (this, subid);
527 sub_shape->setName (name);
528 tab_vertex.push_back (sub_shape);
529 sh_vertex_max = subid;
533 sprintf (chnum, "type=%d, subid=%02d", type, subid);
539 sprintf (chnum, "%4d : ", sh_indice);
541 for (int nro=1; nro<sh_level; nro++) cout << " | ";
544 if (type == TopAbs_VERTEX)
546 TopoDS_Vertex gver = TopoDS::Vertex (shape);
547 gp_Pnt gpoint = BRep_Tool::Pnt (gver);
549 cout << " = (" << gpoint.X()
551 << "," << gpoint.Z() << ")";
556 // ===================================================== updateShape
557 int NewShape::updateShape ()
562 if (NOT brep_defined)
565 shape_defined = true;
566 geom_brep2shape (geo_brep, geo_shape);
567 cout << " ------------------- updateShape " << el_name << endl;
568 parseShape (geo_shape);
571 // ===================================================== updateBrep
572 int NewShape::updateBrep ()
577 if (NOT shape_defined)
581 geom_make_brep (geo_shape, geo_brep);