How does one constrain geometry within a boundary?

I have a couple of cuboids and I want to move them around inside an area that should act as a boundary. From the image below I would like to move the cuboids around without extending “outside” of the surface. How do i constrain the entire shape of the cuboids within that boundary?

Heres a great resource you could use as a foundation - there’s nothing OOTB nor in the Revit API which you can use, so it will involve writing your own algorithms: