Field Arithmetic

Function Syntax fieldmath
Current Version 1.2
Download FieldArithmeticV1-2.lsp
View HTML Version FieldArithmeticV1-2.html

Program Description

This program enables the user to perform arithmetic operations (add, subtract, multiply, divide) on numerical text or fields, with the result of the calculation represented using a field expression.

Upon issuing the command syntax fieldmath at the command-line, the program will prompt the user to select a text object with numerical content. At this prompt, the user may select a single-line text object (DText), multiline text (MText), block attribute, multileader (MLeader), or dimension containing numerical content, or choose the Constant option to enter an arbitrary fixed number for use in the calculation.

The program will then prompt the user to choose an operator (addition, subtraction, multiplication, division) to follow the selected or entered numerical value in the calculation. At this prompt, the user is also provided with the option to Undo the last numerical value added to the calculation, or to complete the calculation by choosing the Result option.

Upon selecting the Result option, the program prompts the user to specify a point or table cell to insert a field expression representing the result of the calculation.

At this prompt, the user may also choose the Object option in order to populate the content of an existing annotation object with the field expression.

Upon choosing this option, the user may select any single-line text (DText), multiline text (MText), single-line or multiline attribute, attributed block, or multileader (MLeader) with either multiline text or attributed block content.

If the user selects an attributed block or attributed multileader with more than one attribute, the user is presented with a dialog interface listing the available attributes, and is prompted to select a destination for the field expression.

The user may optionally predefine the target block/multileader attribute by specifying the attribute tag where noted at the top of the program source code.

Nested Field Expressions

The program also supports nested field expressions: that is, annotation objects which themselves contain field expressions referencing numerical values, as demonstrated by the 'Area' text in the animation below.

When encountering such objects, by default the program will reference the nested field expression directly in the calculation (ignoring any field formatting), as opposed to referencing the text content of the object containing the field expression.

This default behaviour may be toggled by changing the nst parameter within the 'Program Parameters' section of the program source code (see below).


Field Arithmetic Demo

Result Format

The field formatting code indicated at the top of the program source code 'Program Parameters' section may be altered to display the result of the calculation in a desired format:

;;                          Program Parameters                          ;;

        ;; Field formatting for result ("" for no formatting)
        fmt "%lu6%qf1"

        ;; Reference nested fields directly
        nst t

        ;; Optional predefined attribute tag
        tag nil


To determine the correct formatting code to be used, perform the following steps:

  • Type FIELD at the AutoCAD command-line and press ENTER to open the Field dialog.
  • Select Objects from the 'Field Category' drop-down menu.
  • Select Formula from the 'Field Names' section.
  • Enter an arbitrary number in the 'Formula' panel and click Evaluate.
  • Configure the formatting options as desired.
  • Click OK and pick a point in the drawing to create a temporary MText Field.
  • Download & load my Field Formatting Code program.
  • Type fieldformat to run the program and select the temporary MText Field created earlier.
  • Following a valid selection, the field formatting code will be printed to the command-line, e.g.:
  • Formatting code: "%lu4%ds44%th39"
  • Copy the formatting code (including the quotation marks) to the program source code where noted:
  • fmt "%lu4%ds44%th39"
  • Save and load the modified program file.

Instructions for Running

Please refer to How to Run an AutoLISP Program.


increase · reset · decrease

Designed & Created by Lee Mac © 2010