Salome HOME
Copyrights update
[modules/geom.git] / src / GEOMImpl / GEOMImpl_LineDriver.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
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.
8 // 
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.
13 //
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
17 //
18 // See http://www.salome-platform.org/
19 //
20
21 #include <Standard_Stream.hxx>
22
23 #include <GEOMImpl_LineDriver.hxx>
24 #include <GEOMImpl_ILine.hxx>
25 #include <GEOMImpl_Types.hxx>
26 #include <GEOM_Function.hxx>
27
28 #include <BRep_Tool.hxx>
29 #include <BRepBuilderAPI_MakeEdge.hxx>
30
31 #include <TopAbs.hxx>
32 #include <TopExp.hxx>
33 #include <TopoDS.hxx>
34 #include <TopoDS_Edge.hxx>
35 #include <TopoDS_Shape.hxx>
36 #include <TopoDS_Vertex.hxx>
37
38 #include <gp_Pnt.hxx>
39 #include <Precision.hxx>
40 #include <Standard_ConstructionError.hxx>
41 #include <Standard_NullObject.hxx>
42
43 //=======================================================================
44 //function : GetID
45 //purpose  :
46 //=======================================================================
47 const Standard_GUID& GEOMImpl_LineDriver::GetID()
48 {
49   static Standard_GUID aLineDriver("FF1BBB06-5D14-4df2-980B-3A668264EA16");
50   return aLineDriver;
51 }
52
53
54 //=======================================================================
55 //function : GEOMImpl_LineDriver
56 //purpose  :
57 //=======================================================================
58 GEOMImpl_LineDriver::GEOMImpl_LineDriver()
59 {
60 }
61
62 //=======================================================================
63 //function : Execute
64 //purpose  :
65 //=======================================================================
66 Standard_Integer GEOMImpl_LineDriver::Execute(TFunction_Logbook& log) const
67 {
68   if (Label().IsNull())  return 0;
69   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
70
71   GEOMImpl_ILine aPI (aFunction);
72   Standard_Integer aType = aFunction->GetType();
73
74   TopoDS_Shape aShape;
75
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) return 0;
83     if (aShape1.IsSame(aShape2)) {
84       Standard_ConstructionError::Raise("The end points must be different");
85     }
86     gp_Pnt P1 = BRep_Tool::Pnt(TopoDS::Vertex(aShape1));
87     gp_Pnt P2 = BRep_Tool::Pnt(TopoDS::Vertex(aShape2));
88     if (P1.Distance(P2) < Precision::Confusion()) {
89       Standard_ConstructionError::Raise("The end points are too close");
90     }
91     aShape = BRepBuilderAPI_MakeEdge(P1, P2).Shape();
92
93   } else if (aType == LINE_PNT_DIR) {
94     Handle(GEOM_Function) aRefPnt = aPI.GetPoint1();
95     Handle(GEOM_Function) aRefDir = aPI.GetPoint2();
96     TopoDS_Shape aShape1 = aRefPnt->GetValue();
97     TopoDS_Shape aShape2 = aRefDir->GetValue();
98     if (aShape1.ShapeType() != TopAbs_VERTEX ||
99         aShape2.ShapeType() != TopAbs_EDGE) return 0;
100     if (aShape1.IsSame(aShape2)) {
101       Standard_ConstructionError::Raise("The end points must be different");
102     }
103     gp_Pnt P1 = BRep_Tool::Pnt(TopoDS::Vertex(aShape1));
104
105     TopoDS_Edge anE = TopoDS::Edge(aShape2);
106     TopoDS_Vertex V1, V2;
107     TopExp::Vertices(anE, V1, V2, Standard_True);
108     if (V1.IsNull() || V2.IsNull()) {
109       Standard_NullObject::Raise("Line creation aborted: vector is not defined");
110     }
111     gp_Pnt PV1 = BRep_Tool::Pnt(V1);
112     gp_Pnt PV2 = BRep_Tool::Pnt(V2);
113     if (PV1.Distance(PV2) < Precision::Confusion()) {
114       Standard_ConstructionError::Raise("Vector with null magnitude");
115     }
116
117     gp_Pnt P2 (P1.XYZ() + PV2.XYZ() - PV1.XYZ());
118     aShape = BRepBuilderAPI_MakeEdge(P1, P2).Shape();
119   } else {
120   }
121
122   if (aShape.IsNull()) return 0;
123   aShape.Infinite(true);
124
125   aFunction->SetValue(aShape);
126
127   log.SetTouched(Label());
128
129   return 1;
130 }
131
132
133 //=======================================================================
134 //function :  GEOMImpl_LineDriver_Type_
135 //purpose  :
136 //=======================================================================
137 Standard_EXPORT Handle_Standard_Type& GEOMImpl_LineDriver_Type_()
138 {
139
140   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
141   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
142   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
143   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
144   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
145   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
146
147
148   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
149   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_LineDriver",
150                                                          sizeof(GEOMImpl_LineDriver),
151                                                          1,
152                                                          (Standard_Address)_Ancestors,
153                                                          (Standard_Address)NULL);
154
155   return _aType;
156 }
157
158 //=======================================================================
159 //function : DownCast
160 //purpose  :
161 //=======================================================================
162 const Handle(GEOMImpl_LineDriver) Handle(GEOMImpl_LineDriver)::DownCast
163        (const Handle(Standard_Transient)& AnObject)
164 {
165   Handle(GEOMImpl_LineDriver) _anOtherObject;
166
167   if (!AnObject.IsNull()) {
168      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_LineDriver))) {
169        _anOtherObject = Handle(GEOMImpl_LineDriver)((Handle(GEOMImpl_LineDriver)&)AnObject);
170      }
171   }
172
173   return _anOtherObject ;
174 }