I am going a bit insane with this piece of code, its fairly basic and if the dgnfile is open then the text updating works fine, but if I initiate app.OpenDesignFile and then it will crash at t.Rewrite(). Is this something to do with the App not being idle? And if so, how does one implement the idle check in C#? I've tried adding a 5 second thread sleep, but that did nothing, although I also didn't expect it to work since i'm on the same thread as microstation.
This is a C# addin, but I am using COM interface for this portion to update text.
public static void UpdateTextElemsInFile(List<DiTextElem> diTextElems, string fileName) { var app = Utilities.ComApp; DesignFile dgn; bool isOpen = false; if (app.ActiveDesignFile.FullName == fileName) { dgn = app.ActiveDesignFile; isOpen = true; } else { dgn = app.OpenDesignFile(fileName, true); isOpen = true; } foreach (DiTextElem diTextElem in diTextElems) { foreach (ModelReference m in dgn.Models) { if (m.Name == diTextElem.ModelName) { if (diTextElem.ParentID is null) { Element elem = m.GetElementByID64(diTextElem.Id); if (elem.IsTextElement()) { TextElement t = (TextElement)elem; t.Text = diTextElem.NewText; t.Rewrite(); } if (elem.IsTextNodeElement()) { ElementEnumerator enumerator = elem.AsTextNodeElement().GetSubElements(); while (enumerator.MoveNext()) { if (enumerator.Current.ID64 == diTextElem.Id) { TextElement t = (TextElement)enumerator.Current; t.Text = diTextElem.NewText; t.Rewrite(); //elem.Rewrite(); } } } } } } } dgn.Save(); if (!isOpen) dgn.Close(); } }