CONNECT C# Addin Element Template

Is there a way to set attributes to Element according Element Template from attached dgnlib  (C#) ?
In 2016 Jan Šlegr wrote: "I guess there is no documented NET API available", may be after time...

I expect something like this:

ElementTemplateCollection elementTemplates = model.GetDgnFile().GetElementTemplates();
ElementTemplate elementTemplate = elementTemplates.FindByName(<template group name>\<template name>);
LineElement element = new LineElement(.....
elementTemplate.Apply(element);

...or similar...

  • Hi Stanislav,

    please follow the best practices!

    • What product do you use?
    • What version (exactly) do you use?

    Plus please use recommended subject format [<product> <version> <API>] <subject>. It makes posts navigation much easier.

    Also, use Insert > Insert code tool every time you want to share a code snippet (even one line only). To have code inserted as normal plain text does not look good (and readable).

    In 2016 Jan Šlegr wrote:

    Where, in what discussion I wrote it? Why you do not link the discussion?

    And what is more important, for what API version I wrote it? What was true 4 years ago could changed in a meantime.

    Is there a way to set attributes to Element according Element Template

    Did you search for existing discussions? Element templates have been discussed quite often.

    Even when they are typically about C++, but at least the discussion can provide some insight (e.g. that it is not well documented and difficult to use API even in C++).

    In NET, there seems to be ElementTemplateManager class available, but only some methods are listed in documentation, whereas more public methods exist in assembly. But they seem to be not equal to C++ MstnElementTemplateMgr. So it's about to try whether e.g. ApplyTemplateDefaultsToElement can help (unfortunately C++ ApplyElementTemplateParams does not exist in NET).

    I expect

    I think element template is to be applied after an element is created.

    Regards,

      Jan

  • Thank you very much for your answer! 

    • What product do you use?
    • What version (exactly) do you use?

    Closer specification:  AddIn application for OpenRoads 10.08.01.33 (OpenRoads Designer CONNECT Release 2, update 8) with MicroStation 10.13. core, C# ,NET, VS2017Prof.

    Where, in what discussion I wrote it? Why you do not link the discussion?

    Here is that discussion...last article

  • Hi Stanislav,

    Closer specification

    Well, they are not "closer specification", but mandatory (and often crucial) specifications. Whether used product is MicroStation or OpenRoads Designer changes the whole discussion often.

    Here is that discussion...last article

    I think even when situation is better now, because templates were discussed several times, so at least a minimal knowledge base is available. But it's not perfect, because even in C++ API, MstnElementTemplateMgr structure methods are not described enough.

    ...or similar...

    I see a couple of misunderstandings in your expectation:

    • Do not confuse template element with element template:
      • Template element, used in an element constructors, has to be already existing element, that is used to copy properties from. This concept has existed for very long time, before Templates were introduced.
      • Element template is a set of setting, applied to element and (alternatively) linked with the element, so even it's similar, it's not the same.
    • I think the template can be applied to existing element only (but I did not test it).
    Is there a way to set attributes to Element according Element Template from attached dgnlib  (C#) ?

    I think there is, but it requires some research and extra study, because I was not able to find any discussion about ElementTemplateMgr.

    I recommend:

    • Read all discussions about element templates in C++, especially where MstnElementTemplateMgr is discussed.
    • Check C++ documentation (but it does not provide too much valuable information in this case).
    • In Visual Studio (or using a decompiler like DnSpy) check what methods are offered by ElementTemplateMgr (available in ustation.dll assembly).
    • Also check this discussion. I like an idea to assign element template through EC representation.
    I expect something like this:

    Well, I think the workflow should be similar, but used classes and methods different. And it's not quite clear from available documentation, how much of some tasks are done automatically and what have to be implemented.

    • Check whether template exists in active DGN file
    • When not, iterate DGNLIBs to find the template and copy the template to active model
    • Get template id
    • Create an element
    • Assign template id to the element

    I am not sure whether the element has to be added to a model before the template is assigned. Also, it makes sense to encapsulate the operations by single transaction to ensure it's represented as one step in MicroStation interface.

    With regards,

      Jan

  • Hallo Stanislav,

    Yes this is possible using the ElementTemplateMgr class in the Bentley.MstnPlatformNET.XDataTree name space in the ustation.dll assembly.

    See the example below:

    public static bool ApplyElementTemplate(string elementTemplatePath, BDE.Element element, bool addTemplateReference)
    {
        if (!(element is BDE.DisplayableElement)) return false;
        var template = ElementTemplateMgr.FindTemplateByPath(elementTemplatePath);
        if (null != template && null != element)
        {
            return ElementTemplateMgr.ApplyTemplateDefaultsToElement(ref element, template, addTemplateReference);
        }
        return false;
    }
    


    HTH
    Francois