Salome HOME
2ba5be581e6479102a1c92ac38bb816a88431618
[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 #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_Builder.hxx>
30
31 #include <TopExp.hxx>
32 #include <TopoDS_Shape.hxx>
33 #include <TopoDS_Compound.hxx>
34 #include <TopTools_MapOfShape.hxx>
35 #include <TopTools_IndexedMapOfShape.hxx>
36
37 #include <Standard_NullObject.hxx>
38
39 //=======================================================================
40 //function : GEOM_SubShapeDriver
41 //purpose  :
42 //=======================================================================
43 GEOM_SubShapeDriver::GEOM_SubShapeDriver()
44 {
45 }
46
47 //=======================================================================
48 //function : Execute
49 //purpose  :
50 //=======================================================================
51 Standard_Integer GEOM_SubShapeDriver::Execute(TFunction_Logbook& log) const
52 {
53   if (Label().IsNull()) return 0;
54   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
55
56   GEOM_ISubShape aCI (aFunction);
57
58   TDF_Label aLabel = aCI.GetMainShape()->GetOwnerEntry();
59   if (aLabel.IsRoot()) return 0;
60   Handle(GEOM_Object) anObj = GEOM_Object::GetObject(aLabel);
61   if (anObj.IsNull()) return 0;
62   TopoDS_Shape aMainShape = anObj->GetValue();
63   if (aMainShape.IsNull()) return 0;
64
65   Handle(TColStd_HArray1OfInteger) anIndices = aCI.GetIndices();
66   if (anIndices.IsNull() || anIndices->Length() <= 0) return 0;
67
68   BRep_Builder B;
69   TopoDS_Compound aCompound;
70   TopoDS_Shape aShape;
71
72   if (anIndices->Length() == 1 && anIndices->Value(1) == -1) { //The empty subshape
73     B.MakeCompound(aCompound);
74     aShape = aCompound;
75   }
76   else {
77     TopTools_IndexedMapOfShape aMapOfShapes;
78     TopExp::MapShapes(aMainShape, aMapOfShapes);
79
80     if (anIndices->Length() > 1) {
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       int i = anIndices->Lower();
95       if (aMapOfShapes.Extent() < anIndices->Value(i))
96         Standard_NullObject::Raise("GEOM_SubShapeDriver::Execute: Index is out of range");
97       aShape = aMapOfShapes.FindKey(anIndices->Value(i));
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 //function :  GEOM_SubShapeDriver_Type_
112 //purpose  :
113 //=======================================================================
114 Standard_EXPORT Handle_Standard_Type& GEOM_SubShapeDriver_Type_()
115 {
116
117   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
118   if (aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
119   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
120   if (aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
121   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
122   if (aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
123
124
125   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
126   static Handle_Standard_Type _aType = new Standard_Type("GEOM_SubShapeDriver",
127                                                          sizeof(GEOM_SubShapeDriver),
128                                                          1,
129                                                          (Standard_Address)_Ancestors,
130                                                          (Standard_Address)NULL);
131
132   return _aType;
133 }
134
135 //=======================================================================
136 //function : DownCast
137 //purpose  :
138 //=======================================================================
139 const Handle(GEOM_SubShapeDriver) Handle(GEOM_SubShapeDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
140 {
141   Handle(GEOM_SubShapeDriver) _anOtherObject;
142
143   if (!AnObject.IsNull()) {
144      if (AnObject->IsKind(STANDARD_TYPE(GEOM_SubShapeDriver))) {
145        _anOtherObject = Handle(GEOM_SubShapeDriver)((Handle(GEOM_SubShapeDriver)&)AnObject);
146      }
147   }
148
149   return _anOtherObject;
150 }