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