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