0029940: The method TPrsStd_AISPresentation::SetSelectionMode() seems useless for...
[occt.git] / src / TPrsStd / TPrsStd_AISPresentation.cxx
index c80b849..173639a 100644 (file)
@@ -61,11 +61,11 @@ Handle(TPrsStd_AISPresentation) TPrsStd_AISPresentation::Set
   // create associated data (unless already there)
   Handle(TDataXtd_Presentation) aData = TDataXtd_Presentation::Set (theLabel, theDriver);
   if(aData.IsNull())
-    Standard_NoMoreObject::Raise (NO_MORE_OBJECT);
+    throw Standard_NoMoreObject(NO_MORE_OBJECT);
   if ( !theLabel.FindAttribute(TPrsStd_AISPresentation::GetID(), aPresentation) )
   {
     aPresentation = new TPrsStd_AISPresentation();
-    theLabel.AddAttribute(aPresentation);
+    theLabel.AddAttribute(aPresentation, Standard_True);
   }
 
   return aPresentation;
@@ -102,7 +102,7 @@ Handle(TDataXtd_Presentation) TPrsStd_AISPresentation::getData () const
 {
   Handle(TDataXtd_Presentation) aData;
   if (!Label().FindAttribute(TDataXtd_Presentation::GetID(), aData))
-      Standard_NoMoreObject::Raise (NO_MORE_OBJECT);
+      throw Standard_NoMoreObject(NO_MORE_OBJECT);
   return aData;
 }
 
