Salome HOME
0020907: Werror in GEOM: integrate patch from Erwan ADAM
[modules/geom.git] / src / GEOMImpl / GEOMImpl_CircleDriver.cxx
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include <Standard_Stream.hxx>
24
25 #include <GEOMImpl_CircleDriver.hxx>
26 #include <GEOMImpl_ICircle.hxx>
27 #include <GEOMImpl_Types.hxx>
28 #include <GEOM_Function.hxx>
29
30 #include <BRepBuilderAPI_MakeEdge.hxx>
31 #include <BRep_Tool.hxx>
32 #include <TopoDS.hxx>
33 #include <TopoDS_Shape.hxx>
34 #include <TopoDS_Edge.hxx>
35 #include <TopoDS_Vertex.hxx>
36 #include <TopAbs.hxx>
37 #include <TopExp.hxx>
38
39 #include <GC_MakeCircle.hxx>
40 #include <Geom_Circle.hxx>
41
42 #include <Standard_ConstructionError.hxx>
43 #include <Precision.hxx>
44 #include <gp_Pnt.hxx>
45 #include <gp_Vec.hxx>
46 #include <gp_Circ.hxx>
47
48 //=======================================================================
49 //function : GetID
50 //purpose  :
51 //======================================================================= 
52 const Standard_GUID& GEOMImpl_CircleDriver::GetID()
53 {
54   static Standard_GUID aCircleDriver("FF1BBB32-5D14-4df2-980B-3A668264EA16");
55   return aCircleDriver; 
56 }
57
58
59 //=======================================================================
60 //function : GEOMImpl_CircleDriver
61 //purpose  : 
62 //=======================================================================
63 GEOMImpl_CircleDriver::GEOMImpl_CircleDriver() 
64 {
65 }
66
67 //=======================================================================
68 //function : Execute
69 //purpose  :
70 //======================================================================= 
71 Standard_Integer GEOMImpl_CircleDriver::Execute(TFunction_Logbook& log) const
72 {
73   if (Label().IsNull()) return 0;    
74   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
75
76   GEOMImpl_ICircle aCI (aFunction);
77   Standard_Integer aType = aFunction->GetType();
78
79   TopoDS_Shape aShape;
80
81   if (aType == CIRCLE_PNT_VEC_R) {
82     // Center
83     gp_Pnt aP = gp::Origin();
84     Handle(GEOM_Function) aRefPoint = aCI.GetCenter();
85     if (!aRefPoint.IsNull()) {
86       TopoDS_Shape aShapePnt = aRefPoint->GetValue();
87       if (aShapePnt.ShapeType() != TopAbs_VERTEX) {
88         Standard_ConstructionError::Raise
89           ("Circle creation aborted: invalid center argument, must be a point");
90       }
91       aP = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt));
92     }
93     // Normal
94     gp_Vec aV = gp::DZ();
95     Handle(GEOM_Function) aRefVector = aCI.GetVector();
96     if (!aRefVector.IsNull()) {
97       TopoDS_Shape aShapeVec = aRefVector->GetValue();
98       if (aShapeVec.ShapeType() != TopAbs_EDGE) {
99         Standard_ConstructionError::Raise
100           ("Circle creation aborted: invalid vector argument, must be a vector or an edge");
101       }
102       TopoDS_Edge anE = TopoDS::Edge(aShapeVec);
103       TopoDS_Vertex V1, V2;
104       TopExp::Vertices(anE, V1, V2, Standard_True);
105       if (!V1.IsNull() && !V2.IsNull()) {
106         aV = gp_Vec(BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
107         if (aV.Magnitude() < gp::Resolution()) {
108           Standard_ConstructionError::Raise
109             ("Circle creation aborted: vector of zero length is given");
110         }
111       }
112     }
113     // Axes
114     gp_Ax2 anAxes (aP, aV);
115     // Radius
116     double anR = aCI.GetRadius();
117     char aMsg[] = "Circle creation aborted: radius value less than 1e-07 is not acceptable";
118     if (anR < Precision::Confusion())
119       Standard_ConstructionError::Raise(aMsg);
120     // Circle
121     gp_Circ aCirc (anAxes, anR);
122     aShape = BRepBuilderAPI_MakeEdge(aCirc).Edge();
123   }
124   else if (aType == CIRCLE_CENTER_TWO_PNT) {
125     Handle(GEOM_Function) aRefPoint1 = aCI.GetPoint1();
126     Handle(GEOM_Function) aRefPoint2 = aCI.GetPoint2();
127     Handle(GEOM_Function) aRefPoint3 = aCI.GetPoint3();
128     TopoDS_Shape aShapePnt1 = aRefPoint1->GetValue();
129     TopoDS_Shape aShapePnt2 = aRefPoint2->GetValue();
130     TopoDS_Shape aShapePnt3 = aRefPoint3->GetValue();
131     if (aShapePnt1.ShapeType() == TopAbs_VERTEX &&
132         aShapePnt2.ShapeType() == TopAbs_VERTEX &&
133         aShapePnt3.ShapeType() == TopAbs_VERTEX)
134     {
135       gp_Pnt aP1 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt1));
136       gp_Pnt aP2 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt2));
137       gp_Pnt aP3 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt3));
138
139       if (aP1.Distance(aP2) < gp::Resolution() ||
140           aP1.Distance(aP3) < gp::Resolution() ||
141           aP2.Distance(aP3) < gp::Resolution())
142         Standard_ConstructionError::Raise("Circle creation aborted: coincident points given");
143
144       if (gp_Vec(aP1, aP2).IsParallel(gp_Vec(aP1, aP3), Precision::Angular()))
145         Standard_ConstructionError::Raise("Circle creation aborted: points lay on one line");
146
147       double x, y, z, x1, y1, z1, x2, y2, z2, dx, dy, dz, dx2, dy2, dz2, dx3, dy3, dz3, aRadius;
148       //Calculations for Radius
149       x = aP1.X(); y = aP1.Y(); z = aP1.Z();
150       x1 = aP2.X(); y1 = aP2.Y(); z1 = aP2.Z();
151       dx = x1 - x;
152       dy = y1 - y;
153       dz = z1 - z;
154       aRadius = sqrt(dx*dx + dy*dy + dz*dz);
155       //Calculations for Plane Vector
156       x2 = aP3.X(); y2 = aP3.Y(); z2 = aP3.Z();
157       dx2 = x2 - x; dy2 = y2 - y; dz2 = z2 - z;
158       dx3 = ((dy*dz2) - (dy2*dz))/100;
159       dy3 = ((dx2*dz) - (dx*dz2))/100;
160       dz3 = ((dx*dy2) - (dx2*dy))/100;
161       //Make Plane Vector
162       gp_Dir aDir ( dx3, dy3, dz3 );
163       //Make Circle
164       gp_Ax2 anAxes (aP1, aDir);
165       gp_Circ aCirc (anAxes, aRadius);
166       aShape = BRepBuilderAPI_MakeEdge(aCirc).Edge();  
167     }
168   }
169   else if (aType == CIRCLE_THREE_PNT) {
170     Handle(GEOM_Function) aRefPoint1 = aCI.GetPoint1();
171     Handle(GEOM_Function) aRefPoint2 = aCI.GetPoint2();
172     Handle(GEOM_Function) aRefPoint3 = aCI.GetPoint3();
173     TopoDS_Shape aShapePnt1 = aRefPoint1->GetValue();
174     TopoDS_Shape aShapePnt2 = aRefPoint2->GetValue();
175     TopoDS_Shape aShapePnt3 = aRefPoint3->GetValue();
176     if (aShapePnt1.ShapeType() == TopAbs_VERTEX &&
177         aShapePnt2.ShapeType() == TopAbs_VERTEX &&
178         aShapePnt3.ShapeType() == TopAbs_VERTEX) {
179       gp_Pnt aP1 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt1));
180       gp_Pnt aP2 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt2));
181       gp_Pnt aP3 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt3));
182       if (aP1.Distance(aP2) < gp::Resolution() ||
183           aP1.Distance(aP3) < gp::Resolution() ||
184           aP2.Distance(aP3) < gp::Resolution())
185         Standard_ConstructionError::Raise("Circle creation aborted: coincident points given");
186       if (gp_Vec(aP1, aP2).IsParallel(gp_Vec(aP1, aP3), Precision::Angular()))
187         Standard_ConstructionError::Raise("Circle creation aborted: points lay on one line");
188       Handle(Geom_Circle) aCirc = GC_MakeCircle(aP1, aP2, aP3).Value();
189       aShape = BRepBuilderAPI_MakeEdge(aCirc).Edge();
190     }  
191   }
192   else {
193   }
194
195   if (aShape.IsNull()) return 0;
196
197   aFunction->SetValue(aShape);
198
199   log.SetTouched(Label()); 
200
201   return 1;    
202 }
203
204
205 //=======================================================================
206 //function :  GEOMImpl_CircleDriver_Type_
207 //purpose  :
208 //======================================================================= 
209 Standard_EXPORT Handle_Standard_Type& GEOMImpl_CircleDriver_Type_()
210 {
211
212   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
213   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
214   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
215   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared); 
216   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
217   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
218  
219
220   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
221   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_CircleDriver",
222                                                          sizeof(GEOMImpl_CircleDriver),
223                                                          1,
224                                                          (Standard_Address)_Ancestors,
225                                                          (Standard_Address)NULL);
226
227   return _aType;
228 }
229
230 //=======================================================================
231 //function : DownCast
232 //purpose  :
233 //======================================================================= 
234 const Handle(GEOMImpl_CircleDriver) Handle(GEOMImpl_CircleDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
235 {
236   Handle(GEOMImpl_CircleDriver) _anOtherObject;
237
238   if (!AnObject.IsNull()) {
239      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_CircleDriver))) {
240        _anOtherObject = Handle(GEOMImpl_CircleDriver)((Handle(GEOMImpl_CircleDriver)&)AnObject);
241      }
242   }
243
244   return _anOtherObject ;
245 }