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