MicroStation Python: Replace Text


 

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 and Replace texts in elements using Regular Expression, python module.

 

Search and Replace Text

To get started, refer to the Python Manager wiki for instructions on creating and loading a new Python project. Name your project "ReplaceText.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 *
import re


# Search for text string function
def check_text_contains_string(eh: TextHandlerBase, search_text, replace_text):
    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(search_text, TextBlock.FindTextParameters(), 
text_block.CreateStartCaret(),
text_block.CreateEndCaret()) if (result is not None): #SelectionSetManager.GetManager().AddElement(eh.ElementRef, eh.ModelRef) text_str = text_block.ToString().GetWCharCP() #print (text_str) updated_str = re.sub(pattern = search_text, repl = replace_text, string = text_str) #print (updated_str) text_block.ReplaceText(updated_str, text_block.CreateStartCaret(),
text_block.CreateEndCaret()) new_eeh = EditElementHandle() if (TextBlockToElementResult.eTEXTBLOCK_TO_ELEMENT_RESULT_Success ==
TextHandlerBase.CreateElement(new_eeh, eh, text_block)): new_eeh.ReplaceInModel(eh.ElementRef) else: print ("Text Replace failed...") return # Check for Text Element 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) # Select Text function def select_texts(text_to_search, text_to_update): # 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, text_to_search, text_to_update) # Main Function def main(): select_texts("2023", "2024") #select_texts(" 3", " 4") #select_texts("Text", "Texts") # Main if __name__ == "__main__": print("****************") main()

 

Run/Execute project

Load the project “ReplaceText.py” from the Python Manager dialog and Run/Execute the python script.

 

In the following example we are finding elements containing text string “2023” and replacing with "2024" using Regular Expression, python module.

 

 

Happy coding!

 

 

Python: Documentation | API Presentations | FAQs | GitHub | Samples | Wikis | Blogs