[C# MSCE 10.17] Associate Label to Element programmatically using TextFavorite

I have an application where I'm trying to place notes using Text Favorites (note: in the API these are TextSnippets). The text favorites have place holders for itemtype properties that are applied to elements the note points to. I can get the textblock from the textfavorite and apply it to the textblock of the label. Is there a way to associate the label to the element so that the textfavorite reads the itemtype properties? Below is image of a label placed manually of what I'm trying to do.

  • Hi Mike,

    I am not sure about Text Favorites, but if I remember right (I do not search whether there is anything new in U17), text fields (which is technology behind the favorites) can be created using C++ API only.

    With regards,

      Jan

  • Is there a way to associate the label to the element so that the textfavorite reads the itemtype properties?

    As Jan comments, the only API that deals with element properties is the C++ TextBlock.  The relevant methods are...

    • AppendField
    • InsertField
    • SetField
    • GetField

    Association, by the way, has different meaning in different contexts.  You can associate one element with another, which means that they are spatially connected: when you move the host element, the associated element maintains its spatial relationship.  Tag elements are an example: a tag element follows its host when you move the host.

     
    Regards, Jon Summers
    LA Solutions

  • Hi Mike,

    one more comment: I searched MicroStation API a bit more and I found DgnTextSnippet class, which, from documentation, looks like Text Favorite representation in API. But I see no example or description how it can be used to place the favorite to DGN file.

    Regards,

      Jan

  • Jan/Jon,

    I'm not trying to create a text snippet (text favorite), I already have them stored in a dgnlib. I'm able to retrieve the text snippet and make the text block from the snippet the textblock for the NoteCellHeaderElement. I'm trying to figure out if there is a way to associate the label to the element I'm labeling so that the text label reads itemtype properties from the element I'm labeling.

    I can get the list of TextSnippets using the code below:

    public static List<DgnTextSnippet> GetTextFavorites()
    {
        List<DgnTextSnippet> dgnTextSnippetList = new List<DgnTextSnippet>();
    
        //get the snippets in the current file
        DgnTextSnippetCollection localTextSnippets = new DgnTextSnippetCollection(Session.Instance.GetActiveDgnFile());
        foreach (DgnTextSnippet textSnippet in localTextSnippets)
        {
            dgnTextSnippetList.Add(textSnippet);
        }
    
        //get the snippets in the library path
        DgnLibIterator dgnLibIterator = new DgnLibIterator(DgnLibSelector.TextFavoritesOnly);
        var dgnLibEnumerator = dgnLibIterator.GetEnumerator();
    
        while (dgnLibEnumerator.MoveNext())
        {
            DgnFile dgnFile = dgnLibEnumerator.Current;
    
            DgnTextSnippetCollection dgnTextSnippets = new DgnTextSnippetCollection(dgnFile);
    
            foreach (DgnTextSnippet dgnTextSnippet in dgnTextSnippets)
            {
                dgnTextSnippetList.Add(dgnTextSnippet);
            }
        }
    
        return dgnTextSnippetList;
    }

    I then loop thru the list of snippets to find the one I'm looking for then retrieve the TextBlock from the snippet

    //note: dgnTextSnippet may be null if not found above
    TextBlock textBlock  = dgnTextSnippet.GetSnippetText(); 

    Using that textblock I then create a NoteCellHeaderElement and add it to the model

    Element leaderElement = null;
    
    DPoint3d[] leaderPoints = new DPoint3d[3] { new DPoint3d(0, 0, 0), new DPoint3d(5, 5, 0), new DPoint3d(10, 5, 0) }; 
    
    NoteCellHeaderElement noteCellHeaderElement = new NoteCellHeaderElement(out leaderElement, textBlock, dimensionStyle, dgnModel, leaderPoints);
    noteCellHeaderElement.AddToModel(out leaderElement, dgnModel);
    leaderElement.AddToModel();

  • I'm trying to figure out if there is a way to associate the label to the element

    You need to edit the TextBlock's text field and set the target element information.  However, as I've indicated above, those methods are confined to C++.

    Unfortunately, the attraction of .NET is hampered by the absence of those TextBlock methods.  You must use C++ to achieve what you want.

     
    Regards, Jon Summers
    LA Solutions

  • Thanks Jon, I'll look into either using DLL Imports or writing that part in C++ (been along time since I've done that!)