TPrsStd_AISPresentation SetSelectionMode doesn't work

this is code in .\samples\OCCTOverview\code\OcafSamples.cxx in the repository

// Get the TPrsStd_AISPresentation of the new box TNaming_NamedShape

Handle(TPrsStd_AISPresentation) anAisPresentation = TPrsStd_AISPresentation::Set(aLabel, TNaming_NamedShape::GetID());

// Display it
anAisPresentation->Display(1);
anAisPresentation->SetSelectionMode(0);

// Attach an integer attribute to aLabel to memorize it's displayed

TDataStd_Integer::Set(aLabel, 1);
myContext->UpdateCurrentViewer();

i set the SetSelectionMode, but it doesn't work.
please help me
thanks

Attachments: 
gkv311 n's picture

This is a bug in the sample.

TPrsStd_AISViewer displays the object within a dedicated AIS_InteractiveContext, while AIS_InteractiveContext::MoveTo() is done for another AIS_InteractiveContext (where you see AIS_ViewCube).

Managing two AIS_InteractiveContext for the same V3d_Viewer requires additional efforts at application level and is unlikely what normal application will do anyway.

The simplest way to make object selectable in the sample is to assign already created AIS_InteractiveContext to TPrsStd_AISViewer (don't know if it has any side effects for TPrsStd logic):

void OcafSamples::CreateOcafDocument()
{
  Handle(TOcaf_Application) anOcaf_Application = new TOcaf_Application;
  anOcaf_Application->NewDocument("BinOcaf", myOcafDoc);
-  TPrsStd_AISViewer::New(myOcafDoc->Main(), myViewer);
+  Handle(TPrsStd_AISViewer) aTViewer = TPrsStd_AISViewer::New(myOcafDoc->Main(), myViewer);
+  aTViewer->SetInteractiveContext(myContext);

This would, however, raise other errors/limitations in the sample like OcafSamples::ModifyBoxOcafSample() not checking the type of iterated object type within AIS_InteractiveContext.

In general, I would rather NOT recommend to use TPrsStd in a new application as this API breaks/complicates many aspects of a preferred/optimal way to use AIS within modern applications by tightening up Data Model with 3D Viewer, which are better to be clearly separated. Use AIS_Shape, XCAFPrs_AISObject directly for displaying shapes in 3D Viewer and explicitly manage persistence/synchronization of presentation aspects (if really needed).

wu chao's picture

thank you