This solution was provided by Jan Šlegr, a veteran MicroStation developer.
Welcome to the world of MicroStation Python! In this wiki, we will explore how to Search for texts in elements and add to a Selection Set.
To get started, refer to the Python Manager wiki for instructions on creating and loading a new Python project. Name your project "SearchText.py" and save it in a convenient location. Once created, open the project in your preferred code editor to begin writing your Python script.
Here is the complete script.
from MSPyBentley import * from MSPyBentleyGeom import * from MSPyECObjects import * from MSPyDgnPlatform import * from MSPyDgnView import * from MSPyMstnPlatform import * # select_texts function def select_texts(): # Entry point to select text elements MessageCenter.StatusCommand = "Select text elements" MessageCenter.StatusPrompt = "Runs automatically..." active_model = ISessionMgr.GetActiveDgnModel() elements = active_model.GetGraphicElements() for element in elements: eh = ElementHandle(element) if is_free_standing_text(eh): check_text_contains_string(eh) # is_free_standing_text function def is_free_standing_text(eh: ElementHandle) -> bool: # Element should be both TextHandler and simple Text Element return isinstance(eh.GetHandler(), TextHandlerBase) and
eh.GetITextQuery().IsTextElement(eh) # check_text_contains_string function def check_text_contains_string(eh: TextHandlerBase): # Add text element to Selection Set text_query = eh.GetITextQuery() text_part_ids = TextPartIdVector() text_query.GetTextPartIds(eh, ITextQueryOptions(), text_part_ids) first_text_part = text_part_ids[0] text_block = text_query.GetTextPart(eh, first_text_part) result = text_block.FindText("Line", TextBlock.FindTextParameters(),
text_block.CreateStartCaret(),
text_block.CreateEndCaret()) if (result is not None): SelectionSetManager.GetManager().AddElement(eh.ElementRef, eh.ModelRef) # Main function def main(): select_texts() # Main if __name__ == "__main__": main()
Load the project “SearchText.py” from the Python Manager dialog and Run/Execute the python script.
In the following example we are finding elements containing the word “Line” and add them to a Selection Set.
Happy coding!
Python: Documentation | API Presentations | FAQs | GitHub | Samples | Wikis | Blogs