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