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:I need to remove the elements assosiated with the levels
Enumerate the DGN elements of a DGN model. Here's a C# example.
For each element, if it's graphical, get its level. Compare the level name with your criteria. Remove each element that passes your filter.
Regards, Jon Summers LA Solutions
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(); } } } } }
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) { if (level.LevelId == el.LevelId) { el.DeleteFromModel(); } } levelCache.RemoveLevel(null, level); levelCache.Write(); } } } }
Now I have this where I compare levelID. I think this might work, what do you think?
Samuel Wiklund said:I think this might work, what do you think?
I agree: it might work. Why not try it? Let us know!
It worked but I have a problem. Levels thats are of type "Smart solids" doesnt get deleted.. anyone that know why?
Samuel Wiklund said:Levels thats are of type "Smart solids" doesnt get deleted.. anyone that know why?
There is no such thing as a level of type "Smart solids".
Each graphical element is assigned a level. The level may be named "Smart solids", but that doesn't mean much — it's just a label. You could assign that level to a simple line, for example.
It's hard to provide a diagnosis without evidence. Can you post a DGN file where you have this problem?
Hi Samuel,
As Jon wrote, nothing like "Smart Solid level" exist, but what exists is MicroStation SmartSolid element (old, from V8 era).
It looks like you try to write some program without proper knowledge not only of MicroStation NET API, but also DGN format and how MicroStation creates data. Which makes situation that is not easy to give any advice.
As Jon wrote, without sharing data and explanation why you guess "Smart Solid level" exists, it's hard to guess anything.
Crucial is understand how data in DGN format are organized and difference between file / model(s) / level(s) / DGN element(s) and MicroStation (user) element(s). Smart solids are persisted (stored in DGN file) as cells with extra data attached. Moreover, cell is complex element, starting with header, that has no level (because it is not graphical element). So a question is what (what element type) your iteration finds?
With regards,