Salome HOME
Update from BR_V5_DEV 13Feb2009
[modules/geom.git] / src / GEOMImpl / GEOMImpl_ArcDriver.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_ArcDriver.hxx>
25 #include <GEOMImpl_IArc.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_MakeArcOfCircle.hxx>
39 #include <GC_MakeCircle.hxx>
40 #include <GC_MakeArcOfEllipse.hxx>
41 #include <GC_MakeEllipse.hxx>
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 #include <gp_Elips.hxx>
48 #include <Geom_Circle.hxx>
49 #include <Geom_Ellipse.hxx>
50
51 #include "utilities.h"
52
53 //=======================================================================
54 //function : GetID
55 //purpose  :
56 //======================================================================= 
57 const Standard_GUID& GEOMImpl_ArcDriver::GetID()
58 {
59   static Standard_GUID aArcDriver("FF1BBB35-5D14-4df2-980B-3A668264EA16");
60   return aArcDriver; 
61 }
62
63
64 //=======================================================================
65 //function : GEOMImpl_ArcDriver
66 //purpose  : 
67 //=======================================================================
68 GEOMImpl_ArcDriver::GEOMImpl_ArcDriver() 
69 {
70 }
71
72 //=======================================================================
73 //function : Execute
74 //purpose  :
75 //======================================================================= 
76 Standard_Integer GEOMImpl_ArcDriver::Execute(TFunction_Logbook& log) const
77 {
78   if (Label().IsNull()) return 0;    
79   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
80
81   GEOMImpl_IArc aCI (aFunction);
82   Standard_Integer aType = aFunction->GetType();
83
84   TopoDS_Shape aShape;
85   if ((aType == CIRC_ARC_THREE_PNT) || (aType == CIRC_ARC_CENTER) || (aType == ELLIPSE_ARC_CENTER_TWO_PNT))
86   {
87     Handle(GEOM_Function) aRefPoint1 = aCI.GetPoint1();
88     Handle(GEOM_Function) aRefPoint2 = aCI.GetPoint2();
89     Handle(GEOM_Function) aRefPoint3 = aCI.GetPoint3();
90
91     TopoDS_Shape aShapePnt1 = aRefPoint1->GetValue();
92     TopoDS_Shape aShapePnt2 = aRefPoint2->GetValue();
93     TopoDS_Shape aShapePnt3 = aRefPoint3->GetValue();
94
95     if (aShapePnt1.ShapeType() == TopAbs_VERTEX &&
96         aShapePnt2.ShapeType() == TopAbs_VERTEX &&
97         aShapePnt3.ShapeType() == TopAbs_VERTEX)
98     {
99       gp_Pnt aP1 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt1));
100       gp_Pnt aP2 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt2));
101       gp_Pnt aP3 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt3));
102
103       if (aP1.Distance(aP2) < gp::Resolution() ||
104           aP1.Distance(aP3) < gp::Resolution() ||
105           aP2.Distance(aP3) < gp::Resolution())
106         Standard_ConstructionError::Raise("Arc creation aborted: coincident points given");
107
108       if (gp_Vec(aP1, aP2).IsParallel(gp_Vec(aP1, aP3), Precision::Angular()))
109         Standard_ConstructionError::Raise("Arc creation aborted: points lay on one line");
110
111       if (aType == CIRC_ARC_THREE_PNT)
112       {
113         GC_MakeArcOfCircle arc (aP1, aP2, aP3);
114         aShape = BRepBuilderAPI_MakeEdge(arc).Edge();
115       } else if ( aType == CIRC_ARC_CENTER ) { // CIRC_ARC_CENTER
116         Standard_Boolean sense = aCI.GetSense();
117
118         Standard_Real aRad = aP1.Distance(aP2);
119         gp_Vec aV1 (aP1, aP2);
120         gp_Vec aV2 (aP1, aP3);
121         gp_Vec aN = aV1 ^ aV2;
122
123         if (sense)
124           aN = -aN;
125
126         GC_MakeCircle circ (aP1, aN, aRad);
127         Handle(Geom_Circle) aGeomCirc = circ.Value();
128
129         GC_MakeArcOfCircle arc (aGeomCirc->Circ(), aP2, aP3, Standard_True);
130         aShape = BRepBuilderAPI_MakeEdge(arc).Edge();
131       } else if ( aType == ELLIPSE_ARC_CENTER_TWO_PNT ) { // ELLIPSE_ARC_CENTER_TWO_PNT
132         if ( aP1.Distance(aP2) <= aP1.Distance(aP3) ) {
133           // Standard_ConstructionError::Raise("Arc creation aborted: the distance from Center Point to Point 1 needs to be bigger than the distance from Center Point to Point 2");      
134           cout << "aP1.Distance(aP2) <= aP1.Distance(aP3)" << endl;
135           gp_Pnt aTmpP = aP2;
136           aP2 = aP3;
137           aP3 = aTmpP;
138         }
139
140         GC_MakeEllipse ellipse (aP2, aP3, aP1);
141         Handle(Geom_Ellipse) aGeomEllipse = ellipse.Value();
142
143         gp_Vec aV1 (aP1, aP2);
144         gp_Vec aV2 (aP1, aP3);
145
146         double alpha = fabs(aV1.Angle(aV2));
147         
148         GC_MakeArcOfEllipse arc (aGeomEllipse->Elips(), aP2, aP3, Standard_True);
149         aShape = BRepBuilderAPI_MakeEdge(arc).Edge();
150       }
151     }
152   }
153   else {
154   }
155
156   if (aShape.IsNull()) return 0;
157
158   aFunction->SetValue(aShape);
159
160   log.SetTouched(Label());
161   return 1;
162 }
163
164
165 //=======================================================================
166 //function :  GEOMImpl_ArcDriver_Type_
167 //purpose  :
168 //======================================================================= 
169 Standard_EXPORT Handle_Standard_Type& GEOMImpl_ArcDriver_Type_()
170 {
171
172   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
173   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
174   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
175   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared); 
176   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
177   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
178  
179
180   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
181   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_ArcDriver",
182                                                          sizeof(GEOMImpl_ArcDriver),
183                                                          1,
184                                                          (Standard_Address)_Ancestors,
185                                                          (Standard_Address)NULL);
186
187   return _aType;
188 }
189
190 //=======================================================================
191 //function : DownCast
192 //purpose  :
193 //======================================================================= 
194 const Handle(GEOMImpl_ArcDriver) Handle(GEOMImpl_ArcDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
195 {
196   Handle(GEOMImpl_ArcDriver) _anOtherObject;
197
198   if (!AnObject.IsNull()) {
199      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_ArcDriver))) {
200        _anOtherObject = Handle(GEOMImpl_ArcDriver)((Handle(GEOMImpl_ArcDriver)&)AnObject);
201      }
202   }
203
204   return _anOtherObject ;
205 }