Excel — Drawing Automated Shapes with PyXLL Sep 2023

Excel — Drawing Automated Shapes with PyXLL Sep 2023

How's It Made

The Excel Drawing Function

Using PyXLL, I have programmed automatic shapes in Excel. That is, I built a function that draws an arbitrary shape in a user spreadsheet: the shape is defined by a range of coordinates; as the coordinates are edited, a new shape can be drawn.

The shapes are just standard Excel shapes, they can be easily copied, translated, rotated, flipped, scaled, modified in any way.

Shapes/Paths

Shapes are simply closed paths. An open path traverses the user vertices. A closed path traverses the user vertices and returns to the first vertex.

Use Case

My most serious use case is drawing diagrams for engineering work.

Features

  • The shape is drawn accurately to scale.
  • Because the function is in PyXLL, it can be used in any standard (xlsx) spreadsheet
  • That is, it does not require a macro spreadsheet (xlsm)
    or an add-in (xlam).
  • The shape coordinates are in a standard Excel range; the range can be on any worksheet
  • The shape/path is drawn by a function; the function is called from an Excel cell formula
  • There are two boolean parameters:
    first, closed (default) or open shape
    second, arrow on the first line segment (default) or not
    tcmd function signature
  • Therefore, 4 possible combinations:
    Title Page
    Note closeup of the arrow

Side Note

Using PyXLL, I also programmed a shape building tool based on turtle commands.  I built a function called tcmd (turtle command) that allows the following data commands:

  • x 7 → set xnew to 7
  • y 8.5 → set ynew to 8.5
  • xy 4 6 → set xynew to [4, 6]
  • lt 15 → turn left 15 degrees
  • rt 45 → turn right 45 degrees
  • hd 45 → set heading to 45 degrees (0 = +x; 90 = +y)
  • fd 3 → use current heading; move forward 3

For many applications, I find it (an order of magnitude) easier to specify coordinates using ‘path’ commands as used in turtle software.

I import the TNavigator class from standard Python turtle. I take advantage of two functions within, xcor and ycor to build a coordinate list from a series of turtle commands entered in Excel.

Note that the (Part 1) processing of turtle commands is separated from the (Part 2) drawing of the shape. I use standard Excel and PyXLL script to draw the shape. This two-part approach eliminates the need for an appurtenant turtle canvas. Further, the standard Excel shapes can be mixed, matched, formatted, copied, edited, deleted like and with any manually drawn Excel shape.

For instance to draw a pentagon with side of length 5 (no sines or cosines!):

  • fd 5
  • rt 72
  • repeat 5 times

Even if no drawing is needed, the turtle tcmd approach easily calculates coordinates for other Excel uses.

Here is a screenshot of tcmd calculating the pentagon coordinates


TCMD formulas
Using TCMD to draw AISC beams (formulas)
The dimensions are from a PyXLL database function
(AISC Properties in Excel: Almost Magic Oct 2017)
TCMD shapes
Using TCMD to draw AISC beams (shapes)
TCMD formula details
My database formulas in named ranges
TCMD formula details
Formula details (accessing named ranges)

Shear and Moment Diagram

The above shows the shear and moment diagram for a propped cantilever. The leancrew link has a full explanation of the mechanics involved.

https://leancrew.com/all-this/2015/08/super/


A couple examples of Python use in my structural engineering

Leave a Reply

Your email address will not be published. Required fields are marked *