Salome HOME
Mantis issue 0021837: [CEA 654] Add new method to make any point inside a face.
[modules/geom.git] / src / GEOMImpl / GEOMImpl_IGroupOperations.cxx
index ab894fcbcc875bf487443f4d88184379457962f8..db55c72b69a00730bec477a132f0d097da5ff99f 100644 (file)
@@ -1,23 +1,24 @@
-//  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
 //
-//  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
-//  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+// Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
 //
-//  This library is free software; you can redistribute it and/or
-//  modify it under the terms of the GNU Lesser General Public
-//  License as published by the Free Software Foundation; either
-//  version 2.1 of the License.
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License.
 //
-//  This library is distributed in the hope that it will be useful,
-//  but WITHOUT ANY WARRANTY; without even the implied warranty of
-//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-//  Lesser General Public License for more details.
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
 //
-//  You should have received a copy of the GNU Lesser General Public
-//  License along with this library; if not, write to the Free Software
-//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
-//  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 
 #include <Standard_Stream.hxx>
 
@@ -89,7 +90,7 @@ Handle(GEOM_Object) GEOMImpl_IGroupOperations::CreateGroup
   //Set a GROUP type
   aGroup->SetType(GEOM_GROUP);
 
-  //Set a sub shape type
+  //Set a sub-shape type
   TDF_Label aFreeLabel = aGroup->GetFreeLabel();
   TDataStd_Integer::Set(aFreeLabel, (Standard_Integer)theShapeType);
 
