Creating Drawfiles in BASIC with DrawGen (part 1)

DrawGen Basic is an easy way to create drawfiles from simple Basic programs. It is much easier to design accurate diagrams from a program than it is using an editor like Draw, and you can also create figures which would be almost impossible using Draw itself.

In a series of articles in Archive 10.6, 10.7 and 10.12, Jim Lesurf described how to create drawfiles from programs using an application called DrawGen. He mainly concentrated on using C, but briefly mentioned using Basic in issue 10.12.

DrawGen Basic is a further development of his system, and is an even easier way to produce a drawfile from a Basic program. It is a library of procedures and functions called DGBasLib.

DGBasLib is freeware. You may freely copy and distribute it, provided that it is complete, and that you don't sell it.

This article is an introduction to DrawGen Basic, so that you can see for yourself how easy it is to create drawfiles with exact measurements, or complex diagrams.

First steps

  1. The first thing you should do is copy the DrawGen application and the Programs folder to your hard disc.
  2. Double-click with select on the DrawGen icon. This will load the DrawGen module and set up some things ready for use. You only have to do this once. Of course, if you switch off your computer and then switch back on, the module will have disappeared, and you have to install it again by double-clicking.
  3. To check that the DrawGen module has been installed, press <f12>, then type:
    DrawGen help

If the DrawGen module has been loaded, some information on the default settings of DrawGen will be printed out.

If you have a StrongARM machine, it may be that you have an old copy of the ABCLibrary module - this may prevent DrawGen working. To avoid this problem, people who have a StrongARM should ensure that they have version 4.04 of the ABCLibrary module loaded, or in their !System. Modules directory. A copy of the module is inside the DrawGen application.

Start here!

All the programs you'll need for this article are in the Programs directory. Just to confuse you (!), they are numbered Prog01, Prog02, Prog03, ... etc.

When I suggest that you read one of these Progs with Edit, you may, of course, read it with exotic applications such as Zap or StrongED.

If you have dabbled with Basic programming on Acorn computers, you will know that the command:

CIRCLE(600,450,300)

will draw a circle somewhere in the centre(ish) of the screen. The numbers represent respectively: the x-coordinate of the centre of the circle (600), the y-coordinate of the centre of the circle (450) and the radius of the circle (300).

The x and y coordinates are measured from the bottom left of the screen. These are measured in OS-units. The screen is 1280 OS-units wide, and either 1024 or 960 OS-units high, depending on the mode you are using. (Other figures like rectangles, ellipses and lines can be drawn with other similar commands.)

Wouldn't it be nice if we could draw a circle in a drawfile just as easily? Well, now you can - almost! To see how, Drag Prog01 to Edit (henceforth to be known as "Look at Prog01").
It should be as follows:

REM >Prog01
ON ERROR:PRINTREPORT$;"at line";ERL:END
LIBRARY"<DrawGen$Dir>.DGBasLib"
PROCStart("DrawFile")
PROCCircle(600,450,300)
PROCFinish
END

The first line just gives the name of the program, and isn't really essential. The second line just makes sure that if you make a mistake, e.g. in tpying keywrds(!), then the program will tell you in which line the mistake occurred. (Note that this is all one line from ON to END).

The third line tells the program where to find the Basic procedures that are used in DrawGen Basic. This is called a LIBRARY and, as you can see, our Library is called DGBasLib. (If you're interested, you can see it by holding down <shift> and double-clicking on the DrawGen application icon. If you load it into Edit, please don't change anything in it!)

The next command just starts the process, and lets you tell the program what to call the drawfile. Here I've been incredibly imaginative and original and decided to call it DrawFile. These are always the first four lines of every program which uses DrawGen Basic. Just change the name of the program in the first line, and the name of the drawfile in the fourth.

Now comes the difficult bit. Remember the Basic command to draw a circle was CIRCLE(600,450 ,300)? Well, the command to do this in a drawfile is the next line:

PROCCircle(600,450,300)

Then we have to tell the program to finish the job with PROCFinish and END. This conceals all the clever stuff done by Jim Lesurf's module. And no, I don't know how it works either.

Run Prog01, i.e. double-click on Prog01, to get it to work. A file called DrawFile will be created in the same directory as DrawGen.

Look at DrawFile, i.e. load it into Draw. It's a circle!!! - that was easy wasn't it?

Which units?

Now, OS-units are OK if you're drawing on the screen but, in a drawfile which may be printed out, it would be more useful to have real units, such as inches (for people who just can't understand these metric things - "How long is a kilometre anyway?") or even better - centimetres. I will concentrate on using cm, but everything could just as well be in inches.

