Salome HOME
Merge from V6_main 01/04/2013
[modules/geom.git] / src / GEOM / GEOM_SubShapeDriver.cxx
1 // Copyright (C) 2007-2013  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_Builder.hxx>
31
32 #include <TopExp.hxx>
33 #include <TopoDS_Shape.hxx>
34 #include <TopoDS_Compound.hxx>
35 #include <TopTools_MapOfShape.hxx>
36 #include <TopTools_IndexedMapOfShape.hxx>
37
38 #include <Standard_NullObject.hxx>
39
40 //=======================================================================
41 //function : GEOM_SubShapeDriver
42 //purpose  :
43 //=======================================================================
44 GEOM_SubShapeDriver::GEOM_SubShapeDriver()
45 {
46 }
47
48 //=======================================================================
49 //function : Execute
50 //purpose  :
51 //=======================================================================
52 Standard_Integer GEOM_SubShapeDriver::Execute(TFunction_Logbook& log) const
53 {
54   if (Label().IsNull()) return 0;
55   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
56
57   GEOM_ISubShape aCI (aFunction);
58
59   TDF_Label aLabel = aCI.GetMainShape()->GetOwnerEntry();
60   if (aLabel.IsRoot()) return 0;
61   Handle(GEOM_Object) anObj = GEOM_Object::GetObject(aLabel);
62   if (anObj.IsNull()) return 0;
63   TopoDS_Shape aMainShape = anObj->GetValue();
64   if (aMainShape.IsNull()) return 0;
65
66   Handle(TColStd_HArray1OfInteger) anIndices = aCI.GetIndices();
67   if (anIndices.IsNull() || anIndices->Length() <= 0) return 0;
68
69   BRep_Builder B;
70   TopoDS_Compound aCompound;
71   TopoDS_Shape aShape;
72
73   if (anIndices->Length() == 1 && anIndices->Value(1) == -1) { //The empty sub-shape
74     B.MakeCompound(aCompound);
75     aShape = aCompound;
76   }
77   else {
78     TopTools_IndexedMapOfShape aMapOfShapes;
79     TopExp::MapShapes(aMainShape, aMapOfShapes);
80
81     if (anIndices->Length() > 1) {
82       B.MakeCompound(aCompound);
83
84       for (int i = anIndices->Lower(); i <= anIndices->Upper(); i++) {
85         if (aMapOfShapes.Extent() < anIndices->Value(i))
86           Standard_NullObject::Raise("GEOM_SubShapeDriver::Execute: Index is out of range");
87         TopoDS_Shape aSubShape = aMapOfShapes.FindKey(anIndices->Value(i));
88         if (aSubShape.IsNull()) continue;
89         B.Add(aCompound,aSubShape);
90       }
91
92       aShape = aCompound;
93     }
94     else {
95       int i = anIndices->Lower();
96       if (aMapOfShapes.Extent() < anIndices->Value(i))
97         Standard_NullObject::Raise("GEOM_SubShapeDriver::Execute: Index is out of range");
98       aShape = aMapOfShapes.FindKey(anIndices->Value(i));
99     }
100   }
101
102   if (aShape.IsNull()) return 0;
103
104   aFunction->SetValue(aShape);
105
106   log.SetTouched(Label());
107
108   return 1;
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 }