Salome HOME
Join modifications from branch BR_DEBUG_3_2_0b1
[modules/geom.git] / src / GEOMImpl / GEOMImpl_PrismDriver.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_PrismDriver.hxx>
24 #include <GEOMImpl_IPrism.hxx>
25 #include <GEOMImpl_Types.hxx>
26 #include <GEOM_Function.hxx>
27
28 #include <BRepPrimAPI_MakePrism.hxx>
29 #include <BRep_Tool.hxx>
30 #include <TopoDS.hxx>
31 #include <TopoDS_Shape.hxx>
32 #include <TopoDS_Edge.hxx>
33 #include <TopoDS_Vertex.hxx>
34 #include <TopAbs.hxx>
35 #include <TopExp.hxx>
36
37 #include <Precision.hxx>
38 #include <gp_Pnt.hxx>
39 #include <Standard_ConstructionError.hxx>
40
41 //=======================================================================
42 //function : GetID
43 //purpose  :
44 //======================================================================= 
45 const Standard_GUID& GEOMImpl_PrismDriver::GetID()
46 {
47   static Standard_GUID aPrismDriver("FF1BBB17-5D14-4df2-980B-3A668264EA16");
48   return aPrismDriver; 
49 }
50
51
52 //=======================================================================
53 //function : GEOMImpl_PrismDriver
54 //purpose  : 
55 //=======================================================================
56 GEOMImpl_PrismDriver::GEOMImpl_PrismDriver() 
57 {
58 }
59
60 //=======================================================================
61 //function : Execute
62 //purpose  :
63 //======================================================================= 
64 Standard_Integer GEOMImpl_PrismDriver::Execute(TFunction_Logbook& log) const
65 {
66   if (Label().IsNull()) return 0;    
67   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
68
69   GEOMImpl_IPrism aCI (aFunction);
70   Standard_Integer aType = aFunction->GetType();
71
72   TopoDS_Shape aShape;
73
74   if (aType == PRISM_BASE_VEC_H) {
75     Handle(GEOM_Function) aRefBase = aCI.GetBase();
76     Handle(GEOM_Function) aRefVector = aCI.GetVector();
77     TopoDS_Shape aShapeBase = aRefBase->GetValue();
78     TopoDS_Shape aShapeVec = aRefVector->GetValue();
79     if (aShapeVec.ShapeType() == TopAbs_EDGE) {
80       TopoDS_Edge anE = TopoDS::Edge(aShapeVec);
81       TopoDS_Vertex V1, V2;
82       TopExp::Vertices(anE, V1, V2, Standard_True);
83       if (!V1.IsNull() && !V2.IsNull()) {
84         gp_Vec aV (BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
85         if (Abs(aCI.GetH()) < Precision::Confusion()) {
86           Standard_ConstructionError::Raise("Absolute value of prism height is too small");
87         }
88         if (aV.Magnitude() > Precision::Confusion()) {
89           aV.Normalize();
90           aShape = BRepPrimAPI_MakePrism(aShapeBase, aV * aCI.GetH(), Standard_False).Shape();
91         }
92       }
93     }
94   } else if (aType == PRISM_BASE_TWO_PNT) {
95     Handle(GEOM_Function) aRefBase = aCI.GetBase();
96     Handle(GEOM_Function) aRefPnt1 = aCI.GetFirstPoint();
97     Handle(GEOM_Function) aRefPnt2 = aCI.GetLastPoint();
98     TopoDS_Shape aShapeBase = aRefBase->GetValue();
99     TopoDS_Shape aShapePnt1 = aRefPnt1->GetValue();
100     TopoDS_Shape aShapePnt2 = aRefPnt2->GetValue();
101     if (aShapePnt1.ShapeType() == TopAbs_VERTEX &&
102         aShapePnt2.ShapeType() == TopAbs_VERTEX) {
103       TopoDS_Vertex V1 = TopoDS::Vertex(aShapePnt1);
104       TopoDS_Vertex V2 = TopoDS::Vertex(aShapePnt2);
105       if (!V1.IsNull() && !V2.IsNull()) {
106         gp_Vec aV (BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
107         if (aV.Magnitude() > gp::Resolution()) {
108           aShape = BRepPrimAPI_MakePrism(aShapeBase, aV, Standard_False).Shape();
109         }
110       }
111     }
112   } else {
113   }
114
115   if (aShape.IsNull()) return 0;
116
117   aFunction->SetValue(aShape);
118
119   log.SetTouched(Label()); 
120
121   return 1;    
122 }
123
124
125 //=======================================================================
126 //function :  GEOMImpl_PrismDriver_Type_
127 //purpose  :
128 //======================================================================= 
129 Standard_EXPORT Handle_Standard_Type& GEOMImpl_PrismDriver_Type_()
130 {
131
132   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
133   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
134   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
135   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared); 
136   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
137   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
138  
139
140   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
141   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_PrismDriver",
142                                                          sizeof(GEOMImpl_PrismDriver),
143                                                          1,
144                                                          (Standard_Address)_Ancestors,
145                                                          (Standard_Address)NULL);
146
147   return _aType;
148 }
149
150 //=======================================================================
151 //function : DownCast
152 //purpose  :
153 //======================================================================= 
154 const Handle(GEOMImpl_PrismDriver) Handle(GEOMImpl_PrismDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
155 {
156   Handle(GEOMImpl_PrismDriver) _anOtherObject;
157
158   if (!AnObject.IsNull()) {
159      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_PrismDriver))) {
160        _anOtherObject = Handle(GEOMImpl_PrismDriver)((Handle(GEOMImpl_PrismDriver)&)AnObject);
161      }
162   }
163
164   return _anOtherObject ;
165 }