Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[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 <gp_Pnt.hxx>
40
41 //=======================================================================
42 //function : GetID
43 //purpose  :
44 //=======================================================================
45 const Standard_GUID& GEOMImpl_PointDriver::GetID()
46 {
47   static Standard_GUID aPointDriver("FF1BBB02-5D14-4df2-980B-3A668264EA16");
48   return aPointDriver;
49 }
50
51
52 //=======================================================================
53 //function : GEOMImpl_PointDriver
54 //purpose  :
55 //=======================================================================
56 GEOMImpl_PointDriver::GEOMImpl_PointDriver()
57 {
58 }
59
60 //=======================================================================
61 //function : Execute
62 //purpose  :
63 //=======================================================================
64 Standard_Integer GEOMImpl_PointDriver::Execute(TFunction_Logbook& log) const
65 {
66   if (Label().IsNull())  return 0;
67   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
68
69   GEOMImpl_IPoint aPI (aFunction);
70   Standard_Integer aType = aFunction->GetType();
71
72   gp_Pnt aPnt;
73
74   if (aType == POINT_XYZ) {
75     aPnt = gp_Pnt(aPI.GetX(), aPI.GetY(), aPI.GetZ());
76
77   } else if (aType == POINT_XYZ_REF) {
78
79     Handle(GEOM_Function) aRefPoint = aPI.GetRef();
80     TopoDS_Shape aRefShape = aRefPoint->GetValue();
81     if (aRefShape.ShapeType() != TopAbs_VERTEX) {
82       Standard_TypeMismatch::Raise
83         ("Point creation aborted : referenced shape is not a vertex");
84     }
85     gp_Pnt P = BRep_Tool::Pnt(TopoDS::Vertex(aRefShape));
86     aPnt = gp_Pnt(P.X() + aPI.GetX(), P.Y() + aPI.GetY(), P.Z() + aPI.GetZ());
87
88   } else if (aType == POINT_CURVE_PAR) {
89
90     Handle(GEOM_Function) aRefCurve = aPI.GetCurve();
91     TopoDS_Shape aRefShape = aRefCurve->GetValue();
92     if (aRefShape.ShapeType() != TopAbs_EDGE) {
93       Standard_TypeMismatch::Raise
94         ("Point On Curve creation aborted : curve shape is not an edge");
95     }
96     Standard_Real aFP, aLP, aP;
97     Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(aRefShape), aFP, aLP);
98     aP = aFP + (aLP - aFP) * aPI.GetParameter();
99     aPnt = aCurve->Value(aP);
100
101   } else if (aType == POINT_LINES_INTERSECTION) {
102     Handle(GEOM_Function) aRef1 = aPI.GetLine1();
103     Handle(GEOM_Function) aRef2 = aPI.GetLine2();
104
105     TopoDS_Shape aRefShape1 = aRef1->GetValue();
106     TopoDS_Shape aRefShape2 = aRef2->GetValue();
107
108     if (aRefShape1.ShapeType() != TopAbs_EDGE || aRefShape2.ShapeType() != TopAbs_EDGE ) {
109       Standard_TypeMismatch::Raise
110         ("Creation Point On Lines Intersection Aborted : Line shape is not an edge");
111     }
112     //Calculate Lines Intersection Point
113     BRepExtrema_DistShapeShape dst (aRefShape1, aRefShape2);
114     if (dst.IsDone())
115       {
116         gp_Pnt P1, P2;
117         for (int i = 1; i <= dst.NbSolution(); i++) {
118           P1 = dst.PointOnShape1(i);
119           P2 = dst.PointOnShape2(i);
120           Standard_Real Dist = P1.Distance(P2);
121           if ( Dist <= Precision::Confusion() )
122             aPnt = P1;
123           else 
124             Standard_TypeMismatch::Raise ("Lines not have an Intersection Point");
125         }
126       }
127   }
128   else {
129     return 0;
130   }
131
132   BRepBuilderAPI_MakeVertex mkVertex (aPnt);
133   TopoDS_Shape aShape = mkVertex.Shape();
134   aShape.Infinite(Standard_True);
135   aFunction->SetValue(aShape);
136
137   log.SetTouched(Label());
138
139   return 1;
140 }
141
142
143 //=======================================================================
144 //function :  GEOMImpl_PointDriver_Type_
145 //purpose  :
146 //=======================================================================
147 Standard_EXPORT Handle_Standard_Type& GEOMImpl_PointDriver_Type_()
148 {
149
150   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
151   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
152   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
153   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
154   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
155   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
156
157
158   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
159   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_PointDriver",
160                                                          sizeof(GEOMImpl_PointDriver),
161                                                          1,
162                                                          (Standard_Address)_Ancestors,
163                                                          (Standard_Address)NULL);
164
165   return _aType;
166 }
167
168 //=======================================================================
169 //function : DownCast
170 //purpose  :
171 //=======================================================================
172
173 const Handle(GEOMImpl_PointDriver) Handle(GEOMImpl_PointDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
174 {
175   Handle(GEOMImpl_PointDriver) _anOtherObject;
176
177   if (!AnObject.IsNull()) {
178      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_PointDriver))) {
179        _anOtherObject = Handle(GEOMImpl_PointDriver)((Handle(GEOMImpl_PointDriver)&)AnObject);
180      }
181   }
182
183   return _anOtherObject ;
184 }
185
186