Salome HOME
Update copyright notes (for 2010)
[modules/geom.git] / src / GEOMImpl / GEOMImpl_PrismDriver.cxx
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include <Standard_Stream.hxx>
24
25 #include <GEOMImpl_PrismDriver.hxx>
26
27 #include <GEOMImpl_IShapesOperations.hxx>
28 #include <GEOMImpl_IPrism.hxx>
29 #include <GEOMImpl_Types.hxx>
30 #include <GEOM_Function.hxx>
31
32 #include <BRepPrimAPI_MakePrism.hxx>
33 #include <BRepBuilderAPI_Transform.hxx>
34 #include <BRep_Tool.hxx>
35 #include <TopoDS.hxx>
36 #include <TopoDS_Shape.hxx>
37 #include <TopoDS_Edge.hxx>
38 #include <TopoDS_Vertex.hxx>
39 #include <TopAbs.hxx>
40 #include <TopExp.hxx>
41
42 #include <Precision.hxx>
43 #include <gp_Pnt.hxx>
44 #include <gp_Trsf.hxx>
45 #include <gp_Vec.hxx>
46 #include <Standard_ConstructionError.hxx>
47
48 //=======================================================================
49 //function : GetID
50 //purpose  :
51 //======================================================================= 
52 const Standard_GUID& GEOMImpl_PrismDriver::GetID()
53 {
54   static Standard_GUID aPrismDriver("FF1BBB17-5D14-4df2-980B-3A668264EA16");
55   return aPrismDriver; 
56 }
57
58
59 //=======================================================================
60 //function : GEOMImpl_PrismDriver
61 //purpose  : 
62 //=======================================================================
63 GEOMImpl_PrismDriver::GEOMImpl_PrismDriver() 
64 {
65 }
66
67 //=======================================================================
68 //function : Execute
69 //purpose  :
70 //======================================================================= 
71 Standard_Integer GEOMImpl_PrismDriver::Execute(TFunction_Logbook& log) const
72 {
73   if (Label().IsNull()) return 0;    
74   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
75
76   GEOMImpl_IPrism aCI (aFunction);
77   Standard_Integer aType = aFunction->GetType();
78
79   TopoDS_Shape aShape;
80
81   if (aType == PRISM_BASE_VEC_H || aType == PRISM_BASE_VEC_H_2WAYS) {
82     Handle(GEOM_Function) aRefBase = aCI.GetBase();
83     Handle(GEOM_Function) aRefVector = aCI.GetVector();
84     TopoDS_Shape aShapeBase = aRefBase->GetValue();
85     TopoDS_Shape aShapeVec = aRefVector->GetValue();
86     if (aShapeVec.ShapeType() == TopAbs_EDGE) {
87       TopoDS_Edge anE = TopoDS::Edge(aShapeVec);
88       TopoDS_Vertex V1, V2;
89       TopExp::Vertices(anE, V1, V2, Standard_True);
90       if (!V1.IsNull() && !V2.IsNull()) {
91         gp_Vec aV (BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
92         if (Abs(aCI.GetH()) < Precision::Confusion()) {
93           Standard_ConstructionError::Raise("Absolute value of prism height is too small");
94         }
95         if (aV.Magnitude() > Precision::Confusion()) {
96           aV.Normalize();
97           if (aType == PRISM_BASE_VEC_H_2WAYS) {
98             gp_Trsf aTrsf;
99             aTrsf.SetTranslation( (-aV) * aCI.GetH() );
100             BRepBuilderAPI_Transform aTransformation(aShapeBase, aTrsf, Standard_False);
101             aShapeBase = aTransformation.Shape();
102             aCI.SetH( aCI.GetH()*2 );
103           }
104           aShape = BRepPrimAPI_MakePrism(aShapeBase, aV * aCI.GetH(), Standard_False).Shape();
105         }
106       }
107     }
108   } else if (aType == PRISM_BASE_TWO_PNT || aType == PRISM_BASE_TWO_PNT_2WAYS) {
109     Handle(GEOM_Function) aRefBase = aCI.GetBase();
110     Handle(GEOM_Function) aRefPnt1 = aCI.GetFirstPoint();
111     Handle(GEOM_Function) aRefPnt2 = aCI.GetLastPoint();
112     TopoDS_Shape aShapeBase = aRefBase->GetValue();
113     TopoDS_Shape aShapePnt1 = aRefPnt1->GetValue();
114     TopoDS_Shape aShapePnt2 = aRefPnt2->GetValue();
115     if (aShapePnt1.ShapeType() == TopAbs_VERTEX &&
116         aShapePnt2.ShapeType() == TopAbs_VERTEX) {
117       TopoDS_Vertex V1 = TopoDS::Vertex(aShapePnt1);
118       TopoDS_Vertex V2 = TopoDS::Vertex(aShapePnt2);
119       if (!V1.IsNull() && !V2.IsNull()) {
120         gp_Vec aV (BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
121         if (aV.Magnitude() > gp::Resolution()) {
122           if (aType == PRISM_BASE_TWO_PNT_2WAYS)
123             {
124               gp_Trsf aTrsf;
125               aTrsf.SetTranslation(-aV);
126               BRepBuilderAPI_Transform aTransformation(aShapeBase, aTrsf, Standard_False);
127               aShapeBase = aTransformation.Shape();
128               aV = aV * 2;
129             }
130           aShape = BRepPrimAPI_MakePrism(aShapeBase, aV, Standard_False).Shape();
131         }
132       }
133     }
134   } else if (aType == PRISM_BASE_DXDYDZ || aType == PRISM_BASE_DXDYDZ_2WAYS) {
135     Handle(GEOM_Function) aRefBase = aCI.GetBase();
136     TopoDS_Shape aShapeBase = aRefBase->GetValue();
137     gp_Vec aV (aCI.GetDX(), aCI.GetDY(), aCI.GetDZ());
138     if (aV.Magnitude() > gp::Resolution()) {
139       if (aType == PRISM_BASE_DXDYDZ_2WAYS)
140         {
141           gp_Trsf aTrsf;
142           aTrsf.SetTranslation(-aV);
143           BRepBuilderAPI_Transform aTransformation(aShapeBase, aTrsf, Standard_False);
144           aShapeBase = aTransformation.Shape();
145           aV = aV * 2;
146         }
147       aShape = BRepPrimAPI_MakePrism(aShapeBase, aV, Standard_False).Shape();
148     }
149   }
150
151   if (aShape.IsNull()) return 0;
152
153   TopoDS_Shape aRes = GEOMImpl_IShapesOperations::CompsolidToCompound(aShape);
154   aFunction->SetValue(aRes);
155
156   log.SetTouched(Label()); 
157
158   return 1;    
159 }
160
161
162 //=======================================================================
163 //function :  GEOMImpl_PrismDriver_Type_
164 //purpose  :
165 //======================================================================= 
166 Standard_EXPORT Handle_Standard_Type& GEOMImpl_PrismDriver_Type_()
167 {
168
169   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
170   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
171   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
172   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared); 
173   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
174   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
175  
176
177   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
178   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_PrismDriver",
179                                                          sizeof(GEOMImpl_PrismDriver),
180                                                          1,
181                                                          (Standard_Address)_Ancestors,
182                                                          (Standard_Address)NULL);
183
184   return _aType;
185 }
186
187 //=======================================================================
188 //function : DownCast
189 //purpose  :
190 //======================================================================= 
191 const Handle(GEOMImpl_PrismDriver) Handle(GEOMImpl_PrismDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
192 {
193   Handle(GEOMImpl_PrismDriver) _anOtherObject;
194
195   if (!AnObject.IsNull()) {
196      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_PrismDriver))) {
197        _anOtherObject = Handle(GEOMImpl_PrismDriver)((Handle(GEOMImpl_PrismDriver)&)AnObject);
198      }
199   }
200
201   return _anOtherObject ;
202 }