Introduction to Macro Writing Kate Morrical AutoCAD LT Technical Marketing Manager.

53
Introduction to Macro Writing Kate Morrical AutoCAD LT Technical Marketing Manager

Transcript of Introduction to Macro Writing Kate Morrical AutoCAD LT Technical Marketing Manager.

Introduction to Macro Writing

Kate MorricalAutoCAD LT Technical Marketing Manager

Customization Without Programming

Agenda

What’s in: Writing Macros Using DIESEL in Macros Using Macros in CUI Elements Using Macros in Tool Palettes The Action Recorder

What’s out: Linetypes Hatch Patterns Dynamic Blocks Scripts LISP/VBA/.NET/Everything Else

Text Conventions

AutoCAD-generated command line text USER-GENERATED TEXT OR NUMBERS Enter, left-click or other special characters

Writing Macros

Start with “want” statement “I want to…”

First example: “I want to draw a circle with a radius of 1 unit.”

Command line entry:

Command: CIRCLE (enter)Specify center point for circle or [3P/2P/Ttr (tan tan radius)]: (left-click)

Specify radius of circle or [Diameter]: 1 (enter)

Writing Macros, cont.

Put the pieces together: circle ; \ 1 ;

circle;\1;

Not quite done yet, though.

Writing Macros, cont.

What if a command were running when you started the macro? You’d need to cancel it… ^C^C

^C^Ccircle;\1;

What if this was used in a non-English version of AutoCAD? You’d need to translate the command _circle

^C^C_circle;\1;

Detour: CUI Topics & Tool Palettes

Skipping to Page 16 of the handout

Demo

Back on Track: Writing Macros

Special Characters Semicolon (;) Blank Space ( ) Backslash (\) Underscore (_) Dash/Hyphen (-) Asterisk (*) Caret (^) Dollar Sign ($)

Example: Create Multiple Circles *^C^C_circle;\1;

Demo

Rotate By Fixed Angle

Rotate an object by 180 degrees every time

Command: ROTATE (enter)Current positive angle in UCS: ANGDIR=counterclockwise ANGBASE=0

Select objects: (left-click) 1 foundSelect objects: (enter)Specify base point: (left-click)Specify rotation angle or [Copy/Reference] <0>: 180(enter)

Rotate By Fixed Angle

Clear any running commands ^C^C

Start the rotate command _rotate;

Let the user pick the object to rotate \

End the “select object” mode ;

Let the user pick the base point \

Specify the rotation angle and end the command 180;

Put it all together: ^C^C_rotate;\;180;

Demo

Copy and Rotate

Copy an object and then rotate it

Clear any running commands ^C^C

Start the copy command _copy;

Let the user pick the object to copy \

End the “select object” mode ;

Let the user pick the base point and the second point \\

Copy and Rotate, cont.

Start the rotate command _rotate;

Select the last object created L;

End the “select object” mode ;

Use the coordinates of the last point specified @;

Let the user specify the rotation angle \

Put it all together: ^C^C_copy;\;\\_rotate;L;;@;\

Demo

Macros for Dialog-Based Commands

Need to use command-line version Type out the command, pay attention to every time you pick

an option or press Enter. Hatches

-hatch Blocks

-insert Layers

-layer

Create a Hatch

Create a hatch with: A pattern of ANSI37 A scale of 4 And a rotation of 45 degrees Using the “select objects” method

Create a Hatch, cont.

Command: -HATCH (enter)Current hatch pattern: ANSI31Specify internal point or [Properties/Select objects/draW boundary/remove Boundaries/Advanced/DRaw order/Origin/ANnotative]: P (enter)

Enter a pattern name or [?/Solid/User defined] <ANSI31>: ANSI37 (enter)

Specify a scale for the pattern <1.0000>: 4 (enter)Specify an angle for the pattern <0>: 45 (enter)

Create a Hatch, cont.

