Share this:

" /> Visio Guy » Polygon Maker
2002 ford thunderbird very short hair styles for women 2011 thunderstorm wallpaper aston martin lagonda suv rover 3500 cool computer backgrounds for girls funny laws in united states glass flowers 2004 volvo v40 cute quotes on pictures 1983 malanca 125 e 2 cs ob one 5 n maybach exelero 2009 2008 vectrix electric maxi scooter map of africa and europe 2006 peugeot tkr 50 fahrenheit wallpaper dodge challenger 2009 wallpaper is justin bieber gay buy loreal makeup mitsubishi lancer evolution 8 wallpaper cool wallpapers for mobile phones romanticism art nature ural retro solo 750 jessica biel justin timberlake kiss bmw 118d coupe 1988 kawasaki kmx 125 (reduced effect) toyota corolla 2001 le toyota avalon 2010 change wallpaper map of norway airports map
Home » Code, Shapes, Tools

Polygon Maker

Submitted by on November 24, 2006 – 4:10 pm | | 22471 views 3 Comments

Another question popped up on the newsgroup forums today asking about making polygon shapes in Visio. This triggered a neuron, and I was off like a flash to perform a search of my …\Visio directory.

Sure enough, I found “PolygonMaker 2002.vsd”, which I have prettied-up for presentation to you today…

Note: Visio does have some polygon shapes. But they only provide three to eight sides. You can find them here: File > Shapes > Visio Extras > Drawing Tools. The masters are called: Multigon Edge and Multigon Center. You can change the number of sides by right-clicking the shapes and choosing from the menu.

The download that accompanies this post is more flexible in that it uses VBA code to create brand-spankin’ new shapes. This code, of course, allows you to create polygons with any number of sides.

A Quick Note About VBA Macros

The drawing has a Visual Basic for Applications (VBA) project inside it. You’ll have to enable macros, and allow them to run in order for the Polygon Maker to function.

While I could tell you to Always trust software from Visio Guy, that would be irresponsible of me. I urge you to examine all VBA code before you actually run it. A good way to do this is to follow these steps:

  1. Under Tools > Macros > Security, set your macro protection level to Medium.
    This allows you to enable or disable VBA macros to run when you open the drawing.
  2. The first time you open a drawing from a little-known source, simply click the Disable Macros button that appears in the pop-up dialog.
  3. Hit Alt + F11 to open the VBA project. Examine the various modules to make sure there’s no code that deletes your Windows installation or formats your hard drive.
  4. Once you believe that the code is on the up-and-up, you can reopen the .vsd and click Enable Macros. Now you’ll get the full benefit of the code behind the drawing!

Code

I won’t post all of the code here, but just some of the key bits. We’ll ignore all the UI stuff and just post the portion that creates the Visio shape, with its geometry sections.

You can below see that DrawShape takes two arguments. One simply specifiec the number of sides in the polygon, the other specifies the radius of the circle in which the polygon is circumscribed. The radius value is in inches, so you metric folks need to multiply by 25.4 if you want think in millimeters.

Private Sub DrawShape(iNumSides, dRadiusInches)

  Dim i As Integer
  Dim shp As Visio.Shape
  Dim xy() As Double
  Dim ang As Double, angDelta As Double</span>

  '// Create an array to hold all of the points:
  ReDim xy(1 To iNumSides * 2 + 2)</span>

  angDelta = 3.14159265358 / iNumSides

  '// Use trigonometry to calculate each vertex:
  For i = 1 To UBound(xy) Step 2
    ang = (i - 2) * angDelta
    xy(i) = dRadiusInches + dRadiusInches * VBA.Math.Cos(ang)
    xy(i + 1) = dRadiusInches + dRadiusInches * VBA.Math.Sin(ang)
  Next i

  '// Use Visio's DrawPolyline function to create the shape:
  Set shp = Visio.ActivePage.DrawPolyline(xy, 0)
  '// flag = visPolyline1D or visPolyarcs or just 0

  '// Close off the polygon by setting the last geometry
  '// row's formulas to reference the first row:
  shp.Cells("Geometry1.X2").Formula = "Geometry1.X1"
  shp.Cells("Geometry1.Y2").Formula = "Geometry1.Y1"

  '// Set the polygon to be filled:
  shp.Cells("Geometry1.NoFill").Formula = "FALSE"

End Sub
Visio Shape Polygon Maker (40.65 KB) - 329

Geek Notes

It would be useful to build these polygon shapes such that the shapes are exactly R x R in width and height, and the polygon is exactlly centered inside of this rectangle. (R is the radius of the circle which circumscribes the polygon)

The center of such a shape would match the center of the polygon as well. The current shapes are “tightly fit” and the center of the shape does not always match the center of the polygon (for odd number of sides.)

Well, perhaps an update someday…or a reader contribution? ;)

3 Comments »

Leave a comment!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.

*