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