Salome HOME
Merge from branch BR_Dev_For_4_0 (from tag mergeto_BR_QT4_Dev_12Feb08)
[modules/geom.git] / src / GEOMImpl / GEOMImpl_LineDriver.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_LineDriver.hxx>
24 #include <GEOMImpl_ILine.hxx>
25 #include <GEOMImpl_Types.hxx>
26 #include <GEOM_Function.hxx>
27
28 #include <BRep_Tool.hxx>
29 #include <BRepBuilderAPI_MakeEdge.hxx>
30 #include <BRepAlgoAPI_Section.hxx>
31 #include <TopAbs.hxx>
32 #include <TopExp.hxx>
33 #include <TopoDS.hxx>
34 #include <TopoDS_Edge.hxx>
35 #include <TopoDS_Shape.hxx>
36 #include <TopoDS_Vertex.hxx>
37 #include <TopExp_Explorer.hxx>
38 #include <TopTools_MapOfShape.hxx>
39
40 #include <gp_Pnt.hxx>
41 #include <Precision.hxx>
42 #include <Standard_ConstructionError.hxx>
43 #include <Standard_NullObject.hxx>
44
45 //=======================================================================
46 //function : GetID
47 //purpose  :
48 //=======================================================================
49 const Standard_GUID& GEOMImpl_LineDriver::GetID()
50 {
51   static Standard_GUID aLineDriver("FF1BBB06-5D14-4df2-980B-3A668264EA16");
52   return aLineDriver;
53 }
54
55
56 //=======================================================================
57 //function : GEOMImpl_LineDriver
58 //purpose  :
59 //=======================================================================
60 GEOMImpl_LineDriver::GEOMImpl_LineDriver()
61 {
62 }
63
64 //=======================================================================
65 //function : Execute
66 //purpose  :
67 //=======================================================================
68 Standard_Integer GEOMImpl_LineDriver::Execute(TFunction_Logbook& log) const
69 {
70   if (Label().IsNull())  return 0;
71   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
72
73   GEOMImpl_ILine aPI (aFunction);
74   Standard_Integer aType = aFunction->GetType();
75
76   TopoDS_Shape aShape;
77
78   if (aType == LINE_TWO_PNT) {
79     Handle(GEOM_Function) aRefPnt1 = aPI.GetPoint1();
80     Handle(GEOM_Function) aRefPnt2 = aPI.GetPoint2();
81     TopoDS_Shape aShape1 = aRefPnt1->GetValue();
82     TopoDS_Shape aShape2 = aRefPnt2->GetValue();
83     if (aShape1.ShapeType() != TopAbs_VERTEX ||
84         aShape2.ShapeType() != TopAbs_VERTEX) {
85       Standard_ConstructionError::Raise("Wrong arguments: two points must be given");
86     }
87     if (aShape1.IsSame(aShape2)) {
88       Standard_ConstructionError::Raise("The end points must be different");
89     }
90     gp_Pnt P1 = BRep_Tool::Pnt(TopoDS::Vertex(aShape1));
91     gp_Pnt P2 = BRep_Tool::Pnt(TopoDS::Vertex(aShape2));
92     if (P1.Distance(P2) < Precision::Confusion()) {
93       Standard_ConstructionError::Raise("The end points are too close");
94     }
95     aShape = BRepBuilderAPI_MakeEdge(P1, P2).Shape();
96
97   } else if (aType == LINE_TWO_FACES) {
98     Handle(GEOM_Function) aRefFace1 = aPI.GetFace1();
99     Handle(GEOM_Function) aRefFace2 = aPI.GetFace2();
100     TopoDS_Shape aShape1 = aRefFace1->GetValue();
101     TopoDS_Shape aShape2 = aRefFace2->GetValue();
102     if (aShape1.ShapeType() != TopAbs_FACE ||
103         aShape2.ShapeType() != TopAbs_FACE) {
104       Standard_ConstructionError::Raise("Wrong arguments: two faces must be given");
105     }
106     if (aShape1.IsSame(aShape2)) {
107       Standard_ConstructionError::Raise("The end faces must be different");
108     }
109     BRepAlgoAPI_Section E (aShape1, aShape2, Standard_False);
110     E.Approximation(Standard_True);
111     E.Build();
112     if (!E.IsDone()) {
113       Standard_ConstructionError::Raise("Line can not be performed on the given faces");
114     }
115     else
116     {
117         TopExp_Explorer Exp (E, TopAbs_EDGE);
118         if ( Exp.More() ){
119             aShape = Exp.Current();
120             Exp.Next();
121         }
122         else
123           {
124             Standard_ConstructionError::Raise("Faces not have intersection line");
125             aShape = E.Shape();
126           }
127         if ( Exp.More() )
128           aShape = E.Shape();
129     }
130
131   } else if (aType == LINE_PNT_DIR) {
132     Handle(GEOM_Function) aRefPnt = aPI.GetPoint1();
133     Handle(GEOM_Function) aRefDir = aPI.GetPoint2();
134     TopoDS_Shape aShape1 = aRefPnt->GetValue();
135     TopoDS_Shape aShape2 = aRefDir->GetValue();
136     if (aShape1.ShapeType() != TopAbs_VERTEX) {
137       Standard_ConstructionError::Raise("Wrong first argument: must be point");
138     }
139     if (aShape2.ShapeType() != TopAbs_EDGE) {
140       Standard_ConstructionError::Raise("Wrong second argument: must be vector");
141     }
142     if (aShape1.IsSame(aShape2)) {
143       Standard_ConstructionError::Raise("The end points must be different");
144     }
145     gp_Pnt P1 = BRep_Tool::Pnt(TopoDS::Vertex(aShape1));
146
147     TopoDS_Edge anE = TopoDS::Edge(aShape2);
148     TopoDS_Vertex V1, V2;
149     TopExp::Vertices(anE, V1, V2, Standard_True);
150     if (V1.IsNull() || V2.IsNull()) {
151       Standard_NullObject::Raise("Line creation aborted: vector is not defined");
152     }
153     gp_Pnt PV1 = BRep_Tool::Pnt(V1);
154     gp_Pnt PV2 = BRep_Tool::Pnt(V2);
155     if (PV1.Distance(PV2) < Precision::Confusion()) {
156       Standard_ConstructionError::Raise("Vector with null magnitude");
157     }
158
159     gp_Pnt P2 (P1.XYZ() + PV2.XYZ() - PV1.XYZ());
160     aShape = BRepBuilderAPI_MakeEdge(P1, P2).Shape();
161   } else {
162   }
163
164   if (aShape.IsNull()) return 0;
165   aShape.Infinite(true);
166
167   aFunction->SetValue(aShape);
168
169   log.SetTouched(Label());
170
171   return 1;
172 }
173
174
175 //=======================================================================
176 //function :  GEOMImpl_LineDriver_Type_
177 //purpose  :
178 //=======================================================================
179 Standard_EXPORT Handle_Standard_Type& GEOMImpl_LineDriver_Type_()
180 {
181
182   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
183   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
184   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
185   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
186   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
187   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
188
189
190   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
191   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_LineDriver",
192                                                          sizeof(GEOMImpl_LineDriver),
193                                                          1,
194                                                          (Standard_Address)_Ancestors,
195                                                          (Standard_Address)NULL);
196
197   return _aType;
198 }
199
200 //=======================================================================
201 //function : DownCast
202 //purpose  :
203 //=======================================================================
204 const Handle(GEOMImpl_LineDriver) Handle(GEOMImpl_LineDriver)::DownCast
205        (const Handle(Standard_Transient)& AnObject)
206 {
207   Handle(GEOMImpl_LineDriver) _anOtherObject;
208
209   if (!AnObject.IsNull()) {
210      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_LineDriver))) {
211        _anOtherObject = Handle(GEOMImpl_LineDriver)((Handle(GEOMImpl_LineDriver)&)AnObject);
212      }
213   }
214
215   return _anOtherObject ;
216 }