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>
34 #include <TopoDS_Edge.hxx>
35 #include <TopoDS_Shape.hxx>
36 #include <TopoDS_Vertex.hxx>
39 #include <Precision.hxx>
40 #include <Standard_ConstructionError.hxx>
41 #include <Standard_NullObject.hxx>
43 //=======================================================================
46 //=======================================================================
47 const Standard_GUID& GEOMImpl_LineDriver::GetID()
49 static Standard_GUID aLineDriver("FF1BBB06-5D14-4df2-980B-3A668264EA16");
54 //=======================================================================
55 //function : GEOMImpl_LineDriver
57 //=======================================================================
58 GEOMImpl_LineDriver::GEOMImpl_LineDriver()
62 //=======================================================================
65 //=======================================================================
66 Standard_Integer GEOMImpl_LineDriver::Execute(TFunction_Logbook& log) const
68 if (Label().IsNull()) return 0;
69 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
71 GEOMImpl_ILine aPI (aFunction);
72 Standard_Integer aType = aFunction->GetType();
76 if (aType == LINE_TWO_PNT) {
77 Handle(GEOM_Function) aRefPnt1 = aPI.GetPoint1();
78 Handle(GEOM_Function) aRefPnt2 = aPI.GetPoint2();
79 TopoDS_Shape aShape1 = aRefPnt1->GetValue();
80 TopoDS_Shape aShape2 = aRefPnt2->GetValue();
81 if (aShape1.ShapeType() != TopAbs_VERTEX ||
82 aShape2.ShapeType() != TopAbs_VERTEX) {
83 Standard_ConstructionError::Raise("Wrong arguments: two points must be given");
85 if (aShape1.IsSame(aShape2)) {
86 Standard_ConstructionError::Raise("The end points must be different");
88 gp_Pnt P1 = BRep_Tool::Pnt(TopoDS::Vertex(aShape1));
89 gp_Pnt P2 = BRep_Tool::Pnt(TopoDS::Vertex(aShape2));
90 if (P1.Distance(P2) < Precision::Confusion()) {
91 Standard_ConstructionError::Raise("The end points are too close");
93 aShape = BRepBuilderAPI_MakeEdge(P1, P2).Shape();
95 } else if (aType == LINE_PNT_DIR) {
96 Handle(GEOM_Function) aRefPnt = aPI.GetPoint1();
97 Handle(GEOM_Function) aRefDir = aPI.GetPoint2();
98 TopoDS_Shape aShape1 = aRefPnt->GetValue();
99 TopoDS_Shape aShape2 = aRefDir->GetValue();
100 if (aShape1.ShapeType() != TopAbs_VERTEX) {
101 Standard_ConstructionError::Raise("Wrong first argument: must be point");
103 if (aShape2.ShapeType() != TopAbs_EDGE) {
104 Standard_ConstructionError::Raise("Wrong second argument: must be vector");
106 if (aShape1.IsSame(aShape2)) {
107 Standard_ConstructionError::Raise("The end points must be different");
109 gp_Pnt P1 = BRep_Tool::Pnt(TopoDS::Vertex(aShape1));
111 TopoDS_Edge anE = TopoDS::Edge(aShape2);
112 TopoDS_Vertex V1, V2;
113 TopExp::Vertices(anE, V1, V2, Standard_True);
114 if (V1.IsNull() || V2.IsNull()) {
115 Standard_NullObject::Raise("Line creation aborted: vector is not defined");
117 gp_Pnt PV1 = BRep_Tool::Pnt(V1);
118 gp_Pnt PV2 = BRep_Tool::Pnt(V2);
119 if (PV1.Distance(PV2) < Precision::Confusion()) {
120 Standard_ConstructionError::Raise("Vector with null magnitude");
123 gp_Pnt P2 (P1.XYZ() + PV2.XYZ() - PV1.XYZ());
124 aShape = BRepBuilderAPI_MakeEdge(P1, P2).Shape();
128 if (aShape.IsNull()) return 0;
129 aShape.Infinite(true);
131 aFunction->SetValue(aShape);
133 log.SetTouched(Label());
139 //=======================================================================
140 //function : GEOMImpl_LineDriver_Type_
142 //=======================================================================
143 Standard_EXPORT Handle_Standard_Type& GEOMImpl_LineDriver_Type_()
146 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
147 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
148 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
149 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
150 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
151 if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
154 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
155 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_LineDriver",
156 sizeof(GEOMImpl_LineDriver),
158 (Standard_Address)_Ancestors,
159 (Standard_Address)NULL);
164 //=======================================================================
165 //function : DownCast
167 //=======================================================================
168 const Handle(GEOMImpl_LineDriver) Handle(GEOMImpl_LineDriver)::DownCast
169 (const Handle(Standard_Transient)& AnObject)
171 Handle(GEOMImpl_LineDriver) _anOtherObject;
173 if (!AnObject.IsNull()) {
174 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_LineDriver))) {
175 _anOtherObject = Handle(GEOMImpl_LineDriver)((Handle(GEOMImpl_LineDriver)&)AnObject);
179 return _anOtherObject ;