Salome HOME
Porting Salome to OCCT 7.7.0
[modules/geom.git] / src / GEOMImpl / GEOMImpl_LineDriver.cxx
1 // Copyright (C) 2007-2022  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, or (at your option) any later version.
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 <Basics_OCCTVersion.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 <BRepBuilderAPI_MakeVertex.hxx>
33 #include <BRepAlgoAPI_Section.hxx>
34 #include <TopAbs.hxx>
35 #include <TopExp.hxx>
36 #include <TopoDS.hxx>
37 #include <TopoDS_Edge.hxx>
38 #include <TopoDS_Shape.hxx>
39 #include <TopoDS_Vertex.hxx>
40 #include <TopExp_Explorer.hxx>
41 #include <TopTools_MapOfShape.hxx>
42
43 #include <gp_Pnt.hxx>
44 #include <Precision.hxx>
45 #include <Standard_ConstructionError.hxx>
46 #include <Standard_NullObject.hxx>
47
48 //=======================================================================
49 //function : GetID
50 //purpose  :
51 //=======================================================================
52 const Standard_GUID& GEOMImpl_LineDriver::GetID()
53 {
54   static Standard_GUID aLineDriver("FF1BBB06-5D14-4df2-980B-3A668264EA16");
55   return aLineDriver;
56 }
57
58
59 //=======================================================================
60 //function : GEOMImpl_LineDriver
61 //purpose  :
62 //=======================================================================
63 GEOMImpl_LineDriver::GEOMImpl_LineDriver()
64 {
65 }
66
67 //=======================================================================
68 //function : Execute
69 //purpose  :
70 //=======================================================================
71 Standard_Integer GEOMImpl_LineDriver::Execute(Handle(TFunction_Logbook)& log) const
72 {
73   if (Label().IsNull())  return 0;
74   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
75
76   GEOMImpl_ILine aPI (aFunction);
77   Standard_Integer aType = aFunction->GetType();
78
79   TopoDS_Shape aShape;
80
81   if (aType == LINE_TWO_PNT) {
82     Handle(GEOM_Function) aRefPnt1 = aPI.GetPoint1();
83     Handle(GEOM_Function) aRefPnt2 = aPI.GetPoint2();
84     TopoDS_Shape aShape1 = aRefPnt1->GetValue();
85     TopoDS_Shape aShape2 = aRefPnt2->GetValue();
86     if (aShape1.ShapeType() != TopAbs_VERTEX ||
87         aShape2.ShapeType() != TopAbs_VERTEX) {
88       Standard_ConstructionError::Raise("Wrong arguments: two points must be given");
89     }
90     if (aShape1.IsSame(aShape2)) {
91       Standard_ConstructionError::Raise("The end points must be different");
92     }
93     TopoDS_Vertex V1 = TopoDS::Vertex(aShape1);
94     TopoDS_Vertex V2 = TopoDS::Vertex(aShape2);
95     gp_Pnt P1 = BRep_Tool::Pnt(V1);
96     gp_Pnt P2 = BRep_Tool::Pnt(V2);
97     if (P1.Distance(P2) < Precision::Confusion()) {
98       Standard_ConstructionError::Raise("The end points are too close");
99     }
100     aShape = BRepBuilderAPI_MakeEdge(V1, V2).Shape();
101
102   } else if (aType == LINE_TWO_FACES) {
103     Handle(GEOM_Function) aRefFace1 = aPI.GetFace1();
104     Handle(GEOM_Function) aRefFace2 = aPI.GetFace2();
105     TopoDS_Shape aShape1 = aRefFace1->GetValue();
106     TopoDS_Shape aShape2 = aRefFace2->GetValue();
107     if (aShape1.ShapeType() != TopAbs_FACE ||
108         aShape2.ShapeType() != TopAbs_FACE) {
109       Standard_ConstructionError::Raise("Wrong arguments: two faces must be given");
110     }
111     if (aShape1.IsSame(aShape2)) {
112       Standard_ConstructionError::Raise("The end faces must be different");
113     }
114     BRepAlgoAPI_Section E (aShape1, aShape2, Standard_False);
115     E.Approximation(Standard_True);
116 #if OCC_VERSION_LARGE >= 0x07070000
117     E.ComputePCurveOn1(Standard_True);
118     E.ComputePCurveOn2(Standard_True);
119 #endif
120     E.Build();
121     if (!E.IsDone()) {
122       Standard_ConstructionError::Raise("Line can not be performed on the given faces");
123     }
124     else
125     {
126         TopExp_Explorer Exp (E, TopAbs_EDGE);
127         if ( Exp.More() ){
128             aShape = Exp.Current();
129             Exp.Next();
130         }
131         else
132           {
133             Standard_ConstructionError::Raise("Faces not have intersection line");
134             aShape = E.Shape();
135           }
136         if ( Exp.More() )
137           aShape = E.Shape();
138     }
139
140   } else if (aType == LINE_PNT_DIR) {
141     Handle(GEOM_Function) aRefPnt = aPI.GetPoint1();
142     Handle(GEOM_Function) aRefDir = aPI.GetPoint2();
143     TopoDS_Shape aShape1 = aRefPnt->GetValue();
144     TopoDS_Shape aShape2 = aRefDir->GetValue();
145     if (aShape1.ShapeType() != TopAbs_VERTEX) {
146       Standard_ConstructionError::Raise("Wrong first argument: must be point");
147     }
148     if (aShape2.ShapeType() != TopAbs_EDGE) {
149       Standard_ConstructionError::Raise("Wrong second argument: must be vector");
150     }
151     if (aShape1.IsSame(aShape2)) {
152       Standard_ConstructionError::Raise("The end points must be different");
153     }
154     TopoDS_Vertex Vstart = TopoDS::Vertex(aShape1);
155     gp_Pnt P1 = BRep_Tool::Pnt(Vstart);
156
157     TopoDS_Edge anE = TopoDS::Edge(aShape2);
158     TopoDS_Vertex V1, V2;
159     TopExp::Vertices(anE, V1, V2, Standard_True);
160     if (V1.IsNull() || V2.IsNull()) {
161       Standard_NullObject::Raise("Line creation aborted: vector is not defined");
162     }
163     gp_Pnt PV1 = BRep_Tool::Pnt(V1);
164     gp_Pnt PV2 = BRep_Tool::Pnt(V2);
165     if (PV1.Distance(PV2) < Precision::Confusion()) {
166       Standard_ConstructionError::Raise("Vector with null magnitude");
167     }
168     TopoDS_Vertex Vend;
169     if ( V1.IsSame( Vstart ) )
170       Vend = V2;
171     else if ( V2.IsSame( Vstart ) )
172       Vend = V1;
173     else
174       Vend = BRepBuilderAPI_MakeVertex( gp_Pnt(P1.XYZ() + PV2.XYZ() - PV1.XYZ()) );
175     aShape = BRepBuilderAPI_MakeEdge(Vstart, Vend).Shape();
176   } else {
177   }
178
179   if (aShape.IsNull()) return 0;
180   //aShape.Infinite(true); // VSR: 05/04/2010: Fix 20668 (Fit All for points & lines)
181
182   aFunction->SetValue(aShape);
183
184   log->SetTouched(Label());
185
186   return 1;
187 }
188
189 //================================================================================
190 /*!
191  * \brief Returns a name of creation operation and names and values of creation parameters
192  */
193 //================================================================================
194
195 bool GEOMImpl_LineDriver::
196 GetCreationInformation(std::string&             theOperationName,
197                        std::vector<GEOM_Param>& theParams)
198 {
199   if (Label().IsNull()) return 0;
200   Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
201
202   GEOMImpl_ILine aCI( function );
203   Standard_Integer aType = function->GetType();
204
205   theOperationName = "LINE";
206
207   switch ( aType ) {
208   case LINE_TWO_PNT:
209     AddParam( theParams, "Point 1", aCI.GetPoint1() );
210     AddParam( theParams, "Point 2", aCI.GetPoint2() );
211     break;
212   case LINE_TWO_FACES:
213     AddParam( theParams, "Face 1", aCI.GetFace1() );
214     AddParam( theParams, "Face 2", aCI.GetFace2() );
215     break;
216   case LINE_PNT_DIR:
217     AddParam( theParams, "Point 1", aCI.GetPoint1() );
218     AddParam( theParams, "Vector", aCI.GetPoint2() );
219     break;
220   default:
221     return false;
222   }
223
224   return true;
225 }
226
227 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_LineDriver,GEOM_BaseDriver)