I have this:
using (FileLevelCache levelCache = ActiveDgnFile.GetLevelCache()) { LevelHandleCollection levelHandles = levelCache.GetHandles(); foreach (LevelHandle level in levelHandles) { if (level.Name.EndsWith(" P") || level.Name.EndsWith(" L")) {levelCache.RemoveLevel(null, level); levelCache.Write(); } } }
But it dont remove the levels. I think I need to remove the elements assosiated with the levels before removing the levels. But I dont know how...
Can anyone help?
Sorry, but when you ignore this forum best practices constantly , do not expect positive response:
Please correct the issues!
Samuel Wiklund said:But I dont know how...
And did you try to search this forum, code examples delivered with MicroStation SDK or any from SDK documentation? They contain the answer for sure, because to delete element is crucial task. And at last, but not least, IntelliSense in Visual Studio shows you DeleteFromModel() method exists for every Element.
Jan
Bentley Accredited Developer: iTwin Platform - AssociateLabyrinth Technology | dev.notes() | cad.point
Im sorry, this is the first time for me asking for help.
Microstation Connect Update 16
Version 10.16.00.80
Using C#
Code:
DgnFile ActiveDgnFile = Session.Instance.GetActiveDgnFile();
using (FileLevelCache levelCache = ActiveDgnFile.GetLevelCache()) { LevelHandleCollection levelHandles = levelCache.GetHandles(); foreach (LevelHandle level in levelHandles) { if (level.Name.EndsWith(" P") || level.Name.EndsWith(" L")) { levelCache.RemoveLevel(null, level); levelCache.Write(); } } }
Samuel Wiklund said:this is the first time for me asking for help.
That's fine, but it is always recommended to check help, recommendation and best practices at first.
Samuel Wiklund said:Code:
Please use Insert > Insert code tool when sharing a code snippet!
Samuel Wiklund said:Remove Elements in dgn-file with specific name
Jon shared useful article, but as I wrote, both this forum (e.g. this discussion) and MicroStationAPI help (despite of it is about C++ API, see EditElementHandle Struct Reference chapter) provide answer an details about element lifecycle. To search should always be the first step ... and even when no right answer is found, the result is new information about MicroStation programming ;-)
Regards,
using (FileLevelCache levelCache = ActiveDgnFile.GetLevelCache()) { foreach (DgnModel dgnModel in ActiveDgnFile.GetLoadedModelsCollection()) { ModelElementsCollection elementsCollection = dgnModel.GetGraphicElements(); LevelHandleCollection levelHandles = levelCache.GetHandles(); foreach (LevelHandle level in levelHandles) { if (level.Name.EndsWith(" P") || level.Name.EndsWith(" L")) { foreach (Element el in elementsCollection) { el.DeleteFromModel(); } } } } }