Current hatch pattern: ANSI37Specify internal point or [Properties/Select objects/draW boundary/remove Boundaries/Advanced/DRaw order/Origin/ANnotative]: S(enter)

Select objects:(left-click) 1 foundSelect objects: (enter)Current hatch pattern: ANSI37Specify internal point or [Properties/Select objects/draW boundary/remove Boundaries/Advanced/DRaw order/Origin/ANnotative]: (enter)

Create a Hatch, cont.

Replace all (left-click) with backslash and all (enter) with semi-colons:

^C^C-hatch;p;ANSI37;4;45;s;\;;

Demo

Insert a Block

Insert a specified block with: A scale of 12 And a rotation of 90 degrees

Insert a Block, cont.

Command: -INSERT (enter)Enter block name or [?]: SAMPLE (enter)Units: Inches Conversion: 1.0000Specify insertion point or [Basepoint/Scale/X/Y/Z/Rotate]: S (enter)

Specify scale factor for XYZ axes <1>: 12 (enter)Specify insertion point or [Basepoint/Scale/X/Y/Z/Rotate]: R (enter)

Specify rotation angle <0.0000>: 90 (enter)Specify insertion point or [Basepoint/Scale/X/Y/Z/Rotate]: (left-click)

Insert a Block, cont.

Replace all (left-click) with backslash and all (enter) with semi-colons:

^C^C-insert;sample;s;12;r;90;\

Insert the same block exploded: ^C^C-insert;*sample;\12;90;

Insert the block and explode it after placement: ^C^C-insert;sample;s;12;r;90;\explode;L;

Insert the block, explode it after placement, then purge: ^C^C-insert;sample;s;12;r;90;\explode;L; -purge; B;sample;N;

Demo

Create a Layer

Command: -LAYER (enter) Current layer: "0"Enter an option [?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/MATerial/Plot/Freeze/Thaw/LOck

/Unlock/stAte/Description/rEconcile]: M (enter) Enter name for new layer (becomes the current layer) <0>: S-ANNO-TEXT (enter)

Enter an option [?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/MATerial/Plot/Freeze/Thaw/LOck

/Unlock/stAte/Description/rEconcile]: C (enter) New color [Truecolor/COlorbook] : 1 (enter)Enter name list of layer(s) for color 1 (red) <S-ANNO-TEXT>: (enter)

Create a Layer, cont.

Enter an option [?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/MATerial/Plot/Freeze/Thaw/LOck

/Unlock/stAte/Description/rEconcile]: L (enter) Enter loaded linetype name or [?] <Continuous>: continuous (enter)

 Enter name list of layer(s) for linetype "continuous" <S-ANNO-TEXT>: (enter)

Enter an option [?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/MATerial/Plot/Freeze/Thaw/LOck

/Unlock/stAte/Description/rEconcile]: (enter)

Create a Layer, cont.

Replace all (enters) from previous slides with semi-colons:

^C^C-layer;M;S-ANNO-TEXT;C;1;;L;Continuous;;;

What if your linetype isn’t loaded? Load it first! ^C^C-insert;*LT_beam_removed;0,0;1;;-layer ;M;S-BEAM-STEL-DEMO;C;1;;L;BEAM_REMOVED;;;

Demo

Plot a Drawing

Command: -PLOT (enter)Detailed plot configuration? [Yes/No] <No>: N(enter)

Enter a layout name or [?] <Layout1>: Layout1 (enter)

Enter a page setup name <>: Setup1 (enter)Enter an output device name or [?] <HP Officejet 4300 series>: (enter)

Write the plot to a file [Yes/No] <N>: (enter)Save changes to page setup [Yes/No]? <N>: (enter)

Proceed with plot [Yes/No] <Y>: (enter)

Plot a Drawing, cont.

Replace all (enters) from previous slide with semi-colons:

^C^C-plot;N;Layout1;Setup1;;;;;

Demo

Repeating Macros

Can’t use Enter or Spacebar to repeat macros Can use right-click shortcut menu

