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