Salome HOME
Copyrights update
[modules/geom.git] / src / GEOM / GEOM_SubShapeDriver.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/
19 //
20
21 #include <Standard_Stream.hxx>
22
23 #include <GEOM_SubShapeDriver.hxx>
24 #include <GEOM_ISubShape.hxx>
25 #include <GEOM_Function.hxx>
26 #include <GEOM_Object.hxx>
27
28 #include <BRep_Tool.hxx>
29 #include <BRep_Builder.hxx>
30 #include <BRepGProp.hxx>
31
32 #include <TopAbs.hxx>
33 #include <TopExp.hxx>
34 #include <TopoDS.hxx>
35 #include <TopoDS_Shape.hxx>
36 #include <TopoDS_Edge.hxx>
37 #include <TopoDS_Wire.hxx>
38 #include <TopoDS_Solid.hxx>
39 #include <TopoDS_Compound.hxx>
40 #include <TopoDS_Iterator.hxx>
41 #include <TopExp_Explorer.hxx>
42 #include <TopTools_MapOfShape.hxx>
43 #include <TopTools_SequenceOfShape.hxx>
44 #include <TopTools_Array1OfShape.hxx>
45 #include <TopTools_IndexedMapOfShape.hxx>
46
47 #include <GProp_GProps.hxx>
48 #include <gp_Pnt.hxx>
49 #include <TColStd_Array1OfReal.hxx>
50
51 #include <Precision.hxx>
52 #include <Standard_NullObject.hxx>
53 #include <Standard_TypeMismatch.hxx>
54
55
56 //=======================================================================
57 //function : GEOM_SubShapeDriver
58 //purpose  :
59 //=======================================================================
60 GEOM_SubShapeDriver::GEOM_SubShapeDriver()
61 {
62 }
63
64 //=======================================================================
65 //function : Execute
66 //purpose  :
67 //=======================================================================
68 Standard_Integer GEOM_SubShapeDriver::Execute(TFunction_Logbook& log) const
69 {
70   if (Label().IsNull()) return 0;
71   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
72
73   GEOM_ISubShape aCI (aFunction);
74
75   TDF_Label aLabel = aCI.GetMainShape()->GetOwnerEntry();
76   if (aLabel.IsRoot()) return 0;
77   Handle(GEOM_Object) anObj = GEOM_Object::GetObject(aLabel);
78   if (anObj.IsNull()) return 0;
79   TopoDS_Shape aMainShape = anObj->GetValue();
80   if (aMainShape.IsNull()) return 0;
81
82   Handle(TColStd_HArray1OfInteger) anIndices = aCI.GetIndices();
83   if (anIndices.IsNull() || anIndices->Length() <= 0) return 0;
84
85   BRep_Builder B;
86   TopoDS_Compound aCompound;
87   TopoDS_Shape aShape;
88
89   if (anIndices->Length() == 1 && anIndices->Value(1) == -1) { //The empty subshape
90     B.MakeCompound(aCompound);
91     aShape = aCompound;
92
93   } else {
94
95     TopTools_IndexedMapOfShape aMapOfShapes;
96     TopExp::MapShapes(aMainShape, aMapOfShapes);
97
98     if (anIndices->Length() > 1) {
99
100       B.MakeCompound(aCompound);
101
102       for (int i = anIndices->Lower(); i<= anIndices->Upper(); i++) {
103         if (aMapOfShapes.Extent() < anIndices->Value(i))
104           Standard_NullObject::Raise("GEOM_SubShapeDriver::Execute: Index is out of range");
105         TopoDS_Shape aSubShape = aMapOfShapes.FindKey(anIndices->Value(i));
106         if (aSubShape.IsNull()) continue;
107         B.Add(aCompound,aSubShape);
108       }
109
110       aShape = aCompound;
111
112     } else {
113
114       if (aMapOfShapes.Extent() < anIndices->Value(1))
115         Standard_NullObject::Raise("GEOM_SubShapeDriver::Execute: Index is out of range");
116       aShape = aMapOfShapes.FindKey(anIndices->Value(1));
117     }
118   }
119
120   if (aShape.IsNull()) return 0;
121
122   aFunction->SetValue(aShape);
123
124   log.SetTouched(Label());
125
126   return 1;
127 }
128
129
130 //=======================================================================
131 //function :  GEOM_SubShapeDriver_Type_
132 //purpose  :
133 //=======================================================================
134 Standard_EXPORT Handle_Standard_Type& GEOM_SubShapeDriver_Type_()
135 {
136
137   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
138   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
139   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
140   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
141   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
142   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
143
144
145   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
146   static Handle_Standard_Type _aType = new Standard_Type("GEOM_SubShapeDriver",
147                                                          sizeof(GEOM_SubShapeDriver),
148                                                          1,
149                                                          (Standard_Address)_Ancestors,
150                                                          (Standard_Address)NULL);
151
152   return _aType;
153 }
154
155 //=======================================================================
156 //function : DownCast
157 //purpose  :
158 //=======================================================================
159 const Handle(GEOM_SubShapeDriver) Handle(GEOM_SubShapeDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
160 {
161   Handle(GEOM_SubShapeDriver) _anOtherObject;
162
163   if (!AnObject.IsNull()) {
164      if (AnObject->IsKind(STANDARD_TYPE(GEOM_SubShapeDriver))) {
165        _anOtherObject = Handle(GEOM_SubShapeDriver)((Handle(GEOM_SubShapeDriver)&)AnObject);
166      }
167   }
168
169   return _anOtherObject ;
170 }