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