Automatic structure dimension

Best to read over the post linked here, specifically item #7.

This is doable, and in fact there are a dozen such sources thereof. Search the forum for such examples and take a stab at it yourself and see where you get.