Looking for a script to extarct.

Want a script to extract say for example I want to extract the desired shape from a square.

So I draw the shape inside the square, sphere or cylinder then I want to extract it, I want to do this coz I want the extracted shape to be a perfect fit back into the square, sphere or cylinder etc...