Salome HOME
Updated copyright comment
[modules/hexablock.git] / src / HEXABLOCK / HexKas_functions.cxx
1 //
2 // CC++ : Fonctions Interface Cascade
3 //
4 // Copyright (C) 2009-2024  CEA, EDF
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HexKas_functions.hxx"
24 //  #include "HexEdge.hxx"
25
26 #include <TopoDS.hxx>
27 #include <TopoDS_Shape.hxx>
28 #include <TopoDS_Vertex.hxx>
29
30 #include <BRep_Builder.hxx>
31 #include <BRepTools.hxx>
32
33 #include <gp_Pnt.hxx>
34
35 #include <sstream>
36
37 BEGIN_NAMESPACE_HEXA
38
39 // ====================================================== same_coords
40 bool same_coords (gp_Pnt& pa, gp_Pnt& pb, double epsilon)
41 {
42    double d2 = carre (pb.X()-pa.X()) + carre (pb.Y()-pa.Y())
43                                      + carre (pb.Z()-pa.Z()) ;
44    return d2 < epsilon;
45 }
46 // ====================================================== save_brep
47 void save_brep (cpchar nom, std::string brep, int& compteur)
48 {
49     char buff[8];
50     compteur ++;
51     sprintf (buff ,"%d", compteur);
52
53     std::string name (nom);
54     name += buff;
55     name += ".brep";
56     FILE*    fic = fopen (name.c_str(), "w");
57     fprintf (fic, "%s\n", brep.c_str());
58     fclose  (fic);
59 }
60 // ====================================================== geom_make_brep
61 void geom_make_brep (TopoDS_Shape& shape, std::string& brep)
62 {
63    std::ostringstream  stream_shape;
64    BRepTools::Write (shape, stream_shape);
65    brep = stream_shape.str();
66 }
67 // ====================================================== geom_brep2shape (F)
68 TopoDS_Shape geom_brep2shape (rcstring brep)
69 {
70    TopoDS_Shape topo;
71    std::istringstream streamBrep(brep);
72    BRep_Builder aBuilder;
73    BRepTools::Read (topo, streamBrep, aBuilder);
74    return topo;
75 }
76 // ====================================================== geom_brep2shape
77 int geom_brep2shape (rcstring brep, TopoDS_Shape& shape)
78 {
79    std::istringstream streamBrep(brep);
80    BRep_Builder  aBuilder;
81    BRepTools::Read (shape, streamBrep, aBuilder);
82    return HOK;
83 }
84 // ====================================================== geom_brep2point
85 int geom_brep2point (rcstring brep, double& px, double& py, double& pz)
86 {
87    TopoDS_Shape topo;
88    geom_brep2shape (brep, topo);
89
90    if (topo.ShapeType() != TopAbs_VERTEX)
91       return HERR;
92
93    TopoDS_Vertex g_vertex = TopoDS::Vertex (topo);
94    gp_Pnt        g_point  = BRep_Tool::Pnt( g_vertex );
95
96    px = g_point.X();
97    py = g_point.Y();
98    pz = g_point.Z();
99    return HOK;
100 }
101 // ====================================================== clear_associations
102 //  void clear_associations (Edge* edge)
103 //  {
104    //  edge->clearAssociation();
105    //  edge->getVertex(V_AMONT)->clearAssociation();
106    //  edge->getVertex(V_AVAL )->clearAssociation();
107 //  }
108 // ====================================================== clean_brep
109 void clean_brep (std::string& brep)
110 {
111    TopoDS_Shape  shape;
112    BRep_Builder  builder;
113    std::istringstream stream_brep (brep);
114
115    BRepTools::Read  (shape, stream_brep, builder);
116    BRepTools::Clean (shape);
117
118    std::ostringstream    stream_shape;
119    BRepTools::Write (shape, stream_shape);
120    brep = stream_shape.str();
121 }
122 END_NAMESPACE_HEXA