1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 #include <Standard_Stream.hxx>
23 #include <GEOMImpl_LineDriver.hxx>
24 #include <GEOMImpl_ILine.hxx>
25 #include <GEOMImpl_Types.hxx>
26 #include <GEOM_Function.hxx>
28 #include <BRep_Tool.hxx>
29 #include <BRepBuilderAPI_MakeEdge.hxx>
30 #include <BRepAlgoAPI_Section.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>
41 #include <Precision.hxx>
42 #include <Standard_ConstructionError.hxx>
43 #include <Standard_NullObject.hxx>
45 //=======================================================================
48 //=======================================================================
49 const Standard_GUID& GEOMImpl_LineDriver::GetID()
51 static Standard_GUID aLineDriver("FF1BBB06-5D14-4df2-980B-3A668264EA16");
56 //=======================================================================
57 //function : GEOMImpl_LineDriver
59 //=======================================================================
60 GEOMImpl_LineDriver::GEOMImpl_LineDriver()
64 //=======================================================================
67 //=======================================================================
68 Standard_Integer GEOMImpl_LineDriver::Execute(TFunction_Logbook& log) const
70 if (Label().IsNull()) return 0;
71 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
73 GEOMImpl_ILine aPI (aFunction);
74 Standard_Integer aType = aFunction->GetType();
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");
87 if (aShape1.IsSame(aShape2)) {
88 Standard_ConstructionError::Raise("The end points must be different");
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");
95 aShape = BRepBuilderAPI_MakeEdge(P1, P2).Shape();
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");
106 if (aShape1.IsSame(aShape2)) {
107 Standard_ConstructionError::Raise("The end faces must be different");
109 BRepAlgoAPI_Section E (aShape1, aShape2, Standard_False);
110 E.Approximation(Standard_True);
113 Standard_ConstructionError::Raise("Line can not be performed on the given faces");
117 TopExp_Explorer Exp (E, TopAbs_EDGE);
119 aShape = Exp.Current();
124 Standard_ConstructionError::Raise("Faces not have intersection line");
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");
139 if (aShape2.ShapeType() != TopAbs_EDGE) {
140 Standard_ConstructionError::Raise("Wrong second argument: must be vector");
142 if (aShape1.IsSame(aShape2)) {
143 Standard_ConstructionError::Raise("The end points must be different");
145 gp_Pnt P1 = BRep_Tool::Pnt(TopoDS::Vertex(aShape1));
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");
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");
159 gp_Pnt P2 (P1.XYZ() + PV2.XYZ() - PV1.XYZ());
160 aShape = BRepBuilderAPI_MakeEdge(P1, P2).Shape();
164 if (aShape.IsNull()) return 0;
165 aShape.Infinite(true);
167 aFunction->SetValue(aShape);
169 log.SetTouched(Label());
175 //=======================================================================
176 //function : GEOMImpl_LineDriver_Type_
178 //=======================================================================
179 Standard_EXPORT Handle_Standard_Type& GEOMImpl_LineDriver_Type_()
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);
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),
194 (Standard_Address)_Ancestors,
195 (Standard_Address)NULL);
200 //=======================================================================
201 //function : DownCast
203 //=======================================================================
204 const Handle(GEOMImpl_LineDriver) Handle(GEOMImpl_LineDriver)::DownCast
205 (const Handle(Standard_Transient)& AnObject)
207 Handle(GEOMImpl_LineDriver) _anOtherObject;
209 if (!AnObject.IsNull()) {
210 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_LineDriver))) {
211 _anOtherObject = Handle(GEOMImpl_LineDriver)((Handle(GEOMImpl_LineDriver)&)AnObject);
215 return _anOtherObject ;