Visual Studio Developer Center > Visual Studio Forums > Visual Studio Extensibility > Initial location of programmatically created shape in DSL Tools
Ask a questionAsk a question
 

AnswerInitial location of programmatically created shape in DSL Tools

  • Tuesday, October 27, 2009 2:42 AMStijnVNH Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    How can I setup the initial position of an element (Element A) which I create programmatically? The creation of that element occurs when the user of the DSL makes a connection between two other elements (Element B). Instead of drawing a connector between the two B's, I let the DSL connect a B with a newly created A and the A with the other B: B----A----B. I have this working by using a custom connection builder with the following Connect() method:

    public static void Connect(ModelElement sourceElement, ModelElement targetElement)
    {
        B source;
        B target;
        A a;
    
        source = sourceElement as B;
        target = targetElement as B;
    
        a = new A(source.Partition);   
        source.AElements.Add(a);
        a.B= target;
    }
    
    


    I have tried to set the location of Element A with an AddRule as suggested here: http://social.msdn.microsoft.com/forums/en-US/vsx/thread/225836cb-9d86-4035-922c-1e21cc373a62 by Pedro Silva, but the shape.Location = ...; line seems to be ignored. Only when I save and re-open the diagram, the shape is moved to the new location.

    I also tried to solve my problem by overriding the OnShapeInserted() method of Element A. This seems to work, but then I have to use an extra transaction and thus I get an extra (unwanted) undo entry when using the DSL.

    Is there a way to force the shape to use the new location in my AddRule try or is there a way to hide the extra undo entry when overriding OnShapeInserted()?
    • Edited byStijnVNH Tuesday, October 27, 2009 2:55 AMbad code formatting
    •  

Answers

  • Tuesday, November 03, 2009 7:13 PMDuncanPMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Normally, shape creation and placement is done automatically by a process called view fixup. However, you can explicitly create and place shapes if necessary. The following code snippet creates a new element and shape every time the user double-clicks on the design surface.


    partial class TestMinLanguage1Diagram
    {
        public override void OnDoubleClick(Microsoft.VisualStudio.Modeling.Diagrams.DiagramPointEventArgs e)
        {
            base.OnDoubleClick(e);
            AddAndPositionShapeAndElement(this, e.MousePosition);
        }
    
        private static void AddAndPositionShapeAndElement(TestMinLanguage1Diagram diagram, PointD createLocation)
        {
    
            using (Transaction t = diagram.Store.TransactionManager.BeginTransaction("Add and position elements"))
            {
    
                ExampleModel rootElement = diagram.ModelElement as ExampleModel;
                
                // Add a new element and link it to the model
                ExampleElement newElement = new ExampleElement(rootElement.Partition);
                rootElement.Elements.Add(newElement);
    
                // Create a new shape that represents the new element,
                // associate it with the new element, 
                // size it, position it and link it to the diagram.
                ExampleShape newShape = new ExampleShape(diagram.Partition);
                newShape.ModelElement = newElement;
                newShape.AbsoluteBounds = new Microsoft.VisualStudio.Modeling.Diagrams.RectangleD(createLocation, new SizeD(1,1));
                diagram.NestedChildShapes.Add(newShape);
                
                t.Commit();
            }
    
        }
    
    }<br/>
    

All Replies

  • Tuesday, November 03, 2009 7:13 PMDuncanPMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Normally, shape creation and placement is done automatically by a process called view fixup. However, you can explicitly create and place shapes if necessary. The following code snippet creates a new element and shape every time the user double-clicks on the design surface.


    partial class TestMinLanguage1Diagram
    {
        public override void OnDoubleClick(Microsoft.VisualStudio.Modeling.Diagrams.DiagramPointEventArgs e)
        {
            base.OnDoubleClick(e);
            AddAndPositionShapeAndElement(this, e.MousePosition);
        }
    
        private static void AddAndPositionShapeAndElement(TestMinLanguage1Diagram diagram, PointD createLocation)
        {
    
            using (Transaction t = diagram.Store.TransactionManager.BeginTransaction("Add and position elements"))
            {
    
                ExampleModel rootElement = diagram.ModelElement as ExampleModel;
                
                // Add a new element and link it to the model
                ExampleElement newElement = new ExampleElement(rootElement.Partition);
                rootElement.Elements.Add(newElement);
    
                // Create a new shape that represents the new element,
                // associate it with the new element, 
                // size it, position it and link it to the diagram.
                ExampleShape newShape = new ExampleShape(diagram.Partition);
                newShape.ModelElement = newElement;
                newShape.AbsoluteBounds = new Microsoft.VisualStudio.Modeling.Diagrams.RectangleD(createLocation, new SizeD(1,1));
                diagram.NestedChildShapes.Add(newShape);
                
                t.Commit();
            }
    
        }
    
    }<br/>