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