English

How To: Add frames to a layout with Avenue

Procedure

How to use the PageDisplay coordinates to add frames to a layout using Avenue code.

Answer:

The following script demonstrates how to add text to a layout.

Code:
'-- When adding graphics on a layout the coordinates specified must
'-- be in inches, relative to the drawing space.
'-- The extent of the Layout (Drawing Space) is fixed at 1500 x 1500 inches.
'-- The page (whatever size selected) is centered on the layout.
'-- Adding a graphic to the page (text,etc) is done using drawing
'-- space coordinates. This poses a problem when we want a title
'-- say, at the top inch of a 8.5 x 11 page. Generally we will be working
'-- with page units, yet adding elements requires drawing space units.
'-- What can be done is to capture the lower left point of the page space
'-- in drawing space coordinates and add that x,y offset to each element
'-- added to the layout
'
'-- The way to do this:
'-- 1. Get the coordinates for the lower left corner of the
'-- page (0,0) in - drawing space - coordinates.
'
'-- 2. There are two ways to do this.
'
'-- 2a. Use the 2.1 requests that return information relative to
'-- the drawing space, page space, and their overlay coordinates
'
'-- 2b. Using 2.0, with a couple lines of creative code, it is possible to
'-- retrieve the needed coordinates.
'
'-- 3. Add the drawing space coordinates to your page coordinates
'-- when adding an element.
'
'-- For example:
'-- Given our drawing space below of size 1500 x 1500 inches,
'-- Given a page of 8.5 x 11 inches, automatically centered on the
'-- drawing space.
'
'-- We want add a label at a point 3 inches up and 3 inches over on our
'-- 8.5 x 11 inch paper. - Don't ask why, we just do.
'
'-- The code down below provides two methods of returning the lower left
'-- corner of our page space, in drawing space coordinates.
'
'-- I get X and Y coordinates of (745.75,744.5) for the lower left corner
'-- of the page space in drawing space units returned from the msgbox.
'
'-- To add an element at 3 inches up and 3 inches over on our
'-- 8.5 x 11 inch page, add (745.75,744.5) to (3,3) and place the element
'-- at (748.75,747.75).

'
'-- See the code below for examples.
'
' ------------------------ (1500,1500)
' | |
' | |
' | | <--- drawing space (1500,1500)
' | ----- (8.5,11) |
' | | | |
' | | | <--------|------ page space (8.5 x 11) [or whatever]
' | ----- |
' | (0,0)< ----------|------ Lower left of pagespace
' | (745.75,74.5) <--|------ Lower left in drawing space units
' | |
' ------------------------
' (0,0)
'
'-- the layout graphic list = all graphic contained on the layout,
'-- using coordinates in inches, relative to
'-- the drawing space (0,0,1500,1500)
'
'-- the layout display = the display of the layout
'
'-----------------------------------------
'-- Now that the process is outlined, there are two different ways to do this,
'-- Pre 2.1 code and 2.1/3.x code. (Some additional requests have been
'-- added to 2.1/3.x code to extract the drawing space coordinates of the
'-- page space instead of the slight of hand used in 2.0)
'
' ----------------------
' Pre 2.1/3.x code Notes
' ----------------------
'-- By adding a rectangle of size zero, setting the margin of our page to
'-- the zero size rectangle, it is possible to extract the origin of the
'-- page, by a query that returns the origin of the rectangle.
'
' ------------------
' 2.1/3.x Code Notes
' ------------------
'-- New requests used to gather some info about the Drawing space and the
'-- page space coordinates
'
'-- the following requests are new to 2.1/3.x and display information
'-- ReturnExtent -
'-- returns the layout page units (0,0,1500,1500)
'-- ReturnVisExtent -
'-- returns the visible extent of the display (the what?? window in inches)
'-- ReturnMarginExtent -
'-- returns the margin extend of the display (margin rectangle in inches)
'-- ReturnPageExtent -
'-- returns the page extent of a display (size of page surface in inches)
'
'd = thelayoutDisplay
' msgbox.report( d.ReturnExtent.AsString+nl+nl+
' d.ReturnVisExtent.AsString+nl+nl+
' d.ReturnMarginExtent.AsString+nl+nl+
' d.ReturnPageExtent.AsString
' ,"SEE EXTENTS" )
'
'=========================================
' BEGIN SCRIPT HERE, UNCOMMENT THE DIFFERENT SECTIONS OF
' CODE DEPENDING ON WHAT PLATFORM USED
'=========================================
'-- Double comments are used to designate what lines are
'-- to be uncommented
'-----------------------------------------
'-- get the project
theProject = av.GetProject

'-- get the layout
'-- theLayout = av.GetActiveDoc

'-- create a layout, add it to the project
theLayout = layout.Make
theLayoutWin = theLayout.GetWin
theLayoutWin.Open

'-- get the display of the layout
theLayoutDisplay = theLayout.GetDisplay
theLayoutDisplay.Flush

'-- get the graphics list to add elements to
theLayoutGraphicList = theLayout.GetGraphics

'-- set the layout page properties
'-- 11 x 8.5
'-- 0 inch margin

'-- set the layout units
theLayoutDisplay.SetUnits(#UNITS_LINEAR_INCHES)

'----------------
' 2.0 CODE BELOW
'----------------
'-- make a margin rectangle of 0 size for use later
'-- marginRect = Rect.MakeXY( 0.0, 0.0, 0.0, 0.0 )

'-- use the rectangle of no size created earlier to set
'-- the margin of the layout to basically nothing.
'-- used for extracting the lower left corner of the
'-- page in display units

'--theLayoutDisplay.SetMargin(marginRect)
'--theLayoutDisplay.SetMarginVisible(True)

'--get the origin of the lower left corner of page space in
'-- display space coordinates

'--thePageDisplayOrigin = theLayoutDisplay.ReturnMarginRect.ReturnOrigin
'--msgbox.info(thePageDisplayOrigin.AsString,"LL Drawing Page coordinates of Page
space")

'-------------
' END 2.0 CODE
'-------------
'
'-------------------
' 2.1/3.x CODE BELOW
'-------------------
'--get the origin of the lower left corner of page space in
'-- display space coordinates

thePageDisplayOrigin = theLayoutDisplay.ReturnPageExtent.ReturnOrigin
msgbox.info(thePageDisplayOrigin.AsString,"LL Drawing Page coordinates of Page
space")

'-----------------
' END 2.1/3.x CODE
'-----------------

'-- Add the text now
'-- Here is the trick, add thePageDisplayOrigin to the starting point
'-- to get the graphic to be placed properly relative to the drawing space,
'-- while still working in convenient page coordinates
theText = GraphicText.Make("The Text", thePageDisplayOrigin+Point.Make(3,3))

'-- add the element to the graphic list
theLayoutGraphicList.add(theText)

'--- End of Script