Creating, Deleting and Modifying Elements
In this chapter, we're going to learn how to create and modify elements. The most important is that you should not create or modify elements directly like var class1 = new UMLClass() or class1.name = "New Name" because all changes should be done via operations which supports by undo and redo.

Creating elements

Creating a model element

You can call createModel function of app.factory to create a model element with an option object.
The option object may have following fields:
    id : ID of factory function to create an element. To see the full ID list, execute app.factory.getModelIds().
    parent : A parent element where the created element to be contained.
    field (optional) : Field name of the parent element (default is ownedElements).
    modelInitializer (optional) : A function to initialize the created model element.
1
// Get a reference to top-level project
2
var project = app.repository.select("@Project")[0]
3
4
// Create a UMLModel element as a child of project
5
var model1 = app.factory.createModel({ id: "UMLModel", parent: project })
6
7
// Create a UMLClass element as a child of the model
8
var class1 = app.factory.createModel({ id: "UMLClass", parent: model1 })
9
10
// Create a UMLAttribute element and add to the field 'attributes' of the class
11
var attr1 = app.factory.createModel({ id: "UMLAttribute", parent: class1, field: "attributes" })
12
13
// Create a UMLClass with options
14
var options = {
15
id: "UMLClass",
16
parent: model1,
17
modelInitializer: function (elem) {
18
elem.name = "MyClass";
19
elem.isAbstract = true;
20
}
21
}
22
var class2 = app.factory.createModel(options);
Copied!
You can see the created elements in Model Explorer and undo and redo are available for each creation.

Creating a diagram

Call createDiagram function of app.factory to create a diagram with an option object:
The option object may have following fields:
    id : ID of Factory function to create a diagram. To see the full ID list, execute app.factory.getDiagramIds().
    parent : A parent element where the created diagram to be contained.
    options (optional) : An object containing the below options.
    diagramInitializer (optional) : A function to initialize the created diagram.
1
// Get a reference to top-level project
2
var project = app.repository.select("@Project")[0]
3
4
// Create a UMLModel element as a child of project
5
var model1 = app.factory.createModel({ id: "UMLModel", parent: project })
6
7
// Create a UMLClassDiagram as a child of the model
8
var diagram1 = app.factory.createDiagram({ id: "UMLClassDiagram", parent: model1 })
9
10
// Create a UMLClassDiagram with options
11
var options = {
12
id: "UMLClassDiagram",
13
parent: model1,
14
diagramInitializer: function (dgm) {
15
dgm.name = "MyDiagram";
16
dgm.defaultDiagram = true;
17
}
18
}
19
var diagram2 = app.factory.createDiagram(options)
Copied!

Creating a model element and a view element at once

Call createModelAndView function of app.factory to create a model element and a view element at once with an option object.
The option object may have following fields:
    id : ID of Factory function. To see the full ID list, execute Factory.getModelAndViewIds().
    parent : A parent element where the created model element to be contained.
    diagram : A diagram element where the created view element to be contained.
    modelInitializer (optional) : A function to initialize the created model element.
    viewInitializer (optional) : A function to initialize the created view element.
    x1, y1, x2, y2 (optional) : Rectangle coordinate to initialize position and size of the created view element.
    tailView, headView (optional) : If you try to create a relationship (e.g. UMLAssociation), the created view element connects these two view elements tailView and headView.
    tailModel, and headModel (optional) : If you try to create a relationship, the created model element connects these two model elements tailModel and headModel.
    containerView (optional) : A view element where the created view element to be contained.
The function createModelAndView returns the created view element, so you need to get the create model element by accessing model field. (e.g. classView1.model). Following code will create two classes and a association connecting the two classes.
1
// Create a UMLClass and UMLClassView
2
var options1 = {
3
id: "UMLClass",
4
parent: diagram1._parent,
5
diagram: diagram1,
6
x1: 100,
7
y1: 100,
8
x2: 200,
9
y2: 200
10
}
11
var classView1 = app.factory.createModelAndView(options1)
12
13
// Create another UMLClass and UMLClassView
14
var options2 = {
15
id: "UMLClass",
16
parent: diagram1._parent,
17
diagram: diagram1,
18
x1: 400,
19
y1: 100,
20
x2: 500,
21
y2: 200
22
}
23
var classView2 = app.factory.createModelAndView(options2)
24
25
// Create an association connecting the two classes
26
var options3 = {
27
id: "UMLAssociation",
28
parent: diagram1._parent,
29
diagram: diagram1,
30
tailView: classView1,
31
headView: classView2,
32
tailModel: classView1.model,
33
headModel: classView2.model
34
}
35
var assoView = app.factory.createModelAndView(options3)
Copied!

