Salome HOME
Bug 0020057: EDF GEOM: Impossible to explode an object to faces. Infinite loop.
[modules/geom.git] / src / GEOMImpl / GEOMImpl_PointDriver.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_PointDriver.hxx>
24 #include <GEOMImpl_IPoint.hxx>
25 #include <GEOMImpl_Types.hxx>
26 #include <GEOM_Function.hxx>
27
28 #include <BRep_Tool.hxx>
29 #include <BRepBuilderAPI_MakeVertex.hxx>
30 #include <BRepExtrema_DistShapeShape.hxx>
31 #include <Precision.hxx>
32 #include <TopAbs.hxx>
33 #include <TopoDS.hxx>
34 #include <TopoDS_Edge.hxx>
35 #include <TopoDS_Shape.hxx>
36 #include <TopoDS_Vertex.hxx>
37
38 #include <Geom_Curve.hxx>
39 #include <Geom_Surface.hxx>
40 #include <gp_Pnt.hxx>
41 #include <TopoDS_Face.hxx>
42 #include <ShapeAnalysis.hxx>
43
44
45 //=======================================================================
46 //function : GetID
47 //purpose  :
48 //=======================================================================
49 const Standard_GUID& GEOMImpl_PointDriver::GetID()
50 {
51   static Standard_GUID aPointDriver("FF1BBB02-5D14-4df2-980B-3A668264EA16");
52   return aPointDriver;
53 }
54
55
56 //=======================================================================
57 //function : GEOMImpl_PointDriver
58 //purpose  :
59 //=======================================================================
60 GEOMImpl_PointDriver::GEOMImpl_PointDriver()
61 {
62 }
63
64
65 //=======================================================================
66 //function : Execute
67 //purpose  :
68 //=======================================================================
69 Standard_Integer GEOMImpl_PointDriver::Execute(TFunction_Logbook& log) const
70 {
71   if (Label().IsNull())  return 0;
72   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
73
74   GEOMImpl_IPoint aPI (aFunction);
75   Standard_Integer aType = aFunction->GetType();
76
77   gp_Pnt aPnt;
78
79   if (aType == POINT_XYZ) {
80     aPnt = gp_Pnt(aPI.GetX(), aPI.GetY(), aPI.GetZ());
81
82   }
83   else if (aType == POINT_XYZ_REF) {
84
85     Handle(GEOM_Function) aRefPoint = aPI.GetRef();
86     TopoDS_Shape aRefShape = aRefPoint->GetValue();
87     if (aRefShape.ShapeType() != TopAbs_VERTEX) {
88       Standard_TypeMismatch::Raise
89         ("Point creation aborted : referenced shape is not a vertex");
90     }
91     gp_Pnt P = BRep_Tool::Pnt(TopoDS::Vertex(aRefShape));
92     aPnt = gp_Pnt(P.X() + aPI.GetX(), P.Y() + aPI.GetY(), P.Z() + aPI.GetZ());
93
94   }
95   else if (aType == POINT_CURVE_PAR) {
96     Handle(GEOM_Function) aRefCurve = aPI.GetCurve();
97     TopoDS_Shape aRefShape = aRefCurve->GetValue();
98     if (aRefShape.ShapeType() != TopAbs_EDGE) {
99       Standard_TypeMismatch::Raise
100         ("Point On Curve creation aborted : curve shape is not an edge");
101     }
102     Standard_Real aFP, aLP, aP;
103     Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(aRefShape), aFP, aLP);
104     aP = aFP + (aLP - aFP) * aPI.GetParameter();
105     aPnt = aCurve->Value(aP);
106   }
107   else if (aType == POINT_SURFACE_PAR) {
108     Handle(GEOM_Function) aRefCurve = aPI.GetSurface();
109     TopoDS_Shape aRefShape = aRefCurve->GetValue();
110     if (aRefShape.ShapeType() != TopAbs_FACE) {
111       Standard_TypeMismatch::Raise
112         ("Point On Surface creation aborted : surface shape is not a face");
113     }
114     TopoDS_Face F = TopoDS::Face(aRefShape);
115     Handle(Geom_Surface) aSurf = BRep_Tool::Surface(F);
116     Standard_Real U1,U2,V1,V2;
117     //aSurf->Bounds(U1,U2,V1,V2);
118     ShapeAnalysis::GetFaceUVBounds(F,U1,U2,V1,V2);
119     Standard_Real U = U1 + (U2-U1) * aPI.GetParameter();
120     Standard_Real V = V1 + (V2-V1) * aPI.GetParameter2();
121     aPnt = aSurf->Value(U,V);
122   }
123   else if (aType == POINT_LINES_INTERSECTION) {
124     Handle(GEOM_Function) aRef1 = aPI.GetLine1();
125     Handle(GEOM_Function) aRef2 = aPI.GetLine2();
126
127     TopoDS_Shape aRefShape1 = aRef1->GetValue();
128     TopoDS_Shape aRefShape2 = aRef2->GetValue();
129
130     if (aRefShape1.ShapeType() != TopAbs_EDGE || aRefShape2.ShapeType() != TopAbs_EDGE ) {
131       Standard_TypeMismatch::Raise
132         ("Creation Point On Lines Intersection Aborted : Line shape is not an edge");
133     }
134     //Calculate Lines Intersection Point
135     BRepExtrema_DistShapeShape dst (aRefShape1, aRefShape2);
136     if (dst.IsDone())
137       {
138         gp_Pnt P1, P2;
139         for (int i = 1; i <= dst.NbSolution(); i++) {
140           P1 = dst.PointOnShape1(i);
141           P2 = dst.PointOnShape2(i);
142           Standard_Real Dist = P1.Distance(P2);
143           if ( Dist <= Precision::Confusion() )
144             aPnt = P1;
145           else 
146             Standard_TypeMismatch::Raise ("Lines not have an Intersection Point");
147         }
148       }
149   }
150   else {
151     return 0;
152   }
153
154   BRepBuilderAPI_MakeVertex mkVertex (aPnt);
155   TopoDS_Shape aShape = mkVertex.Shape();
156   aShape.Infinite(Standard_True);
157   aFunction->SetValue(aShape);
158
159   log.SetTouched(Label());
160
161   return 1;
162 }
163
164
165 //=======================================================================
166 //function :  GEOMImpl_PointDriver_Type_
167 //purpose  :
168 //=======================================================================
169 Standard_EXPORT Handle_Standard_Type& GEOMImpl_PointDriver_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_PointDriver",
182                                                          sizeof(GEOMImpl_PointDriver),
183                                                          1,
184                                                          (Standard_Address)_Ancestors,
185                                                          (Standard_Address)NULL);
186
187   return _aType;
188 }
189
190 //=======================================================================
191 //function : DownCast
192 //purpose  :
193 //=======================================================================
194
195 const Handle(GEOMImpl_PointDriver) Handle(GEOMImpl_PointDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
196 {
197   Handle(GEOMImpl_PointDriver) _anOtherObject;
198
199   if (!AnObject.IsNull()) {
200      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_PointDriver))) {
201        _anOtherObject = Handle(GEOMImpl_PointDriver)((Handle(GEOMImpl_PointDriver)&)AnObject);
202      }
203   }
204
205   return _anOtherObject ;
206 }
207
208