index > Visual Studio Team System - Domain-Specific Language Tools > Add shapes and connectors to diagram programmatically

Add shapes and connectors to diagram programmatically


Hi all,

I would like to be able to add/delete shapes and connectors at runtime.

Is it possible? Sample code would be helpful. Thanks all.

Will96

Hi Will,

The standard way to do this is to create the domain elements and relationships that the shapes and connectors depict. So for example, starting from the "Minimal Language" solution template, you can write things like:

Suppose you add a command to create a new object for each selected object, and a relationship between them. (To see how to create a command, see the examples in Program Files\Visual Studio 2005 SDK\2006.06\VisualStudioIntegration\Samples\DslTools\Example.Customization\Example.ClassDesigner, and look in DslPackage\CtcComponents\ChangeAssociationSort)

internal void OnMenuChangeAssociationSort(object sender, EventArgs e)
{

foreach (object selectedObject in this.CurrentSelection)
{

ExampleElement e1 = object as ExampleElement;
if (e1 == null) continue;

// All updates must be done inside a transaction
using (Transaction t = e1.Store.TransactionManager.BeginTransaction())
{

// create new elements
ExampleElement e2 = new Component(e1.Store);

// add them to the tree
e1.ExampleModel = myModel; // same effect as myModel.Elements.Add(e1)
e2.ExampleModel = myModel;

// add a relationship link
e1.Targets.Add(e2);

t.Commit(); // creates the shapes and connectors and displays them
}
}
}





- Alan [Microsoft]
Alan Cameron Wills
Thank you, Alan. My problem was solved.

Here is another question: is there a way that I can load my language file programmatically? For example, I have "test.mydsl" and I want to add shapes in this file by clicking on context menu in the designer area.

Thanks again.
Will96

I haven't tried this myself, but if you take a look at DslPackage\DocData.cs, search on LoadModelAndDiagram. That will give you some hints as to what to do.

You will end up with two modelRoot instances. Your code then needs to decide how to fix that up so that you just have one.

Alan




- Alan [Microsoft]
Alan Cameron Wills
reply 4

You can use google to search for other answers

 

More Articles

Template transformation error in designer project
Shapes that are not directly related to the Domain class
How to Export diagram as image
Upgrade bug: Only 8.1 version of TextTemplating available on upgr...
UML Sequence diagram in DSL?
bidirectional connectors
A minor gotcha: reserved words
How it the class mentioned in directiveprocessor loaded?
Concepts behind the design of DSL tools
March CTP DSL uninstall / VS (RC) issue
Welcome to Bokebb   New Update   Joins the collection  
 

New Articles

Unable to Replace Old Files by New ones
Shape inside a shape
How to set the connector decorator posit…
Double Click on Compartment Item
Compartiment with multiple classes to be…
Can we host DSL designer in a custom hos…
Domain Model Definition Schema
Making a reference from a connector class
How to find the parent of a relationship…
Define a menu command
change literals of enumeration property …
How to change the font of a shape?
Can I make two DSL models interact ?
Adopt model manually
What is the future of MDA?

Hot Articles

November release Designers compile errors
How to Block Self-Relationship
Transform Model to DSL
How to access to the shape connectors by…
Compartiment with multiple classes to be…
Problem running Debug solution
November DSL Toolkit Release: where are …
Right mouse click on model results in a …
Dynamically Create Links OnAttributeChan…
Is there any documentation regarding the…
How to expose domain model objects throu…
Problem loading different models in Dire…
Production quality planning of MS DSL to…
VS Host Assembly Resolving and parameter…
Jochen Seemann's presentation on PDC05

Recommend Articles

Migrating compartments
Problem Installing DSL Tools September 2…
How to add a custom rule that listens on…
Enable/Disable property OnValueChanging/…
Own shape decorators
Licensing & pricing
Use of the TextTransform tool
schedule for beta and V1 releases?
Shape Icon and Shape Tool Minor Bug
Migrating to May CTP: Text Template Prob…
newbie question,thank you for your atten…
Come Join the World's Largest Software F…
How access to a connector property
How to find the parent of a relationship…
Preventing model elements from being del…