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