how to map selection to the OCAF GUID ?

karim

CAD community veteran
Hi all.
I have an OCAF Document with some named shapes. I add the shapes to the AIS_interactive_context and then display them. When I do the selection, I want to recover which object in the OCAF document I have selected. Is there a tool for this or should I somehow write it myself ?
thanks.
 

natalia

Moderator
Staff member
Hi @karim

In most cases, we hold some map of AIS_InteractiveObject to the model object (e.g., it might be the entry of OCAF object, or pointer to object if the architecture of the application data model has such one). When the selection happens, we get the selected interactive object and obtain the OCAF object from this map.

In addition, OCAF has a way to work with AIS_InteractiveObject itself through TPrsStd_AISPresentation object in connection to own Interactive Context. It might be useful to have a look at it. However, it is not very generic way and more for save/restore visual parameters of the presentation in OCAF document.
 

karim

CAD community veteran
Hi @karim

In most cases, we hold some map of AIS_InteractiveObject to the model object (e.g., it might be the entry of OCAF object, or pointer to object if the architecture of the application data model has such one). When the selection happens, we get the selected interactive object and obtain the OCAF object from this map.

In addition, OCAF has a way to work with AIS_InteractiveObject itself through TPrsStd_AISPresentation object in connection to own Interactive Context. It might be useful to have a look at it. However, it is not very generic way and more for save/restore visual parameters of the presentation in OCAF document.
I prefer the first option as it is more straight forward. However, the process is a bit vague for now.
Iterating over the labels, I find the named shape, and create a AIS_Shape from it, then add it to the context like this:
for (TDF_ChildIterator it (root, Standard_True); it.More(); it.Next())
{
auto label = it.Value(); // get the label.
Handle(TNaming_NamedShape) namedshape;
if(label.FindAttribute(TNaming_NamedShape::GetID(), namedshape))
{
TopoDS_Shape shape = TNaming_Tool::GetShape(namedshape);
auto ais_shape = new AIS_Shape(shape);
// std::cout << label << ": adding shape to context" << std::endl;
child->getContext()->Display(ais_shape, Standard_False);
}
}

when I iterate over the selected objects with the below code:

for (myContext->InitSelected(); myContext->MoreSelected(); myContext->NextSelected())
{
Handle(AIS_InteractiveObject) selected = myContext->SelectedInteractive();
auto owner = selected->GetOwner();

Handle(AIS_Shape) aShape=Handle(AIS_Shape)::DownCast(selected);
TopoDS_Shape topShape = aShape->Shape();

}

I can get the owner of the selection. I dont know which pointers are the same in the above blocks of code ? In other words what pointers should go into the map<HANDLE<??> , LABEL> ?
 

natalia

Moderator
Staff member
Hi, @karim

The information about shape is inside TDF_Label, which is ‘label’ variable of your code.

But, the label is not a pointer, so it’s better communicating to this object by its entry. You can get the entry using the code like:

C++:
TCollection_AsciiString entry;
TDF_Tool::Entry(label, entry);

And, later you can get the label back using the code:
C++:
TCollection_AsciiString entry = …; // comes from the map

TDF_Label label;
TDF_Tool::Label(document->GetData(), entry, label);

So, the proposal here is the map with values:
map<TCollection_AsciiString, Handle(AIS_Shape)> with the label entry as a key. The label entry is unique, and only one named shape attribute is usually used below the label. It is also possible to use AIS_Shape as the key.
 

karim

CAD community veteran
Hi, @karim

The information about shape is inside TDF_Label, which is ‘label’ variable of your code.

But, the label is not a pointer, so it’s better communicating to this object by its entry. You can get the entry using the code like:

C++:
TCollection_AsciiString entry;
TDF_Tool::Entry(label, entry);

And, later you can get the label back using the code:
C++:
TCollection_AsciiString entry = …; // comes from the map

TDF_Label label;
TDF_Tool::Label(document->GetData(), entry, label);

So, the proposal here is the map with values:
map<TCollection_AsciiString, Handle(AIS_Shape)> with the label entry as a key. The label entry is unique, and only one named shape attribute is usually used below the label. It is also possible to use AIS_Shape as the key.

Thanks Natalia. very nice. So it seems the AIS_Shape pointer I get from the selection is the same pointer that I add to the context ?
As I want to recover the label from the shape pointer it seems my map should look like map<Handle<AIS_Shape>, TCollection_AsciiString> then. Right ?



Also I wonder how to recover AIS_Shape (ais_shape) pointer from the selection?
auto ais_shape = new AIS_Shape(shape);
// std::cout << label << ": adding shape to context" << std::endl;
child->getContext()->Display(ais_shape, Standard_False);
 
Last edited:

natalia

Moderator
Staff member
Hi, @karim

Thanks Natalia. very nice. So it seems the AIS_Shape pointer I get from the selection is the same pointer that I add to the context ?
As I want to recover the label from the shape pointer it seems my map should look like map<Handle<AIS_Shape>, TCollection_AsciiString> then. Right ?
Yes, sure.

Also I wonder how to recover AIS_Shape (ais_shape) pointer from the selection?

You correctly do this in your second piece of code:
C++:
for (myContext->InitSelected(); myContext->MoreSelected(); myContext->NextSelected())
{
  Handle(AIS_InteractiveObject) selected = myContext->SelectedInteractive();
  Handle(AIS_Shape) aShape = Handle(AIS_Shape) ::DownCast(selected);
  if (!aShape.IsNull())
  {
    // find it in your map and get the label entry
  }
}
 
Top