@@ -113,7 +114,7 @@ void GEOMImpl_IGroupOperations::AddObject(Handle(GEOM_Object) theGroup, int theS
   SetErrorCode(KO);
   if(theGroup.IsNull()) return;
 
-  Handle(GEOM_Function) aFunction = theGroup->GetFunction(1);
+  Handle(GEOM_Function) aFunction = theGroup->GetLastFunction();
   if(aFunction.IsNull()) return;
 
   GEOM_ISubShape aSSI (aFunction);
@@ -146,12 +147,20 @@ void GEOMImpl_IGroupOperations::AddObject(Handle(GEOM_Object) theGroup, int theS
     for(Standard_Integer i = 1; i<=aLength; i++) {
       aNewSeq->SetValue(i, aSeq->Value(i));
       if(aSeq->Value(i) == theSubShapeID) {
-       SetErrorCode(ALREADY_PRESENT);
-       return; //
+        SetErrorCode(ALREADY_PRESENT);
+        return; //
       }
     }
     aNewSeq->SetValue(aLength+1, theSubShapeID);
-    aSSI.SetIndices(aNewSeq);
+    if ( aFunction->IsLastFuntion() ) {
+      aSSI.SetIndices(aNewSeq);
+    }
+    else {
+      aFunction = theGroup->AddFunction( GEOM_Object::GetSubShapeID(), 0, true );
+      GEOM_ISubShape aSSI2 (aFunction);
+      aSSI2.SetIndices(aNewSeq);
+      aSSI2.SetMainShape( aSSI.GetMainShape() );
+    }
   }
 
   // As we do not recompute here our group, lets mark it as Modified
@@ -176,7 +185,7 @@ void GEOMImpl_IGroupOperations::RemoveObject (Handle(GEOM_Object) theGroup, int
   SetErrorCode(KO);
   if(theGroup.IsNull()) return;
 
-  Handle(GEOM_Function) aFunction = theGroup->GetFunction(1);
+  Handle(GEOM_Function) aFunction = theGroup->GetLastFunction();
   if(aFunction.IsNull()) return;
 
   GEOM_ISubShape aSSI(aFunction);
@@ -218,7 +227,15 @@ void GEOMImpl_IGroupOperations::RemoveObject (Handle(GEOM_Object) theGroup, int
     }
   }
 
-  aSSI.SetIndices(aNewSeq);
+  if ( aFunction->IsLastFuntion() ) {
+    aSSI.SetIndices(aNewSeq);
+  }
+  else {
+    aFunction = theGroup->AddFunction( GEOM_Object::GetSubShapeID(), 0, true );
+    GEOM_ISubShape aSSI2 (aFunction);
+    aSSI2.SetIndices(aNewSeq);
+    aSSI2.SetMainShape( aSSI.GetMainShape() );
+  }
 
   // As we do not recompute here our group, lets mark it as Modified
   TDF_Label aLabel = aSSI.GetMainShape()->GetOwnerEntry();
@@ -249,11 +266,12 @@ void GEOMImpl_IGroupOperations::UnionList (Handle(GEOM_Object) theGroup,
 
   Standard_Integer aLen = theSubShapes->Length();
   if (aLen < 1) {
-    SetErrorCode("The list is empty");
+    //SetErrorCode("The list is empty");
+    SetErrorCode(OK);
     return;
   }
 
-  Handle(GEOM_Function) aFunction = theGroup->GetFunction(1);
+  Handle(GEOM_Function) aFunction = theGroup->GetLastFunction();
   if (aFunction.IsNull()) return;
 
   GEOM_ISubShape aSSI (aFunction);
@@ -296,6 +314,7 @@ void GEOMImpl_IGroupOperations::UnionList (Handle(GEOM_Object) theGroup,
     Handle(GEOM_Object) anObj_i = Handle(GEOM_Object)::DownCast(theSubShapes->Value(i));
 
     TopoDS_Shape aShape_i = anObj_i->GetValue();
+    if ( aShape_i.IsNull() ) continue;
     TopAbs_ShapeEnum aType_i = aShape_i.ShapeType();
 
     // 1. If aShape_i is sub-shape of aMainShape - add it
@@ -358,20 +377,22 @@ void GEOMImpl_IGroupOperations::UnionList (Handle(GEOM_Object) theGroup,
     for (; aNewIDsIter.More(); aNewIDsIter.Next(), k++) {
       aNewSeq->SetValue(k, aNewIDsIter.Value());
     }
-
-    aSSI.SetIndices(aNewSeq);
-
+    if ( aFunction->IsLastFuntion() ) {
+      aSSI.SetIndices(aNewSeq);
+    }
+    else {
+      aFunction = theGroup->AddFunction( GEOM_Object::GetSubShapeID(), 0, true );
+      GEOM_ISubShape aSSI2 (aFunction);
+      aSSI2.SetIndices(aNewSeq);
+      aSSI2.SetMainShape(aMainShapeFunc);
+    }
     // As we do not recompute here our group, lets mark it as Modified
     Standard_Integer aTic = aMainObj->GetTic(); // tic of main shape
     theGroup->SetTic(aTic - 1);
   }
 
   //Make a Python command
-  Handle(GEOM_Object) aLatest = GEOM::GetCreatedLast(theSubShapes);
-  aLatest = GEOM::GetCreatedLast(aLatest, theGroup);
-  Handle(GEOM_Function) aLastFunc = aLatest->GetLastFunction();
-
-  GEOM::TPythonDump pd (aLastFunc, /*append=*/true);
+  GEOM::TPythonDump pd (aFunction, /*append=*/true);
   pd << "geompy.UnionList(" << theGroup << ", [";
 
   for (i = 1; i <= aLen; i++) {
@@ -395,11 +416,12 @@ void GEOMImpl_IGroupOperations::DifferenceList (Handle(GEOM_Object) theGroup,
 
   Standard_Integer aLen = theSubShapes->Length();
   if (aLen < 1) {
-    SetErrorCode("The list is empty");
+    //SetErrorCode("The list is empty");
+    SetErrorCode(OK);
     return;
   }
 
-  Handle(GEOM_Function) aFunction = theGroup->GetFunction(1);
+  Handle(GEOM_Function) aFunction = theGroup->GetLastFunction();
   if (aFunction.IsNull()) return;
 
   GEOM_ISubShape aSSI (aFunction);
@@ -412,8 +434,9 @@ void GEOMImpl_IGroupOperations::DifferenceList (Handle(GEOM_Object) theGroup,
   if (aSeq.IsNull()) return;
   Standard_Integer aLength = aSeq->Length();
 
-  if (aLength == 1 && aSeq->Value(1) == -1) // empty group
-    return;
+  // VSR 28/04/2011 commented to allow operation even for empty group
+  //   if (aLength == 1 && aSeq->Value(1) == -1) // empty group
+  //     return;
 
   TColStd_MapOfInteger mapIDsCurrent;
   Standard_Integer j = 1;
@@ -447,7 +470,7 @@ void GEOMImpl_IGroupOperations::DifferenceList (Handle(GEOM_Object) theGroup,
     // 1. If aShape_i is sub-shape of aMainShape - remove it
     if (mapIndices.Contains(aShape_i)) {
       rem_id = mapIndices.FindIndex(aShape_i);
-      if (mapIDsCurrent.Contains(rem_id)) {
+      if (rem_id > 0 && mapIDsCurrent.Contains(rem_id)) {
         mapIDsToRemove.Add(rem_id);
       }
     }
@@ -461,7 +484,7 @@ void GEOMImpl_IGroupOperations::DifferenceList (Handle(GEOM_Object) theGroup,
         TopoDS_Shape aSubShape_i = mapIndices_i.FindKey(ii);
         if (mapIndices.Contains(aSubShape_i)) {
           rem_id = mapIndices.FindIndex(aSubShape_i);
-          if (mapIDsCurrent.Contains(rem_id)) {
+          if (rem_id > 0 && mapIDsCurrent.Contains(rem_id)) {
             mapIDsToRemove.Add(rem_id);
           }
         }
@@ -474,7 +497,7 @@ void GEOMImpl_IGroupOperations::DifferenceList (Handle(GEOM_Object) theGroup,
         TopoDS_Shape aSubShape_i = aSubShapes_i.Current();
         if (mapIndices.Contains(aSubShape_i)) {
           rem_id = mapIndices.FindIndex(aSubShape_i);
-          if (mapIDsCurrent.Contains(rem_id)) {
+          if (rem_id > 0 && mapIDsCurrent.Contains(rem_id)) {
             mapIDsToRemove.Add(rem_id);
           }
         }
@@ -484,28 +507,37 @@ void GEOMImpl_IGroupOperations::DifferenceList (Handle(GEOM_Object) theGroup,
 
   if (mapIDsToRemove.Extent() > 0) {
     Standard_Integer k = 1, aRemLength = mapIDsToRemove.Extent();
-    Handle(TColStd_HArray1OfInteger) aNewSeq = new TColStd_HArray1OfInteger(1, aLength - aRemLength);
-
-    for (j = 1; j <= aLength; j++) {
-      if (!mapIDsToRemove.Contains(aSeq->Value(j))) {
-        aNewSeq->SetValue(k, aSeq->Value(j));
-        k++;
+    Handle(TColStd_HArray1OfInteger) aNewSeq;
+    if ( aLength - aRemLength > 0 ) {
+      aNewSeq = new TColStd_HArray1OfInteger(1, aLength - aRemLength);
+      for (j = 1; j <= aLength; j++) {
+       if (!mapIDsToRemove.Contains(aSeq->Value(j))) {
+         aNewSeq->SetValue(k, aSeq->Value(j));
+         k++;
+       }
       }
     }
+    else {
+      aNewSeq = new TColStd_HArray1OfInteger(1,1);
+      aNewSeq->SetValue(1, -1);
+    }
 
-    aSSI.SetIndices(aNewSeq);
-
+    if ( aFunction->IsLastFuntion() ) {
+      aSSI.SetIndices(aNewSeq);
+    }
+    else {
+      aFunction = theGroup->AddFunction( GEOM_Object::GetSubShapeID(), 0, true );
+      GEOM_ISubShape aSSI2 (aFunction);
+      aSSI2.SetIndices(aNewSeq);
+      aSSI2.SetMainShape(aMainShapeFunc);
+    }
     // As we do not recompute here our group, lets mark it as Modified
     Standard_Integer aTic = aMainObj->GetTic(); // tic of main shape
     theGroup->SetTic(aTic - 1);
   }
 
   //Make a Python command
-  Handle(GEOM_Object) aLatest = GEOM::GetCreatedLast(theSubShapes);
-  aLatest = GEOM::GetCreatedLast(aLatest, theGroup);
-  Handle(GEOM_Function) aLastFunc = aLatest->GetLastFunction();
-
-  GEOM::TPythonDump pd (aLastFunc, /*append=*/true);
+  GEOM::TPythonDump pd (aFunction, /*append=*/true);
   pd << "geompy.DifferenceList(" << theGroup << ", [";
 
   for (i = 1; i <= aLen; i++) {
@@ -529,11 +561,12 @@ void GEOMImpl_IGroupOperations::UnionIDs (Handle(GEOM_Object) theGroup,
 
   Standard_Integer aLen = theSubShapes->Length();
   if (aLen < 1) {
-    SetErrorCode("The list is empty");
+    //SetErrorCode("The list is empty");
+    SetErrorCode(OK);
     return;
   }
 
-  Handle(GEOM_Function) aFunction = theGroup->GetFunction(1);
+  Handle(GEOM_Function) aFunction = theGroup->GetLastFunction();
   if (aFunction.IsNull()) return;
 
   GEOM_ISubShape aSSI (aFunction);
@@ -590,9 +623,15 @@ void GEOMImpl_IGroupOperations::UnionIDs (Handle(GEOM_Object) theGroup,
     for (; aNewIDsIter.More(); aNewIDsIter.Next(), k++) {
       aNewSeq->SetValue(k, aNewIDsIter.Value());
     }
-
-    aSSI.SetIndices(aNewSeq);
-
+    if ( aFunction->IsLastFuntion() ) {
+      aSSI.SetIndices(aNewSeq);
+    }
+    else {
+      aFunction = theGroup->AddFunction( GEOM_Object::GetSubShapeID(), 0, true );
+      GEOM_ISubShape aSSI2 (aFunction);
+      aSSI2.SetIndices(aNewSeq);
+      aSSI2.SetMainShape(aMainShapeFunc);
+    }
     // As we do not recompute here our group, lets mark it as Modified
     Standard_Integer aTic = aMainObj->GetTic(); // tic of main shape
     theGroup->SetTic(aTic - 1);
@@ -621,11 +660,12 @@ void GEOMImpl_IGroupOperations::DifferenceIDs (Handle(GEOM_Object) theGroup,
 
   Standard_Integer aLen = theSubShapes->Length();
   if (aLen < 1) {
-    SetErrorCode("The list is empty");
+    //SetErrorCode("The list is empty");
+    SetErrorCode(OK);
     return;
   }
 
-  Handle(GEOM_Function) aFunction = theGroup->GetFunction(1);
+  Handle(GEOM_Function) aFunction = theGroup->GetLastFunction();
   if (aFunction.IsNull()) return;
 
   GEOM_ISubShape aSSI (aFunction);
@@ -638,8 +678,9 @@ void GEOMImpl_IGroupOperations::DifferenceIDs (Handle(GEOM_Object) theGroup,
   if (aSeq.IsNull()) return;
   Standard_Integer aLength = aSeq->Length();
 
-  if (aLength == 1 && aSeq->Value(1) == -1) // empty group
-    return;
+  // VSR 28/04/2011 commented to allow operation even for empty group
+  //   if (aLength == 1 && aSeq->Value(1) == -1) // empty group
+  //     return;
 
   TColStd_MapOfInteger mapIDsCurrent;
   Standard_Integer j = 1;
@@ -664,24 +705,36 @@ void GEOMImpl_IGroupOperations::DifferenceIDs (Handle(GEOM_Object) theGroup,
   Standard_Integer i, rem_id;
   for (i = 1; i <= aLen; i++) {
     rem_id = theSubShapes->Value(i);
-    if (mapIDsCurrent.Contains(rem_id)) {
+    if (rem_id > 0 && mapIDsCurrent.Contains(rem_id)) {
       mapIDsToRemove.Add(rem_id);
     }
   }
 
   if (mapIDsToRemove.Extent() > 0) {
     Standard_Integer k = 1, aRemLength = mapIDsToRemove.Extent();
-    Handle(TColStd_HArray1OfInteger) aNewSeq = new TColStd_HArray1OfInteger(1, aLength - aRemLength);
-
-    for (j = 1; j <= aLength; j++) {
-      if (!mapIDsToRemove.Contains(aSeq->Value(j))) {
-        aNewSeq->SetValue(k, aSeq->Value(j));
-        k++;
+    Handle(TColStd_HArray1OfInteger) aNewSeq;
+    if ( aLength - aRemLength > 0 ) {
+      aNewSeq = new TColStd_HArray1OfInteger(1, aLength - aRemLength);
+      for (j = 1; j <= aLength; j++) {
+       if (!mapIDsToRemove.Contains(aSeq->Value(j))) {
+         aNewSeq->SetValue(k, aSeq->Value(j));
+         k++;
+       }
       }
     }
-
-    aSSI.SetIndices(aNewSeq);
-
+    else {
+      aNewSeq = new TColStd_HArray1OfInteger(1,1);
+      aNewSeq->SetValue(1, -1);
+    }
+    if ( aFunction->IsLastFuntion() ) {
+      aSSI.SetIndices(aNewSeq);
+    }
+    else {
+      aFunction = theGroup->AddFunction( GEOM_Object::GetSubShapeID(), 0, true );
+      GEOM_ISubShape aSSI2 (aFunction);
+      aSSI2.SetIndices(aNewSeq);
+      aSSI2.SetMainShape(aMainShapeFunc);
+    }
     // As we do not recompute here our group, lets mark it as Modified
     Standard_Integer aTic = aMainObj->GetTic(); // tic of main shape
     theGroup->SetTic(aTic - 1);
@@ -755,7 +808,7 @@ Handle(TColStd_HArray1OfInteger) GEOMImpl_IGroupOperations::GetObjects(Handle(GE
 
   if(theGroup.IsNull()) return NULL;
 
-  Handle(GEOM_Function) aFunction = theGroup->GetFunction(1);
+  Handle(GEOM_Function) aFunction = theGroup->GetLastFunction();
   if(aFunction.IsNull()) return NULL;
 
   GEOM_ISubShape aSSI(aFunction);