How to create a tag that becomes visible in the Tree?
I am trying to create a tag that contains objects of a certain type. The "API and XML Online Reference Guide for Mechanical" shows there's a class called ObjectTag but I can't find how to create one. I have used Ansys.Mechanical.Application.ObjectTag. This does create the tag object and I can add objects to it but it doesn't appear in the Tree. Checking with print(ExtAPI.DataModel.ObjectTags.TagNames) returns an empty list. What is he correct way to create a Tag that does become visible in the Tree?
def tag_comments(tagname):
"""
Adds a Tag which contains all Comments in the model
tagname: name to be given to the created tag
"""
OT=Ansys.Mechanical.Application.ObjectTag(tagname)
all_comments = ExtAPI.DataModel.GetObjectsByType(Ansys.ACT.Automation.Mechanical.Comment)
for comment in all_comments:
OT.AddObject(comment)
Create a tag for the comments
tag_comments('Comments_tag')
Check created object (WORKS WELL)
print(OT.Name)
for item in OT.Objects:
print(item.Name)
print(" ")
Check if tag visible in Tree (NOT working. Returns empty list)
print('Available tags:')
print(ExtAPI.DataModel.ObjectTags.TagNames)
Answers
-
I managed to get it done by using this:
ExtAPI.DataModel.ObjectTags.Add(OT)The complete code becomes:
def tag_comments(tagname):
"""
Adds a Tag which contains all Comments in the model
tagname: name to be given to the created tag
"""
if tagname in ExtAPI.DataModel.ObjectTags.TagNames:
for item in ExtAPI.DataModel.ObjectTags:
if item.Name==tagname:
ExtAPI.DataModel.ObjectTags.Remove(item)
print('Tag '+tagname+' removed')
break#create new tag OT=Ansys.Mechanical.Application.ObjectTag(tagname) ExtAPI.DataModel.ObjectTags.Add(OT) print('Tag '+tagname+' Added') #Add objects to tag all_comments = ExtAPI.DataModel.GetObjectsByType(Ansys.ACT.Automation.Mechanical.Comment) for tag in ExtAPI.DataModel.ObjectTags: if tag.Name==tagname: for comment in all_comments: tag.AddObject(comment) #Create a tag for the comments
tag_comments('Comments_tag')
0