diff --git a/dagmc/dagnav.py b/dagmc/dagnav.py index 4d79bb6..7eb2719 100644 --- a/dagmc/dagnav.py +++ b/dagmc/dagnav.py @@ -231,11 +231,13 @@ def id(self) -> int: @id.setter def id(self, i: int): """Set the DAGMC set's ID.""" - if i in self.model.used_ids[type(self)]: + if i is None: + i = max(self.model.used_ids[type(self)], default=0) + 1 + elif i in self.model.used_ids[type(self)]: raise ValueError(f'{self.category} ID {i} is already in use in this model.') - else: - self.model.used_ids[type(self)].discard(self.id) - self.model.used_ids[type(self)].add(i) + + self.model.used_ids[type(self)].discard(self.id) + self.model.used_ids[type(self)].add(i) self._tag_set_data(self.model.id_tag, i) @@ -367,7 +369,12 @@ def get_triangle_coordinate_mapping(self, compress=False): return tri_map, coords def delete(self): - """Delete this group from the DAGMC file.""" + """Delete this set from the MOAB database, but doesn't + delete this DAGSet object. The object remains but no + longer refers to anything in the model. In many cases, it may + make sense to delete this DAGSet object immediately following + this operation.""" + self.model.used_ids[type(self)].discard(self.id) self.model.mb.delete_entity(self.handle) self.handle = None self.model = None @@ -381,8 +388,7 @@ def create(cls, model: DAGModel, global_id: Optional[int] = None) -> DAGSet: ent_set.category = cls._category # Now that the entity set has proper tags, create derived class and return out = cls(model, ent_set.handle) - if global_id is not None: - out.id = global_id + out.id = global_id return out @@ -678,9 +684,6 @@ def create(cls, model: DAGModel, name: Optional[str] = None, group_id: Optional[ # Now that entity set has proper tags, create Group, assign name, and return group = cls(model, ent_set.handle) - if group_id is None: - group_id = max((grp.id for grp in model.groups), default=0) + 1 - group.id = group_id if name is not None: diff --git a/test/test_basic.py b/test/test_basic.py index eafb2cb..4b672b2 100644 --- a/test/test_basic.py +++ b/test/test_basic.py @@ -185,6 +185,16 @@ def test_id_safety(request): v1.id = safe_vol_id assert v1.id == safe_vol_id + v2 = Volume.create(model) + assert v2.id == safe_vol_id + 1 + + safe_vol_id = 101 + v1.id = safe_vol_id + del v2 + + v3 = Volume.create(model) + assert v3.id == safe_vol_id + 1 + s1 = model.surfaces_by_id[1] used_surf_id = 2 @@ -195,6 +205,13 @@ def test_id_safety(request): s1.id = safe_surf_id assert s1.id == safe_surf_id + s2 = model.surfaces_by_id[2] + s2.id = None + assert s2.id == safe_surf_id + 1 + + s2.id = 2 + assert s2.id == 2 + g1 = model.groups_by_name['mat:fuel'] used_grp_id = 2