Salome HOME
Update from BR_V5_DEV 13Feb2009
[modules/geom.git] / src / GEOMImpl / GEOMImpl_DiskDriver.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_DiskDriver.hxx>
25 #include <GEOMImpl_IDisk.hxx>
26 #include <GEOMImpl_Types.hxx>
27 #include <GEOM_Function.hxx>
28
29 #include <BRepBuilderAPI_MakeEdge.hxx>
30 #include <BRepBuilderAPI_MakeWire.hxx>
31 #include <BRepBuilderAPI_MakeFace.hxx>
32 #include <BRep_Tool.hxx>
33 #include <TopoDS.hxx>
34 #include <TopoDS_Shape.hxx>
35 #include <TopoDS_Edge.hxx>
36 #include <TopoDS_Vertex.hxx>
37 #include <TopoDS_Wire.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_DiskDriver::GetID()
55 {
56   static Standard_GUID aDiskDriver("C1FEEF9D-1C6D-41ce-9507-F10D75430CE1");
57   return aDiskDriver; 
58 }
59
60
61 //=======================================================================
62 //function : GEOMImpl_DiskDriver
63 //purpose  : 
64 //=======================================================================
65 GEOMImpl_DiskDriver::GEOMImpl_DiskDriver() 
66 {
67 }
68
69 //=======================================================================
70 //function : Execute
71 //purpose  :
72 //======================================================================= 
73 Standard_Integer GEOMImpl_DiskDriver::Execute(TFunction_Logbook& log) const
74 {
75   if (Label().IsNull()) return 0;    
76   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
77
78   GEOMImpl_IDisk aCI (aFunction);
79   Standard_Integer aType = aFunction->GetType();
80
81   TopoDS_Shape aShape;
82
83   if (aType == DISK_PNT_VEC_R) {
84     Handle(GEOM_Function) aRefPoint  = aCI.GetCenter();
85     Handle(GEOM_Function) aRefVector = aCI.GetVector();
86     TopoDS_Shape aShapePnt = aRefPoint->GetValue();
87     TopoDS_Shape aShapeVec = aRefVector->GetValue();
88     if (aShapePnt.ShapeType() == TopAbs_VERTEX &&
89         aShapeVec.ShapeType() == TopAbs_EDGE) {
90       gp_Pnt aP = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt));
91       TopoDS_Edge anE = TopoDS::Edge(aShapeVec);
92       TopoDS_Vertex V1, V2;
93       TopExp::Vertices(anE, V1, V2, Standard_True);
94       if (!V1.IsNull() && !V2.IsNull()) {
95         gp_Vec aV (BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
96         gp_Ax2 anAxes (aP, -aV);
97         gp_Circ aCirc (anAxes, aCI.GetRadius());
98         TopoDS_Shape aCircle = BRepBuilderAPI_MakeEdge(aCirc).Edge();
99         BRepBuilderAPI_MakeWire MW;
100         MW.Add(TopoDS::Edge(aCircle));
101         BRepBuilderAPI_MakeFace MF (MW, Standard_False);
102         aShape = MF.Shape();
103       }
104     }
105   }
106   else if (aType == DISK_THREE_PNT) {
107     Handle(GEOM_Function) aRefPoint1 = aCI.GetPoint1();
108     Handle(GEOM_Function) aRefPoint2 = aCI.GetPoint2();
109     Handle(GEOM_Function) aRefPoint3 = aCI.GetPoint3();
110     TopoDS_Shape aShapePnt1 = aRefPoint1->GetValue();
111     TopoDS_Shape aShapePnt2 = aRefPoint2->GetValue();
112     TopoDS_Shape aShapePnt3 = aRefPoint3->GetValue();
113     if (aShapePnt1.ShapeType() == TopAbs_VERTEX &&
114         aShapePnt2.ShapeType() == TopAbs_VERTEX &&
115         aShapePnt3.ShapeType() == TopAbs_VERTEX) {
116       gp_Pnt aP1 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt1));
117       gp_Pnt aP2 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt2));
118       gp_Pnt aP3 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt3));
119       if (aP1.Distance(aP2) < gp::Resolution() ||
120           aP1.Distance(aP3) < gp::Resolution() ||
121           aP2.Distance(aP3) < gp::Resolution())
122         Standard_ConstructionError::Raise("Disk creation aborted: coincident points given");
123       if (gp_Vec(aP1, aP2).IsParallel(gp_Vec(aP1, aP3), Precision::Angular()))
124         Standard_ConstructionError::Raise("Disk creation aborted: points lay on one line");
125       Handle(Geom_Circle) aCirc = GC_MakeCircle(aP3, aP2, aP1).Value();
126       TopoDS_Shape aCircle = BRepBuilderAPI_MakeEdge(aCirc).Edge();
127       BRepBuilderAPI_MakeWire MW;
128       MW.Add(TopoDS::Edge(aCircle));
129       BRepBuilderAPI_MakeFace MF (MW, Standard_False);
130       aShape = MF.Shape();
131     }  
132   }
133   else if (aType == DISK_R) {
134     int anOrient = aCI.GetOrientation();
135     gp_Pnt aP = gp::Origin();
136     gp_Vec aV;
137     if (anOrient == 1)
138       aV = gp::DZ();
139     else if (anOrient == 2)
140       aV = gp::DX();
141     else if (anOrient == 3)
142       aV = gp::DY();
143
144     gp_Ax2 anAxes (aP, -aV);
145     gp_Circ aCirc (anAxes, aCI.GetRadius());
146     TopoDS_Shape aCircle = BRepBuilderAPI_MakeEdge(aCirc).Edge();
147     BRepBuilderAPI_MakeWire MW;
148     MW.Add(TopoDS::Edge(aCircle));
149     BRepBuilderAPI_MakeFace MF (MW, Standard_False);
150     aShape = MF.Shape();
151   }
152    else {
153   }
154
155   if (aShape.IsNull()) return 0;
156
157   aFunction->SetValue(aShape);
158
159   log.SetTouched(Label()); 
160
161   return 1;    
162 }
163
164
165 //=======================================================================
166 //function :  GEOMImpl_DiskDriver_Type_
167 //purpose  :
168 //======================================================================= 
169 Standard_EXPORT Handle_Standard_Type& GEOMImpl_DiskDriver_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_DiskDriver",
182                                                          sizeof(GEOMImpl_DiskDriver),
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_DiskDriver) Handle(GEOMImpl_DiskDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
195 {
196   Handle(GEOMImpl_DiskDriver) _anOtherObject;
197
198   if (!AnObject.IsNull()) {
199      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_DiskDriver))) {
200        _anOtherObject = Handle(GEOMImpl_DiskDriver)((Handle(GEOMImpl_DiskDriver)&)AnObject);
201      }
202   }
203
204   return _anOtherObject ;
205 }