]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOMImpl/GEOMImpl_IGroupOperations.cxx
Salome HOME
Merge with version on tag OCC-V2_1_0d
[modules/geom.git] / src / GEOMImpl / GEOMImpl_IGroupOperations.cxx
1 using namespace std;
2
3 #include "GEOMImpl_IGroupOperations.hxx"
4
5 #include "utilities.h"
6 #include "OpUtil.hxx"
7 #include "Utils_ExceptHandlers.hxx"
8
9 #include <TFunction_DriverTable.hxx>
10 #include <TFunction_Driver.hxx>
11 #include <TFunction_Logbook.hxx>
12 #include <TDF_Tool.hxx>
13 #include <TDataStd_Integer.hxx>
14
15 #include "GEOM_Function.hxx"
16 #include "GEOMImpl_Types.hxx"
17 #include "GEOM_ISubShape.hxx"
18
19 #include <TopExp.hxx>
20 #include <TopTools_IndexedMapOfShape.hxx>
21 #include <TColStd_HArray1OfInteger.hxx>
22
23 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
24
25 //=============================================================================
26 /*!
27  *   constructor:
28  */
29 //=============================================================================
30 GEOMImpl_IGroupOperations::GEOMImpl_IGroupOperations (GEOM_Engine* theEngine, int theDocID) 
31 : GEOM_IOperations(theEngine, theDocID)
32 {
33   MESSAGE("GEOMImpl_IGroupOperations::GEOMImpl_IGroupOperations");
34 }
35
36 //=============================================================================
37 /*!
38  *  destructor
39  */
40 //=============================================================================
41 GEOMImpl_IGroupOperations::~GEOMImpl_IGroupOperations()
42 {
43   MESSAGE("GEOMImpl_IGroupOperations::~GEOMImpl_IGroupOperations");
44 }
45
46
47 //=============================================================================
48 /*!
49  *  CreateGroup
50  */
51 //=============================================================================
52 Handle(GEOM_Object) GEOMImpl_IGroupOperations::CreateGroup(Handle(GEOM_Object) theMainShape, TopAbs_ShapeEnum  theShapeType)
53 {
54   SetErrorCode(KO);
55
56   Handle(TColStd_HArray1OfInteger) anArray = new TColStd_HArray1OfInteger(1,1);
57   anArray->SetValue(1, -1);
58
59   //Add a new Fillet object  
60   Handle(GEOM_Object) aGroup = GetEngine()->AddSubShape(theMainShape, anArray);
61
62   //Set a GROUP type
63   aGroup->SetType(GEOM_GROUP);
64
65   //Set a sub shape type
66   TDF_Label aFreeLabel = aGroup->GetFreeLabel();
67   TDataStd_Integer::Set(aFreeLabel, (Standard_Integer)theShapeType);
68  
69   //Make a Python command 
70   TCollection_AsciiString anEntry, aDescr("");
71   TDF_Tool::Entry(aGroup->GetEntry(), anEntry);
72   aDescr = anEntry + " = IGroupOperations.CreateGroup(";
73   TDF_Tool::Entry(theMainShape->GetEntry(), anEntry);
74   aDescr += (anEntry+", ");
75   aDescr += (TCollection_AsciiString((int)theShapeType)+")");
76
77   Handle(GEOM_Function) aFunction = aGroup->GetFunction(1);
78   aFunction->SetDescription(aDescr);
79
80   SetErrorCode(OK);
81   return aGroup; 
82 }
83
84 //=============================================================================
85 /*!
86  *  AddObject
87  */
88 //=============================================================================
89 void GEOMImpl_IGroupOperations::AddObject(Handle(GEOM_Object) theGroup, int theSubShapeID)
90 {
91   SetErrorCode(KO);
92    if(theGroup.IsNull()) return;
93
94   Handle(GEOM_Function) aFunction = theGroup->GetFunction(1);
95   if(aFunction.IsNull()) return;
96   
97   GEOM_ISubShape aSSI(aFunction);
98   Handle(TColStd_HArray1OfInteger) aSeq = aSSI.GetIndices();
99   if(aSeq.IsNull()) return;
100   if(aSeq->Length() == 1 && aSeq->Value(1) == -1) {
101     aSeq->SetValue(1, theSubShapeID);
102   }
103   else {
104     Standard_Integer aLength = aSeq->Length();
105     Handle(TColStd_HArray1OfInteger) aNewSeq = new TColStd_HArray1OfInteger(1, aLength+1);
106     for(Standard_Integer i = 1; i<=aLength; i++) {
107       aNewSeq->SetValue(i, aSeq->Value(i));
108       if(aSeq->Value(i) == theSubShapeID) {
109         SetErrorCode(ALREADY_PRESENT);
110         return; //
111       }
112     }
113     aNewSeq->SetValue(aLength+1, theSubShapeID);
114     aSSI.SetIndices(aNewSeq);
115   }
116
117   SetErrorCode(OK);
118   return; 
119 }
120
121 //=============================================================================
122 /*!
123  *  RemoveObject
124  */
125 //=============================================================================
126 void GEOMImpl_IGroupOperations::RemoveObject(Handle(GEOM_Object) theGroup, int theSubShapeID)
127 {
128   SetErrorCode(KO);
129    if(theGroup.IsNull()) return;
130
131
132  Handle(GEOM_Function) aFunction = theGroup->GetFunction(1);
133   if(aFunction.IsNull()) return;
134   
135  
136   GEOM_ISubShape aSSI(aFunction);
137   Handle(TColStd_HArray1OfInteger) aSeq = aSSI.GetIndices();
138   if(aSeq.IsNull()) return;
139   if(aSeq->Length() == 1 && aSeq->Value(1) == -1) {
140     SetErrorCode(NOT_EXISTS);
141     return;
142   }
143   else {
144     Handle(TColStd_HArray1OfInteger) aNewSeq;
145     Standard_Integer aLength = aSeq->Length();
146     if(aLength == 1) {
147       if(aSeq->Value(1) != theSubShapeID) {
148         SetErrorCode(NOT_EXISTS);
149         return;         
150       }
151       aNewSeq = new TColStd_HArray1OfInteger(1,1);
152       aNewSeq->SetValue(1, -1);
153     }
154     else {
155       aNewSeq = new TColStd_HArray1OfInteger(1, aLength-1);
156       Standard_Boolean isFound = Standard_False;
157       for(Standard_Integer i = 1, k=1; i<=aLength; i++) {
158         if(i == aLength && !isFound) {
159           SetErrorCode(NOT_EXISTS);
160           return; 
161         }
162         if(aSeq->Value(i) == theSubShapeID) {
163           isFound = Standard_True;
164           continue;
165         }
166         aNewSeq->SetValue(k, aSeq->Value(i));
167         k++;
168       }
169
170       if(!isFound) {
171         SetErrorCode(NOT_EXISTS);
172         return; 
173       }
174     }
175
176     aSSI.SetIndices(aNewSeq);
177   }
178
179
180   SetErrorCode(OK);
181   return; 
182 }
183
184 //=============================================================================
185 /*!
186  *  GetType
187  */
188 //=============================================================================
189 TopAbs_ShapeEnum GEOMImpl_IGroupOperations::GetType(Handle(GEOM_Object) theGroup)
190 {
191   SetErrorCode(KO);
192
193   TDF_Label aFreeLabel = theGroup->GetFreeLabel();
194   Handle(TDataStd_Integer) anAttrib;
195   if(!aFreeLabel.FindAttribute(TDataStd_Integer::GetID(), anAttrib)) return TopAbs_SHAPE;
196  
197   SetErrorCode(OK);
198   return (TopAbs_ShapeEnum) anAttrib->Get(); 
199 }
200
201 //=============================================================================
202 /*!
203  *  GetMainShape
204  */
205 //=============================================================================
206 Handle(GEOM_Object) GEOMImpl_IGroupOperations::GetMainShape(Handle(GEOM_Object) theGroup)
207 {
208   SetErrorCode(KO);
209
210   if(theGroup.IsNull()) return NULL;
211
212   Handle(GEOM_Function) aFunction = theGroup->GetFunction(1);
213   if(aFunction.IsNull()) return NULL;
214   
215   GEOM_ISubShape aSSI(aFunction);
216   aFunction = aSSI.GetMainShape();
217   if(aFunction.IsNull()) return NULL;
218
219   TDF_Label aLabel = aFunction->GetOwnerEntry();
220   Handle(GEOM_Object) aMainShape = GEOM_Object::GetObject(aLabel);
221   if(aMainShape.IsNull()) return NULL;
222   
223   SetErrorCode(OK);
224   return aMainShape; 
225 }
226
227 //=============================================================================
228 /*!
229  *  GetObjects
230  */
231 //=============================================================================
232 Handle(TColStd_HArray1OfInteger) GEOMImpl_IGroupOperations::GetObjects(Handle(GEOM_Object) theGroup)
233 {
234   SetErrorCode(KO);
235   
236    if(theGroup.IsNull()) return NULL;
237
238   Handle(GEOM_Function) aFunction = theGroup->GetFunction(1);
239   if(aFunction.IsNull()) return NULL;
240   
241   GEOM_ISubShape aSSI(aFunction);
242   Handle(TColStd_HArray1OfInteger) aSeq = aSSI.GetIndices();
243   if(aSeq.IsNull()) return NULL;
244
245   if(aSeq->Length() == 1 && aSeq->Value(1) == -1) {
246     SetErrorCode(OK);
247     return NULL;
248   }
249
250   SetErrorCode(OK);
251   return aSeq;
252 }