自定义元素工具

在某些 DSL 定义,则表示一个概念作为一组元素。 例如,因此,如果创建组件具有一组固定端口的模型,总是需要端口是在其父元素的同时。 因此,您必须自定义组件创建工具,以便创建一组元素而不是一个。 为此,可以自定义组件创建工具如何初始化。

还可以重写时会发生什么,如果工具拖动到关系图或元素上时

自定义组件工具的内容

每个元素工具存储 ElementGroupPrototype (EGP 的实例),包含一个或多个模型元素和链接的一个序列化的版本。 默认情况下,元素工具的 EGP 包含您指定工具的类的一个实例。 可以通过重写的 TheLanguageToolboxHelper.CreateElementToolPrototype来更改此设置。 ,在 DSL 包加载时,调用此方法。

方法的参数是您在 DSL 定义指定类的 ID。 在调用方法时使用类时您感兴趣,可以添加额外的元素。 EGP。

EGP 必须包含嵌入主元素的链接到附属元素。 它也可以包含引用链接。

下面的示例创建一个主要组件和两个嵌入元素。 主类称为 Resistor,因此,它对名为 Terminal 元素的两个嵌入的关系。 嵌入的角色名为的属性 Terminal1 和 Terminal2,因此,两个具有重数 1..1。

using Microsoft.VisualStudio.Modeling; ...  
public partial class CircuitDiagramToolboxHelper
{
  protected override ElementGroupPrototype    CreateElementToolPrototype(Store store, Guid domainClassId)
  {
    // A case for each tool to customize:  
    if (domainClassId == Resistor.DomainClassId)
    {
      // Set up the prototype elements and links:
      Resistor resistor = new Resistor(store);
      resistor.Terminal1 = new Terminal(store); 
      resistor.Terminal2 = new Terminal(store);
      resistor.Terminal1.Name = "T1"; // embedding
      resistor.Terminal2.Name = "T2"; // embedding
      // We could also set up reference links.

      // Create an element group prototype for the toolbox:
      ElementGroup egp = new ElementGroup(store.DefaultPartition);
      egp.AddGraph(resistor, true);
      // We do not have to explicitly include embedded children.
      return egp.CreatePrototype();
    }
    // Element tools for other classes:
    else
      return base.CreateElementToolPrototype(store, domainClassId);
  }
}

请参见

概念

自定义元素创建和移动