Salome HOME
Update copyright
[modules/geom.git] / src / GEOMImpl / GEOMImpl_DiskDriver.cxx
1 // Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <Standard_Stream.hxx>
21
22 #include <GEOMImpl_DiskDriver.hxx>
23 #include <GEOMImpl_IDisk.hxx>
24 #include <GEOMImpl_Types.hxx>
25 #include <GEOM_Function.hxx>
26
27 #include <BRepBuilderAPI_MakeEdge.hxx>
28 #include <BRepBuilderAPI_MakeWire.hxx>
29 #include <BRepBuilderAPI_MakeFace.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 <TopoDS_Wire.hxx>
36 #include <TopAbs.hxx>
37 #include <TopExp.hxx>
38
39 #include <GC_MakeCircle.hxx>
40 #include <Geom_Circle.hxx>
41
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
48 //=======================================================================
49 //function : GetID
50 //purpose  :
51 //======================================================================= 
52 const Standard_GUID& GEOMImpl_DiskDriver::GetID()
53 {
54   static Standard_GUID aDiskDriver("C1FEEF9D-1C6D-41ce-9507-F10D75430CE1");
55   return aDiskDriver; 
56 }
57
58
59 //=======================================================================
60 //function : GEOMImpl_DiskDriver
61 //purpose  : 
62 //=======================================================================
63 GEOMImpl_DiskDriver::GEOMImpl_DiskDriver() 
64 {
65 }
66
67 //=======================================================================
68 //function : Execute
69 //purpose  :
70 //======================================================================= 
71 Standard_Integer GEOMImpl_DiskDriver::Execute(TFunction_Logbook& log) const
72 {
73   if (Label().IsNull()) return 0;    
74   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
75
76   GEOMImpl_IDisk aCI (aFunction);
77   Standard_Integer aType = aFunction->GetType();
78
79   TopoDS_Shape aShape;
80
81   if (aType == DISK_PNT_VEC_R) {
82     Handle(GEOM_Function) aRefPoint  = aCI.GetCenter();
83     Handle(GEOM_Function) aRefVector = aCI.GetVector();
84     TopoDS_Shape aShapePnt = aRefPoint->GetValue();
85     TopoDS_Shape aShapeVec = aRefVector->GetValue();
86     if (aShapePnt.ShapeType() == TopAbs_VERTEX &&
87         aShapeVec.ShapeType() == TopAbs_EDGE) {
88       gp_Pnt aP = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt));
89       TopoDS_Edge anE = TopoDS::Edge(aShapeVec);
90       TopoDS_Vertex V1, V2;
91       TopExp::Vertices(anE, V1, V2, Standard_True);
92       if (!V1.IsNull() && !V2.IsNull()) {
93         gp_Vec aV (BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
94         gp_Ax2 anAxes (aP, -aV);
95         gp_Circ aCirc (anAxes, aCI.GetRadius());
96         TopoDS_Shape aCircle = BRepBuilderAPI_MakeEdge(aCirc).Edge();
97         BRepBuilderAPI_MakeWire MW;
98         MW.Add(TopoDS::Edge(aCircle));
99         BRepBuilderAPI_MakeFace MF (MW, Standard_False);
100         aShape = MF.Shape();
101       }
102     }
103   }
104   else if (aType == DISK_THREE_PNT) {
105     Handle(GEOM_Function) aRefPoint1 = aCI.GetPoint1();
106     Handle(GEOM_Function) aRefPoint2 = aCI.GetPoint2();
107     Handle(GEOM_Function) aRefPoint3 = aCI.GetPoint3();
108     TopoDS_Shape aShapePnt1 = aRefPoint1->GetValue();
109     TopoDS_Shape aShapePnt2 = aRefPoint2->GetValue();
110     TopoDS_Shape aShapePnt3 = aRefPoint3->GetValue();
111     if (aShapePnt1.ShapeType() == TopAbs_VERTEX &&
112         aShapePnt2.ShapeType() == TopAbs_VERTEX &&
113         aShapePnt3.ShapeType() == TopAbs_VERTEX) {
114       gp_Pnt aP1 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt1));
115       gp_Pnt aP2 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt2));
116       gp_Pnt aP3 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt3));
117       if (aP1.Distance(aP2) < gp::Resolution() ||
118           aP1.Distance(aP3) < gp::Resolution() ||
119           aP2.Distance(aP3) < gp::Resolution())
120         Standard_ConstructionError::Raise("Disk creation aborted: coincident points given");
121       if (gp_Vec(aP1, aP2).IsParallel(gp_Vec(aP1, aP3), Precision::Angular()))
122         Standard_ConstructionError::Raise("Disk creation aborted: points lay on one line");
123       Handle(Geom_Circle) aCirc = GC_MakeCircle(aP3, aP2, aP1).Value();
124       TopoDS_Shape aCircle = BRepBuilderAPI_MakeEdge(aCirc).Edge();
125       BRepBuilderAPI_MakeWire MW;
126       MW.Add(TopoDS::Edge(aCircle));
127       BRepBuilderAPI_MakeFace MF (MW, Standard_False);
128       aShape = MF.Shape();
129     }  
130   }
131   else if (aType == DISK_R) {
132     int anOrient = aCI.GetOrientation();
133     gp_Pnt aP = gp::Origin();
134     gp_Vec aV;
135     if (anOrient == 1)
136       aV = gp::DZ();
137     else if (anOrient == 2)
138       aV = gp::DX();
139     else if (anOrient == 3)
140       aV = gp::DY();
141
142     gp_Ax2 anAxes (aP, -aV);
143     gp_Circ aCirc (anAxes, aCI.GetRadius());
144     TopoDS_Shape aCircle = BRepBuilderAPI_MakeEdge(aCirc).Edge();
145     BRepBuilderAPI_MakeWire MW;
146     MW.Add(TopoDS::Edge(aCircle));
147     BRepBuilderAPI_MakeFace MF (MW, Standard_False);
148     aShape = MF.Shape();
149   }
150    else {
151   }
152
153   if (aShape.IsNull()) return 0;
154
155   aFunction->SetValue(aShape);
156
157   log.SetTouched(Label()); 
158
159   return 1;    
160 }
161
162
163 //=======================================================================
164 //function :  GEOMImpl_DiskDriver_Type_
165 //purpose  :
166 //======================================================================= 
167 Standard_EXPORT Handle_Standard_Type& GEOMImpl_DiskDriver_Type_()
168 {
169
170   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
171   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
172   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
173   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared); 
174   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
175   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
176  
177
178   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
179   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_DiskDriver",
180                                                          sizeof(GEOMImpl_DiskDriver),
181                                                          1,
182                                                          (Standard_Address)_Ancestors,
183                                                          (Standard_Address)NULL);
184
185   return _aType;
186 }
187
188 //=======================================================================
189 //function : DownCast
190 //purpose  :
191 //======================================================================= 
192 const Handle(GEOMImpl_DiskDriver) Handle(GEOMImpl_DiskDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
193 {
194   Handle(GEOMImpl_DiskDriver) _anOtherObject;
195
196   if (!AnObject.IsNull()) {
197      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_DiskDriver))) {
198        _anOtherObject = Handle(GEOMImpl_DiskDriver)((Handle(GEOMImpl_DiskDriver)&)AnObject);
199      }
200   }
201
202   return _anOtherObject ;
203 }