Salome HOME
e2af0a730727343ef667e24c2371be987b58fff6
[modules/geom.git] / src / GEOMImpl / GEOMImpl_DiskDriver.cxx
1 // Copyright (C) 2007-2023  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, or (at your option) any later version.
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_Pln.hxx>
45 #include <gp_Pnt.hxx>
46 #include <gp_Vec.hxx>
47 #include <gp_Circ.hxx>
48
49 //=======================================================================
50 //function : GetID
51 //purpose  :
52 //======================================================================= 
53 const Standard_GUID& GEOMImpl_DiskDriver::GetID()
54 {
55   static Standard_GUID aDiskDriver("C1FEEF9D-1C6D-41ce-9507-F10D75430CE1");
56   return aDiskDriver; 
57 }
58
59
60 //=======================================================================
61 //function : GEOMImpl_DiskDriver
62 //purpose  : 
63 //=======================================================================
64 GEOMImpl_DiskDriver::GEOMImpl_DiskDriver() 
65 {
66 }
67
68 //=======================================================================
69 //function : Execute
70 //purpose  :
71 //======================================================================= 
72 Standard_Integer GEOMImpl_DiskDriver::Execute(Handle(TFunction_Logbook)& log) const
73 {
74   if (Label().IsNull()) return 0;    
75   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
76
77   GEOMImpl_IDisk aCI (aFunction);
78   Standard_Integer aType = aFunction->GetType();
79
80   TopoDS_Shape aShape;
81
82   if (aType == DISK_PNT_VEC_R) {
83     Handle(GEOM_Function) aRefPoint  = aCI.GetCenter();
84     Handle(GEOM_Function) aRefVector = aCI.GetVector();
85     TopoDS_Shape aShapePnt = aRefPoint->GetValue();
86     TopoDS_Shape aShapeVec = aRefVector->GetValue();
87     if (aShapePnt.ShapeType() == TopAbs_VERTEX &&
88         aShapeVec.ShapeType() == TopAbs_EDGE) {
89       gp_Pnt aP = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt));
90       TopoDS_Edge anE = TopoDS::Edge(aShapeVec);
91       TopoDS_Vertex V1, V2;
92       TopExp::Vertices(anE, V1, V2, Standard_True);
93       if (!V1.IsNull() && !V2.IsNull()) {
94         gp_Vec aV (BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
95         gp_Ax2 anAxes (aP, aV);
96         gp_Ax3 anAxes3(anAxes);
97         gp_Pln aPln(anAxes3);
98         gp_Ax2 anAxes1(aP, -aV);
99         gp_Circ aCirc (anAxes1, aCI.GetRadius());
100         TopoDS_Shape aCircle = BRepBuilderAPI_MakeEdge(aCirc).Edge();
101         BRepBuilderAPI_MakeWire MW;
102         MW.Add(TopoDS::Edge(aCircle));
103         BRepBuilderAPI_MakeFace MF (aPln, MW);
104         aShape = MF.Shape();
105       }
106     }
107   }
108   else if (aType == DISK_THREE_PNT) {
109     Handle(GEOM_Function) aRefPoint1 = aCI.GetPoint1();
110     Handle(GEOM_Function) aRefPoint2 = aCI.GetPoint2();
111     Handle(GEOM_Function) aRefPoint3 = aCI.GetPoint3();
112     TopoDS_Shape aShapePnt1 = aRefPoint1->GetValue();
113     TopoDS_Shape aShapePnt2 = aRefPoint2->GetValue();
114     TopoDS_Shape aShapePnt3 = aRefPoint3->GetValue();
115     if (aShapePnt1.ShapeType() == TopAbs_VERTEX &&
116         aShapePnt2.ShapeType() == TopAbs_VERTEX &&
117         aShapePnt3.ShapeType() == TopAbs_VERTEX) {
118       gp_Pnt aP1 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt1));
119       gp_Pnt aP2 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt2));
120       gp_Pnt aP3 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt3));
121       if (aP1.Distance(aP2) < gp::Resolution() ||
122           aP1.Distance(aP3) < gp::Resolution() ||
123           aP2.Distance(aP3) < gp::Resolution())
124         Standard_ConstructionError::Raise("Disk creation aborted: coincident points given");
125       if (gp_Vec(aP1, aP2).IsParallel(gp_Vec(aP1, aP3), Precision::Angular()))
126         Standard_ConstructionError::Raise("Disk creation aborted: points lay on one line");
127       Handle(Geom_Circle) aCirc = GC_MakeCircle(aP3, aP2, aP1).Value();
128       gp_Circ aGpCirc = aCirc->Circ();
129       gp_Ax2 anAxes = aGpCirc.Position();
130       gp_Ax3 anAxes3(anAxes.Location(), -anAxes.Direction());
131       gp_Pln aPln(anAxes3);
132       TopoDS_Shape aCircle = BRepBuilderAPI_MakeEdge(aCirc).Edge();
133       BRepBuilderAPI_MakeWire MW;
134       MW.Add(TopoDS::Edge(aCircle));
135       BRepBuilderAPI_MakeFace MF (aPln, MW);
136       aShape = MF.Shape();
137     }  
138   }
139   else if (aType == DISK_R) {
140     int anOrient = aCI.GetOrientation();
141     gp_Pnt aP = gp::Origin();
142     gp_Vec aV;
143     if (anOrient == 1)
144       aV = gp::DZ();
145     else if (anOrient == 2)
146       aV = gp::DX();
147     else if (anOrient == 3)
148       aV = gp::DY();
149
150     gp_Ax2 anAxes (aP, aV);
151     gp_Ax2 anAxes1(aP, -aV);
152     gp_Ax3 anAxes3(anAxes);
153     gp_Pln aPln(anAxes3);
154     gp_Circ aCirc (anAxes1, aCI.GetRadius());
155     TopoDS_Shape aCircle = BRepBuilderAPI_MakeEdge(aCirc).Edge();
156     BRepBuilderAPI_MakeWire MW;
157     MW.Add(TopoDS::Edge(aCircle));
158     BRepBuilderAPI_MakeFace MF (aPln, MW);
159     aShape = MF.Shape();
160   }
161    else {
162   }
163
164   if (aShape.IsNull()) return 0;
165
166   aFunction->SetValue(aShape);
167
168   log->SetTouched(Label());
169
170   return 1;    
171 }
172
173 //================================================================================
174 /*!
175  * \brief Returns a name of creation operation and names and values of creation parameters
176  */
177 //================================================================================
178
179 bool GEOMImpl_DiskDriver::
180 GetCreationInformation(std::string&             theOperationName,
181                        std::vector<GEOM_Param>& theParams)
182 {
183   if (Label().IsNull()) return 0;
184   Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
185
186   GEOMImpl_IDisk aCI( function );
187   Standard_Integer aType = function->GetType();
188
189   theOperationName = "DISK";
190
191   switch ( aType ) {
192   case DISK_PNT_VEC_R:
193     AddParam( theParams, "Center Point", aCI.GetCenter() );
194     AddParam( theParams, "Vector", aCI.GetVector() );
195     AddParam( theParams, "Radius", aCI.GetRadius() );
196     break;
197   case DISK_THREE_PNT:
198     AddParam( theParams, "Point 1", aCI.GetPoint1() );
199     AddParam( theParams, "Point 2", aCI.GetPoint2() );
200     AddParam( theParams, "Point 3", aCI.GetPoint3() );
201     break;
202   case DISK_R:
203     AddParam( theParams, "Radius", aCI.GetRadius() );
204     AddParam( theParams, "Orientation", aCI.GetOrientation() );
205     break;
206   default:
207     return false;
208   }
209   
210   return true;
211 }
212
213 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_DiskDriver,GEOM_BaseDriver)