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