Adding a Shape from a Stencil via Automation?

S

Scott Metzger

Hi,

How would I take a shape from one document or a stencil and add that
shape to a new page for my current drawing?

This is an MFC C++ .exe using the drawing control and I have imported
the Visio 2003 Type Library.

I have attempted to load the shape from a stencil by doing the following:
1) Open the stencil
2) Loop through the Masters in the Stencil to find the one I want.
3) Get the Shapes object from the Master object
4) In my solution there will be a single Shape object in the collection,
so I get the first one.
5) Add a new page to the drawing document
6) Add a new Layer to the new page.
7) Add the Shape from step 4 to the Layer.

I get an unhandled exception on step 7.

Here is some code...
CVPages vPages = MyDocument->get_Pages();
CVPage vPage = vPages.Add();
CVLayers vLayers = vPage.get_Layers();
CVLayer vLayer = vLayers.Add("Top");
vShapes = vMaster.get_Shapes();
junkVar.iVal = 1;
CVShape docShape = vShapes.get_Item(junkVar);
CString docShapeName = docShape.get_Name();
vLayer.Add((LPDISPATCH)&docShape, 1); // unhandled exception here

Any ideas?

Thanks,
Scott Metzger
 
P

Peter Suter

Hi Scott

I think, you can't add your master to your layer directly.
After point 6) you have first to "DROP" your master to the page (see Help)
This will give you a new shape-object.
Add this shape to your layer.

Ok?

Peter
 
S

soso

Peter Suter said:
Hi Scott

I think, you can't add your master to your layer directly.
After point 6) you have first to "DROP" your master to the page (see Help)
....

In VB.NET it could look like following:

Public Function MyAddShape(ByVal sMasterName As String, ByVal nX As
Integer, ByVal nY As Integer)
Dim VisPage As Page
Dim VisMaster As Master
Dim VisShape As Shape
Dim VisStencil As Document = ...Init it by a Stencil somewhere
before
Dim VisApp As Application = ...Init it by Visio app instance
somewhere before

Try
VisMaster = VisStencil.Masters(sMasterName)
VisShape = VisApp.ActivePage.Drop(xVisMaster, nX, nY)
Catch xException As Exception
MessageBox.Show("Can't add shape with the name '" & sMasterName
& "'", _
"Error",
MessageBoxButtons.OK)
Exit Function
End Try
End Function ' MyAddShape
 
M

Mai-lan [MS]

A quick note to add: if you're dropping more than one shape on a page, here's a couple performance enhancements you can make
1. Use the DropMany metho
2. Drop all the shapes on the page and after all the shapes are on the page, lay them out. It is much more performant than trying to layout shapes one by one as they are dropped

Thanks
Mai-la
 
M

Mark Nelson [MS]

A couple more performance tips:
- If you drop shapes on a page and then invoke layout, do not drop every
shape in the same spot like (0,0). Do something simple like increment the X
or Y coordinate each time a shape is dropped. Having a stack of shapes in
one place makes Visio do more work to keep track of shapes on the page.
- If you are generating a multi-page drawing using automation, create all
your pages first and then go back and populate them with shapes. The
performance difference can be as much as 40% of the overall execution time.

--
Mark Nelson
Microsoft Corporation

This posting is provided "AS IS" with no warranties, and confers no rights.


Mai-lan said:
A quick note to add: if you're dropping more than one shape on a page,
here's a couple performance enhancements you can make:
1. Use the DropMany method
2. Drop all the shapes on the page and after all the shapes are on the
page, lay them out. It is much more performant than trying to layout shapes
one by one as they are dropped.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top