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