The Finder API is the most used scripting API in medini and we have already several articles about the API and its usage. For some use cases however, its not sufficient to just search over a certain scope and using certain criteria but rather filter elements based on the fact, that they are visible on a diagram or on any diagram at all.
A while ago there has been a topic on scripting against medini diagrams and it was noted there already that diagrams are somewhat special and not by default part of the containment hierarchy - so basically more difficult to search and query for.
See the article on diagram links. In that article we uploaded a diagram.js scripting library which can be used to work with diagram shapes and connections.
For the above mentioned use case we now have added several functions to that library to make filtering based on diagram visibility much easier. The library introduces three more methods:
- onDiagram(diagram, element): returns true if a given element is present on the given diagram, where diagram is a PJDiagram hook and element is an arbitrary model element (shape or connector kind)
- onAnyDiagram(diagrams, element): the same functionality but based on an array of diagrams (easy to pass the result of a finder query for diagrams)
- onAnyDiagramInScope(scope, element): lastly a version that accepts a scope element as first parameter. The scope is searched for diagrams, then the second method is called with that
Sample code looks like:
// search for requirements that are visible on diagrams
if (selection) {
finder = Global.getFinder(selection[0]);
var reqs = finder.findByType(Metamodel.safetygoals.SafetyRequirement)
.asArray();
// requirement on any diagram?
reqs.forEach(function(r) {
var onAnyDia = onAnyDiagramInScope(selection[0], r);
console.log("Req {0} is on diagram: {1}", r.identifier, onAnyDia);
});
}
Of course, the diagram.js library must be loaded before.
Happy Scripting
Jan