LevelCache Code Samples

Get level definition data for an element

If you have an ElementRef, information about its level in the file in which it is stored is always available by getting the level from the FileLevelCache of its DgnModel.

void getElementLevel (ElementRefP elementRef)
{
// Since we have an elementRef from the DgnModel, we know that FileLevelCache is never NULL.
FileLevelCache* fileLevelCache = elementRef->GetDgnModelP()->GetFileLevelCacheP ();
LevelHandle level = levelCache.GetLevel (elementRef->GetLevel ());
printf ("level name is %S\n", level.GetName ());
}
If you are or may be viewing the element through a DgnAttachment, then ask the model reference for the level.
void getElementLevelPossiblyOverride (ElementRefP ref, DgnModelRefP modelRef)
{
// If modelRef is a DgnAttachment, its GetLevelCacheR method will return a DgnAttachmentLevelCache.
// If modelRef is an ordinary DgnModel, its GetLevelCache method will return the FileLevelCache of the file that contains it.
LevelCache& levelCache = modelRef->GetLevelCacheR ();
LevelHandle level = levelCache.GetLevel (ref->GetLevel ());
printf ("by-level weight is %d\n", level.GetByLevelWeight());
}
See also
Bentley::DgnModelRef::GetFileLevelCacheP, Bentley::DgnModelRef::GetLevelCacheR, Bentley::DgnPlatform::LevelCache::GetLevel, Bentley::DgnPlatform::LevelHandle::GetByLevelWeight, Bentley::DgnPlatform::LevelHandle::GetName

Iterate levels in a level cache

A LevelCache is an STL collection of LevelDefinitionHandles. You can iterate it
using standard iteration syntax:
void iterateLevelsStd (DgnFileR file)
{
LevelCache& levelCache = file.GetLevelCacheR();
for (LevelHandle level (levelCache.begin()); level != levelCache.end(); ++level)
{
if (level.GetDisplay ())
... do something ...
}
}

You can also use Microsoft enhanced iteration syntax:

void iterateLevelsEnhanced (DgnFileR file)
{
for each (LevelHandle const& level in file.GetLevelCacheR())
{
if (level.GetDisplay ())
... do something ...
}
}

The most convenient way to get read-write access to all levels in a cache is to iterate using LevelDefinitionEditHandles. Note that you cannot use enhanced for-each iteration syntax in this case.

void iterateAndEditLevels (DgnFileR file)
{
// In this example, increase the by-level weight of each level by 1
LevelCache& levelCache = file.GetLevelCacheR();
for (EditLevelHandle level (levelCache.begin()); level != levelCache.end(); ++level)
{
level.SetByLevelWeight (level.GetByLevelWeight() + 1);
...
}
levelCache.Write (); // don't forget to save your changes
}
See also
Bentley::DgnPlatform::LevelCache::begin, Bentley::DgnPlatform::LevelHandle, Bentley::DgnPlatform::EditLevelHandle, Bentley::DgnPlatform::FileLevelCache::Write

Get the by-level color for an element

void getByLevelColor (ElementHandleCR eh)
{
// If eh has been set up with a modelref, then it might refer to a DgnAttachment. If so, its GetLevelCacheR
// method will return a DgnAttachmentLevelCache.
// Otherwise, get the FileLevelCache of the file that contains the element.
LevelCache& levelCache = (eh.GetDgnModelRef() != NULL)? eh.GetDgnModelRef()->GetLevelCacheR(): eh.GetDgnModelP()->GetLevelCacheR ();
LevelHandle level = levelCache.GetLevel (eh.GetElementCP()->ehdr.level);
// Get the by-level color
LevelDefinitionColor byLevelColor = level.GetByLevelColor ();
// A color may be an RGB value, a color book entry, or an entry in the definition file's color table.
IntColorDef colorDef;
DgnColorMap::ExtractElementColorInfo (&colorDef, NULL, NULL, NULL, NULL, byLevelColor.GetCode(), byLevelColor.GetDefinitionFile());
printf ("RBG color is %x\n", colorDef.m_rgb);
}
See also
Bentley::DgnModelRef::GetLevelCacheR
Bentley::DgnPlatform::LevelHandle::GetByLevelColor

Create a level

void createLevel (DgnFileR file)
{
// You can create levels only in a FileLevelCache.
FileLevelCache& levelCache = file.GetLevelCacheR ();
EditLevelHandle level = levelCache.CreateLevel (L"newName");
if (!level.IsValid ())
{
// "newName" might already be in use. Check level.GetStatus() for the value LevelCacheErrorCode::LevelNameIsDuplicate.
return;
}
// You can set up the level's attributes before writing it to file.
level.SetByLevelWeight (5);
// Save the new level.
if (levelCache.Write () != LevelCacheErrorCode::None)
{
// levelCache might be read-only, or the file might be read-only.
}
}
See also
Bentley::DgnModelRef::GetLevelCacheR
Bentley::DgnPlatform::FileLevelCache::CreateLevel
Bentley::DgnPlatform::FileLevelCache::Write
Bentley::DgnPlatform::EditLevelHandle
Create a level

Remove a level

To remove a level when you know its LevelId, LevelCode, or name:
void removeThisLevel (DgnFileR file, LevelId levelId)
{
LevelCache& levelCache = file.GetLevelCacheR();
LevelHandle level = levelCache.GetLevel (levelId);
// You could check that level.IsValid() == true, or you could just go on to the next step:
if (levelCache.RemoveLevel (level) != LevelCacheErrorCode::None)
// level was not found.
levelCache.Write (); // don't forget to save your change
}

To remove a level while iterating:

void removeLevelWhileIterating (DgnFileR file)
{
LevelCache& levelCache = file.GetLevelCacheR();
for (EditLevelHandle level (levelCache.begin()); level != levelCache.end(); )
{
if ( ... this is the level to remove ... )
levelCache.RemoveLevel (&level, level); // modifies level to point to the next level in the cache
else
++level;
}
}
See also
Bentley::DgnPlatform::LevelCache::begin
Bentley::DgnPlatform::LevelCache::GetLevel
Bentley::DgnPlatform::FileLevelCache::RemoveLevel
Bentley::DgnPlatform::FileLevelCache::Write
Bentley::DgnPlatform::LevelHandle

Copyright © 2017 Bentley Systems, Incorporated. All rights reserved.