Salome HOME
Bug 0020374: Partition operation leads to missing volume. A fix by PKV.
[modules/geom.git] / src / GEOMImpl / GEOMImpl_PositionDriver.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_PositionDriver.hxx>
25 #include <GEOMImpl_IPosition.hxx>
26 #include <GEOMImpl_Types.hxx>
27 #include <GEOM_Function.hxx>
28
29 #include <GEOMImpl_IMeasureOperations.hxx>
30
31 // OCCT Includes
32 #include <BRepBuilderAPI_Transform.hxx>
33 #include <BRep_Tool.hxx>
34 #include <TopoDS.hxx>
35 #include <TopoDS_Shape.hxx>
36 #include <TopoDS_Vertex.hxx>
37 #include <TopAbs.hxx>
38 #include <TopExp.hxx>
39 #include <gp_Pln.hxx>
40 #include <Geom_Plane.hxx>
41 #include <GProp_GProps.hxx>
42 #include <BRepGProp.hxx>
43
44 #include <Precision.hxx>
45 #include <gp_Pnt.hxx>
46
47 //=======================================================================
48 //function : GetID
49 //purpose  :
50 //=======================================================================
51 const Standard_GUID& GEOMImpl_PositionDriver::GetID()
52 {
53   static Standard_GUID aPositionDriver("FF1BBB69-5D14-4df2-980B-3A668264EA16");
54   return aPositionDriver;
55 }
56
57
58 //=======================================================================
59 //function : GEOMImpl_PositionDriver
60 //purpose  :
61 //=======================================================================
62 GEOMImpl_PositionDriver::GEOMImpl_PositionDriver()
63 {
64 }
65
66 //=======================================================================
67 //function : Execute
68 //purpose  :
69 //=======================================================================
70 Standard_Integer GEOMImpl_PositionDriver::Execute(TFunction_Logbook& log) const
71 {
72   if (Label().IsNull()) return 0;
73   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
74
75   GEOMImpl_IPosition aCI (aFunction);
76   Standard_Integer aType = aFunction->GetType();
77
78   TopoDS_Shape aShape;
79
80   if (aType == POSITION_SHAPE || aType == POSITION_SHAPE_COPY) {
81     Handle(GEOM_Function) aRefShape = aCI.GetShape();
82     Handle(GEOM_Function) aRefStartLCS = aCI.GetStartLCS();
83     Handle(GEOM_Function) aRefEndLCS = aCI.GetEndLCS();
84
85     TopoDS_Shape aShapeBase = aRefShape->GetValue();
86     TopoDS_Shape aShapeStartLCS = aRefStartLCS->GetValue();
87     TopoDS_Shape aShapeEndLCS = aRefEndLCS->GetValue();
88
89     if (aShapeBase.IsNull() || aShapeStartLCS.IsNull() ||
90         aShapeEndLCS.IsNull() || aShapeEndLCS.ShapeType() != TopAbs_FACE)
91       return 0;
92
93     gp_Trsf aTrsf;
94     gp_Ax3 aStartAx3, aDestAx3;
95
96     // End LCS
97     aDestAx3 = GEOMImpl_IMeasureOperations::GetPosition(aShapeEndLCS);
98
99     // Start LCS
100     aStartAx3 = GEOMImpl_IMeasureOperations::GetPosition(aShapeStartLCS);
101
102     // Set transformation
103     aTrsf.SetDisplacement(aStartAx3, aDestAx3);
104
105     // Perform transformation
106     BRepBuilderAPI_Transform aBRepTrsf (aShapeBase, aTrsf, Standard_False);
107     aShape = aBRepTrsf.Shape();
108   }
109   else if (aType == POSITION_SHAPE_FROM_GLOBAL ||
110            aType == POSITION_SHAPE_FROM_GLOBAL_COPY) {
111     Handle(GEOM_Function) aRefShape = aCI.GetShape();
112     Handle(GEOM_Function) aRefEndLCS = aCI.GetEndLCS();
113
114     TopoDS_Shape aShapeBase = aRefShape->GetValue();
115     TopoDS_Shape aShapeEndLCS = aRefEndLCS->GetValue();
116
117     if (aShapeBase.IsNull() || aShapeEndLCS.IsNull() ||
118         aShapeEndLCS.ShapeType() != TopAbs_FACE)
119       return 0;
120
121     gp_Trsf aTrsf;
122     gp_Ax3 aStartAx3, aDestAx3;
123
124     // End LCS
125     aDestAx3 = GEOMImpl_IMeasureOperations::GetPosition(aShapeEndLCS);
126
127     // Set transformation
128     aTrsf.SetDisplacement(aStartAx3, aDestAx3);
129
130     // Perform transformation
131     BRepBuilderAPI_Transform aBRepTrsf (aShapeBase, aTrsf, Standard_False);
132     aShape = aBRepTrsf.Shape();
133   }
134   else
135     return 0;
136
137   if (aShape.IsNull()) return 0;
138
139   aFunction->SetValue(aShape);
140
141   log.SetTouched(Label());
142
143   return 1;
144 }
145
146
147 //=======================================================================
148 //function :  GEOMImpl_PositionDriver_Type_
149 //purpose  :
150 //=======================================================================
151 Standard_EXPORT Handle_Standard_Type& GEOMImpl_PositionDriver_Type_()
152 {
153
154   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
155   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
156   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
157   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
158   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
159   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
160
161
162   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
163   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_PositionDriver",
164                                                          sizeof(GEOMImpl_PositionDriver),
165                                                          1,
166                                                          (Standard_Address)_Ancestors,
167                                                          (Standard_Address)NULL);
168
169   return _aType;
170 }
171
172 //=======================================================================
173 //function : DownCast
174 //purpose  :
175 //=======================================================================
176 const Handle(GEOMImpl_PositionDriver) Handle(GEOMImpl_PositionDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
177 {
178   Handle(GEOMImpl_PositionDriver) _anOtherObject;
179
180   if (!AnObject.IsNull()) {
181      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_PositionDriver))) {
182        _anOtherObject = Handle(GEOMImpl_PositionDriver)((Handle(GEOMImpl_PositionDriver)&)AnObject);
183      }
184   }
185
186   return _anOtherObject ;
187 }