Refactor and simplify get_obj_num_by_kind().  Patch by daniel.santos.  Closes #1707.