Book a Demo

Author Topic: How to select element and scroll it in visible area of diagram  (Read 6386 times)

pssp.sven

  • EA Novice
  • *
  • Posts: 8
  • Karma: +0/-0
    • View Profile
Hi,

what I've achieved so far is to open the diagram and select the element or connector. But I also need to scroll the element in the visible area of the diagram. Otherwise the user needs to search for the selection on large diagrams.
There is the "Find in all diagrams..." command in the context menu of the project browser. Maybe it is possible to call this command somehow but it should be possible to skip the diagram selection dialog and to tell the command which diagram should be directly used instead.

Here is a short snippet of the code:

Code: [Select]
                EA.Diagram diagram = _eaRepository.GetDiagramByGuid(diagramId.ToString("B"));
                _eaRepository.OpenDiagram(diagram.DiagramID);
                this.UnselectAllElementsOf(diagram);

                // Select element in diagram.
                if (isConnector)
                {
                    EA.Connector connector = _eaRepository.GetConnectorByID(objectId);
                    diagram.SelectedConnector = connector;
                    diagram.SelectedConnector = connector; // TODO: Find better solution. Need to execute this twice otherwise connector is not selected?!
                }
                else
                {
                    diagram.SelectedObjects.AddNew(objectId, string.Empty);
                }

                // TODO: Scroll selection in to center of diagram window.

I additionally would like to know how the second selection of the connector (diagram.SelectedConnector = connector;) can be avoided. If this isn't called a second time the selection is not visible in the diagram.

I really appreciate your help, thanks in advance,
Sven

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: How to select element and scroll it in visible area of diagram
« Reply #1 on: March 15, 2018, 01:46:31 am »
A relatively new operation that has been add-in recently is

EA.Diagram.FindElementInDiagram (long ElementID)

From the help file:
Quote
Boolean

Notes: This function activates the Diagram View and displays the diagram with the diagram object selected. If the diagram is too large to display all of it on the screen, the portion of the diagram containing the object is displayed with the object shown in the center of the screen. Diagram objects flagged as non-selected are shown but are not selected

Returns True if the diagram object was found, the diagram displayed and the object selected (or at least displayed) in the view. Returns False if the diagram object was not found in the diagram and the diagram not displayed.

Parameter

ElementID: Long - the element ID of the diagram object to locate

I have not yet used it myself.

With regards to the selection of the connector. I think it already works the first time, but it is not yet visible because the focus is still somewhere else.
I've seen similar thing when selecting the connector from the EA Navigator.

In that case, if doing it twice solves the problem for you, I wouldn't worry too much. I've make far worse workarounds  ::)

Geert

Guillaume

  • EA Practitioner
  • ***
  • Posts: 1405
  • Karma: +42/-2
    • View Profile
    • www.umlchannel.com
Re: How to select element and scroll it in visible area of diagram
« Reply #2 on: March 15, 2018, 02:32:42 am »
Excellent, EA.Diagram.FindElementInDiagram is exactly what I've recently been looking for.
I tested it and confirm that it selects the element in the diagram and focuses onto it.

When used in an add-in, will it work with any version of EA or only with versions where the API supports this method?
Guillaume

Blog: www.umlchannel.com | Free utilities addin: www.eautils.com


Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: How to select element and scroll it in visible area of diagram
« Reply #3 on: March 15, 2018, 04:19:32 am »
When used in an add-in, will it work with any version of EA or only with versions where the API supports this method?

No it won't, and you have to be careful not to call it on a version of EA that doesn't support it.
Even calling it conditionally in a property get can be a problem as (for some reason) it still somehow notices the operation is not available and throws an error.

Putting it in a separate method and testing the version of EA before actually using it works.

Geert

Guillaume

  • EA Practitioner
  • ***
  • Posts: 1405
  • Karma: +42/-2
    • View Profile
    • www.umlchannel.com
Re: How to select element and scroll it in visible area of diagram
« Reply #4 on: March 15, 2018, 10:11:39 pm »
Thanks for the info Geert.

Since this method was introduced in build 1305, I added a condition where FindElementInDiagram is run if Repository.LibraryVersion > 1304, otherwise I run something like  selectedDiagram.SelectedObjects.AddNew(elt.ElementID.ToString(), "") ...

The latter option does not have the diagram focus updated.

Guillaume

Blog: www.umlchannel.com | Free utilities addin: www.eautils.com


pssp.sven

  • EA Novice
  • *
  • Posts: 8
  • Karma: +0/-0
    • View Profile