@@ -386,8 +386,17 @@ void TPrsStd_AISPresentation::SetColor(const Quantity_NameOfColor theColor)
 {
   Backup();
   if ( getData()->HasOwnColor() && getData()->Color() == theColor )
-    if ( !myAIS.IsNull() && myAIS->HasColor() && myAIS->Color() == theColor )
-      return;
+  {
+    if (!myAIS.IsNull() && myAIS->HasColor())
+    {
+      Quantity_Color aColor;
+      myAIS->Color (aColor);
+      if (aColor.Name() == theColor)
+      {
+        return;
+      }
+    }
+  }
 
   getData()->SetColor(theColor);
 
@@ -396,8 +405,15 @@ void TPrsStd_AISPresentation::SetColor(const Quantity_NameOfColor theColor)
 
   if ( !myAIS.IsNull() )
   {
-    if ( myAIS->HasColor() && myAIS->Color() == theColor )
-      return;   // AIS has already had that color
+    if (myAIS->HasColor())
+    {
+      Quantity_Color aColor;
+      myAIS->Color (aColor);
+      if (aColor.Name() == theColor)
+      {
+        return;   // AIS has already had that color
+      }
+    }
 
     Handle(AIS_InteractiveContext) aContext = getAISContext();
 
@@ -575,11 +591,16 @@ Standard_Boolean TPrsStd_AISPresentation::HasOwnSelectionMode() const
 //function : SetSelectionMode
 //purpose  : 
 //=======================================================================
-void TPrsStd_AISPresentation::SetSelectionMode(const Standard_Integer theSelectionMode)
+void TPrsStd_AISPresentation::SetSelectionMode(const Standard_Integer theSelectionMode, const Standard_Boolean theTransaction)
 {
-  Backup();
-  getData()->SetSelectionMode (theSelectionMode);
-  AISUpdate();
+  if (theTransaction)
+    Backup();
+  getData()->SetSelectionMode (theSelectionMode, theTransaction);
+  
+  if ( myAIS.IsNull() )
+    AISUpdate();
+  else
+    ActivateSelectionMode();
 }
 
 //=======================================================================
@@ -640,6 +661,18 @@ void TPrsStd_AISPresentation::Paste (const Handle(TDF_Attribute)& theInto,
     Handle(TPrsStd_AISPresentation)::DownCast(theInto);
 
   anInto->Backup();
+  if (!anInto->myAIS.IsNull())
+  {
+    // Save displayed flag.
+    Standard_Boolean displayed = anInto->IsDisplayed();
+    // Erase the interactive object.
+    anInto->AISErase(Standard_True);
+    // Restore the displayed flag.
+    if (displayed)
+      anInto->SetDisplayed(displayed);
+  }
+  // Nullify the interactive object.
+  // It will be restored on the next call to AISUpdate().
   anInto->myAIS.Nullify();
 }
 
@@ -772,7 +805,7 @@ void TPrsStd_AISPresentation::AISUpdate()
           if ( !(anObj ==  myAIS) )
           {
             if ( !aContext.IsNull() )
-              aContext->Remove(myAIS);
+              aContext->Remove (myAIS, Standard_False);
 
             // Driver has built new AIS.
             myAIS = anObj;
@@ -789,7 +822,9 @@ void TPrsStd_AISPresentation::AISUpdate()
     if ( HasOwnColor() )
     {
       Quantity_NameOfColor aColor = Color();
-      if ( !(myAIS->HasColor()) || (myAIS->HasColor() && myAIS->Color() != aColor) )
+      Quantity_Color aPrsColor;
+      myAIS->Color (aPrsColor);
+      if ( !(myAIS->HasColor()) || (myAIS->HasColor() && aPrsColor.Name() != aColor) )
       {
         if ( !aContext.IsNull() )
           aContext->SetColor(myAIS, aColor, Standard_False);
@@ -841,36 +876,7 @@ void TPrsStd_AISPresentation::AISUpdate()
         myAIS->SetDisplayMode(aMode);
     }
 
-    if ( !aContext.IsNull() && IsDisplayed() )
-      aContext->Redisplay(myAIS, Standard_False);
-
-    if (HasOwnSelectionMode()) {
-      if (!aContext.IsNull())
-      {
-        TColStd_ListOfInteger anActivatedModes;
-        aContext->ActivatedModes (myAIS, anActivatedModes);
-        Standard_Boolean isActivated = Standard_False;
-        Standard_Integer aSelectionMode = SelectionMode();
-        if (aSelectionMode == -1)
-        {
-          aContext->Deactivate(myAIS);
-        }
-        else
-        {
-          for (TColStd_ListIteratorOfListOfInteger aModeIter (anActivatedModes); aModeIter.More(); aModeIter.Next())
-          {
-            if (aModeIter.Value() == aSelectionMode)
-            {
-              isActivated = Standard_True;
-              break;
-            }
-          }
-          if (!isActivated)
-            aContext->Activate (myAIS, aSelectionMode, Standard_False);
-        }
-      } 
-    }
-
+    ActivateSelectionMode();
   }
   
   if (IsDisplayed() && !aContext.IsNull())
@@ -893,7 +899,7 @@ void TPrsStd_AISPresentation::AISDisplay()
     if ( !myAIS.IsNull() )
     {
       if ( !(myAIS->GetContext()).IsNull() && (myAIS->GetContext()) != aContext )
-        myAIS->GetContext()->Remove(myAIS);
+        myAIS->GetContext()->Remove (myAIS, Standard_False);
 
       if ( IsDisplayed() && aContext->IsDisplayed(myAIS) )
         return;
@@ -983,3 +989,40 @@ Handle(AIS_InteractiveContext) TPrsStd_AISPresentation::getAISContext() const
 
   return Handle_AIS_InteractiveContext();
 }
+
+//=======================================================================
+//function : ActivateSelectionMode
+//purpose  : Activates selection mode of the interactive object.
+//           It is called internally on change of selection mode and AISUpdate().
+//=======================================================================
+void TPrsStd_AISPresentation::ActivateSelectionMode()
+{
+  if (!myAIS.IsNull() && HasOwnSelectionMode())
+  {
+    Handle(AIS_InteractiveContext) aContext = getAISContext();
+    if (!aContext.IsNull())
+    {
+      TColStd_ListOfInteger anActivatedModes;
+      aContext->ActivatedModes (myAIS, anActivatedModes);
+      Standard_Boolean isActivated = Standard_False;
+      Standard_Integer aSelectionMode = SelectionMode();
+      if (aSelectionMode == -1)
+      {
+        aContext->Deactivate(myAIS);
+      }
+      else
+      {
+        for (TColStd_ListIteratorOfListOfInteger aModeIter (anActivatedModes); aModeIter.More(); aModeIter.Next())
+        {
+          if (aModeIter.Value() == aSelectionMode)
+          {
+            isActivated = Standard_True;
+            break;
+          }
+        }
+        if (!isActivated)
+          aContext->Activate (myAIS, aSelectionMode, Standard_False);
+      }
+    } 
+  }
+}