Visio Guy

Visio Discussions => Programming & Code => Topic started by: MrSir on August 19, 2014, 10:34:53 AM

Title: Dropping a shape inside another shape - Visio 2013
Post by: MrSir on August 19, 2014, 10:34:53 AM
Hello

Im using COM Interop to generate diagrams with C#. I cant figure out how to drop a shape inside another shape, i cant seem to find any methods for this on the shape object.
How do i drop a shape inside another shape?

Regards MrSir
Title: Re: Dropping a shape inside another shape - Visio 2013
Post by: Jumpy on August 19, 2014, 11:52:03 AM
Shape inside another shape. You mean grouping?

http://msdn.microsoft.com/en-us/library/office/ff769170%28v=office.15%29.aspx
Title: Re: Dropping a shape inside another shape - Visio 2013
Post by: MrSir on August 19, 2014, 12:16:24 PM
Hello Jumpy

No i dont want to group the shapes, i want to place a shape inside another shape, like the attached picture.

Regards MrSir
Title: Re: Dropping a shape inside another shape - Visio 2013
Post by: aledlund on August 20, 2014, 03:25:52 AM
retrieve the pinx/piny of shape1 (the outer shape) and then assign those values to shape2.
al
Title: Re: Dropping a shape inside another shape - Visio 2013
Post by: Yacine on August 20, 2014, 06:20:41 AM
Astonishing simple. It definitely helps reading carefully the question ;)
Title: Re: Dropping a shape inside another shape - Visio 2013
Post by: MrSir on August 21, 2014, 07:08:53 AM
Quote from: aledlund on August 20, 2014, 03:25:52 AM
retrieve the pinx/piny of shape1 (the outer shape) and then assign those values to shape2.
al

Thanks, i have tried with the following code:


public void encapsulateShape(int outerShapeId, int innerShapeId) {
    Shape outerShape = visioPage.Shapes.get_ItemFromID(outerShapeId);
    Shape innerShape = visioPage.Shapes.get_ItemFromID(innerShapeId);

    int xCoord = outerShape.get_Cells("PinX").get_ResultInt(VisUnitCodes.visMillimeters, (short)0.0);
    int yCoord = outerShape.get_Cells("PinY").get_ResultInt(VisUnitCodes.visMillimeters, (short)0.0);

    Console.WriteLine(xCoord + " - " + yCoord);
    Console.ReadKey();

    innerShape.SetCenter((double)xCoord, (double)yCoord);
}


But nothing happens, the shape is not moved. How do i move the shape?

Regards MrSir
Title: Re: Dropping a shape inside another shape - Visio 2013
Post by: Jumpy on August 21, 2014, 09:41:00 AM
I will assume that you tested that outershape and innershape are assigned and that xcoord, ycoord contain values.

From that I don't know, why the shape doesn't move, but if it would move, I don't know if it would move o the center of the outer shape, because your X/Y coordinates are in milimeter and the build in Visio functions normally expect the values in inch.
And no matter which measurements you use Xcoord/ycoord should be doubles not integers.

Instead of using SetCenter you could set PinX/Y of the shape individually.

And the function only works when the LocPinX/Y are in the center of both shapes. If they are not, the formula for the exact values to place innershape get more complicated.

Last but not least you have to ensure that innershape has a higher Z-order so it will not be hidden behind outer shape.
Title: Re: Dropping a shape inside another shape - Visio 2013
Post by: Paul Herber on August 21, 2014, 10:12:47 AM
MrSir, I would really recommend downloading the Visio SDK, it has many examples of basic Visio coding like this.