Salome HOME
Merge with version on tag OCC-V2_1_0d
[modules/geom.git] / src / GEOM / GEOM_SubShapeDriver.cxx
1
2 using namespace std;
3 #include "GEOM_SubShapeDriver.hxx"
4 #include "GEOM_ISubShape.hxx"
5 #include "GEOM_Function.hxx"
6 #include "GEOM_Object.hxx"
7
8 #include <BRep_Tool.hxx>
9 #include <BRep_Builder.hxx>
10 #include <BRepGProp.hxx>
11
12 #include <TopAbs.hxx>
13 #include <TopExp.hxx>
14 #include <TopoDS.hxx>
15 #include <TopoDS_Shape.hxx>
16 #include <TopoDS_Edge.hxx>
17 #include <TopoDS_Wire.hxx>
18 #include <TopoDS_Solid.hxx>
19 #include <TopoDS_Compound.hxx>
20 #include <TopoDS_Iterator.hxx>
21 #include <TopExp_Explorer.hxx>
22 #include <TopTools_MapOfShape.hxx>
23 #include <TopTools_SequenceOfShape.hxx>
24 #include <TopTools_Array1OfShape.hxx>
25 #include <TopTools_IndexedMapOfShape.hxx>
26
27 #include <GProp_GProps.hxx>
28 #include <gp_Pnt.hxx>
29 #include <TColStd_Array1OfReal.hxx>
30
31 #include <Precision.hxx>
32 #include <Standard_NullObject.hxx>
33 #include <Standard_TypeMismatch.hxx>
34
35
36 //=======================================================================
37 //function : GEOM_SubShapeDriver
38 //purpose  :
39 //=======================================================================
40 GEOM_SubShapeDriver::GEOM_SubShapeDriver()
41 {
42 }
43
44 //=======================================================================
45 //function : Execute
46 //purpose  :
47 //=======================================================================
48 Standard_Integer GEOM_SubShapeDriver::Execute(TFunction_Logbook& log) const
49 {
50   if (Label().IsNull()) return 0;
51   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
52
53   GEOM_ISubShape aCI (aFunction);
54
55   TDF_Label aLabel = aCI.GetMainShape()->GetOwnerEntry();
56   if (aLabel.IsRoot()) return 0;
57   Handle(GEOM_Object) anObj = GEOM_Object::GetObject(aLabel);
58   if (anObj.IsNull()) return 0;
59   TopoDS_Shape aMainShape = anObj->GetValue();
60   if (aMainShape.IsNull()) return 0;
61
62   Handle(TColStd_HArray1OfInteger) anIndices = aCI.GetIndices();
63   if (anIndices.IsNull() || anIndices->Length() <= 0) return 0;
64
65   BRep_Builder B;
66   TopoDS_Compound aCompound;
67   TopoDS_Shape aShape;
68
69   if (anIndices->Length() == 1 && anIndices->Value(1) == -1) { //The empty subshape
70     B.MakeCompound(aCompound);
71     aShape = aCompound;
72
73   } else {
74
75     TopTools_IndexedMapOfShape aMapOfShapes;
76     TopExp::MapShapes(aMainShape, aMapOfShapes);
77
78     if (anIndices->Length() > 1) {
79
80       B.MakeCompound(aCompound);
81
82       for (int i = anIndices->Lower(); i<= anIndices->Upper(); i++) {
83         if (aMapOfShapes.Extent() < anIndices->Value(i))
84           Standard_NullObject::Raise("GEOM_SubShapeDriver::Execute: Index is out of range");
85         TopoDS_Shape aSubShape = aMapOfShapes.FindKey(anIndices->Value(i));
86         if (aSubShape.IsNull()) continue;
87         B.Add(aCompound,aSubShape);
88       }
89
90       aShape = aCompound;
91
92     } else {
93
94       if (aMapOfShapes.Extent() < anIndices->Value(1))
95         Standard_NullObject::Raise("GEOM_SubShapeDriver::Execute: Index is out of range");
96       aShape = aMapOfShapes.FindKey(anIndices->Value(1));
97     }
98   }
99
100   if (aShape.IsNull()) return 0;
101
102   aFunction->SetValue(aShape);
103
104   log.SetTouched(Label());
105
106   return 1;
107 }
108
109
110 //=======================================================================
111 //function :  GEOM_SubShapeDriver_Type_
112 //purpose  :
113 //=======================================================================
114 Standard_EXPORT Handle_Standard_Type& GEOM_SubShapeDriver_Type_()
115 {
116
117   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
118   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
119   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
120   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
121   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
122   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
123
124
125   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
126   static Handle_Standard_Type _aType = new Standard_Type("GEOM_SubShapeDriver",
127                                                          sizeof(GEOM_SubShapeDriver),
128                                                          1,
129                                                          (Standard_Address)_Ancestors,
130                                                          (Standard_Address)NULL);
131
132   return _aType;
133 }
134
135 //=======================================================================
136 //function : DownCast
137 //purpose  :
138 //=======================================================================
139 const Handle(GEOM_SubShapeDriver) Handle(GEOM_SubShapeDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
140 {
141   Handle(GEOM_SubShapeDriver) _anOtherObject;
142
143   if (!AnObject.IsNull()) {
144      if (AnObject->IsKind(STANDARD_TYPE(GEOM_SubShapeDriver))) {
145        _anOtherObject = Handle(GEOM_SubShapeDriver)((Handle(GEOM_SubShapeDriver)&)AnObject);
146      }
147   }
148
149   return _anOtherObject ;
150 }