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