Salome HOME
79f5fccdc2a432d2648b4b65d643859e3f063dfa
[modules/geom.git] / src / GEOMImpl / GEOMImpl_MeasureDriver.cxx
1
2 #include <Standard_Stream.hxx>
3
4 #include <GEOMImpl_MeasureDriver.hxx>
5 #include <GEOMImpl_IMeasure.hxx>
6 #include <GEOMImpl_IMeasureOperations.hxx>
7 #include <GEOMImpl_Types.hxx>
8 #include <GEOM_Function.hxx>
9
10 #include <BRep_Tool.hxx>
11 #include <BRepGProp.hxx>
12 #include <BRepBuilderAPI_MakeVertex.hxx>
13
14 #include <TopAbs.hxx>
15 #include <TopoDS.hxx>
16 #include <TopoDS_Shape.hxx>
17
18 #include <GProp_GProps.hxx>
19
20 #include <gp_Pnt.hxx>
21 #include <Precision.hxx>
22 #include <Standard_NullObject.hxx>
23
24 //=======================================================================
25 //function : GetID
26 //purpose  :
27 //======================================================================= 
28 const Standard_GUID& GEOMImpl_MeasureDriver::GetID()
29 {
30   static Standard_GUID aMeasureDriver("FF1BBB65-5D14-4df2-980B-3A668264EA16");
31   return aMeasureDriver; 
32 }
33
34
35 //=======================================================================
36 //function : GEOMImpl_MeasureDriver
37 //purpose  : 
38 //=======================================================================
39 GEOMImpl_MeasureDriver::GEOMImpl_MeasureDriver() 
40 {
41 }
42
43 //=======================================================================
44 //function : Execute
45 //purpose  :
46 //======================================================================= 
47 Standard_Integer GEOMImpl_MeasureDriver::Execute(TFunction_Logbook& log) const
48 {
49   if (Label().IsNull()) return 0;    
50   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
51
52   GEOMImpl_IMeasure aCI (aFunction);
53   Standard_Integer aType = aFunction->GetType();
54
55   TopoDS_Shape aShape;
56
57   if (aType == CDG_MEASURE) {
58     Handle(GEOM_Function) aRefBase = aCI.GetBase();
59     TopoDS_Shape aShapeBase = aRefBase->GetValue();
60     if (aShapeBase.IsNull()) {
61       Standard_NullObject::Raise("Shape for centre of mass calculation is null");
62     }
63
64     GProp_GProps aSystem;
65     gp_Pnt aCenterMass;
66
67     if (aShapeBase.ShapeType() == TopAbs_VERTEX) {
68       aCenterMass = BRep_Tool::Pnt(TopoDS::Vertex(aShapeBase));
69     } else if (aShapeBase.ShapeType() == TopAbs_EDGE || aShapeBase.ShapeType() == TopAbs_WIRE) {
70       BRepGProp::LinearProperties(aShapeBase, aSystem);
71       aCenterMass = aSystem.CentreOfMass();
72     } else if (aShapeBase.ShapeType() == TopAbs_FACE || aShapeBase.ShapeType() == TopAbs_SHELL) {
73       BRepGProp::SurfaceProperties(aShapeBase, aSystem);
74       aCenterMass = aSystem.CentreOfMass();
75     } else {
76       BRepGProp::VolumeProperties(aShapeBase, aSystem);
77       aCenterMass = aSystem.CentreOfMass();
78     }
79
80     aShape = BRepBuilderAPI_MakeVertex(aCenterMass).Shape();
81
82   } else {
83   }
84
85   if (aShape.IsNull()) return 0;
86
87   aFunction->SetValue(aShape);
88
89   log.SetTouched(Label()); 
90
91   return 1;    
92 }
93
94
95 //=======================================================================
96 //function :  GEOMImpl_MeasureDriver_Type_
97 //purpose  :
98 //======================================================================= 
99 Standard_EXPORT Handle_Standard_Type& GEOMImpl_MeasureDriver_Type_()
100 {
101
102   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
103   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
104   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
105   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared); 
106   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
107   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
108  
109
110   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
111   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_MeasureDriver",
112                                                          sizeof(GEOMImpl_MeasureDriver),
113                                                          1,
114                                                          (Standard_Address)_Ancestors,
115                                                          (Standard_Address)NULL);
116
117   return _aType;
118 }
119
120 //=======================================================================
121 //function : DownCast
122 //purpose  :
123 //======================================================================= 
124 const Handle(GEOMImpl_MeasureDriver) Handle(GEOMImpl_MeasureDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
125 {
126   Handle(GEOMImpl_MeasureDriver) _anOtherObject;
127
128   if (!AnObject.IsNull()) {
129      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_MeasureDriver))) {
130        _anOtherObject = Handle(GEOMImpl_MeasureDriver)((Handle(GEOMImpl_MeasureDriver)&)AnObject);
131      }
132   }
133
134   return _anOtherObject ;
135 }