Salome HOME
PAL17233: Projection 2D doesn't work (bis)
[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/ or email : webmaster.salome@opencascade.com
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) {
83       Standard_ConstructionError::Raise("Wrong arguments: two points must be given");
84     }
85     if (aShape1.IsSame(aShape2)) {
86       Standard_ConstructionError::Raise("The end points must be different");
87     }
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");
92     }
93     aShape = BRepBuilderAPI_MakeEdge(P1, P2).Shape();
94
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");
102     }
103     if (aShape2.ShapeType() != TopAbs_EDGE) {
104       Standard_ConstructionError::Raise("Wrong second argument: must be vector");
105     }
106     if (aShape1.IsSame(aShape2)) {
107       Standard_ConstructionError::Raise("The end points must be different");
108     }
109     gp_Pnt P1 = BRep_Tool::Pnt(TopoDS::Vertex(aShape1));
110
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");
116     }
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");
121     }
122
123     gp_Pnt P2 (P1.XYZ() + PV2.XYZ() - PV1.XYZ());
124     aShape = BRepBuilderAPI_MakeEdge(P1, P2).Shape();
125   } else {
126   }
127
128   if (aShape.IsNull()) return 0;
129   aShape.Infinite(true);
130
131   aFunction->SetValue(aShape);
132
133   log.SetTouched(Label());
134
135   return 1;
136 }
137
138
139 //=======================================================================
140 //function :  GEOMImpl_LineDriver_Type_
141 //purpose  :
142 //=======================================================================
143 Standard_EXPORT Handle_Standard_Type& GEOMImpl_LineDriver_Type_()
144 {
145
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);
152
153
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),
157                                                          1,
158                                                          (Standard_Address)_Ancestors,
159                                                          (Standard_Address)NULL);
160
161   return _aType;
162 }
163
164 //=======================================================================
165 //function : DownCast
166 //purpose  :
167 //=======================================================================
168 const Handle(GEOMImpl_LineDriver) Handle(GEOMImpl_LineDriver)::DownCast
169        (const Handle(Standard_Transient)& AnObject)
170 {
171   Handle(GEOMImpl_LineDriver) _anOtherObject;
172
173   if (!AnObject.IsNull()) {
174      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_LineDriver))) {
175        _anOtherObject = Handle(GEOMImpl_LineDriver)((Handle(GEOMImpl_LineDriver)&)AnObject);
176      }
177   }
178
179   return _anOtherObject ;
180 }