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?
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
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,
Jan
Bentley Accredited Developer: iTwin Platform - AssociateLabyrinth Technology | dev.notes() | cad.point
For some reason i did this element removed, but all others with the ending of " P". So I thought It might depends of the type.
Samuel Wiklund said:So I thought It might depends of the type.
Yes, but not "level type" (which does not exist), but element type.
I recommend:
Regards,
Thank you so much for your time helping me understand.
Is it something to do with the Properties of the element thats "Not Modified"?
It worked to connect the element ID with the ID of the level to delete for simple lines but not for solids.
Maybe there is a better way to do the if-sentens to match the levels with the elements.
Samuel Wiklund said:Is it something to do with the Properties of the element thats "Not Modified"?
Again, lack of knowledge (that cannot be substituted by discussions, but solved only by some training).
Modified is an element property, showing whether it was modified (at least once) from time it was created.
Samuel Wiklund said:Maybe there is a better way to do the if-sentens to match the levels with the elements.
There is no better way, but the mandatory condition is you must understand how DGN format is designed and how API works. I wrote several times you must analyze, what Element (sub)class is returned, when SmartSolid is found, and also what ElementID:
So you must analyze what elements are iterated and adapt the evaluation accordingly to their types. Because complex headers have no element, you must decide what it means "SmartSolid is in particular level" (e.g. all elements/content are in the same level) and to write proper code.
Of course, you can use workaround and to use key-in to delete specific level.
One more questing.
foreach (var dgnFilePath in dgnFiles) { using (DgnDocument targetDgnDocument = DgnDocument.CreateForLocalFile(dgnFilePath)) using (DgnFileOwner dgnFileOwner = DgnFile.Create(targetDgnDocument, DgnFileOpenMode.ReadOnly)) { dgnFileOwner.DgnFile.LoadDgnFile(out StatusInt statusInt); DgnFile dgnFile = dgnFileOwner.DgnFile; DgnModel dgnModel = dgnFile.LoadRootModelById(out StatusInt loadDetails, dgnFile.DefaultModelId); ActiveDgnFile.CreateNewModel(out DgnModelStatus error, dgnModel.ModelName, DgnModelType.Normal, true, dgnModel); ModelId newDgnModelId = ActiveDgnFile.FindModelIdByName(dgnModel.ModelName); DgnModel newDgnModel = ActiveDgnFile.LoadRootModelById(out StatusInt errorDetails, newDgnModelId); DgnFile.CopyModelContents(newDgnModel, dgnModel); } }
I have this to create my dgn-file with all the models from a folder containing several dgn-files and it works.
But i would like to iterate through all models, activate them and remove levels ending with " V".
Something like this:
ModelIndexCollection models = dgnFile.GetModelIndexCollection(); foreach (ModelIndex model in models) { //Delete the levels in the model... //maybe with keyin? like: Session.Instance.Keyin("level element delete \"* V\"") }
Or do you have any suggesting?
My goal is to create a cell-library with just models without the levels ending with " V"
And also a cell-library with just models ending with " V". So 2 different cell-library.