How to get the selected interactive objects in a local context?

Hi,

In the neutral point, no problem to get the selected interactive objects(called "current" objects). But in a opened local context, I can't get them(called "selected" objects from the doc).
On the AIS_InteractiveContext class, there are routines :
InitSelected ()
MoreSelected ()
NextSelected ()
They allow to iterate other the selected objects, but there is no "CurrentSelected ()" function, like "Current ()" for the neutral point.
Where is the corresponding function for local contexts?

Thanks.

Patrik Mueller's picture

Hi Hugues,

haven't tried it - but how about "SelectedInteractive()"?

Best regards,

Patrik

Hugues's picture

Thanks a lot Patrick.

I think it's working now with this function.
To get the selected objects in a opened local context, I did:

Handle_AIS_InteractiveContext _ais_context;
// Initialize _ais_context ...
// Begin traversal of selected objects :
_ais_context->InitSelected ();
while (_ais_context->MoreSelected ())
{
// Do something with: _ais_context->SelectedInteractive ();
_ais_context->NextSelected ();
}

In debug mode, the count of loops in this while statement is the same as the count of selected objects.

Now what I want to do is to clear the selection(unselect everything), and reselect the previous items that I recorded in a list.

To clear the selection I'm using AIS_InteractiveContext::ClearSelected. When executed, this routine hilights every displayed items, but apparently it does clear selection.

To select an AIS_InteractiveObject instance, I use AIS_InteractiveContext::SetSelected (still in an opened local context).

I can't get it to work, no items appear to be selected. I tried also AddOrRemoveSelected but with no success too.

What routines have to be used to achieve what I want to do?

Hugues's picture

There is a strange thing with selections in an opened local context.
The following code is supposed to visit the selected interactive items, and to check that the items visited are actually selected :

AIS_InteractiveContext ais_context;
//Initialize the the context, and open a local context into it
//...
ais_context->InitSelected ();
while (ais_context->MoreSelected ())
{
assert (ais_context->IsSelected (ais_context->SelectedInteractive ()));
ais_context->NextSelected ();
}

The assertion fails during execution, which should not happen.
I am missing something, but I don't know what.
From the documentation, it is said:

Standard_Boolean IsSelected (const Handle(AIS_InteractiveObject)& aniobj) const;

Purpose:
Finds the selected object aniobj in local context and returns its name.

What is this story about the name?
There is also an example and remarks in the PDF of the Visualization module:

myAISCtx->InitSelected();
while (myAISCtx->MoreSelected())
{
if (myAISCtx->HasSelectedShape)
{
TopoDS_Shape ashape = myAISCtx->SelectedShape();
// to be able to use the picked shape
}
else
{
Handle_AIS_InteractiveObject aniobj = myAISCtx-
>Interactive();
// to be able to use the picked interactive object
}
myAISCtx->NextSelected();
}
Remarks:
In Local Context and in the iteration loop which allows you to recover selected entities, you have to ask whether you have selected a shape or an interactiveobject before you can recover the entity. If you have selected a Shape from TopoDS on decomposition in standard mode, the Interactive() function returns the interactive object which provided the selected shape.

I don't understand all of what is explained. Why distinguish between TopoDS_Shape and AIS_InterativeObject in the selection?
Has anyone ever used these routines?

Thanks a lot for your replies.

Hugues's picture

OK, I have understood the documentation.

Let start from the beginning:
When I read a CAD file (IGES, STEP, etc.) I always produce a TopoDS_Compound object of the loaded shapes.
Then in an AIS_InteractiveContext object, I open a local context with face-oriented selection, and create one AIS_Shape object that owns the previous TopoDS_Compound object.
At this point, the user is able to select some faces.
As there is only one AIS_Shape instance, you can't use AIS_InteractiveContext::SelectedInteractive () to get the selected faces as AIS_Shape objects.
You have to use AIS_InteractiveContext::SelectedShape () to achieve this.
At this point everything is ok.
But if you want to further select and unselect faces with programmatically means, it seems to be not possible (I tried AIS_InteractiveContext::AddOrRemoveSelected with TopoDS_Shape, I could not get it to work).
How I solved this is simple : I get selected shapes, and create an AIS_Shape object owning with these shapes, and keep a reference to this object.
Then it is possible to select and unselect it correctly.

Hope this helps anyone who has that problem.

Anton Shabalin's picture

Hallo Hugues.
unfortunatly i couls not get the selected interactive objects in local context...this is what i did may b u could help.

myContext->OpenLocalContext();
myContext->ActivateStandardMode(TopAbs_VERTEX);
myContext->Select();
myContext->InitSelected();
while(myContext->MoreSelected())
{
if(myContext->HasSelectedShape())
{

TopoDS_Shape vertex = myContext->SelectedShape();
//here i added a code to use the vertex i selected but it seems
//nothing worked.

}
else
{
TopoDS_Shape vertex = Handle(AIS_Shape)::DownCast( myContext->SelectedInteractive() )->Shape();
//here i did the same..nothing worked
}

myContext->NextSelected();
}
it seems every codes coming after i have open a local context ist just ignored.
Any help will b welcome.
Thanks.

DELORME's picture

Hello,

Why do you call AIS_InteractiveContext::Select() after ActivateStandardMode()?
Selection is performed by the user.
Apart this, your code seems ok.