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