如何通过 C# 插件删除企业架构图中的连接器

How to delete a connector in enterprise architect diagram through c# addin

我正在使用特定关联连接器 C 连接到 Enterprise Architect 中的源 element Atarget element B

如果用户在不同的源元素和目标元素之间使用此连接器,我想显示警告消息并使用 c# 加载项代码删除连接器:

以下是我使用的代码。我无法删除连接器。

public void ToDeleteConnectorByID(int connectorID)
        {
            try
            {

                EA.Connector addedConnector = Session.Repository.GetConnectorByID(Convert.ToInt32(connectorID));
                 EA.Element cuurentobjectconnectorele = Session.Repository.GetElementByID(addedConnector.ClientID);

                        for (short m = 0; m < cuurentobjectconnectorele.Connectors.Count - 1; m++)
                        {
                            cuurentobjectconnectorele.Connectors.Delete(m);
                            cuurentobjectconnectorele.Update();
                        }

                    }

            catch (Exception ex)
            {
                MessageBox.Show("no connector deleted-exception");
            }

        }

试试这段代码:

public void ToDeleteConnectorByID(int connectorID) {
  try {
    EA.Connector addedConnector = Session.Repository.GetConnectorByID(Convert.ToInt32(connectorID));
    EA.Element currentobjectconnectorele = Session.Repository.GetElementByID(addedConnector.ClientID);

    for (short m = 0; m < currentobjectconnectorele.Connectors.Count; m++) {
      if (addConnector.ConnectorID == currentobjectconnectorele.ConnectorID) {
        currentobjectconnectorele.Connectors.Delete(m);
      }
    }
  }

  catch (Exception ex) {
    MessageBox.Show("no connector deleted-exception");
  }
}

您的 Update 是多余的,您正在删除所有连接器,而不仅仅是您要查找的连接器。另外你的循环太短了。

N.B:我不懂 C#,但在 if 中你可以离开循环。我猜 break 是关键字。

以下方法可用于根据条件在元素之间添加连接符。当用户将新连接器从工具箱或资源 window 拖动到图表上时,将触发它。在创建连接器之前立即提供通知,以便加载项可以禁用添加连接器。

    /// <summary>
    /// EA_OnPreNewConnector notifies Add-Ins that a new connector is about to be created on a diagram. 
    /// It enables Add-Ins to permit or deny creation of a new connector.
    /// </summary>
    /// <param name="Repository"></param>
    /// <param name="Info"></param>
    /// <returns>Return True to enable addition of the new connector to the model. Return False to disable addition of the new connector.</returns>

public bool EA_OnPreNewConnector(EA.Repository Repository, EA.EventProperties Info)
{
    try
    {
            //To get added Connector Type
            string connectorType = "";
            EA.EventProperty connectorTypePropID;
            connectorTypePropID = Info.Get(0);
            connectorType = connectorTypePropID.Value;

            //To get added Connector stereotype
            string connectorStereotype = "";
            EA.EventProperty connectorStereotypePropID;
            connectorStereotypePropID = Info.Get(2);
            connectorStereotype = connectorStereotypePropID.Value;

            //To get added Connector client ID
            int clientID = 0;
            EA.EventProperty clientIDPropID;
            clientIDPropID = Info.Get(3);
            clientID = Convert.ToInt32(clientIDPropID.Value);

            //To get added Connector Supplier ID
            int supplierID = 0;
            EA.EventProperty supplierIDPropID;
            supplierIDPropID = Info.Get(4);
            supplierID = Convert.ToInt32(supplierIDPropID.Value);

            //To get added Connector diagram
            int diagramID = 0;
            EA.EventProperty diagramIDPropID;
            diagramIDPropID = Info.Get(5);
            diagramID = Convert.ToInt32(diagramIDPropID.Value);

            EA.Element sourceElemnet = Session.Repository.GetElementByID(clientID);
            EA.Element destinationElemnet = Session.Repository.GetElementByID(supplierID);

            if (sourceElemnet != null && destinationElemnet != null)
            {
               //Your condition based on when the connector needs to be created.



                return true;
            }

           MessageBox.Show("This connection is not possible.");
            return false;
        }
        catch (Exception ex)
        {
            MessageBox.Show("This connection is not possible.");
           return false;
        }
    }

Returninbg TRUE ,connector will be created. Returning FALSE ,Connector won't be created

希望它能帮助您在创建连接器时删除连接器。