Re: How to select element and scroll it in visible area of diagram
« Reply #5 on: March 16, 2018, 09:32:34 am »
Hi Geert and Guillaume,

first of all I wanna thank you for your quick answers.
The new function FindElementInDiagram would be the perfect fit, so thanks for that Geert. Sadly I've to support EA12 (1215) where this is not available. Do you have any other ideas how to handle that? Probably any dirty Win32 API hacks would be possible, but I really would like avoid them.

Thanks, Sven

Quote
I've make far worse workarounds  ::)
I bet you have. :D  Seems to be pretty common regarding EA development... ;)

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: How to select element and scroll it in visible area of diagram
« Reply #6 on: March 16, 2018, 03:29:33 pm »
No, I think that is about it for version 12.
Even if you somehow managed to manipulate the scrolling through windows clickevents, I don't think there's any way of knowing which part of the diagram is being shown.

Geert

pssp.sven

  • EA Novice
  • *
  • Posts: 8
  • Karma: +0/-0
    • View Profile
Re: How to select element and scroll it in visible area of diagram
« Reply #7 on: March 26, 2018, 09:56:32 pm »
Hi Geert,

I've found a solution that works for my scenario. Maybe it's useful for someone else so I wanted to share it here.
The main idea is to use EA's shortcuts.

Scenario 1:
The element - that is not a connector - has just an instance on one diagram.
Solution:
Select the element in the project browser via repository.ShowInProjectView(element).
After that send the shortcut Ctrl-U for Find in all diagrams... to EA. That way the diagram opens and the element appears almost centered and selected in the view.

Scenario 2:
The element - that is not a connector - has several instances on more diagrams.
Solution:
Select the element in the project browser via repository.ShowInProjectView(element).
After that send the shortcut Ctrl-U for Find in all diagrams... to EA. That way a dialog pops up where the user can select which diagram he wants to open.

Scenario 3:
a) The element is a connnector.
b) The element has several instances on more diagrams but you want to directly open a special diagram without the selection dialog popping up.
Solution:
Open the diagram, select the element/connector and send a shortcut for Zoom in. The Zoom in does not only zoom into the view but also centers the selected item. But to be able to call the Zoom in the shortcut must be defined previously in Tools\Customize....


Here are some extension methods as code snippets to give you more than just the explanations above. If somebody wants to use this approach the Windows Input Simulator is required. Please have look here https://archive.codeplex.com/?p=inputsimulator.

Code: [Select]
        /// <summary>
        /// Select element in project browser.
        /// </summary>
        /// <param name="repository">The EA repository.</param>
        /// <param name="elementId">The ID of the <see cref="EA.Element"/>.</param>
        public static void SelectElementInProjectBrowser(this EA.Repository repository, int elementId)
        {
            // Select element in project browser.
            EA.Element element = repository.GetElementByID(elementId);
            repository.ShowInProjectView(element);
        }

        /// <summary>
        /// Sends the shortcut "Ctrl+U" to the EA window and executes the command "Find in all diagrams...".
        /// </summary>
        /// <param name="repository">The EA repository.</param>
        public static void CallFindSelectedItemOfProjectBrowserInAllDiagramsViaKeystroke(this EA.Repository repository)
        {
            // Open diagram and unselect all elements.
            var diagram = repository.GetCurrentDiagram();
            if (diagram != null)
            {
                UnselectAllElementsOf(diagram);
            }

            var sim = new InputSimulator();

            Application.DoEvents();
            EaWindow.Activate();
            sim.Keyboard.ModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.VK_U);
        }

        /// <summary>
        /// Sends the shortcuts "Ctrl + +" and "Ctrl + -" to the EA window and executes the commands
        /// "Zoom in" and "Zoom out". That way the selected items appear centered in the diagram.
        /// </summary>
        private static void CallZoomInAndZoomOutViaKeystrokes()
        {
            var sim = new InputSimulator();

            Application.DoEvents();
            EaWindow.Activate();
            sim.Keyboard.ModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.OEM_PLUS);

            Application.DoEvents();
            EaWindow.Activate();
            sim.Keyboard.ModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.OEM_MINUS);
        }


To complete the solution above it would be great to be able to define shortcuts via the addin. But that will be the subject of another topic...  ;)

Sven
« Last Edit: March 26, 2018, 10:00:11 pm by pssp.sven »

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: How to select element and scroll it in visible area of diagram
« Reply #8 on: March 26, 2018, 11:58:00 pm »
Thanks for the update Sven.

Might come in handy some day.

Geert