May 31, 2006

Need a Date?

There are, no doubt, many ways to get a date (ask her out, already, will you?). In AutoCAD and Architectural Desktop, you can use DIESEL (Direct Interpretively Evaluated String Expression Language - not the motor fuel) to generate a formatted date string, which can be used in a menu macro or within an AutoLISP-defined command or subroutine. If you are not familiar with DIESEL, the ADT Help has detailed instructions under AutoCAD Help > Customization Guide > DIESEL.

As noted in the Help, DIESEL expressions "take strings as input and generate string results." Two DIESEL functions are needed to generate a formatted date (or time) string, "getvar" and "edtime". As you might expect, "getvar" allows you to retrieve the value of the Date AutoCAD System Variable, which the "edtime" function can then process and return as a string in the desired format.

getvar DIESEL Function:
$(getvar, varname)
returns the value in the system variable named in the varname argument.

edtime DIESEL Function:
$(edtime, time, picture)
returns a formatted date/time based on the AutoCAD Julian date specified in the time argument and the format specified by the picture argument. There are predefined codes for various date and time output (see below); any other characters are copied directly into the string. If you want a comma in your string, enclose it with double quotation marks to avoid having it be interpreted as an argument separator.

The following formatting codes are recognized by the edtime DIESEL function; output assumes that the date and time are Saturday, 3 June 2006 4:53:17.506.

Format CodeOutput Value

The only case-sensitive formatting codes are the last four; the others generate the same output whether entered in upper or lower case ("dddd" is the same as "DDDD"). If one of the "am or pm" codes is used, the hour value will be based on a twelve-hour clock; if not, the hour value will be based on a twenty-four hour clock.

DIESEL was set up for menu macros; you can access it in an AutoLISP expression by using the menucmd AutoLISP function. The following example sets the variable sdate to a string showing the current date in "mm/dd/yyyy" format.

(setq sdate (menucmd "M=$(edtime,$(getvar, date),MO/DD/YYYY)"))

You can then use that value in your AutoLISP routine, for example, by using it as the value of an attribute in a block that your routine inserts in a drawing. You can find an example of that in this thread in the Autodesk Architectural Desktop Discussion Group. Download the file attached to my second reply to that thread.

No comments: