Given the limited functions that we have, the algorithm to do this for any given container and any possible set of items would be extraordinarily complicated. You'd have to store each base item's dimensions, then 'fill' the container one bit at a time. The trick would not be so much knowing if you could fit another item, as in knowing if they could fit if placed a different way.
To do it as I think you mean, however, it would be manageable given a few simplifying factors:
1) the wand would only work if the bag were empty
2) the 'certain type of thing' you want to bundle is either all the same base item type, or, at a minimum, of another type of identical size
From there it's relatively easy to calculate how many would fit of a given type - you could even make a function to return limit by type.
As for the sizes of the items, they're stored in baseitems.2da, in columns labeled InvSlotWidth and InvSlotHeight. You can view 2das using
NWN Explorer.
Funky