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