Salome HOME
NPAL: 16557, 16558, 16578, 16549, 16561
[modules/geom.git] / src / GEOMImpl / GEOMImpl_CircleDriver.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20
21 #include <Standard_Stream.hxx>
22
23 #include <GEOMImpl_CircleDriver.hxx>
24 #include <GEOMImpl_ICircle.hxx>
25 #include <GEOMImpl_Types.hxx>
26 #include <GEOM_Function.hxx>
27
28 #include <BRepBuilderAPI_MakeEdge.hxx>
29 #include <BRep_Tool.hxx>
30 #include <TopoDS.hxx>
31 #include <TopoDS_Shape.hxx>
32 #include <TopoDS_Edge.hxx>
33 #include <TopoDS_Vertex.hxx>
34 #include <TopAbs.hxx>
35 #include <TopExp.hxx>
36
37 #include <GC_MakeCircle.hxx>
38 #include <Geom_Circle.hxx>
39
40 #include <Standard_ConstructionError.hxx>
41 #include <Precision.hxx>
42 #include <gp_Pnt.hxx>
43 #include <gp_Vec.hxx>
44 #include <gp_Circ.hxx>
45
46 //=======================================================================
47 //function : GetID
48 //purpose  :
49 //======================================================================= 
50 const Standard_GUID& GEOMImpl_CircleDriver::GetID()
51 {
52   static Standard_GUID aCircleDriver("FF1BBB32-5D14-4df2-980B-3A668264EA16");
53   return aCircleDriver; 
54 }
55
56
57 //=======================================================================
58 //function : GEOMImpl_CircleDriver
59 //purpose  : 
60 //=======================================================================
61 GEOMImpl_CircleDriver::GEOMImpl_CircleDriver() 
62 {
63 }
64
65 //=======================================================================
66 //function : Execute
67 //purpose  :
68 //======================================================================= 
69 Standard_Integer GEOMImpl_CircleDriver::Execute(TFunction_Logbook& log) const
70 {
71   if (Label().IsNull()) return 0;    
72   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
73
74   GEOMImpl_ICircle aCI (aFunction);
75   Standard_Integer aType = aFunction->GetType();
76
77   TopoDS_Shape aShape;
78
79   if (aType == CIRCLE_PNT_VEC_R) {
80     Handle(GEOM_Function) aRefPoint  = aCI.GetCenter();
81     Handle(GEOM_Function) aRefVector = aCI.GetVector();
82     TopoDS_Shape aShapePnt = aRefPoint->GetValue();
83     TopoDS_Shape aShapeVec = aRefVector->GetValue();
84     if (aShapePnt.ShapeType() == TopAbs_VERTEX &&
85         aShapeVec.ShapeType() == TopAbs_EDGE) {
86       gp_Pnt aP = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt));
87       TopoDS_Edge anE = TopoDS::Edge(aShapeVec);
88       TopoDS_Vertex V1, V2;
89       TopExp::Vertices(anE, V1, V2, Standard_True);
90       if (!V1.IsNull() && !V2.IsNull()) {
91         gp_Vec aV (BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
92         gp_Ax2 anAxes (aP, aV);
93         gp_Circ aCirc (anAxes, aCI.GetRadius());
94         aShape = BRepBuilderAPI_MakeEdge(aCirc).Edge();
95       }
96     }
97   }
98  else if (aType == CIRCLE_CENTER_TWO_PNT) {
99        Handle(GEOM_Function) aRefPoint1 = aCI.GetPoint1();
100        Handle(GEOM_Function) aRefPoint2 = aCI.GetPoint2();
101        Handle(GEOM_Function) aRefPoint3 = aCI.GetPoint3();
102        TopoDS_Shape aShapePnt1 = aRefPoint1->GetValue();
103        TopoDS_Shape aShapePnt2 = aRefPoint2->GetValue();
104        TopoDS_Shape aShapePnt3 = aRefPoint3->GetValue();
105        if (aShapePnt1.ShapeType() == TopAbs_VERTEX && aShapePnt2.ShapeType() == TopAbs_VERTEX &&
106            aShapePnt3.ShapeType() == TopAbs_VERTEX)
107        {
108          gp_Pnt aP1 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt1));
109          gp_Pnt aP2 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt2));
110          gp_Pnt aP3 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt3));
111
112          if (aP1.Distance(aP2) < gp::Resolution() ||
113              aP1.Distance(aP3) < gp::Resolution() ||
114              aP2.Distance(aP3) < gp::Resolution())
115            Standard_ConstructionError::Raise("Circle creation aborted: coincident points given");
116          if (gp_Vec(aP1, aP2).IsParallel(gp_Vec(aP1, aP3), Precision::Angular()))
117            Standard_ConstructionError::Raise("Circle creation aborted: points lay on one line");
118          double x, y, z, x1, y1, z1, x2, y2, z2, dx, dy, dz, dx2, dy2, dz2, dx3, dy3, dz3, aRadius;
119          //Calculations for Radius
120          x = aP1.X(); y = aP1.Y(); z = aP1.Z();
121          x1 = aP2.X(); y1 = aP2.Y(); z1 = aP2.Z();
122          dx = x1 - x;
123          dy = y1 - y;
124          dz = z1 - z;
125          aRadius = sqrt(dx*dx + dy*dy + dz*dz);
126          //Calculations for Plane Vector
127          x2 = aP3.X(); y2 = aP3.Y(); z2 = aP3.Z();
128          dx2 = x2 - x; dy2 = y2 - y; dz2 = z2 - z;
129          dx3 = ((dy*dz2) - (dy2*dz))/100;
130          dy3 = ((dx2*dz) - (dx*dz2))/100;
131          dz3 = ((dx*dy2) - (dx2*dy))/100;
132          //Make Plane Vector
133          gp_Dir aDir ( dx3, dy3, dz3 );
134          //Make Circle
135          gp_Ax2 anAxes (aP1, aDir);
136          gp_Circ aCirc (anAxes, aRadius);
137          aShape = BRepBuilderAPI_MakeEdge(aCirc).Edge();  
138        }
139   }
140   else if (aType == CIRCLE_THREE_PNT) {
141     Handle(GEOM_Function) aRefPoint1 = aCI.GetPoint1();
142     Handle(GEOM_Function) aRefPoint2 = aCI.GetPoint2();
143     Handle(GEOM_Function) aRefPoint3 = aCI.GetPoint3();
144     TopoDS_Shape aShapePnt1 = aRefPoint1->GetValue();
145     TopoDS_Shape aShapePnt2 = aRefPoint2->GetValue();
146     TopoDS_Shape aShapePnt3 = aRefPoint3->GetValue();
147     if (aShapePnt1.ShapeType() == TopAbs_VERTEX &&
148         aShapePnt2.ShapeType() == TopAbs_VERTEX &&
149         aShapePnt3.ShapeType() == TopAbs_VERTEX) {
150       gp_Pnt aP1 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt1));
151       gp_Pnt aP2 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt2));
152       gp_Pnt aP3 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt3));
153       if (aP1.Distance(aP2) < gp::Resolution() ||
154           aP1.Distance(aP3) < gp::Resolution() ||
155           aP2.Distance(aP3) < gp::Resolution())
156         Standard_ConstructionError::Raise("Circle creation aborted: coincident points given");
157       if (gp_Vec(aP1, aP2).IsParallel(gp_Vec(aP1, aP3), Precision::Angular()))
158         Standard_ConstructionError::Raise("Circle creation aborted: points lay on one line");
159       Handle(Geom_Circle) aCirc = GC_MakeCircle(aP1, aP2, aP3).Value();
160       aShape = BRepBuilderAPI_MakeEdge(aCirc).Edge();
161     }  
162   }
163    else {
164   }
165
166   if (aShape.IsNull()) return 0;
167
168   aFunction->SetValue(aShape);
169
170   log.SetTouched(Label()); 
171
172   return 1;    
173 }
174
175
176 //=======================================================================
177 //function :  GEOMImpl_CircleDriver_Type_
178 //purpose  :
179 //======================================================================= 
180 Standard_EXPORT Handle_Standard_Type& GEOMImpl_CircleDriver_Type_()
181 {
182
183   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
184   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
185   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
186   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared); 
187   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
188   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
189  
190
191   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
192   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_CircleDriver",
193                                                          sizeof(GEOMImpl_CircleDriver),
194                                                          1,
195                                                          (Standard_Address)_Ancestors,
196                                                          (Standard_Address)NULL);
197
198   return _aType;
199 }
200
201 //=======================================================================
202 //function : DownCast
203 //purpose  :
204 //======================================================================= 
205 const Handle(GEOMImpl_CircleDriver) Handle(GEOMImpl_CircleDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
206 {
207   Handle(GEOMImpl_CircleDriver) _anOtherObject;
208
209   if (!AnObject.IsNull()) {
210      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_CircleDriver))) {
211        _anOtherObject = Handle(GEOMImpl_CircleDriver)((Handle(GEOMImpl_CircleDriver)&)AnObject);
212      }
213   }
214
215   return _anOtherObject ;
216 }