Creating a view element of an existing model element

Call createViewOf function of app.factory to create a view element of an existing model element with an option object.
The option object may have following fields:
    model : A model element referenced by the created view element.
    diagram : A diagram element where the created view element to be contained.
    viewInitializer (optional) : A function to initialize the created view element.
    x, y (optional) : Position of the created view element.
    containerView (optional) : A view element where the created view element to be contained.
1
var options = {
2
model: classView1.model,
3
diagram: diagram1,
4
x: 500,
5
y: 500,
6
}
7
app.factory.createViewOf(options)
Copied!
You will see the one more class view element at (500, 500).

Adding tags to an element

If you want to extend an element with additional tags, you can create tags by calling createModel function with Tag parameter of app.factory. There are five kinds of Tag: String, Number, Boolean, Reference, and Hidden. Hidden tags are not shown in diagrams, but other tags are shown as properties. (Check Format > Show Property menu). Following code will create a string tag to the selected element.
1
// Get a selected element
2
var selected = app.selections.getSelected()
3
4
// Options for creating a tag
5
var options = {
6
id: "Tag",
7
parent: selected,
8
field: "tags",
9
modelInitializer: function (tag) {
10
tag.name = "Tag1";
11
tag.kind = type.Tag.TK_STRING; // or TK_BOOLEAN, TK_NUMBER, TK_REFERENCE, TK_HIDDEN
12
tag.value = "String Value...";
13
// tag.checked = true; // for TK_BOOLEAN
14
// tag.number = 100; // for TK_NUMBER
15
// tag.reference = ...; // for TK_REFERENCE
16
}
17
}
18
19
// Create a tag to the selected element
20
var tag1 = app.factory.createModel(options)
Copied!

Deleting elements

To delete some elements, call app.engine.deleteElements function with model and view elements as arguments.
1
// Delete selected model elements
2
var models = app.selections.getSelectedModels()
3
app.engine.deleteElements(models, [])
4
5
// Or, delete selected view elements
6
var views = app.selections.getSelectedViews()
7
app.engine.deleteElements([], views)
8
9
// Delete all selected model and view elements
10
var models = app.selections.getSelectedModels()
11
var views = app.selections.getSelectedViews()
12
app.engine.deleteElements(models, views)
Copied!

Modifying elements

Change property value

You should not modify a property of an element directly like class1.name = "New Name" because all changes should be done via operations which supports by undo and redo.
To change property value, use app.engine.setProperty() function as below:
1
// Get a selected element
2
var selected = app.selections.getSelected()
3
// Change property value
4
app.engine.setProperty(selected, 'name', 'New Name')
Copied!

Examples

Sequence Diagram

Here is an example to create a Sequence Diagram including two Lifelines and a Message.
1
var project = app.repository.select("@Project")[0]
2
3
var model1 = app.factory.createModel({ id: "UMLModel", parent: project })
4
5
// Create a Sequence Diagram
6
var options = {
7
id: "UMLSequenceDiagram",
8
parent: model1,
9
diagramInitializer: function (dgm) {
10
dgm.name = "MyDiagram";
11
}
12
}
13
var diagram1 = app.factory.createDiagram(options)
14
15
// Create a Lifeline
16
var options1 = {
17
id: "UMLLifeline",
18
parent: diagram1._parent,
19
diagram: diagram1,
20
x1: 50,
21
y1: 20,
22
x2: 50,
23
y2: 20
24
}
25
var lifelineView1 = app.factory.createModelAndView(options1)
26
27
// Create another Lifeline
28
var options2 = {
29
id: "UMLLifeline",
30
parent: diagram1._parent,
31
diagram: diagram1,
32
x1: 150,
33
y1: 20,
34
x2: 150,
35
y2: 20
36
}
37
var lifelineView2 = app.factory.createModelAndView(options2)
38
39
// Create a Message connecting the above two Lifelines
40
var options3 = {
41
id: "UMLMessage",
42
parent: diagram1._parent,
43
diagram: diagram1,
44
x1: 50,
45
y1: 100,
46
x2: 150,
47
y2: 100,
48
tailView: lifelineView1,
49
headView: lifelineView2,
50
tailModel: lifelineView1.model,
51
headModel: lifelineView2.model
52
}
53
var messageView1 = app.factory.createModelAndView(options3)
Copied!
Last modified 8mo ago