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