Unless the macro was launched from the Ribbon (known issue)

Bringing Macros Through Upgrades

Only thing to watch out for is changes in command options or names

For example Introduction of PEDITACCEPT meant you could skip a step in the

PEDIT command—broke a few macros that way

DIESEL

Direct Interpretively Evaluated String Expression Language

Always preceded with a dollar sign $M=$(diesel expression here)

Operators go first $M=$(+,2,3) $M=$(/,12,4)

Using Variables in DIESEL

Start by using GETVAR to retrieve variable value: $(getvar,DIMSCALE)

Then do some math with it: $M=$(*,$(getvar,DIMSCALE),0.25)

Finally put it into a command: ^C^C^Rfillet;r;<need number here>; ^C^C^Rfillet;r;$M=$(*,$(getvar,DIMSCALE),0.25);

Demo

More DIESEL Macros

Two more macros that use DIMSCALE: Start with:

$M=$(*,$(getvar,DIMSCALE),0.125)

Set single-line text height based on DIMSCALE: ^C^C_dtext;<need number here>; ^C^C_dtext;\$M=$(*,$(getvar,DIMSCALE),0.125);

Set multi-line text height based on DIMSCALE: ^C^C_mtext;\h;<need number here>;\ ^C^C_mtext;\h;$M=$(*,$(getvar,DIMSCALE),0.125);\

Demo

Conditional Expressions

DIESEL IF function similar to Excel IF function $M=$(if,expression,true,false)

Expression: statement to evaluate True: command sequence if expression is true False: command sequence if expression is false

Good for toggling values

Conditional Expressions, cont.

Build macro for toggling cursor size from 5% of screen to 100% of screen

Verbalize IF function: “If the CURSORSIZE variable is already set to 100, change it to 5. If

it’s not 100, change it to 100. Verbalize the expression:

“Is the cursor size already 100%?” Write the expression:

$(=,$(getvar,CURSORSIZE),100)

Conditional Exprsesions, cont.

Verbalize the TRUE condition: “If the expression is true, set the cursor size to 5%.”

Write the TRUE condition: CURSORSIZE;5

Verbalize the FALSE condition: “If the expression is false, set the cursor size to 100%.”

Write the FALSE condition: CURSORSIZE;100

Put it all together: $M=$(if,expression,true,false) $M=$(if,$(=,$(getvar,CURSORSIZE),100), CURSORSIZE;5,CURSORSIZE;100)

Demo

User Variables

10 built-in variables Can’t rename, can’t use more

Five for integers USERI1 through USERI5

Five for real numbers (with decimal points) USERR1 through USERR5

User Variables, cont.

Use a variable to automatically increment the value of a block attribute

Start the Insert command and place the block: Block definition must already exist in the drawing or in one of the

support paths! ^C^C-insert;label;\;;

Retrieve current value of USERI1: $M=$(getvar,USERI1)

Use USERI1 as attribute value: Block definition should contain only one attribute! USERI1;

User Variables, cont.

Increase the value of USERI1 by 1: $M=$(+,$(getvar,USERI1),1);

Put it all together: *^C^C-insert;label;\;;$M=$(getvar,USERI1); USERI1;$M=$(+,$(getvar,USERI1),1);

Asterisk repeats the command multiple times You do have to remember to reset the value of USERI1 every

time you run the macro

The Action Recorder

1

2 3 4 5

6

7

The Action Recorder

Guidelines for Creating Action Macros Don’t use dialog-based commands

OR Use the command-line equivalent for dialog boxes Don’t accept the default entry for commands Place macros in read-only folders for sharing

Demo

Any Questions?

How to reach me: [email protected] LT Unlimited: http://ltunlimited.typepad.com

More resources www.autodesk.com/discussion www.augi.com (forums & ATP classes)

Please remember to take the class survey sometime today (they’re really important!)

Thanks for coming, and enjoy the rest of AU!