rashmi
rashmi

Reputation: 203

Restricting child element in Enterprise Architect through C# add-in

I am working on C# add-ins in Enterprise Architect to give a restriction to user so that only a particular child element can be added to a specific parent element.

For example if child element A must be dropped on parent element B it is deleted if child element A is dragged and dropped on parent element C. I am using EA_OnPostNewElement method and a delete method for the same and it works fine.

My doubt is, after the user has dropped the child element on the specific parent, after some time he can drag the child element outside the parent element and add it as a child to any other element in the diagram.

Is there a way to add a restriction here by observing the changes made by user on Enterprise architect GUI and bring back the child element to original parent location. Kindly help.

Upvotes: 0

Views: 259

Answers (2)

Dah Sra
Dah Sra

Reputation: 4425

You need to use both EA_OnContextItemChanged and EA_OnNotifyContextItemModified so you can achieve it .

declare a dictonry

 public Dictionary<int, int> lstChildElements = new Dictionary<int, int>();

and here is the sample code

public void EA_OnContextItemChanged(EA.Repository Repository, string GUID, EA.ObjectType ot)
    {

        EA.Element addedElement = Repository.GetElementByGuid(GUID);
        if (addedElement == null)
            return;
        int identifiedParentID = 0;
        bool isAvailable = lstChildElements.TryGetValue(addedElement.ElementID, out identifiedParentID);
        if (!isAvailable)
        {
            if (addedElement.Stereotype == "Child" && addedElement.ParentID != 0)
            {
                EA.Element parentElemnt = Session.Repository.GetElementByID(addedElement.ParentID);
                if (parentElemnt != null)
                    if (parentElemnt.Stereotype == "anyCustomDefined")
                        lstChildElements.Add(addedElement.ElementID, addedElement.ParentID);
            }
        }


    }

    public void EA_OnNotifyContextItemModified(EA.Repository Repository, string GUID, EA.ObjectType ot)
    {

        EA.Element addedElement = Repository.GetElementByGuid(GUID);
        if (addedElement == null)
            return;
        int identifiedParentID = 0;
        bool isAvailable = lstChildElements.TryGetValue(addedElement.ElementID, out identifiedParentID);
        if (isAvailable)
        {
            if (addedElement.Stereotype == "Child" && addedElement.ParentID != 0)
            {
                EA.Element parentElemnt = Repository.GetElementByID(addedElement.ParentID);
                if (parentElemnt != null)
                    if (parentElemnt.Stereotype != "anyCustomDefined")
                    {
                        addedElement.ParentID = identifiedParentID != 0 ? identifiedParentID : addedElement.ParentID;
                        addedElement.Update();
                        lstChildElements[addedElement.ElementID] = addedElement.ParentID;
                    }
            }
            else if (addedElement.Stereotype == "Child" && addedElement.ParentID == 0)
            {
                addedElement.ParentID = identifiedParentID;
                addedElement.Update();
            }
        }



    }

Hope it helps..!

and for updating in diagram need to reload it.

EA.Diagram activeDiagram = Session.Repository.GetCurrentDiagram();
        if (activeDiagram != null)
            Session.Repository.ReloadDiagram(activeDiagram.DiagramID);

or

Repository.RefreshOpenDiagrams();

Both the codes can be used for reloading the diagram.

Upvotes: 1

Geert Bellekens
Geert Bellekens

Reputation: 13740

You can use the context events to keep track of the selected element and it's owner.

I'm not sure if the event EA_OnNotifyContextItemModified is being fired when you change the owner of an element. But even it that is not the case you can verify if it still has the same owner after a new element has been selected.

Upvotes: 0

Related Questions