Working with Selections
In this chapter, we're going to learn how to get selected elements, force to select particular elements, and do something by listening to selection changed events.
Getting selected elements
Users can select elements in a diagram or in Model Explorer. Sometimes we may need to access only the selected elements.
We need to distinguish between selected views and selected models. If you select the Book class in a diagram, then there is a selected view (UMLClassView
) and a selected model (UMLClass
). If you select the Author class in Model Explorer, then there is a selected model (UMLClass
) and no selected views.
We can access selected elements using app.selections
as following:
Enforce to select particular elements
To select a model element in Model Explorer, use ModelExplorerView module. (Assume that Book.mdj
used in Accessing Elements were loaded)
To scroll automatically so as to show the element, pass true
value as the second parameter.
To select a view element in diagram, use app.diagrams
. You can find more functions about selection in API Reference.
Listening selection changed event
Now we will show how to listen and handle a selection change event. An array of selected model elements and an array of selected view elements are passed to the second and the third parameters respectively to the callback function.
Last updated