Speed Problem Creating many objects

I have a utility that need to create many different blocks. I am using the MakeBox routine BRepPrimAPI_MakeBox. When I create many boxes it is very slow as it displays each one. You can actually see it creating an object, which seems ridiculous. I assume i am doing something wrong

Handle_AIS_Shape myBox;
BRepPrimAPI_MakeBox B(gp_Pnt(X1,Y1,Z1), X2,Y2,Z2);
myBox = new AIS_Shape(B.Shape());
if(myAISContext->HasOpenedContext())
myAISContext->CloseLocalContext();
myAISContext->SetMaterial(myBox,Graphic3d_NOM_DEFAULT);
Quantity_Color col = Quantity_Color(r/255.,g/255.,b/255.,Quantity_TOC_RGB);
myAISContext->SetColor (myBox,col.Name());
myAISContext->SetDisplayMode(myBox,1);
myAISContext->Erase(myBox,Standard_False, Standard_True);
myAISContext->SetDisplayMode(1, Standard_False);
myAISContext->Display(myBox, Standard_False);

This same routine is called for each box. Is there a way to speed this up?

Thanks in advance!
Hamish

varad's picture

u can do one thing (probably this may work)

create all blocks. crate a compound object(TopoDS_Compound)
add all these objects into this object and display compound object..
(u will find this example in tutor-bottle creation pdf... end lines of complete src code)

try it out....

Davide Canalia's picture

Why do you erase the myBox object?

Also you can check only once the local context if you create all the box at once and update the view at the end of the creation.

Other thant all looks good.

How many box do you need?

hamishl's picture

Thanks for the replys. It is still slower than it should be, but it is much fast than it was. I changed the cose to just
myAISContext->SetMaterial(myBox,Graphic3d_NOM_DEFAULT);
Quantity_Color col = Quantity_Color(r/255.,g/255.,b/255.,Quantity_TOC_RGB);
myAISContext->SetColor (myBox,col.Name());
myAISContext->SetDisplayMode(myBox,1, Standard_False);

Then after I have created all boxes I run through a map of all the objects and disply them. This increased the speed so that it can now create and display 500 blocks in 5 seconds on my machine, which is way slower than it should be, but is much better than the previous 25 seconds.

Davide Canalia's picture

Happy to be helpfull.

I think that it is possible to optimize a bit the process using the same approach.

ie you can:
1) do the creation without setting color and material
3) create the color/material object
2) run through the map for display (setting the color/material)

i don't think that this code will improve as the previous optimization but you can avoid 500 color creation