void dwg_set_next_objhandle (Dwg_Object *obj)
{
  Dwg_Data *dwg = obj->parent;
  if (!dwg->object_map)
    dwg->object_map = hash_new (200);
  if (dwg->next_hdl)
    {
      obj->handle.value = dwg->next_hdl;
      set_handle_size (&obj->handle);
      hash_set (dwg->object_map, obj->handle.value, (uint32_t)obj->index);
      dwg->next_hdl = 0;
      return;
    }
  if (!dwg->num_objects)
    {
      obj->handle.size = 1;
      obj->handle.value = 1UL;
    }
  else
    {
      Dwg_Object *lastobj = &dwg->object[dwg->num_objects - 1];
      /* ADD_OBJECT might have just added a zeroed object */
      if (!lastobj->handle.value && dwg->num_objects > 1)
        lastobj = &dwg->object[dwg->num_objects - 2];
      obj->handle.value = lastobj->handle.value + 1;
      set_handle_size (&obj->handle);
    }
  hash_set (dwg->object_map, obj->handle.value, (uint32_t)obj->index);
  dwg->next_hdl = 0;
  return;
}
void dwg_set_next_objhandle (Dwg_Object *obj)
{
  Dwg_Data *dwg = obj->parent;
  if (!dwg->object_map)
    dwg->object_map = hash_new (200);
  if (dwg->next_hdl)
    {
      obj->handle.value = dwg->next_hdl;
      set_handle_size (&obj->handle);
      hash_set (dwg->object_map, obj->handle.value, (uint32_t)obj->index);
      dwg->next_hdl = 0;
      return;
    }
  if (!dwg->num_objects)
    {
      obj->handle.size = 1;
      obj->handle.value = 1UL;
    }
  else
    {
      Dwg_Object *lastobj = &dwg->object[dwg->num_objects - 1];
       
      if (!lastobj->handle.value && dwg->num_objects > 1)
        lastobj = &dwg->object[dwg->num_objects - 2];
      obj->handle.value = lastobj->handle.value + 1;
      set_handle_size (&obj->handle);
    }
  hash_set (dwg->object_map, obj->handle.value, (uint32_t)obj->index);
  dwg->next_hdl = 0;
  return;
}