def run(TagNameList, ObjectList, CurrVal, Iteration, TagList, IgnoreEmptyTags): if CurrVal != '': hdl_list = [int(s) for s in CurrVal.split(',')] else: hdl_list = [] if len(TagNameList) < len(hdl_list): raise RuntimeError('Not enough tag names specified, expected at ' + 'least' + str(len(hdl_list)) + ' elements') hnd_reg = CHandleRegistry.Instance() # Before processing, remove any objects from tags for tag in TagNameList: tag_obj = tag_utils.get_tag_object(tag) # Clear anything the tag points to exist_list = tag_utils.get_tagged_objects([tag_obj]) for rem_obj in exist_list: rem_obj.RemoveObject(tag_obj, RelationType('UserTag')) for tag, hdl in zip(TagNameList, hdl_list): tag_obj = tag_utils.get_tag_object(tag) obj = hnd_reg.Find(hdl) # Is this an error if we are getting an invalid handle? if obj is None: continue # Add the object to the tag (not using util, since it operates on str) obj.AddObject(tag_obj, RelationType('UserTag')) return True
def test_get_tagged_objects(stc): stc_sys = CStcSystem.Instance() project = stc_sys.GetObject("Project") ctor = CScriptableCreator() port1 = ctor.Create("Port", project) port2 = ctor.Create("Port", project) port3 = ctor.Create("Port", project) tags = project.GetObject("Tags") if tags is None: tags = ctor.Create("Tags", project) assert tags is not None tag1 = ctor.Create("Tag", tags) tag1.Set("Name", "EastPortGroup") port1.AddObject(tag1, RelationType("UserTag")) port2.AddObject(tag1, RelationType("UserTag")) tags.AddObject(tag1, RelationType("UserTag")) tag2 = ctor.Create("Tag", tags) tag2.Set("Name", "WestPortGroup") port2.AddObject(tag2, RelationType("UserTag")) port3.AddObject(tag2, RelationType("UserTag")) tags.AddObject(tag2, RelationType("UserTag")) res = tag_utils.get_tagged_objects([tag1]) assert len(res) == 2 port1_count = 0 port2_count = 0 port3_count = 0 tags_count = 0 for item in res: if item.GetObjectHandle() == port1.GetObjectHandle(): port1_count = port1_count + 1 elif item.GetObjectHandle() == port2.GetObjectHandle(): port2_count = port2_count + 1 elif item.GetObjectHandle() == port3.GetObjectHandle(): port3_count = port3_count + 1 elif item.GetObjectHandle() == tags.GetObjectHandle(): tags_count = tags_count + 1 assert port1_count == 1 assert port2_count == 1 assert port3_count == 0 assert tags_count == 0 res = tag_utils.get_tagged_objects([tag1], ignore_tags_obj=False) assert len(res) == 3 port1_count = 0 port2_count = 0 port3_count = 0 tags_count = 0 for item in res: if item.GetObjectHandle() == port1.GetObjectHandle(): port1_count = port1_count + 1 elif item.GetObjectHandle() == port2.GetObjectHandle(): port2_count = port2_count + 1 elif item.GetObjectHandle() == port3.GetObjectHandle(): port3_count = port3_count + 1 elif item.GetObjectHandle() == tags.GetObjectHandle(): tags_count = tags_count + 1 assert port1_count == 1 assert port2_count == 1 assert port3_count == 0 assert tags_count == 1 # Note that the Tags object will appear twice in this test due # to it coming from both tag1 and tag2 res = tag_utils.get_tagged_objects([tag1, tag2], ignore_tags_obj=False, remove_duplicates=False) assert len(res) == 6 port1_count = 0 port2_count = 0 port3_count = 0 tags_count = 0 for item in res: if item.GetObjectHandle() == port1.GetObjectHandle(): port1_count = port1_count + 1 elif item.GetObjectHandle() == port2.GetObjectHandle(): port2_count = port2_count + 1 elif item.GetObjectHandle() == port3.GetObjectHandle(): port3_count = port3_count + 1 elif item.GetObjectHandle() == tags.GetObjectHandle(): tags_count = tags_count + 1 assert port1_count == 1 assert port2_count == 2 assert port3_count == 1 assert tags_count == 2 # Tags object will not appear in the results of this call # but port2 will appear twice res = tag_utils.get_tagged_objects([tag1, tag2], remove_duplicates=False) assert len(res) == 4 port1_count = 0 port2_count = 0 port3_count = 0 tags_count = 0 for item in res: if item.GetObjectHandle() == port1.GetObjectHandle(): port1_count = port1_count + 1 elif item.GetObjectHandle() == port2.GetObjectHandle(): port2_count = port2_count + 1 elif item.GetObjectHandle() == port3.GetObjectHandle(): port3_count = port3_count + 1 elif item.GetObjectHandle() == tags.GetObjectHandle(): tags_count = tags_count + 1 assert port1_count == 1 assert port2_count == 2 assert port3_count == 1 assert tags_count == 0 # Test filtering on class_name (with duplicates) # class_name takes precedence over ignore_tags_obj res = tag_utils.get_tagged_objects([tag1, tag2], ignore_tags_obj=False, remove_duplicates=False, class_name="Port") assert len(res) == 4 port1_count = 0 port2_count = 0 port3_count = 0 tags_count = 0 for item in res: if item.GetObjectHandle() == port1.GetObjectHandle(): port1_count = port1_count + 1 elif item.GetObjectHandle() == port2.GetObjectHandle(): port2_count = port2_count + 1 elif item.GetObjectHandle() == port3.GetObjectHandle(): port3_count = port3_count + 1 elif item.GetObjectHandle() == tags.GetObjectHandle(): tags_count = tags_count + 1 assert port1_count == 1 assert port2_count == 2 assert port3_count == 1 assert tags_count == 0 # Test filtering on class_name (without duplicates) # class_name takes precedence over ignore_tags_obj res = tag_utils.get_tagged_objects([tag1, tag2], ignore_tags_obj=True, class_name="Port", remove_duplicates=True) assert len(res) == 3 port1_count = 0 port2_count = 0 port3_count = 0 tags_count = 0 for item in res: if item.GetObjectHandle() == port1.GetObjectHandle(): port1_count = port1_count + 1 elif item.GetObjectHandle() == port2.GetObjectHandle(): port2_count = port2_count + 1 elif item.GetObjectHandle() == port3.GetObjectHandle(): port3_count = port3_count + 1 elif item.GetObjectHandle() == tags.GetObjectHandle(): tags_count = tags_count + 1 assert port1_count == 1 assert port2_count == 1 assert port3_count == 1 assert tags_count == 0