[MStn CE U17.2 C#] Hide/Unhide levels of referenced/nested reference file.

Hi Community,

I'm currently attempting to hide elements located within a specific level name. These elements are within a reference file, and sometimes they are nested within other references. I've tried several workarounds found in this forum, but none have proven effective.

 DgnAttachmentCollection dgnAttachments = BM.Session.Instance.GetActiveDgnModel().GetDgnAttachments();
 foreach (DgnAttachment dgnatc in dgnAttachments)
 {
     try
     {

        
         LevelCache levelCache = dgnatc.GetLevelCache();
         FileLevelCache fileCache = dgnatc.GetFileLevelCache();
         LevelHandle testHandle = fileCache.GetLevelByName("1100_P1,P2", true);
         LevelHandleCollection lhcLevel = levelCache.GetHandles(); 
         EditLevelHandle editLevelHandle= testHandle.GetEditHandle();
         editLevelHandle.Display = false;

         LevelHandleCollection lhcFile = fileCache.GetHandles();
         List<string> levelsName = new List<string>();
         foreach (LevelHandle lh in lhcFile)
         {
             levelsName.Add(lh.Name);
             if (lh.Name != "1100_P1,P2") // this is true for every case as the list that i am getting does not contains this
             {
                 continue;
             }
             EditLevelHandle edaHandle = lh.GetEditHandle();
           
             edaHandle.Display = false;
         }
     }
     catch (Exception)
     {


     }
 }

When I query this LevelHandle testHandle = fileCache.GetLevelByName("1100_P1,P2", false);

The response that I am getting is shown below.

And when I query this LevelHandleCollection lhcFile = fileCache.GetHandles();

The list that I get in return does not contain the nested reference levels.

Is there anything that I could do differently? Thanks.

Parents
  • I did find a partial solution to this problem.

     private static bool FindLevelInAttachment(DgnAttachment dgnFile, string Name)
     {
         DgnAttachmentCollection dgnAttachments = dgnFile.GetDgnAttachments();
         bool levelFound = SearchForLevelInAttachments(dgnAttachments);
    
         if (levelFound)
         {
             // The level was found
             return true;
         }
         else
         {
             // Level was not found after iterating through all attachments
         }
    
         bool SearchForLevelInAttachments(DgnAttachmentCollection attachments)
         {
             foreach (DgnAttachment attachment in attachments)
             {
                 if (CheckIfAttachmentContainsLevel(attachment))
                 {
                     return true; // Level found, exit and return true
                 }
                 else
                 {
                     // Check for nested attachments
                     DgnAttachmentCollection nestedAttachments = attachment.GetDgnAttachments();
    
                     if (SearchForLevelInAttachments(nestedAttachments))
                     {
                         return true; // Level found in a nested attachment, exit and return true
                     }
                 }
             }
    
             return false; // Level not found in this attachment or its nested attachments
         }
    
         bool CheckIfAttachmentContainsLevel(DgnAttachment attachment)
         {
             // Add your logic here to check if the attachment contains the desired level
             // Return true if the level is found, otherwise return false
             LevelCache levelCache = attachment.GetLevelCache();
             FileLevelCache fileCache = attachment.GetFileLevelCache();
    
             LevelHandleCollection lhcLevel = levelCache.GetHandles();
    
             LevelHandleCollection lhcFile = fileCache.GetHandles();
    
             foreach (LevelHandle lh in lhcLevel)
             {
    
                 if (lh.Name == Name)
                 {
                     EditLevelHandle edaHandle = lh.GetEditHandle();
                     string handleName = edaHandle.Name;
                     //edaHandle.Hidden = false;
                     edaHandle.Display = false;
                     return true;
                 }
    
             }
             return false;
         }
         return false;
     }

    This function seems to work when there is a reference already attached to a DGN(when I open a DGN with a reference attached).

    When I call this function while attaching a reference, it seems to execute without any error but does not override the levels as shown below.

    DgnModel dgnSheetModel = activeDgn.LoadRootModelById(out StatusInt statusInt, sheetmodleid, true, true, true);
    DgnAttachment refToCreate;
    try
    {
        refToCreate = dgnSheetModel.CreateDgnAttachment(activeDgn.GetDocument().GetMoniker(), Properties.Settings.Default.ModelName_Seed);
        double decimalScale = 1.0 / Properties.Settings.Default.Scale;
        refToCreate.ApplyStandardView(standardView, 1, decimalScale); /*0.01*/
        refToCreate.SetRefOrigin(viewLocation);
        refToCreate.NestDepth = 6;
        refToCreate.WriteToModel(true);
        if (standardView == StandardView.Top && !string.IsNullOrEmpty(Properties.Settings.Default.LevelsToHide))
        {
            string[] levels = Properties.Settings.Default.LevelsToHide.Split('#');
    
            foreach (string level in levels)
            {
                try
                {
                    FindLevelInAttachment(refToCreate, level);
                }
                catch (Exception)
                {
    
                    
                }
    
            }
        }
    }
    catch (Exception ex)
    {
    
        MessageBox.Show(ex.Message);
    }

    Could anyone explain why it doesn't work in the latter case?

  • Could anyone explain why it doesn't work in the latter case?

    I can speculate, but I can't explain.

    This extract is from the MicroStationAPI C++ documentation (it's always useful to check that document, which is often more verbose than the .NET help): To create an attachment, call DgnModelRef::CreateDgnAttachment, then set up the defining parameters and logical, and then call WriteToModel. To set up defining parameters or to modify the parameters of an attachment, call Set functions, and then call WriteToModel..

     
    Regards, Jon Summers
    LA Solutions

  • Hi Jon,

    It did help me to hide some of the elements. But what I noticed is the element that is shown below 

    When I am hiding the above level the element that is 'Cell: W201 \ Line String' is not hiding even though I could see the level set to not visible in the level manager. The element that is a simple line string is getting hidden successfully.

    I could not understand what was happening in the background. 

    Please suggest any solution to this.

  • the element that is 'Cell: W201 \ Line String' is not hiding

    If a level is the active level it cannot be hidden.  Using MicroStation tools, can you make that line-string invisible?

     
    Regards, Jon Summers
    LA Solutions

  • Yes, it gets hidden when I manually try hiding it from the layer manager.

Reply Children
No Data