- > Do you need the 2012 VERSION?
UML elements on any UML diagram can be linked to another element, UML diagram, or parts of any other file by dragging. Open the linked item by double-clicking. This sample is an extension of Visual Studio Ultimate.
For example, you could:




You could extend this sample code to do more - for example:
The source code demonstrates a number of techniques:
You can easily adapt this sample code. For example:
Multiple Links
The sample code allows each element to be linked to only one item. But the IReference mechanism can accept multiple references. To adapt the code to allow multiple links:
protected void SetReference(IElement element, string fullFilePath)
{
element.AddReference(ModelLinkReferenceTag, RelativeFilePath(fullFilePath), true);
}
protected void SetReference(IElement element, string fullFilePath) { element.AddReference(ModelLinkReferenceTag, RelativeFilePath(fullFilePath), true); }
public static bool HasReference(IElement element)
{
return element != null && element.GetReferences(ModelLinkReferenceTag).Count() > 0;
}
public static bool HasReference(IElement element) { return element != null && element.GetReferences(ModelLinkReferenceTag).Count() > 0; }
IEnumerable<string> references = element.GetReferences(ModelLinkReferenceTag).Select(r => r.Value);
string theReference = references.Count() > 1
? UserChooseOne(references)
: references.FirstOrDefault();
if (theReference != null) ...
IEnumerable<string> references = element.GetReferences(ModelLinkReferenceTag).Select(r => r.Value); string theReference = references.Count() > 1 ? UserChooseOne(references) : references.FirstOrDefault(); if (theReference != null) ...
URLs
The sample code lets you link URLs to an element. When you double-click the element, the URL opens in the VS browser. But to create a link to a URL, you have to follow a slightly odd procedure: first drag the URL from the address bar of your browser onto the Windows desktop, to create a .url shortcut; then drag the shortcut onto the UML diagram element; then you can delete the .url shortcut.
You could improve the code by discovering how to extract the URL from the IDataObject that is passed when the user drags from a browser's address bar directly onto the diagram. Bear in mind that different browsers might behave differently in this respect.
When you've cracked it, kindly post the result on the Visualization and Modeling SDK Forum.
Layer Diagrams
The sample code works only with UML diagrams, and not with layer diagrams. You could add code to let users link layers to other items. I'd suggest you consider the following points:
/// <summary>
/// Set the reference on an element to point to a file.
/// </summary>
/// <param name="element">shape whose element is to be linked</param>
/// <param name="fullFilePath">UML diagram file path</param>
protected void SetReference(ShapeElement shape, string fullFilePath)
{
IElement dropElement = shape.ModelElement as IElement;
if (dropElement != null)
{
// It's a UML model element. Add a Reference:
SetReference(dropElement, fullFilePath);
}
else
{
IShape ishape = shape.CreateIShape();
if (ishape != null)
{
ILayerElement layer = ishape.GetLayerElement();
if (layer != null)
{
// It's a Layer. Add a Property:
layer.Properties[ModelLinkReferenceTag] = RelativeFilePath(fullFilePath);
}
}
}
}
/// <summary> /// Set the reference on an element to point to a file. /// </summary> /// <param name="element">shape whose element is to be linked</param> /// <param name="fullFilePath">UML diagram file path</param> protected void SetReference(ShapeElement shape, string fullFilePath) { IElement dropElement = shape.ModelElement as IElement; if (dropElement != null) { // It's a UML model element. Add a Reference: SetReference(dropElement, fullFilePath); } else { IShape ishape = shape.CreateIShape(); if (ishape != null) { ILayerElement layer = ishape.GetLayerElement(); if (layer != null) { // It's a Layer. Add a Property: layer.Properties[ModelLinkReferenceTag] = RelativeFilePath(fullFilePath); } } } }
IShape ishape = sourceShapeElement.CreateIShape();
if (ishape != null)
{
ILayerElement layer = ishape.GetLayerElement();
if (layer != null)
{
layer.Properties.TryGetValue(ModelLinkReferenceTag, out reference);
}
}
IShape ishape = sourceShapeElement.CreateIShape(); if (ishape != null) { ILayerElement layer = ishape.GetLayerElement(); if (layer != null) { layer.Properties.TryGetValue(ModelLinkReferenceTag, out reference); } }
For more about the Layer API, see Creating Extensions for Layer Diagrams.
To install the extension on other computers:
To uninstall it, use Tools>Extension Manager in Visual Studio.
You can work with models that contain links on computers where the extension is not installed. The links should not be lost.
This sample is provided solely for demonstrating techniques that you could use in your own programs. It has not been thoroughly tested.
Please post suggestions and questions in the Visual Studio Visualization and Modeling SDK Forum.
Changes