Look at Prog02. After PROCStart(("DrawFile"), we put the command to change the units to centimetres, i.e.

PROCUnits("cm")

and we've changed the circle command to use cm:

PROCCircle(5.5,7.3,3.85)

Run Prog02. Still a circle, but the centre is 5.5 cm across and 7.3 cm up, and its radius is 3.85 cm.

Try changing the program. Change the numbers after PROCCircle, but don't make them too big as the paper is usually A4 - about 20 cm wide and 30 cm high - and you can't print on all of it anyway.

Then press <f3>, change the name to "Test" and press <return>. This saves your new program. Run it to see the changes. You can drag the Basic program icon to any directory you like; it doesn't have to be in the Programs directory, but no matter where the Basic program is, the drawfile will still be created in the same place.

Shape up!

Instead of the PROCCircle command in Prog02, try creating one of the following shapes (all the measurements are in cm).

  1. PROCRectangle(x,y,w,h) Variables x and y are the coordinates of the bottom lefthand corner of the rectangle, and w and h are its width and height. Try PROCRectangle(4.5,3.0,4.9,3.8).
  2. PROCEllipse(x,y,w,h) Variables x and y are the coordinates of the centre of the ellipse; w and h are its width and height. Try PROCEllipse(4.5,3.0,4.9,3.8).
  3. PROCLine(x1,y1,x2,y2) This draws a line from the point (x1,y1) to the point (x2,y2). Try PROCLine(9,1,12,4).
  4. PROCSector(x,y,r,t1,t2) This draws a sector of a circle. Variables x and y are the coordinates of the centre of the circle and r is its radius. Variables t1 and t2 are the angles in degrees which define the extent of the sector, i.e. the sector goes from t1 degrees to the x-axis to t2 degrees to the x-axis. Try PROCSector(10,5,4,0,90).
  5. PROCArc(x,y,r,t1,t2) This draws an arc of a circle. The parameters are exactly the same as for PROCSector. Try PROCArc(6,6,5,45,180).

Prog03 has examples of each of these figures.

Red or yellow or...

Look at Prog04. The only extra command is:

PROCLineColour(red%)

The result is that all the figures after this command are drawn in red. The rectangle was drawn before this command, so it is still in the default colour, black. Note the % sign after the word red% - this shows that the parameter in brackets is really an integer number - more on this in the next article.

Try changing red% to one of the other colours: green%, blue%, grey%, lightgrey%, darkgrey%, lightblue%, darkblue%, yellow%, magenta%, orange%, cream%, pureyellow%, purecyan% or puremagenta%. Try adding a different PROCLineColour() before each figure.

Look at Prog05. The PROCLineColour(red%) has been changed to PROCFillColour(red%). If you run it, you will see that the sector has been filled, and the arc has become a filled segment. However, none of the other figures has been filled. Why? Because the other figures have equivalent commands to draw filled figures, i.e. PROCRectangleFill(), PROCCircleFill() and PROCEllipseFill().

Prog06 shows each of these being used, together with changes to the line colours.

Thick, thin and dotted

All the lines so far have been drawn in the default line width, which is 0.5 points, but the line width can be changed by the program. Look at Prog07. The only extra command is:

PROCLineWidth(0.1,"cm")

This changes the width of every subsequent line to 0.1 cm. If you run the program, you will see that all the lines are much thicker than before. Try changing the line thickness before each figure. You can also use other units, e.g.

PROCLineWidth(1.0,"mm")
PROCLineWidth(0.03936,"inch")
PROCLineWidth(2.80,"points")

These will all produce lines of the same width. The units used here have no effect on the units used in PROCUnits, which stay the same throughout the program.

If you now look at Prog08, this uses the command

PROCLinePattern(n)

where n is an integer from 0 to 8. The default is 0, and gives a continuous line; the others are various dotted line patterns. Note that the linecolour is also changed for each line, but instead of using the named colours, like red%, Colour%(n) is used. Although we only use numbers up to 8 in this example, you can use numbers up to 15. The line patterns can be used to draw any of the figures described above, in any colour.

Print and be *****!

Look at Prog09. This prints text in a drawfile. First we have to decide which font to use, and the width and height of the characters. PROCFont does this. The first parameter is the font number. DrawGen has six fonts which it recognises. Their font numbers are:

1 = Homerton.Medium
2 = Homerton.Medium.Oblique
3 = Homerton.Bold
4 = Trinity.Medium
5 = Trinity.Medium.Italic
6 = Trinity.Bold

In Prog09, we will use Homerton.Medium with width and height both 20 points; i.e.

PROCFont(1,20,20)

If text$="Hello world!" then PROCPrint(text$,3,3) will print the words with the lower lefthand corner of the "H" at (3,3).

Try changing the font number, the size and the text string.

Look at Prog10. This program prints text with a little more control over its position. It first plots a light blue line 10 cm from the left edge of the drawfile (... so we can see where 10 cm is!)

PROCLeftPrint(text$,10,10) prints the text with its left bottom edge at (10,10). It does the same as PROCPrint on the face of it, but there is a difference, which I will explain later; PROCRightPrint(text$,10,8) prints the text with its right bottom edge at (10,8); PROCCentrePrint(text$,10,6) prints the text with its centre at (10,6); and PROCFractionPrint(text$,10,4,0.3) prints the text with 0.3 of its length to the left of point (10,4).

In other words, PROCRightPrint is equivalent to PROCFractionPrint(,,,1) and PROCLeftPrint is equivalent to PROCFractionPrint(,,,0) and PROCCentrePrint is equivalent to PROCFractionPrint(,,,0.5).

Printing in colour

Look at Prog11. This shows printing in different colours. Use PROCFontColour with the colours black%, red%, green% ..... white%, or use Colour%(n%) where n%=0 to 13.

e.g. PROCFontColour(red%)

or PROCFontColour(Colour%(1))

The colours are:

Colour%(0)=black%
Colour%(1)=red%
Colour%(2)=green%
Colour%(3)=blue%
Colour%(4)=grey%
Colour%(5)=lightgrey%
Colour%(6)=darkgrey%
Colour%(7)=lightblue%
Colour%(8)=darkblue%
Colour%(9)=yellow%
Colour%(10)=magenta%
Colour%(11)=orange%
Colour%(12)=cream%
Colour%(13)=white%

These are the only colours that can be used with fonts. Note that if you change the Font colour in the middle of the program, you have to use PROCFont again in order for the change in colour to have any effect.


Source: Archive Magazine 12.10
Publication: Archive Magazine
Contributor: Brynmor Owen