Salome HOME
0772c588c02be4e1e01f144c1130450ecbc2c16a
[modules/geom.git] / src / GEOMImpl / GEOMImpl_ArcDriver.cxx
1 // Copyright (C) 2007-2023  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, or (at your option) any later version.
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
23 #include <Standard_Stream.hxx>
24
25 #include <GEOMImpl_ArcDriver.hxx>
26 #include <GEOMImpl_IArc.hxx>
27 #include <GEOMImpl_Types.hxx>
28 #include <GEOM_Function.hxx>
29
30 #include <BRepBuilderAPI_MakeEdge.hxx>
31 #include <BRep_Tool.hxx>
32 #include <TopoDS.hxx>
33 #include <TopoDS_Shape.hxx>
34 #include <TopoDS_Edge.hxx>
35 #include <TopoDS_Vertex.hxx>
36 #include <TopAbs.hxx>
37 #include <TopExp.hxx>
38
39 #include <GC_MakeArcOfCircle.hxx>
40 #include <GC_MakeCircle.hxx>
41 #include <GC_MakeArcOfEllipse.hxx>
42 #include <GC_MakeEllipse.hxx>
43 #include <Standard_ConstructionError.hxx>
44 #include <Precision.hxx>
45 #include <gp_Pnt.hxx>
46 #include <gp_Vec.hxx>
47 #include <gp_Circ.hxx>
48 #include <gp_Elips.hxx>
49 #include <Geom_Circle.hxx>
50 #include <Geom_Ellipse.hxx>
51
52 #include "utilities.h"
53
54 //=======================================================================
55 //function : GetID
56 //purpose  :
57 //======================================================================= 
58 const Standard_GUID& GEOMImpl_ArcDriver::GetID()
59 {
60   static Standard_GUID aArcDriver("FF1BBB35-5D14-4df2-980B-3A668264EA16");
61   return aArcDriver; 
62 }
63
64
65 //=======================================================================
66 //function : GEOMImpl_ArcDriver
67 //purpose  : 
68 //=======================================================================
69 GEOMImpl_ArcDriver::GEOMImpl_ArcDriver() 
70 {
71 }
72
73 //=======================================================================
74 //function : Execute
75 //purpose  :
76 //======================================================================= 
77 Standard_Integer GEOMImpl_ArcDriver::Execute(Handle(TFunction_Logbook)& log) const
78 {
79   if (Label().IsNull()) return 0;    
80   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
81
82   GEOMImpl_IArc aCI (aFunction);
83   Standard_Integer aType = aFunction->GetType();
84
85   TopoDS_Shape aShape;
86   if ((aType == CIRC_ARC_THREE_PNT) || (aType == CIRC_ARC_CENTER) || (aType == ELLIPSE_ARC_CENTER_TWO_PNT))
87   {
88     Handle(GEOM_Function) aRefPoint1 = aCI.GetPoint1();
89     Handle(GEOM_Function) aRefPoint2 = aCI.GetPoint2();
90     Handle(GEOM_Function) aRefPoint3 = aCI.GetPoint3();
91
92     TopoDS_Shape aShapePnt1 = aRefPoint1->GetValue();
93     TopoDS_Shape aShapePnt2 = aRefPoint2->GetValue();
94     TopoDS_Shape aShapePnt3 = aRefPoint3->GetValue();
95
96     if (aShapePnt1.ShapeType() == TopAbs_VERTEX &&
97         aShapePnt2.ShapeType() == TopAbs_VERTEX &&
98         aShapePnt3.ShapeType() == TopAbs_VERTEX)
99     {
100       gp_Pnt aP1 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt1));
101       gp_Pnt aP2 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt2));
102       gp_Pnt aP3 = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt3));
103
104       if (aP1.Distance(aP2) < gp::Resolution() ||
105           aP1.Distance(aP3) < gp::Resolution() ||
106           aP2.Distance(aP3) < gp::Resolution())
107         Standard_ConstructionError::Raise("Arc creation aborted: coincident points given");
108
109       if (gp_Vec(aP1, aP2).IsParallel(gp_Vec(aP1, aP3), Precision::Angular()))
110         Standard_ConstructionError::Raise("Arc creation aborted: points lay on one line");
111
112       if (aType == CIRC_ARC_THREE_PNT)
113       {
114         GC_MakeArcOfCircle arc (aP1, aP2, aP3);
115         aShape = BRepBuilderAPI_MakeEdge(arc.Value()).Edge();
116       } else if ( aType == CIRC_ARC_CENTER ) { // CIRC_ARC_CENTER
117         Standard_Boolean sense = aCI.GetSense();
118
119         Standard_Real aRad = aP1.Distance(aP2);
120         gp_Vec aV1 (aP1, aP2);
121         gp_Vec aV2 (aP1, aP3);
122         gp_Vec aN = aV1 ^ aV2;
123
124         if (sense)
125           aN = -aN;
126
127         GC_MakeCircle circ (aP1, aN, aRad);
128         Handle(Geom_Circle) aGeomCirc = circ.Value();
129
130         GC_MakeArcOfCircle arc (aGeomCirc->Circ(), aP2, aP3, Standard_True);
131         aShape = BRepBuilderAPI_MakeEdge(arc.Value()).Edge();
132       } else if ( aType == ELLIPSE_ARC_CENTER_TWO_PNT ) { // ELLIPSE_ARC_CENTER_TWO_PNT
133         if ( aP1.Distance(aP2) <= aP1.Distance(aP3) ) {
134           // 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");      
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.Value()).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  * \brief Returns a name of creation operation and names and values of creation parameters
167  */
168 //================================================================================
169
170 bool GEOMImpl_ArcDriver::
171 GetCreationInformation(std::string&             theOperationName,
172                        std::vector<GEOM_Param>& theParams)
173 {
174   if (Label().IsNull()) return 0;
175   Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
176
177   GEOMImpl_IArc aCI( function );
178   Standard_Integer aType = function->GetType();
179
180   theOperationName = "ARC";
181
182   switch ( aType ) {
183   case CIRC_ARC_THREE_PNT:
184     AddParam( theParams, "Point 1", aCI.GetPoint1() );
185     AddParam( theParams, "Point 2", aCI.GetPoint2() );
186     AddParam( theParams, "Point 3", aCI.GetPoint3() );
187     break;
188   case CIRC_ARC_CENTER:
189     AddParam( theParams, "Center Point", aCI.GetPoint1() );
190     AddParam( theParams, "Point Start", aCI.GetPoint2() );
191     AddParam( theParams, "Point End", aCI.GetPoint3() );
192     AddParam( theParams, "Reverse", aCI.GetSense() );
193     break;
194   case ELLIPSE_ARC_CENTER_TWO_PNT:
195     AddParam( theParams, "Center Point", aCI.GetPoint1() );
196     AddParam( theParams, "Point 1", aCI.GetPoint2() );
197     AddParam( theParams, "Point 2", aCI.GetPoint3() );
198     break;
199   default:
200     return false;
201   }
202   
203   return true;
204 }
205
206 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_ArcDriver,GEOM_BaseDriver)