How to implement selection?

Hello all,

I've just started trying to learn Open Cascade, so I'm completely new to OC and solid modeling for that matter. I'm also learning QT at the same time.

I have a very simple example that just diplays a 3D cube, but am wondering how to implement selection. I have the following 3 objects set up:

Handle(AIS_InteractiveContext) myContext;
Handle(V3d_Viewer) myViewer;
Handle(V3d_View) myView;

I set up the interactive context with:

myContext = new AIS_InteractiveContext(myViewer);

I build a box with:

TopoDS_Shape theBox = BRepPrimAPI_MakeBox (10, 10, 10);

and create an AIS_Shape with:

Handle(AIS_Shape) AISshape = new AIS_Shape(aShape);

I have no problem displaying the box with:

myContext->SetMaterial(AISshape, Graphic3d_NOM_GOLD);
myContext->SetDisplayMode(AISshape, 1, Standard_False);
myContext->Display(AISshape, Standard_False);
myContext->SetCurrentObject(AISshape, Standard_False);
myView->FitAll();
myView->ZFitAll();
myView->Redraw();

but I want to make it selectable, so before the FitAll function I try to load the shape into the interactive context with:

myContext->Load(AISshape, 1, Standard_True);

When I do this, it crashes.

I was wondering if someone could give me a hint how to get started with selection. I read the visulaization guide, and it mentions loading the interactive object into the interactive context, but I'm having trouble getting a simple example working. I think I'm not understanding what it is trying to tell me :)

On another topic, I was wondering if there would be interest in generating a number of very simple examples that demonstrate one concept at a time? I would be willing to help with this. I can build the tutorial example in QT, but I must admit I'm having trouble understanding everything that is going on. The makebottle tutorial and associated function is great, but of course it depends on lots of QT specific stuff, and the Visulaization components. I find it a little bit overwhelming....too much for my little brain to process at one time :)

The OC libraries seem to be excellent, and I'm not surprised that there is a lot to learn.

Thanks in advance for any help,
Garrett.

Paul Jimenez's picture

I would replace your code like this:

myContext->Display(AISshape, Standard_False);
myContext->SetMaterial(AISshape, Graphic3d_NOM_GOLD);
myContext->SetDisplayMode(AISshape, 1, Standard_False);
myView->FitAll();
myView->ZFitAll();
myView->Redraw();

Notice that the AIS_Shape is Displayed first, then its attributes are modified through the Context. Another way to do it is to modify the attributes of the AIS_Shape directly before Displaying it.

For Selection to work, you need to call MoveTo in the Context when the mouse moves. Once you do that, you can query the Context for Detected shapes, or you can call Select and then query for Current/Selected objects. If you want to select parts of the object (like Faces of the cube instead of the whole cube), open a Local Context and activate the corresponding mode (all through the Context). Now you can check and query for Selected Shapes.

In case you are using OpenCASCADE on Windows, check the MFC examples for an idea of how to do a couple of things.

I hope that helps you to get started.

garrett77's picture

Many thanks Paul!

Your explaination helped a lot.

Thanks again,
Garrett.