Salome HOME
PAL12867: EDF214 GEOM DifferenceList does not remove the elements of the list. Algori...
[modules/geom.git] / src / GEOMImpl / GEOMImpl_IGroupOperations.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 #include <Standard_Stream.hxx>
21
22 #include <GEOMImpl_IGroupOperations.hxx>
23
24 #include <GEOMImpl_Types.hxx>
25
26 #include <GEOM_Function.hxx>
27 #include <GEOM_ISubShape.hxx>
28 #include <GEOM_PythonDump.hxx>
29
30 #include "utilities.h"
31 #include <OpUtil.hxx>
32 #include <Utils_ExceptHandlers.hxx>
33
34 #include <TFunction_DriverTable.hxx>
35 #include <TFunction_Driver.hxx>
36 #include <TFunction_Logbook.hxx>
37 #include <TDF_Tool.hxx>
38 #include <TDataStd_Integer.hxx>
39
40 #include <TopExp.hxx>
41 #include <TopExp_Explorer.hxx>
42 #include <TopTools_IndexedMapOfShape.hxx>
43
44 #include <TColStd_HArray1OfInteger.hxx>
45 #include <TColStd_MapOfInteger.hxx>
46 #include <TColStd_ListOfInteger.hxx>
47 #include <TColStd_ListIteratorOfListOfInteger.hxx>
48
49 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
50
51 //=============================================================================
52 /*!
53  *   constructor:
54  */
55 //=============================================================================
56 GEOMImpl_IGroupOperations::GEOMImpl_IGroupOperations (GEOM_Engine* theEngine, int theDocID) 
57 : GEOM_IOperations(theEngine, theDocID)
58 {
59   MESSAGE("GEOMImpl_IGroupOperations::GEOMImpl_IGroupOperations");
60 }
61
62 //=============================================================================
63 /*!
64  *  destructor
65  */
66 //=============================================================================
67 GEOMImpl_IGroupOperations::~GEOMImpl_IGroupOperations()
68 {
69   MESSAGE("GEOMImpl_IGroupOperations::~GEOMImpl_IGroupOperations");
70 }
71
72
73 //=============================================================================
74 /*!
75  *  CreateGroup
76  */
77 //=============================================================================
78 Handle(GEOM_Object) GEOMImpl_IGroupOperations::CreateGroup
79        (Handle(GEOM_Object) theMainShape, TopAbs_ShapeEnum theShapeType)
80 {
81   SetErrorCode(KO);
82
83   Handle(TColStd_HArray1OfInteger) anArray = new TColStd_HArray1OfInteger(1,1);
84   anArray->SetValue(1, -1);
85
86   //Add a new Fillet object
87   Handle(GEOM_Object) aGroup = GetEngine()->AddSubShape(theMainShape, anArray);
88
89   //Set a GROUP type
90   aGroup->SetType(GEOM_GROUP);
91
92   //Set a sub shape type
93   TDF_Label aFreeLabel = aGroup->GetFreeLabel();
94   TDataStd_Integer::Set(aFreeLabel, (Standard_Integer)theShapeType);
95
96   //Make a Python command
97   Handle(GEOM_Function) aFunction = aGroup->GetFunction(1);
98
99   GEOM::TPythonDump(aFunction) << aGroup
100     << " = geompy.CreateGroup(" << theMainShape << ", " << theShapeType << ")";
101
102   SetErrorCode(OK);
103   return aGroup;
104 }
105
106 //=============================================================================
107 /*!
108  *  AddObject
109  */
110 //=============================================================================
111 void GEOMImpl_IGroupOperations::AddObject(Handle(GEOM_Object) theGroup, int theSubShapeID)
112 {
113   SetErrorCode(KO);
114   if(theGroup.IsNull()) return;
115
116   Handle(GEOM_Function) aFunction = theGroup->GetFunction(1);
117   if(aFunction.IsNull()) return;
118
119   GEOM_ISubShape aSSI (aFunction);
120
121   // Check sub-shape index validity
122   TDF_Label aLabel = aSSI.GetMainShape()->GetOwnerEntry();
123   if (aLabel.IsRoot()) return;
124   Handle(GEOM_Object) anObj = GEOM_Object::GetObject(aLabel);
125   if (anObj.IsNull()) return;
126   TopoDS_Shape aMainShape = anObj->GetValue();
127   if (aMainShape.IsNull()) return;
128
129   TopTools_IndexedMapOfShape aMapOfShapes;
130   TopExp::MapShapes(aMainShape, aMapOfShapes);
131
132   if (theSubShapeID < 1 || aMapOfShapes.Extent() < theSubShapeID) {
133     SetErrorCode("Invalid sub-shape index: out of range");
134     return;
135   }
136
137   // Add sub-shape index
138   Handle(TColStd_HArray1OfInteger) aSeq = aSSI.GetIndices();
139   if(aSeq.IsNull()) return;
140   if(aSeq->Length() == 1 && aSeq->Value(1) == -1) {
141     aSeq->SetValue(1, theSubShapeID);
142   }
143   else {
144     Standard_Integer aLength = aSeq->Length();
145     Handle(TColStd_HArray1OfInteger) aNewSeq = new TColStd_HArray1OfInteger(1, aLength+1);
146     for(Standard_Integer i = 1; i<=aLength; i++) {
147       aNewSeq->SetValue(i, aSeq->Value(i));
148       if(aSeq->Value(i) == theSubShapeID) {
149         SetErrorCode(ALREADY_PRESENT);
150         return; //
151       }
152     }
153     aNewSeq->SetValue(aLength+1, theSubShapeID);
154     aSSI.SetIndices(aNewSeq);
155   }
156
157   //Make a Python command
158   GEOM::TPythonDump(aFunction, /*append=*/true)
159     << "geompy.AddObject(" << theGroup << ", " << theSubShapeID << ")";
160
161   SetErrorCode(OK);
162   return;
163 }
164
165 //=============================================================================
166 /*!
167  *  RemoveObject
168  */
169 //=============================================================================
170 void GEOMImpl_IGroupOperations::RemoveObject (Handle(GEOM_Object) theGroup, int theSubShapeID)
171 {
172   SetErrorCode(KO);
173   if(theGroup.IsNull()) return;
174
175   Handle(GEOM_Function) aFunction = theGroup->GetFunction(1);
176   if(aFunction.IsNull()) return;
177
178   GEOM_ISubShape aSSI(aFunction);
179   Handle(TColStd_HArray1OfInteger) aSeq = aSSI.GetIndices();
180   if(aSeq.IsNull()) return;
181   if(aSeq->Length() == 1 && aSeq->Value(1) == -1) {
182     SetErrorCode(NOT_EXISTS);
183     return;
184   }
185   else {
186     Handle(TColStd_HArray1OfInteger) aNewSeq;
187     Standard_Integer aLength = aSeq->Length();
188     if(aLength == 1) {
189       if(aSeq->Value(1) != theSubShapeID) {
190         SetErrorCode(NOT_EXISTS);
191         return;         
192       }
193       aNewSeq = new TColStd_HArray1OfInteger(1,1);
194       aNewSeq->SetValue(1, -1);
195     }
196     else {
197       aNewSeq = new TColStd_HArray1OfInteger(1, aLength-1);
198       Standard_Boolean isFound = Standard_False;
199       for(Standard_Integer i = 1, k=1; i<=aLength; i++) {
200         if(i == aLength && !isFound) {
201           SetErrorCode(NOT_EXISTS);
202           return; 
203         }
204         if(aSeq->Value(i) == theSubShapeID) {
205           isFound = Standard_True;
206           continue;
207         }
208         aNewSeq->SetValue(k, aSeq->Value(i));
209         k++;
210       }
211
212       if(!isFound) {
213         SetErrorCode(NOT_EXISTS);
214         return; 
215       }
216     }
217
218     aSSI.SetIndices(aNewSeq);
219   }
220
221   //Make a Python command 
222   GEOM::TPythonDump(aFunction, /*append=*/true)
223     << "geompy.RemoveObject(" << theGroup << ", " << theSubShapeID << ")";
224
225   SetErrorCode(OK);
226   return; 
227 }
228
229 //=============================================================================
230 /*!
231  *  UnionList
232  */
233 //=============================================================================
234 void GEOMImpl_IGroupOperations::UnionList (Handle(GEOM_Object) theGroup,
235                                            const Handle(TColStd_HSequenceOfTransient)& theSubShapes)
236 {
237   SetErrorCode(KO);
238   if (theGroup.IsNull()) return;
239
240   Standard_Integer aLen = theSubShapes->Length();
241   if (aLen < 1) {
242     SetErrorCode("The list is empty");
243     return;
244   }
245
246   Handle(GEOM_Function) aFunction = theGroup->GetFunction(1);
247   if (aFunction.IsNull()) return;
248
249   GEOM_ISubShape aSSI (aFunction);
250
251   // New contents of the group
252   TColStd_ListOfInteger aNewIDs;
253   TColStd_MapOfInteger mapIDs;
254
255   // Add current IDs to the list
256   Handle(TColStd_HArray1OfInteger) aSeq = aSSI.GetIndices();
257   if (aSeq.IsNull()) return;
258   Standard_Integer val_j, aLength = aSeq->Length();
259
260   for (Standard_Integer j = 1; j <= aLength; j++) {
261     val_j = aSeq->Value(j);
262     if (val_j > 0 && mapIDs.Add(val_j)) {
263       aNewIDs.Append(val_j);
264     }
265   }
266
267   // Get Main Shape
268   Handle(GEOM_Function) aMainShapeFunc = aSSI.GetMainShape();
269   if (aMainShapeFunc.IsNull()) return;
270   TDF_Label aLabel = aMainShapeFunc->GetOwnerEntry();
271   if (aLabel.IsRoot()) return;
272   Handle(GEOM_Object) aMainObj = GEOM_Object::GetObject(aLabel);
273   if (aMainObj.IsNull()) return;
274   TopoDS_Shape aMainShape = aMainObj->GetValue();
275   if (aMainShape.IsNull()) return;
276
277   TopTools_IndexedMapOfShape mapIndices;
278   TopExp::MapShapes(aMainShape, mapIndices);
279
280   // Get group type
281   TopAbs_ShapeEnum aType = GetType(theGroup);
282
283   // Get IDs of sub-shapes to add
284   Standard_Integer i, new_id;
285   for (i = 1; i <= aLen; i++) {
286     Handle(GEOM_Object) anObj_i = Handle(GEOM_Object)::DownCast(theSubShapes->Value(i));
287
288     TopoDS_Shape aShape_i = anObj_i->GetValue();
289     TopAbs_ShapeEnum aType_i = aShape_i.ShapeType();
290
291     // 1. If aShape_i is sub-shape of aMainShape - add it
292     if (anObj_i->IsMainShape()) {
293       if (aType_i != aType && aType != TopAbs_SHAPE && aType != TopAbs_COMPOUND) {
294         SetErrorCode("Operation aborted: one of given objects has a wrong type");
295         return;
296       }
297       if (!mapIndices.Contains(aShape_i)) {
298         SetErrorCode("Operation aborted: not a sub-shape given");
299         return;
300       }
301       new_id = mapIndices.FindIndex(aShape_i);
302       if (mapIDs.Add(new_id)) {
303         aNewIDs.Append(new_id);
304       }
305     }
306     // 2. If type of group is not defined - add all sub-shapes of aShape_i
307     else if (aType == TopAbs_SHAPE || aType == TopAbs_COMPOUND) {
308       TopTools_IndexedMapOfShape mapIndices_i;
309       TopExp::MapShapes(aShape_i, mapIndices_i);
310       Standard_Integer ii = 1, nbSubSh = mapIndices_i.Extent();
311       Standard_Boolean someGood = Standard_False;
312       for (; ii <= nbSubSh; ii++) {
313         TopoDS_Shape aSubShape_i = mapIndices_i.FindKey(ii);
314         if (mapIndices.Contains(aSubShape_i)) {
315           someGood = Standard_True;
316           new_id = mapIndices.FindIndex(aSubShape_i);
317           if (mapIDs.Add(new_id)) {
318             aNewIDs.Append(new_id);
319           }
320         }
321       }
322       if (!someGood) {
323         SetErrorCode("Operation aborted: not a sub-shape given");
324         return;
325       }
326     }
327     // 3. If type of group is defined - add all sub-shapes of aShape_i of that type
328     else {
329       TopExp_Explorer aSubShapes_i (aShape_i, aType);
330       for (; aSubShapes_i.More(); aSubShapes_i.Next()) {
331         TopoDS_Shape aSubShape_i = aSubShapes_i.Current();
332         if (!mapIndices.Contains(aSubShape_i)) {
333           SetErrorCode("Operation aborted: not a sub-shape given");
334           return;
335         }
336         new_id = mapIndices.FindIndex(aSubShape_i);
337         if (mapIDs.Add(new_id)) {
338           aNewIDs.Append(new_id);
339         }
340       }
341     }
342   }
343
344   if (aNewIDs.Extent() > 0) {
345     Standard_Integer k = 1;
346     TColStd_ListIteratorOfListOfInteger aNewIDsIter (aNewIDs);
347     Handle(TColStd_HArray1OfInteger) aNewSeq = new TColStd_HArray1OfInteger(1, aNewIDs.Extent());
348     for (; aNewIDsIter.More(); aNewIDsIter.Next(), k++) {
349       aNewSeq->SetValue(k, aNewIDsIter.Value());
350     }
351
352     aSSI.SetIndices(aNewSeq);
353   }
354
355   //Make a Python command
356   Handle(GEOM_Object) aLatest = GEOM::GetCreatedLast(theSubShapes);
357   aLatest = GEOM::GetCreatedLast(aLatest, theGroup);
358   Handle(GEOM_Function) aLastFunc = aLatest->GetLastFunction();
359
360   GEOM::TPythonDump pd (aLastFunc, /*append=*/true);
361   pd << "geompy.UnionList(" << theGroup << ", [";
362
363   for (i = 1; i <= aLen; i++) {
364     Handle(GEOM_Object) anObj_i = Handle(GEOM_Object)::DownCast(theSubShapes->Value(i));
365     pd << anObj_i << (( i < aLen ) ? ", " : "])");
366   }
367
368   SetErrorCode(OK);
369 }
370
371 //=============================================================================
372 /*!
373  *  DifferenceList
374  */
375 //=============================================================================
376 void GEOMImpl_IGroupOperations::DifferenceList (Handle(GEOM_Object) theGroup,
377                                                 const Handle(TColStd_HSequenceOfTransient)& theSubShapes)
378 {
379   SetErrorCode(KO);
380   if (theGroup.IsNull()) return;
381
382   Standard_Integer aLen = theSubShapes->Length();
383   if (aLen < 1) {
384     SetErrorCode("The list is empty");
385     return;
386   }
387
388   Handle(GEOM_Function) aFunction = theGroup->GetFunction(1);
389   if (aFunction.IsNull()) return;
390
391   GEOM_ISubShape aSSI (aFunction);
392
393   // Map of IDs to be removed
394   TColStd_MapOfInteger mapIDsToRemove;
395
396   // Map of current IDs
397   Handle(TColStd_HArray1OfInteger) aSeq = aSSI.GetIndices();
398   if (aSeq.IsNull()) return;
399   Standard_Integer aLength = aSeq->Length();
400
401   if (aLength == 1 && aSeq->Value(1) == -1) // empty group
402     return;
403
404   TColStd_MapOfInteger mapIDsCurrent;
405   Standard_Integer j = 1;
406   for (; j <= aLength; j++) {
407     mapIDsCurrent.Add(aSeq->Value(j));
408   }
409
410   // Get Main Shape
411   Handle(GEOM_Function) aMainShapeFunc = aSSI.GetMainShape();
412   if (aMainShapeFunc.IsNull()) return;
413   TDF_Label aLabel = aMainShapeFunc->GetOwnerEntry();
414   if (aLabel.IsRoot()) return;
415   Handle(GEOM_Object) aMainObj = GEOM_Object::GetObject(aLabel);
416   if (aMainObj.IsNull()) return;
417   TopoDS_Shape aMainShape = aMainObj->GetValue();
418   if (aMainShape.IsNull()) return;
419
420   TopTools_IndexedMapOfShape mapIndices;
421   TopExp::MapShapes(aMainShape, mapIndices);
422
423   // Get group type
424   TopAbs_ShapeEnum aType = GetType(theGroup);
425
426   // Get IDs of sub-shapes to be removed
427   Standard_Integer i, rem_id;
428   for (i = 1; i <= aLen; i++) {
429     Handle(GEOM_Object) anObj_i = Handle(GEOM_Object)::DownCast(theSubShapes->Value(i));
430
431     TopoDS_Shape aShape_i = anObj_i->GetValue();
432
433     // 1. If aShape_i is sub-shape of aMainShape - remove it
434     if (mapIndices.Contains(aShape_i)) {
435       rem_id = mapIndices.FindIndex(aShape_i);
436       if (mapIDsCurrent.Contains(rem_id)) {
437         mapIDsToRemove.Add(rem_id);
438       }
439     }
440     // 2. If type of group is not defined - remove all sub-shapes of aShape_i
441     else if (aType == TopAbs_SHAPE || aType == TopAbs_COMPOUND) {
442       TopTools_IndexedMapOfShape mapIndices_i;
443       TopExp::MapShapes(aShape_i, mapIndices_i);
444       Standard_Integer nbSubSh = mapIndices_i.Extent();
445       Standard_Integer ii = 1;
446       for (; ii <= nbSubSh; ii++) {
447         TopoDS_Shape aSubShape_i = mapIndices_i.FindKey(ii);
448         if (mapIndices.Contains(aSubShape_i)) {
449           rem_id = mapIndices.FindIndex(aSubShape_i);
450           if (mapIDsCurrent.Contains(rem_id)) {
451             mapIDsToRemove.Add(rem_id);
452           }
453         }
454       }
455     }
456     // 3. If type of group is defined - remove all sub-shapes of aShape_i of that type
457     else {
458       TopExp_Explorer aSubShapes_i (aShape_i, aType);
459       for (; aSubShapes_i.More(); aSubShapes_i.Next()) {
460         TopoDS_Shape aSubShape_i = aSubShapes_i.Current();
461         if (mapIndices.Contains(aSubShape_i)) {
462           rem_id = mapIndices.FindIndex(aSubShape_i);
463           if (mapIDsCurrent.Contains(rem_id)) {
464             mapIDsToRemove.Add(rem_id);
465           }
466         }
467       }
468     }
469   }
470
471   if (mapIDsToRemove.Extent() > 0) {
472     Standard_Integer k = 1, aRemLength = mapIDsToRemove.Extent();
473     Handle(TColStd_HArray1OfInteger) aNewSeq = new TColStd_HArray1OfInteger(1, aLength - aRemLength);
474
475     for (j = 1; j <= aLength; j++) {
476       if (!mapIDsToRemove.Contains(aSeq->Value(j))) {
477         aNewSeq->SetValue(k, aSeq->Value(j));
478         k++;
479       }
480     }
481
482     aSSI.SetIndices(aNewSeq);
483   }
484
485   //Make a Python command
486   Handle(GEOM_Object) aLatest = GEOM::GetCreatedLast(theSubShapes);
487   aLatest = GEOM::GetCreatedLast(aLatest, theGroup);
488   Handle(GEOM_Function) aLastFunc = aLatest->GetLastFunction();
489
490   GEOM::TPythonDump pd (aLastFunc, /*append=*/true);
491   pd << "geompy.DifferenceList(" << theGroup << ", [";
492
493   for (i = 1; i <= aLen; i++) {
494     Handle(GEOM_Object) anObj_i = Handle(GEOM_Object)::DownCast(theSubShapes->Value(i));
495     pd << anObj_i << (( i < aLen ) ? ", " : "])");
496   }
497
498   SetErrorCode(OK);
499 }
500
501 //=============================================================================
502 /*!
503  *  UnionIDs
504  */
505 //=============================================================================
506 void GEOMImpl_IGroupOperations::UnionIDs (Handle(GEOM_Object) theGroup,
507                                           const Handle(TColStd_HSequenceOfInteger)& theSubShapes)
508 {
509   SetErrorCode(KO);
510   if (theGroup.IsNull()) return;
511
512   Standard_Integer aLen = theSubShapes->Length();
513   if (aLen < 1) {
514     SetErrorCode("The list is empty");
515     return;
516   }
517
518   Handle(GEOM_Function) aFunction = theGroup->GetFunction(1);
519   if (aFunction.IsNull()) return;
520
521   GEOM_ISubShape aSSI (aFunction);
522
523   // New contents of the group
524   TColStd_ListOfInteger aNewIDs;
525   TColStd_MapOfInteger mapIDs;
526
527   // Add current IDs to the list
528   Handle(TColStd_HArray1OfInteger) aSeq = aSSI.GetIndices();
529   if (aSeq.IsNull()) return;
530   Standard_Integer val_j, aLength = aSeq->Length();
531
532   for (Standard_Integer j = 1; j <= aLength; j++) {
533     val_j = aSeq->Value(j);
534     if (val_j > 0 && mapIDs.Add(val_j)) {
535       aNewIDs.Append(val_j);
536     }
537   }
538
539   // Get Main Shape
540   Handle(GEOM_Function) aMainShapeFunc = aSSI.GetMainShape();
541   if (aMainShapeFunc.IsNull()) return;
542   TDF_Label aLabel = aMainShapeFunc->GetOwnerEntry();
543   if (aLabel.IsRoot()) return;
544   Handle(GEOM_Object) aMainObj = GEOM_Object::GetObject(aLabel);
545   if (aMainObj.IsNull()) return;
546   TopoDS_Shape aMainShape = aMainObj->GetValue();
547   if (aMainShape.IsNull()) return;
548
549   TopTools_IndexedMapOfShape mapIndices;
550   TopExp::MapShapes(aMainShape, mapIndices);
551
552   // Get IDs of sub-shapes to add
553   Standard_Integer i, new_id;
554   for (i = 1; i <= aLen; i++) {
555     new_id = theSubShapes->Value(i);
556
557     if (0 < new_id && new_id <= mapIndices.Extent()) {
558       if (mapIDs.Add(new_id)) {
559         aNewIDs.Append(new_id);
560       }
561     }
562   }
563
564   if (aNewIDs.Extent() > 0) {
565     Standard_Integer k = 1;
566     TColStd_ListIteratorOfListOfInteger aNewIDsIter (aNewIDs);
567     Handle(TColStd_HArray1OfInteger) aNewSeq = new TColStd_HArray1OfInteger(1, aNewIDs.Extent());
568     for (; aNewIDsIter.More(); aNewIDsIter.Next(), k++) {
569       aNewSeq->SetValue(k, aNewIDsIter.Value());
570     }
571
572     aSSI.SetIndices(aNewSeq);
573   }
574
575   //Make a Python command
576   GEOM::TPythonDump pd (aFunction, /*append=*/true);
577   pd << "geompy.UnionIDs(" << theGroup << ", [";
578   for (i = 1; i < aLen; i++)
579     pd << theSubShapes->Value(i) << ", ";
580   pd << theSubShapes->Value(aLen) << "])";
581
582   SetErrorCode(OK);
583 }
584
585 //=============================================================================
586 /*!
587  *  DifferenceIDs
588  */
589 //=============================================================================
590 void GEOMImpl_IGroupOperations::DifferenceIDs (Handle(GEOM_Object) theGroup,
591                                                const Handle(TColStd_HSequenceOfInteger)& theSubShapes)
592 {
593   SetErrorCode(KO);
594   if (theGroup.IsNull()) return;
595
596   Standard_Integer aLen = theSubShapes->Length();
597   if (aLen < 1) {
598     SetErrorCode("The list is empty");
599     return;
600   }
601
602   Handle(GEOM_Function) aFunction = theGroup->GetFunction(1);
603   if (aFunction.IsNull()) return;
604
605   GEOM_ISubShape aSSI (aFunction);
606
607   // Map of IDs to be removed
608   TColStd_MapOfInteger mapIDsToRemove;
609
610   // Map of current IDs
611   Handle(TColStd_HArray1OfInteger) aSeq = aSSI.GetIndices();
612   if (aSeq.IsNull()) return;
613   Standard_Integer aLength = aSeq->Length();
614
615   if (aLength == 1 && aSeq->Value(1) == -1) // empty group
616     return;
617
618   TColStd_MapOfInteger mapIDsCurrent;
619   Standard_Integer j = 1;
620   for (; j <= aLength; j++) {
621     mapIDsCurrent.Add(aSeq->Value(j));
622   }
623
624   // Get Main Shape
625   Handle(GEOM_Function) aMainShapeFunc = aSSI.GetMainShape();
626   if (aMainShapeFunc.IsNull()) return;
627   TDF_Label aLabel = aMainShapeFunc->GetOwnerEntry();
628   if (aLabel.IsRoot()) return;
629   Handle(GEOM_Object) aMainObj = GEOM_Object::GetObject(aLabel);
630   if (aMainObj.IsNull()) return;
631   TopoDS_Shape aMainShape = aMainObj->GetValue();
632   if (aMainShape.IsNull()) return;
633
634   TopTools_IndexedMapOfShape mapIndices;
635   TopExp::MapShapes(aMainShape, mapIndices);
636
637   // Get IDs of sub-shapes to be removed
638   Standard_Integer i, rem_id;
639   for (i = 1; i <= aLen; i++) {
640     rem_id = theSubShapes->Value(i);
641     if (mapIDsCurrent.Contains(rem_id)) {
642       mapIDsToRemove.Add(rem_id);
643     }
644   }
645
646   if (mapIDsToRemove.Extent() > 0) {
647     Standard_Integer k = 1, aRemLength = mapIDsToRemove.Extent();
648     Handle(TColStd_HArray1OfInteger) aNewSeq = new TColStd_HArray1OfInteger(1, aLength - aRemLength);
649
650     for (j = 1; j <= aLength; j++) {
651       if (!mapIDsToRemove.Contains(aSeq->Value(j))) {
652         aNewSeq->SetValue(k, aSeq->Value(j));
653         k++;
654       }
655     }
656
657     aSSI.SetIndices(aNewSeq);
658   }
659
660   //Make a Python command
661   GEOM::TPythonDump pd (aFunction, /*append=*/true);
662   pd << "geompy.DifferenceIDs(" << theGroup << ", [";
663   for (i = 1; i < aLen; i++)
664     pd << theSubShapes->Value(i) << ", ";
665   pd << theSubShapes->Value(aLen) << "])";
666
667   SetErrorCode(OK);
668 }
669
670 //=============================================================================
671 /*!
672  *  GetType
673  */
674 //=============================================================================
675 TopAbs_ShapeEnum GEOMImpl_IGroupOperations::GetType(Handle(GEOM_Object) theGroup)
676 {
677   SetErrorCode(KO);
678
679   TDF_Label aFreeLabel = theGroup->GetFreeLabel();
680   Handle(TDataStd_Integer) anAttrib;
681   if(!aFreeLabel.FindAttribute(TDataStd_Integer::GetID(), anAttrib)) return TopAbs_SHAPE;
682
683   SetErrorCode(OK);
684   return (TopAbs_ShapeEnum) anAttrib->Get(); 
685 }
686
687 //=============================================================================
688 /*!
689  *  GetMainShape
690  */
691 //=============================================================================
692 Handle(GEOM_Object) GEOMImpl_IGroupOperations::GetMainShape (Handle(GEOM_Object) theGroup)
693 {
694   SetErrorCode(KO);
695
696   if(theGroup.IsNull()) return NULL;
697
698   Handle(GEOM_Function) aGroupFunction = theGroup->GetFunction(1);
699   if (aGroupFunction.IsNull()) return NULL;
700
701   GEOM_ISubShape aSSI (aGroupFunction);
702   Handle(GEOM_Function) aMainShapeFunction = aSSI.GetMainShape();
703   if (aMainShapeFunction.IsNull()) return NULL;
704
705   TDF_Label aLabel = aMainShapeFunction->GetOwnerEntry();
706   Handle(GEOM_Object) aMainShape = GEOM_Object::GetObject(aLabel);
707   if (aMainShape.IsNull()) return NULL;
708
709   //Make a Python command
710   GEOM::TPythonDump(aGroupFunction, /*append=*/true)
711     << aMainShape << " = geompy.GetMainShape(" << theGroup << ")";
712
713   SetErrorCode(OK);
714   return aMainShape; 
715 }
716
717 //=============================================================================
718 /*!
719  *  GetObjects
720  */
721 //=============================================================================
722 Handle(TColStd_HArray1OfInteger) GEOMImpl_IGroupOperations::GetObjects(Handle(GEOM_Object) theGroup)
723 {
724   SetErrorCode(KO);
725
726   if(theGroup.IsNull()) return NULL;
727
728   Handle(GEOM_Function) aFunction = theGroup->GetFunction(1);
729   if(aFunction.IsNull()) return NULL;
730
731   GEOM_ISubShape aSSI(aFunction);
732   Handle(TColStd_HArray1OfInteger) aSeq = aSSI.GetIndices();
733   if(aSeq.IsNull()) return NULL;
734
735   if(aSeq->Length() == 1 && aSeq->Value(1) == -1) {
736     SetErrorCode(OK);
737     return NULL;
738   }
739
740   SetErrorCode(OK);
741   return aSeq;
742 }