Salome HOME
Merge with version on tag OCC-V2_1_0d
[modules/geom.git] / src / GEOMImpl / GEOMImpl_LineDriver.cxx
1
2 using namespace std;
3 #include "GEOMImpl_LineDriver.hxx"
4 #include "GEOMImpl_ILine.hxx"
5 #include "GEOMImpl_Types.hxx"
6 #include "GEOM_Function.hxx"
7
8 #include <BRep_Tool.hxx>
9 #include <BRepBuilderAPI_MakeEdge.hxx>
10
11 #include <TopAbs.hxx>
12 #include <TopExp.hxx>
13 #include <TopoDS.hxx>
14 #include <TopoDS_Edge.hxx>
15 #include <TopoDS_Shape.hxx>
16 #include <TopoDS_Vertex.hxx>
17
18 #include <gp_Pnt.hxx>
19 #include <Precision.hxx>
20 #include <Standard_ConstructionError.hxx>
21 #include <Standard_NullObject.hxx>
22
23 //=======================================================================
24 //function : GetID
25 //purpose  :
26 //=======================================================================
27 const Standard_GUID& GEOMImpl_LineDriver::GetID()
28 {
29   static Standard_GUID aLineDriver("FF1BBB06-5D14-4df2-980B-3A668264EA16");
30   return aLineDriver;
31 }
32
33
34 //=======================================================================
35 //function : GEOMImpl_LineDriver
36 //purpose  :
37 //=======================================================================
38 GEOMImpl_LineDriver::GEOMImpl_LineDriver()
39 {
40 }
41
42 //=======================================================================
43 //function : Execute
44 //purpose  :
45 //=======================================================================
46 Standard_Integer GEOMImpl_LineDriver::Execute(TFunction_Logbook& log) const
47 {
48   if (Label().IsNull())  return 0;
49   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
50
51   GEOMImpl_ILine aPI (aFunction);
52   Standard_Integer aType = aFunction->GetType();
53
54   TopoDS_Shape aShape;
55
56   if (aType == LINE_TWO_PNT) {
57     Handle(GEOM_Function) aRefPnt1 = aPI.GetPoint1();
58     Handle(GEOM_Function) aRefPnt2 = aPI.GetPoint2();
59     TopoDS_Shape aShape1 = aRefPnt1->GetValue();
60     TopoDS_Shape aShape2 = aRefPnt2->GetValue();
61     if (aShape1.ShapeType() != TopAbs_VERTEX ||
62         aShape2.ShapeType() != TopAbs_VERTEX) return 0;
63     if (aShape1.IsSame(aShape2)) {
64       Standard_ConstructionError::Raise("The end points must be different");
65     }
66     gp_Pnt P1 = BRep_Tool::Pnt(TopoDS::Vertex(aShape1));
67     gp_Pnt P2 = BRep_Tool::Pnt(TopoDS::Vertex(aShape2));
68     if (P1.Distance(P2) < Precision::Confusion()) {
69       Standard_ConstructionError::Raise("The end points are too close");
70     }
71     aShape = BRepBuilderAPI_MakeEdge(P1, P2).Shape();
72
73   } else if (aType == LINE_PNT_DIR) {
74     Handle(GEOM_Function) aRefPnt = aPI.GetPoint1();
75     Handle(GEOM_Function) aRefDir = aPI.GetPoint2();
76     TopoDS_Shape aShape1 = aRefPnt->GetValue();
77     TopoDS_Shape aShape2 = aRefDir->GetValue();
78     if (aShape1.ShapeType() != TopAbs_VERTEX ||
79         aShape2.ShapeType() != TopAbs_EDGE) return 0;
80     if (aShape1.IsSame(aShape2)) {
81       Standard_ConstructionError::Raise("The end points must be different");
82     }
83     gp_Pnt P1 = BRep_Tool::Pnt(TopoDS::Vertex(aShape1));
84
85     TopoDS_Edge anE = TopoDS::Edge(aShape2);
86     TopoDS_Vertex V1, V2;
87     TopExp::Vertices(anE, V1, V2, Standard_True);
88     if (V1.IsNull() || V2.IsNull()) {
89       Standard_NullObject::Raise("Line creation aborted: vector is not defined");
90     }
91     gp_Pnt PV1 = BRep_Tool::Pnt(V1);
92     gp_Pnt PV2 = BRep_Tool::Pnt(V2);
93     if (PV1.Distance(PV2) < Precision::Confusion()) {
94       Standard_ConstructionError::Raise("Vector with null magnitude");
95     }
96
97     gp_Pnt P2 (P1.XYZ() + PV2.XYZ() - PV1.XYZ());
98     aShape = BRepBuilderAPI_MakeEdge(P1, P2).Shape();
99   } else {
100   }
101
102   if (aShape.IsNull()) return 0;
103   aShape.Infinite(true);
104
105   aFunction->SetValue(aShape);
106
107   log.SetTouched(Label());
108
109   return 1;
110 }
111
112
113 //=======================================================================
114 //function :  GEOMImpl_LineDriver_Type_
115 //purpose  :
116 //=======================================================================
117 Standard_EXPORT Handle_Standard_Type& GEOMImpl_LineDriver_Type_()
118 {
119
120   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
121   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
122   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
123   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
124   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
125   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
126
127
128   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
129   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_LineDriver",
130                                                          sizeof(GEOMImpl_LineDriver),
131                                                          1,
132                                                          (Standard_Address)_Ancestors,
133                                                          (Standard_Address)NULL);
134
135   return _aType;
136 }
137
138 //=======================================================================
139 //function : DownCast
140 //purpose  :
141 //=======================================================================
142 const Handle(GEOMImpl_LineDriver) Handle(GEOMImpl_LineDriver)::DownCast
143        (const Handle(Standard_Transient)& AnObject)
144 {
145   Handle(GEOMImpl_LineDriver) _anOtherObject;
146
147   if (!AnObject.IsNull()) {
148      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_LineDriver))) {
149        _anOtherObject = Handle(GEOMImpl_LineDriver)((Handle(GEOMImpl_LineDriver)&)AnObject);
150      }
151   }
152
153   return _anOtherObject ;
154 }