Salome HOME
2e06e0573f656fda738b6763054525e2904f56d7
[modules/hexablock.git] / src / HEXABLOCK / HexVertexShape.cxx
1
2 // C++ : Gestion des soous-shapes
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 "HexVertexShape.hxx"
25 #include "HexNewShape.hxx"
26 #include "HexXmlWriter.hxx"
27
28 #include <TopoDS.hxx>
29 #include <TopoDS_Vertex.hxx>
30 #include <BRepTools.hxx>
31 #include <BRep_Builder.hxx>
32 #include <BRepBuilderAPI_MakeVertex.hxx>
33 #include <gp_Pnt.hxx>
34
35 BEGIN_NAMESPACE_HEXA
36
37 // static bool db = on_debug ();  // == getenv ("HEXA_DB") > 0
38
39 // ====================================================== Constructeur
40 VertexShape::VertexShape (NewShape* dad, int id)
41          : SubShape  (dad, id, 0)
42 {
43    free_point      = false;
44    maj_coords      = true;
45    ss_coord[dir_x] = ss_coord[dir_y]  = ss_coord[dir_z] = 0;
46
47                                // Les coordonnées
48    updateCoords ();
49 }
50 // ====================================================== Constructeur bis*
51 VertexShape::VertexShape (NewShape* dad, int id, double* point)
52          : SubShape  (dad, id, 0)
53 {
54    free_point      = true;
55    maj_coords      = false;
56    maj_shape       = false;
57    for (int nc=dir_x ; nc<=dir_z ; ++nc)
58         ss_coord[nc] = point[nc];
59 }
60 // ====================================================== updateCoords
61 void VertexShape::updateCoords ()
62 {
63    if (free_point)
64       return;
65    if (maj_shape)
66        updateShape ();
67
68    maj_coords = false;
69    TopoDS_Vertex gver = TopoDS::Vertex (geo_shape);
70    gp_Pnt      gpoint = BRep_Tool::Pnt (gver);
71
72    ss_coord [dir_x]   = gpoint.X();
73    ss_coord [dir_y]   = gpoint.Y();
74    ss_coord [dir_z]   = gpoint.Z();
75 }
76 // ====================================================== getCoords
77 void VertexShape::getCoords (double& px, double& py, double& pz)
78 {
79    if (maj_coords)
80        updateCoords ();
81
82    px = ss_coord[dir_x];
83    py = ss_coord[dir_y];
84    pz = ss_coord[dir_z];
85 }
86 #if 0
87 // ====================================================== getShape
88 const TopoDS_Shape& VertexShape::getShape()
89 {
90     if (geo_shape.IsNull() && ss_parent != NULL &&
91             ss_parent->getOrigin() == SH_CLOUD)
92     {
93         double x, y, z;
94         getCoords(x, y, z);
95         gp_Pnt pnt(x, y, z);
96         geo_shape = BRepBuilderAPI_MakeVertex(pnt);
97     }
98
99     return SubShape::getShape ();
100 }
101 #endif
102 // ====================================================== addAssociation
103 void VertexShape::addAssociation (Vertex* elt)
104 {
105    tab_assoc.push_back (elt);
106    is_associated = true ;
107 }
108 // ====================================================== getAssociation
109 Vertex* VertexShape::getAssociation (int nro)
110 {
111    if (nro>0 && nro < (int)tab_assoc.size())
112       return tab_assoc[nro];
113    else
114       return NULL;
115 }
116 // ====================================================== definedBy 
117 bool VertexShape::definedBy (double point[])
118 {
119    if (maj_coords)
120        updateCoords ();
121
122    const double Epsilon2 = 1e-4;
123    bool   rep = same_coords (point, ss_coord, Epsilon2);
124    return rep; 
125 }
126 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
127 // ====================================================== saveXml
128 void VertexShape::saveXml (XmlWriter* xml)
129 {
130    char coord[80];
131    sprintf (coord, "%g %g %g", ss_coord[dir_x], ss_coord[dir_y],
132                                                 ss_coord[dir_z]);
133
134    xml->openMark     ("Vertex");
135    xml->addAttribute ("subid", sub_ident);
136    xml->addAttribute ("coord", coord);
137    xml->closeMark ();
138 }
139 END_NAMESPACE_HEXA