Salome HOME
f7e08b58b4e7130458fcfc76c561ab1801d681b0
[modules/geom.git] / src / GEOM / GEOM_SubShapeDriver.cxx
1 // Copyright (C) 2007-2023  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, or (at your option) any later version.
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 "GEOM_SubShapeDriver.hxx"
24
25 #include "GEOM_ISubShape.hxx"
26 #include "GEOM_Function.hxx"
27 #include "GEOM_Object.hxx"
28
29 #include <BRep_Builder.hxx>
30 #include <TopExp.hxx>
31 #include <TopTools_IndexedMapOfShape.hxx>
32 #include <TopTools_MapOfShape.hxx>
33 #include <TopoDS_Compound.hxx>
34 #include <TopoDS_Shape.hxx>
35 #include <TDataStd_Integer.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(Handle(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 sub-shape
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   log->SetTouched(Label());
105
106   return 1;
107 }
108
109 //================================================================================
110 /*!
111  * \brief Returns a name of creation operation and names and values of creation parameters
112  */
113 //================================================================================
114
115 bool GEOM_SubShapeDriver::
116 GetCreationInformation(std::string&             theOperationName,
117                        std::vector<GEOM_Param>& theParams)
118 {
119   if (Label().IsNull()) return 0;
120   Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
121   GEOM_ISubShape aCI( function );
122
123   enum { GEOM_SUBSHAPE = 28, GEOM_GROUP = 37 };
124
125   TDF_Label aLabel = function->GetOwnerEntry();
126   if (aLabel.IsRoot()) return false;
127   Handle(GEOM_Object) obj = GEOM_Object::GetObject( aLabel );
128   if ( obj.IsNull() ) return false;
129
130   switch ( obj->GetType() ) {
131   case GEOM_SUBSHAPE:
132     theOperationName = "EXPLODE";
133     AddParam( theParams, "Main Object", aCI.GetMainShape() );
134     AddParam( theParams, "Index", aCI.GetIndices() );
135     break;
136   case GEOM_GROUP:
137   {
138     theOperationName = "GROUP_CREATE";
139     TopAbs_ShapeEnum type = TopAbs_SHAPE;
140     {
141       TDF_Label aFreeLabel = obj->GetFreeLabel();
142       Handle(TDataStd_Integer) anAttrib;
143       if(aFreeLabel.FindAttribute(TDataStd_Integer::GetID(), anAttrib))
144         type = (TopAbs_ShapeEnum) anAttrib->Get();
145     }
146     AddParam( theParams, "Shape Type", type );
147     AddParam( theParams, "Main Shape", aCI.GetMainShape() );
148     AddParam( theParams, "Indices", aCI.GetIndices() );
149     break;
150   }
151   default:
152     return false;
153   }
154   
155   return true;
156 }
157
158 IMPLEMENT_STANDARD_RTTIEXT (GEOM_SubShapeDriver,GEOM_BaseDriver)