2016-08-20 9 views
0

我正在使用特定的關聯連接器C連接到源element Atarget element BEnterprise Architect如何通過c#插件刪除企業架構圖中的連接器

如果用戶使用不同的源和目的地元件欲顯示警告消息並使用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"); 
      } 

     } 

回答

0

以下方法可用於基於條件在元素之間添加連接器。當用戶將一個新的連接器從工具箱或資源窗口拖到一個圖上時,它將被觸發。在創建連接器之前立即提供通知,以便加載項可禁用連接器的添加。

/// <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,連接器將被創建。 返回FALSE,將不會創建連接器

希望它可以幫助您在創建自己時刪除連接器。

+0

謝謝。 。它工作正常... – rashmi

+0

@rashmi如果解決了,請接受它作爲答案 – Arshad

1

嘗試這段代碼:

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是關鍵字。

+0

我試過這個代碼..still連接器沒有自動刪除 – rashmi

+1

檢查connectorID是否正確:打印並使用SQL窗口找到它:'SELECT * FROM t_connector WHERE connector_id = ' –