The Dcl Autolisp Tutorial

download The Dcl Autolisp Tutorial

of 100

Transcript of The Dcl Autolisp Tutorial

  • 7/31/2019 The Dcl Autolisp Tutorial

    1/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    1

    The AutoLisp Tutorial - DCL

    Dialog Control Language

    Ngn ngiu khin hp thoi

    Contents

    Getting Started Rows and Columns Controls Image Action Set_Tile and Mode_Tile List and how to handle them. Saving data from the dialog box Part 1 - Buttons Part 2 - Edit_Box Part 3 - List_Box Part 4 - PopUp_List Part 5 - Radio_Buttons Part 6 - Text and Toggle

    Part 7 - Putting it all together

    Getting Started (Mu)

    I'm not going to explain everything there is to know about Dialog Control Language. There are plenty ofbooks on the subject. I will attempt to give you a good understanding of how DCL interacts with AutoLisp.You should be able to write your first DCL driven Autolisp program in no time. Let's get to it.

    Ti khng gii thch mi iu hiu bit v ngn ngiu khin hp thoi. C rt nhiu ti liu v mn hcny. Ti s c gng cung cp cho bn mt kin thc tt v cch m DCL tng tc vI Autolisp. Bn snhanh chng c th vit chng trnh Autolisp iu khin DCL u tin ca bn . No ta hy bt u nh.

    Here is the basic order for things to happen inside the AutoLisp file: (y l trt t cbn cho mi th xyra trong tp tin Autolisp: )

    (defun C:MyProgram() ; Define the main program (nh ngha chng trnh chnh)

  • 7/31/2019 The Dcl Autolisp Tutorial

    2/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    2

    (defun myFunction1() ; Define the functions you need like saveVars (nh ngha chngtrnh m bn cn chng hn nh saveVar lu bin)

    ;do stuff he ;Thc hin mt s nhim v no

    ) ; Kt thc myFunction1

    (defun myFunction2() ;(nh ngha chng trnh m bn cn)

    ;do stuff here ;Thc hin mt s nhim v no

    ) ; Kt thc myFunction2

    (setq list1(list "a" "b" "c" "d")) ; Build the list if any are required (To mt danh

    sch nu c yu cu)

    (setq list2(list "1" "2" "3" "4")) ; (To mt danh sch nu c yu cu)

    (setq list3(list "x" "y" "z")) ; (To mt danh sch nu c yu cu)

    load_dialog ;Load the DCL file (Ti tp tin DCL)

    new_dialog ;Load the DCL definition (Ti nh ngha DCL)

    start_list ;Put the list in the dialog box(t danh sch vo hp thoi)add_listend_list

    start_list ;As many list as you need (Thm danh sch nhiu nh bn mun)add_listend_list

    set_tile ; Set the tiles (if necessary) (t cc phn t hp thoi nu cn)

    mode_tile ; Enable or disable tiles (as necessary) (Kch hot hoc tt ccphn t hp thoi nu cn thit)

    ;Set up as many action tiles as you need. (t cc phn t hnh ng nhiu nh bn mun)

    (action_tile "accept" "(setq d 2)(myFunction1)(done_dialog)")(action_tile "cancel" "(setq d 1)(myFunction2)(done_dialog)")

    ;Two action tiles need to have the done_dialog call. One of these needs to save the settings before; the done_dialog call.

    ; Hai phn t hnh ng cn phi c gi hp thoi hnh ng. Mt trong chng l lu vic t li

  • 7/31/2019 The Dcl Autolisp Tutorial

    3/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    3

    ;trc khi gi hp thoi hnh ng

    start_dialog ; Start the dialog box(Bt u hp thoi)

    unload_dialog ; Unload the dialog box (Thot khi hp thoi); Do stuff if the user presses the cancel key (Thc hin mt s nhim v no nu ngi s dng

    nhn nt Cancel)

    (if (= d 1)

    ;do stuff here if cancel was pressed (Thc hin mt s nhim v no nungi s dng nhn nt Cancel)

    )

    ; Do stuff if the user presses the accept key (Thc hin mt s nhim v no nu ngi s dngnhn nt Accept)

    (if (= d 2)

    ;do stuff here if okay was pressed (Thc hin mt s nhim v no nu ngis dng nhn nt OK)

    )

    ) ; close the program (ng chng trnh)

    We will discuss this in detail later on. (Chng ta s tho lun chi tit vn ny phn sau)

    I've found the hardest thing to get a grip on is laying out the dialog box. Please take a look at the rows andcolumns section if you need help with laying out a dialog box. For now let's look at the basics.I'm going tothrow a lot at you, but don't panic. I'll cover them a little closer later on. We will cover these items first:

    button, row, column, boxed_row, and boxed_column.

    Ti pht hin iu kh nht nm vng l b tr hp thoi. Hy xem phn cc hng v cc ct khi bn cngip vic b tr mt hp thoi. By gita s xem xt phn cbn. Ti sp qung cho bn mt l xch xngcc vn , nhngng c run nh. Ti sgii quyt chngtng cht mtphn sau. Chng ta hy bt uvi cc mc Nt, hng, ct, hng hp v ct hp

    Okay and Cancel Buttons - The DCL code for a these buttons look like this: (M DCL cho cc nt ny l:)

    : button { \\ Define the button(nh ngha nt)

    key = "accept"; \\ Define the action key (This is my name assigned to this button)(nh ngha kha hnh ng. y l tn ca ti gn cho nt ny)

  • 7/31/2019 The Dcl Autolisp Tutorial

    4/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    4

    label = " Okay "; \\ This is the text displayed on the button. (y l ch hin th trnnt)

    is_default = true; \\ Allows the button to activate when the user presses the enter key.(Cho php kch hot khi ngi s dng nhn Enter)

    } \\ Close the button definition (ng nh ngha nt)

    : button { \\ Define another button (nh ngha mt nt khc)

    key = "cancel"; \\ Define the action key. (I chose this name for the button.) (nhngha kha hnh ng. Ti chn tn ny cho nt mi)

    label = " Cancel "; \\ Text displayed on the button. (Ch hin th trn nt)

    is_default = false; \\ Does not allow this key to activate when the enter key is pressed. (Khng cho php kha ny kch hot khi nhn Enter)

    is_cancel = true; \\ Activate this key when the close button [ X ] is selected. (Kch hotkha ny khi nt Close [X] c chn)

    } \\ Close the button definition(ng nh ngha nt ny)

    Rows and Columns designate areas to lay your controls on. [Controls are list boxes, edit boxes, buttons,etc. ]. (l cc vng c thit kt cc nt iu khin ca bn.[Nt iu khin lcc hp danh sch, hpchnh sa, cc nt, vvvv] )

    Here is the DCL code for a column: (y l m DCL cho ct)

    : column {

    }

    Here is the DCL code for a row: (y l m DCL cho hng)

    : row {}

    Simple right? Tht n gin, phi khng?

    Here is the code for a column with two buttons: y l m cho mt ct vi hai nt:

  • 7/31/2019 The Dcl Autolisp Tutorial

    5/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    5

    : column {

    : button {key = "accept";label = " Okay ";is_default = true;

    }

    : button {key = "cancel";label = " Cancel ";is_default = false;is_cancel = true;

    }

    }

    Notice the buttons are stacked on top of each other.

    (Ch l cc nt chng ln nhau)

    Here is the code for a row with two buttons: y l m cho mt hng vi hai nt:

    : row {

    : button {key = "accept";label = " Okay ";

    is_default = true;}

    : button {key = "cancel";label = " Cancel ";is_default = false;is_cancel = true;

    }

    }

    Notice the buttons are side by side.

    Ch l cc nt nm cnh nhau

    Let's turn the above into Boxed Rows and Boxed Columns. Here is the code for a boxed_column withtwo buttons: Quay trli vi cc hng hp v ct hp ni trn. y l m cho mt ct hp vi haint:

  • 7/31/2019 The Dcl Autolisp Tutorial

    6/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    6

    : boxed_column {

    : button {key = "accept";label = " Okay ";is_default = true;

    }

    : button {key = "cancel";label = " Cancel ";is_default = false;is_cancel = true;

    }

    }

    Notice the box around the buttons.

    Ch ti hp xung quanh cc nt

    Here is the code for a boxed_row with two buttons: y l m cho mt hng hp vi hai nt:

    : boxed_row {

    : button {key = "accept";label = " Okay ";is_default = true;

    }

    : button {key = "cancel";label = " Cancel ";is_default = false;is_cancel = true;

    }

    }

    Notice the box around the buttons.

    Ch ti hp xung quanh cc nt

    Now we know the difference between a row and a boxed_row. We also know that controls inside a columnget stacked on top of each other [ vertical ] and controls inside a row are next to each other [ horizontal ].

    Gita bit s khc nhau gia hng v hng hp. Ta cng bit rng cc nt iu khin trong mt ct thxp chng ln nhau [thng ng], cn trong mt hng th xp nm cnh nhau.[ nm ngang]

    Important: You should never ever attempt to build a dialog box without a cancel button. Trust me. Or youcan do what I did and find out for yourself.

    Ch quan trng:ng bao gic gng to mt hp thoi khng c nt Cancel. Hy tin ti i.Hoc l

    bn c thclm iu ti lm v ttm ra kt lun ca bn.

  • 7/31/2019 The Dcl Autolisp Tutorial

    7/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    7

    Overview (Tng quan)

    Let's take a look at the entire code for the dialog box above, including the LSP file: (Ta hy xem xt m honchnh cho hp thai trn bao gm c tp tin Autolisp)

    DCL FILE NAMED: DCL_TEST.DCL AUTOLISP PROGRAM NAMED: DCL_TEST.lsp

    TN TP TIN DCL: DCL_TEST.DCL TN CHNG TRNH AUTOLISP: DCL_TEST.lsp

    DCL_TEST : dialog {

    : boxed_row {

    : button {key = "accept";label = " Okay ";is_default = true;

    }

    : button {key = "cancel";label = " Cancel ";is_default = false;is_cancel = true;

    }

    }

    }

    (defun C:DCL_TEST()(setq dcl_id (load_dialog "DCL_TEST.dcl"))(if (not (new_dialog "DCL_TEST" dcl_id) )

    (exit))(action_tile "cancel" "(setq

    ddiag1)(done_dialog)")(action_tile "accept" "(setq

    ddiag2)(done_dialog)")(start_dialog)(unload_dialog dcl_id)(if (= ddiag 1)(princ "\n \n ...DCL_TEST Cancelled. \n

    "))

    (if (= ddiag 2)(alert "You pressed the OKAY button!")

    ))

    AutoLISP

    Let's take a look at the AutoLisp part of this simple program. (Ta hy xem xt phn Autolisp ca chung trnhn gin ny)

    First we defined our program: (Trc ht ta xc nh chng trnh ca mnh)

    (defun C:DCL_TEST()

    The second thing was to load the DCL file from disk. Insert a path if necessary, but I always liked to use theautocad search path to find the file. Notice the dcl_id variable used. We will need this later on. Thisidentifies the file opened.

    Th hai l ti tp tin DCL ta. a vo ng dn nu cn, m ti lun thch s dng ng tm kimca AutoCad tm tp tin .Ch rng ta s dng bin dcl_id m ta s cn sau ny. iu ny cng nh tp

    tin c m.

  • 7/31/2019 The Dcl Autolisp Tutorial

    8/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    8

    ;;;--- Put up the dialog box (t hp thoi)(setq dcl_id (load_dialog "DCL_TEST.dcl"))

    Since a DCL file can contain multiple dialog box definitions, [ I'll show you an example later on. ] we needto specify which dialog box we want to load. This is taken care of in the next statement. Notice the name

    matches the definition inside the DCL file.

    Do mt tp tin DCL c th cha nhiu nh ngha hp thoi [ti s ch cho bn mt v dphn sau],chng ta cn m t hp thoi no m ta mun ti. iu ny c bo m trong thng bo tip theo. Ch ti ci tn trng vi nh ngha trong tp tin DCL

    ;;;--- See if it is already loaded (Kim tra xem n c ti cha)(if (not (new_dialog "DCL_TEST" dcl_id) ) (exit))

    Next, we define our action keys. These tell the program what to do whenever a userpresses a button, selects an item in a list, etc. Notice the two action keys. "cancel"and "accept" . These match the keys in the DCL file. I made these names up. You can

    name them whatever you want as long as it is not a name used by AutoCAD and it onlyappears once in each DCL definition. You can't have two controls with the same key, theprogram would get confused. So would I!!! Whenever the user presses the cancel key,the program sets variable ddiag to 1 and closes the dialog box. Upon pressing the Okaybutton, [ key = accept ], the program sets variable ddiag to 2 and closes the dialogbox. That's it. Nothing else to add except, you could have just about anything inhere, including instructions to execute a function [ I'll demonstrate that later. ]

    Tip theo, ta xc nh cc kha hnh ng ca chng ta. iu ny s bo chng trnh iu phi lm bt ckhi no ngi s dng nhn mt nt, chn mt khon mc trong mt danh sch, vvv.. Ch ti hai khahnh ng Cancel v Accept. Chng trng vi cc kha trong tp tin DCL. Ti to cc tn ny. Bnc tht bt c tn g bn thch, di thoi mi m khng phi cc tn c AutoCad s dng v n ch

    xut hin mt ln trong mi nh ngha ca DCL. Bn khng th c hai kh nng iu khin cho cng mtkha, chng trnh s b ri. Ti b nh vy!!!. Bt c khi no ngi s dng nhn kha Cancel, chngtrnh st bin ddiag v 1 v ng hp thai. p dng vic nhn nt Okay [kho l Accept], chng trnh st bin ddiag thnh 2 v ng hp thoi. Vy . Chng c g khc c thm vo tr phi bn phi c ci g y, bao gm cc ch dn thc hin mt hm [Ti s biu din iu sau]

    ;;;--- If an action event occurs, do this function (Nu mt s hnh ng xy ra, thc hinhm sau)

    (action_tile "cancel" "(setq ddiag 1)(done_dialog)")(action_tile "accept" "(setq ddiag 2)(done_dialog)")

    Finally, the big step. Everything is defined, let's display the dialog box.(Cui cng,mt bc vi. Mi th kt thc, ta hy hin th hp thoi)

    ;;;--- Display the dialog box (Hin th hp thoi)(start_dialog)

    The program halts here until the dialog box is issued a "done_dialog" call. In themean time the user is interacting with the action keys. When the user presses a buttonthe program kills the dialog box with the unload_dialog call. Notice the dcl_id passedas a parameter. You could have more than one dialog file open at one time. So, theunload_dialog function needs to know which one to unload.

    Chng trnh s treo y cho ti khi hp thoi c mt thao tc gi Hpthoi_hnhng.Lc ngi

  • 7/31/2019 The Dcl Autolisp Tutorial

    9/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    9

    s dng phi c tng tc vi cc kho hnh ng. Khi ngi s dng nhn mt nt, chng trnh s hy hpthoi vi vic gi lnh hp thoi thot ti (unload_dialog). Ch rng bin dcl_id thot ra nh mt tham s.Bn c th c hn mt tp tin hp thoi mtrong mt ln. V th hm lnh unload_dialog cn phi bit thotkhi hp thoi no.

    ;;;--- Display the dialog box (Hin th hp thoi)(unload_dialog dcl_id)

    Finally the dialog box is gone. What next? Find out which button the user pressed?Exactly! Remember setting the ddiag variable to either 1 or 2. We will use that now todetermine which button was pressed. There is a better way to do this, I'll show youlater.

    Hp thoi kt thc xong. iu g k tip? Hy tm xem nt no c ngi s dng nhn? Chnhxc?Hy nhti vic t bin ddiag v gi tr 1 hoc 2. Ta s s dng chng xc nh xem nt no cnhn. C mt cch tt hn lm iu ny, ti schcho bn sau.

    ;;;--- If the cancel button was pressed - display message (Nu nt Cancel b nhn, hin th)(if (= ddiag 1)

    (princ "\n \n ...DCL_LSP Cancelled. \n "))

    ;;;--- If the "Create" button was pressed (Nu nt Create b nhn)(if (= ddiag 2)

    (alert "You pressed the OKAY button!"))

    And the last step, close the autolisp program.(v bc cui cng, ng chng trnh Autolisp))

    The basic DCL model (Mt mu tp tin DCL cbn)

    I will now give you the model I use for 98% of all dialog based programs I write. I start with this basicprogram and build from there. It is basically the same thing we've gone over before. I put up a dialog box. Iallow the user to press buttons and manipulate data. The only difference is, when the user presses the OKAY

    button I save the settings in the dialog file before it closes. I do this inside the action key call with a function

    called saveVars. Without further ado, here's the model:

    By giti s cung cp cho bn mt mu m ti s dng cho 98% cc chng trnh cha hp thoi m tivit. Ti bt u vi chng trnh cbn ny v to dng tip t.iu cng nhiu tng t chngta tng lm trc y. Ti t mt hp thoi. cho php ngi s dng nhn cc nt v thao tc vi cc dliu. iu khc duy nht l khi ngi s dng nhn nt Okay ti s lu li nhng iu t trong tp tin hpthoi trc khi ng n. Ti thc hin iu ny trong kha hnh ng km theo hm c gi l hmsaveVar . Ngoi ra chng cn g khc na, v y l mu .

    The DCL File: Tp tin DCL

    EXAMPLE : dialog {

  • 7/31/2019 The Dcl Autolisp Tutorial

    10/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    10

    label = "EXAMPLE.lsp"; \\ Puts a label on the dialog box (t nhn cho hp thoi)initial_focus = "textval"; \\ Sets the initial focus (t mc tiu ban u): column {: row {: boxed_column {

    : edit_box { \\ The edit_box control - Something new! (Ci ny mi n:Nt iu khin hp thoi hiu chnh)

    key = "textval";label = "Text Value to Find";edit_width = 30;value = "";

    }}

    }: row {: boxed_row {

    : button {key = "accept";label = " Okay ";is_default = true;

    }: button {key = "cancel";label = " Cancel ";

    is_default = false;is_cancel = true;}

    }}

    }}

    The AutoLisp File: (Tp tin Autolisp)

    ;;;--- EXAMPLE.lsp - Text Find (Tm dng vn bn)

    (defun saveVars()

    ;;;--- Save the input from the dialog box (Lu li u vo ca hp thoi)(setq textVal (get_tile "textval"))

    )

    (defun C:EXAMPLE()

    ;;;--- Load the dcl file ( Ti tp tin DCL)(setq dcl_id (load_dialog "EXAMPLE.dcl"))

    ;;;--- Load the dialog definition if it is not already loaded (Ti nh

  • 7/31/2019 The Dcl Autolisp Tutorial

    11/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    11

    ngha hp thoi nu n cha c ti)(if (not (new_dialog "EXAMPLE" dcl_id) ) (exit))

    ;;;--- If an action event occurs, do this function (Nu mt s hnh ng xyra, thc hin hm ny)(action_tile "cancel" "(setq ddiag 1)(done_dialog)")(action_tile "accept" "(setq ddiag 2)(saveVars)(done_dialog)")

    ;;;--- Display the dialog box (Hin th hp thoi)(start_dialog)

    ;;;--- If the cancel button was pressed - display message (Hin th nu ntCancel c nhn)(if (= ddiag 1)

    (princ "\n \n ...EXAMPLE Cancelled. \n "))

    ;;;--- If the "Okay" button was pressed (Nu nt Okay c nhn)(if (= ddiag 2)

    (princ "\n \n ...Example Complete!"))

    ;;;--- Suppress the last echo for a clean exit (Trit tiu vic lp li thao tc cuiv thot m)(princ)

    )

    Screen Shot: Hin th trn mn hnh:

    If you could not follow some of the autolisp functions, you can ignore them for now or read the AutolispTutorial. (Nu bn khng hiu ch no trong chng trnh Autolisp trn, hy c li phn t hc Autolisp)

    This is the model. Very little will change from this setup. You may add some controls to the DCL filewhich in turn will add Action calls and influence the number of lines inside the saveVars routine. The onlyother thing that will change is inside the OKAY button function. [ When ddiag = 2 ]. You will have to tellthe program what to do when all of the information is gathered. We will cover these later.

    y l mt mu . C rt t thay i so vi ban u. Bn c th thm vi nt iu khin vo tp tin DCL m

    n s thm vo cc vic gi lnh hnh ng v nh hng n s dng bn trong ca vic lu bin saveVar.iu khc duy nht l c s thay i bn trong chc nng ca nt Okay.[Khi ddiag = 2]. Bn phi bo

  • 7/31/2019 The Dcl Autolisp Tutorial

    12/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    12

    chng trnh iu n cn lm khi tt c cc thng tin c nhm li. Ta s hc nhng iu ny sau.

    Back

    Rows and Columns (Cc hng v cc ct)

    Let's get a feel for laying out a dialog box using nested rows and columns. I'll do this by showing you theDCL code next to a picture of the dialog box. I can't think of any easier way to do it. Ignore all of the

    Labels and TEXT controls. I will use these to visually show the location of things.

    Ta hy hnh dung s b tr mt hp thoi s dng cc t hp hng v ct. Ti s thc hin iu ny bng

    cch trnh by cho bn m DCL bn cnh hnh th hin ca hp thoi. Ti khng th nghra cch no d dnghn cch ny. Hy qun i tt c cc nhn v cc text iu khin. Ti s dng chng ch th hin ci cht chng m thi.

    Example 1:

    EXAMPLE : dialog {label = "EXAMPLE.lsp";: column {

    : boxed_column {label = "column 1";: boxed_column {label = "column2";

    }: boxed_column {label = "column 3";

    }}ok_cancel;

    }

    }

    Column (Mt ct chnh bao gm)Column (Mt ct hp 1 cha)Column (Cc ct hp bn trong 2,3)

    T hp hng gm hai nt OK v Cancel

  • 7/31/2019 The Dcl Autolisp Tutorial

    13/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    13

    EXAMPLE : dialog {label = "EXAMPLE.lsp";: column {

    : boxed_row {label = "row 1";: boxed_column {label = "column2";

    }: boxed_column {label = "column 3";

    }}ok_cancel;

    }

    }

    Notice I changed the first boxed column into aboxed row. (Ch s thay i ct hp th nhtthnh hng hp)

    Ct chnh bao gm:

    Row (Hng hp 1 cha )Column (Ct hp 2 bn trong)

    Column (Ct hp 3 bn trong)Hng cha hai nt OK v Cancel

    EXAMPLE : dialog {label = "EXAMPLE.lsp";: column {: boxed_column {label = "column 1";: boxed_column {label = "column2";

    : text {

    key = "t1";value = "Text1";

    }: text {

    key = "t2";value = "Text2";

    }}: boxed_column {label = "column 3";: text {

    key = "t3";value = "Text3";

    }: text {

    key = "t4";value = "Text4";

    }}

    }ok_cancel;

    }

    }

    Column (Ct chnh bao gm)Column (Ct hp 1 cha)Column (Cc ct hp bn trong 2,3 cha cc

    kho t1, t2 v cc kha t3,t4)

    Hng hai nt OK v Cancel

  • 7/31/2019 The Dcl Autolisp Tutorial

    14/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    14

    EXAMPLE : dialog {label = "EXAMPLE.lsp";: column {: boxed_row {label = "row 1";

    : boxed_column {label = "column2";

    : text {key = "t1";value = "Text1";

    }: text {

    key = "t2";value = "Text2";

    }}: boxed_column {label = "column 3";: text {key = "t3";value = "Text3";

    }: text {

    key = "t4";value = "Text4";

    }}

    }ok_cancel;

    }}

    Ct chnh bao gm

    Row (Hng hp 1 cha hai ct hp 2,3)Column (Ct hp 2 cha hai kho t1, t2)Column (Ct hp 3 cha hai kha t3,t4)

    Hng cha hai nt OK v Cancel

  • 7/31/2019 The Dcl Autolisp Tutorial

    15/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    15

    EXAMPLE : dialog {label = "EXAMPLE.lsp";: column {: boxed_row {label = "row 1";

    : boxed_row {label = "row 2";: text {

    key = "t1";value = "Text1";

    }: text {

    key = "t2";value = "Text2";

    }}: boxed_row {label = "row 3";: text {key = "t3";value = "Text3";

    }: text {

    key = "t4";value = "Text4";

    }}

    }ok_cancel;

    }}

    Ct chnh bao gm:

    Row (Hng hp 1 cha cc hng hp 2,3)Row (Hng hp 2 cha cc kha t1,t2)Row Hng hp 3 cha cc kho t3,t4)

    Hng cha hai nT OK va Cancel

  • 7/31/2019 The Dcl Autolisp Tutorial

    16/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    16

    EXAMPLE : dialog {label = "EXAMPLE.lsp";: column {: boxed_column {

    label = "row 1";: boxed_row {label = "row 2";: text {

    key = "t1";value = "Text1";

    }: text {

    key = "t2";value = "Text2";

    }

    }: boxed_row {label = "row 3";: text {key = "t3";value = "Text3";

    }: text {

    key = "t4";value = "Text4";

    }

    }}ok_cancel;

    }}

    Ct chnh bao gm:

    Column (Ct hp 1 cha cc hng hp 2,3)Row (Hng hp 2 cha cc kha t1, t2)Row (Hng hp 3 cha cc kho t3, t4)

    Hng cha hai nt OK v Cancel

    I hope this puts an end to the rows and columns issue. If not, you know where to find me. (Ti hy vng ny c th kt thc ci mn hng v ct ny. Nu khng chc bn bit tm ti u ri.

    Back

    Dialog Control LanguageControls (Cc nt iu khin trong DCL)

    Let's take a look at the different controls you can put on the dialog box. You've seen a few of them in theoverview portion but let's go over the majority of them now. I will only show the most used properties for

    each control.

  • 7/31/2019 The Dcl Autolisp Tutorial

    17/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    17

    Ta hy i kim cc nt iu khin khc nhau m bn c tht vo hp thoi. Bn tng ng thychng trong phn tng quan nhng by gita s tp trung ngm cu chng. Ti s ch trnh by nhng thuctnh s dng chnh ca mi nt .

    LAYOUT CONTROLS:

    : column {label = "My Column";

    }

    Column (Ct)

    You can omit the label. Bn c th b qua vic gn nhn

    : boxed_column {label = "My Column";

    }

    Boxed Column ( Ct hp)

    You can omit the label. Bn c th b qua vic gn nhn

    : row {label = "My Row";

    }

    Row (Hng)

    You can omit the label. Bn c th b qua vic gn nhn

    : boxed_row {label = "My Row";

    }

    Boxed Row (Hng hp)

    You can omit the label. Bn c th b qua vic gn nhn

    : spacer {width = 10;height = 15;

    }

    A normal spacer to help align other controls. You can omit thewidth and height properties. (Mt khong khng gian bnh thng gip cho vic cn chnh v tr ca nt iu khin khc. Bn cth b qua thuc tnh rng v chiu cao)

    : spacer_0;

    A spacer that usually has no width. It simply means, if you haveto stretch something in this row to make things fit, stretch thisspacer instead of the other items.. (Mt khng gian c rng lun

    bng 0. N n gin c ngha l nu bn phi dui di nt no trn hng ny to mt s hi ho th hy dn khng gian nythay cho cc khng gian khc)

    : spacer_1; The smallest spacer that's visually noticable. (Khng gian nhnht c th thy c)

  • 7/31/2019 The Dcl Autolisp Tutorial

    18/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    18

    BUTTON (NT)

    : button {key = "button1;label = "Okay";is_tab_stop = true;

    is_cancel = false;is_default = true;width = 15;height = 10;mnemonic = "B";

    }

    key = The name you assign to the button. (Tnbn gn cho nt)

    label = The text displayed on the button. Dngch th hin trn nt)

    is_cancel = Determines if this is the cancelbutton. One control must be assigned to be thecancel action. (Xc nh y c phi l nt xakhng. Mt qu trnh iu khin c gn chohnh ng xo ny)

    is_default = Determines if this button activateswhen the user presses the enter key. (Xc nh ntny c b kch hot khi ngi s dng nhn khaEnter hay khng)

    mnemonic = Determines if you can press ALT+Bto move to this action key. You assign the letterand DCL will underscore the letter on the dialog

    box to let the user know it is ALT-able. If that isa word!(Xc nh bn c th s dng t hp phmAlt +B chuyn ti kho hnh ng ny hay

    khng. Bn gn ch ci v DCL s gch t chci trong hp thoi bo cho ngi s dngbit n c kh nng kt hp vi phm Alt. Nu l mt t)

    BOXED RADIO COLUMN, RADIO COLUMN, & RADIO BUTTON

    CT LA CHN HP, CT LA CHN, V NT LA CHN

    Ci tn ny ttba ra v cc bit nndch n l g, thi th ccho n mt ci tn T To nhvy mc tip. Ai c ci tn hay hn, ng hn th ch gim t, xin cm n trc nh.

    : radio_column {label = "Choices";key = "choices";

    // Use boxed_radio_column if a box isrequired. (S dng ct la chn hp nu mthp c yu cu)// Label for the column or boxed_column(Gn nhn cho ct hay ct hp)// Action key for the radio column (Kha

    hnh ng cho ct la chn): radio_button { // First radio button (Nt la chn th nht)

  • 7/31/2019 The Dcl Autolisp Tutorial

    19/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    19

    label = "Choice 1";key = "choice1";

    }

    : radio_button {label = "Choice 2";key = "choice2";

    }

    // Second radio button (Nt la chn th hai)

    : radio_button {label = "Choice 3";key = "choice3";

    }

    // Third radio button (Nt la chn th ba)

    : radio_button {label = "Choice 4";key = "choice4";

    }

    // Fourth radio button (Nt la chn th t)

    } // Close the radio_column (ng ct lachn)

    BOXED RADIO ROW, RADIO ROW, & RADIO BUTTON

    HNG LA CHN HP, HNG LA CHN, V NT LA CHN

    : radio_row {label = "Choices";key = "choices";

    // Use boxed_radio_row for box. (S dnghng la chn hp cho mt hp)// Label for the row or boxed_row (Gnnhn cho hng hay hng hp)// Action key for the radio row (Kha hnhng cho hng la chn)

    : radio_button {label = "1";key = "choice1";

    }

    // First radio button (Nt la chn thnht)

    : radio_button {label = "2";key = "choice2";

    }

    // Second radio button (Nt la chn thhai)

    : radio_button {label = "3";key = "choice3";

    }

    // Third radio button (Nt la chn th ba)

    : radio_button {label = "4";

    key = "choice4";}

    // Fourth radio button (Nt la chn tht)

  • 7/31/2019 The Dcl Autolisp Tutorial

    20/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    20

    } // Close the radio_row (ng hng lachn)

    EDIT BOX (HP HIU CHNH)

    : edit_box {key = "myval";label = "Value:";edit_width = 10;value = "";

    }

    // Action key (Kha hnh ng)// Label for the edit box (Nhn gncho hp hiu chnh)// Character width ( rng k t)// Initial value (Gi tr ban u)

    LIST BOX (HP DANH SCH)

    : list_box {label ="Choose Items";key = "mylist";height = 15;width = 25;multiple_select = true;fixed_width_font = true;value = "0";

    }

    // Label for the list box (Nhn chohp danh sch)// Action key (Kho hnh ng)// Height of list box ( cao hp)// Width of list box ( rng hp)// Multiple Select = TRUE (nhiu lachn)// Use fixed with font = TRUE (S

    dng font chm cnh)// Initial selection = 1st item (Lachn ban u l khon mc u tin)

    Fixed Width Font = TRUE

    Multiple Select = TRUE

    View of list box with... (Hnh hin thhp danh sch vi: )

    Fixed Width Font = FALSE. (Font chmnh)

    Multiple Select = TRUE. (Cho chnnhiu khon mc)

    View of list box with... (Hnh hin thhp danh sch vi: )

    Fixed Width Font = FALSE. (Fontch mnh)

    Multiple Select = FALSE. (Khngcho chn nhiu khon mc)

  • 7/31/2019 The Dcl Autolisp Tutorial

    21/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    21

    POPUP LIST (DANH SCH TH XUNG)

    : popup_list {key = "mylist";label = "Select Item";fixed_width_font =

    false;width = 15;value = 0;

    }

    // Action key (Kho hnh ng)// Label (nhn gn)// Use fixed with font = false (Khngs dng font chm)// Width of list box ( rng hpdanh sch)// Initial selection = 1st item (Lachn ban u l khon mc th nht)

    TEXT (VN BN)

    : text {key = "mytext";value = "This is text!";

    }

    // Action key (Kha hnh ng)// Value (Gi tr vn bn)

    Okay. That's it for controls. There are others but, these are the most used controls. Let's move on.(OK. l cc nt iu khin. Cn c cc nt khc nhng y l nhng nt s dng ch yu. Ta sang mc khc nh.

    Back

    Dialog Control Language Image (Hnh nh trong DCL)

    Image (Cc hnh nh)

    Image (Hnh nh)

    An Image is used to display a vector graphic picture inside a rectangle. To create the image you can usethree different methods. (Mt hnh nh c s dng hin th mt bchnh ho vector trong mt khungch nht. to mt hnh nh bn c th s dng 3 phng php khc nhau)

    Vector_image - Draws a single straight line in the current image. (nh Vector - V mt onthng trong nh hin ti)

    Fill_image - Draws a filled rectangle in the current image.( nh c- V mt khung chnht c trong nh hin ti)

    Slide_image - Draws an AutoCAD slide in the image. (nh Silse- V mt nh AutoCadSlide trong nh hin ti)

  • 7/31/2019 The Dcl Autolisp Tutorial

    22/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    22

    For this tutorial we will concentrate on the Slide_Image function. Everyone knows how to create a slideright? The MSLIDE command. Okay. We will assume our image is ready.

    hc iu ny ta s tp trung vo hm nh Slide. Mi ngi bit cch to mt nh Slide ri, ngkhng? Lnh Mslide na. RI Ok. Ta gi s rng ta c nh sn sng ri.

    Let's look at the DCL Image definition and the AutoLisp functions required to display the image. ( Hy xemnh ngha hnh nh trong DCL v cc hm Autolisp cn c hin th hnh nh ny.)

    DCL - You must supply either the width and height or one of these plus an aspect_ratio. (Bn phi p dnghoc l cao v rng hoc l mt trong chng v mt t lnh)

    : image {key = "sld";height = 30;width = 30;

    color = 0;is_enabled = false;is_tab_stop = false;

    }

    AutoLisp

    ;;;--- First we need a slide name(Trc ht bn cn t tn nh silde)

    (setq mySlideName "c:/acad/myslide.sld")

    ;;;--- Second we need the key to the image control(Th hai bn cn t kho iu khin hnhnh)(setq myKey "sld")

    ;;;--- Next we send the slide name and the key to the update function(Tip theo ta gi tnv kha ny vo hm update hnh nh)(upDateImage mySlideNamemyKey)

    ;NOTE: Notice mySlideName becomes sldName and myKey becomes key when passedas; parameters to the upDateImage function. ( Ch :Bin mySlideName trthnh sdlName v binmyKey trthnh key khi i qua hm update hnh nh nh cc tham s)

    ;;;--- Function to update the slide (Hm update hnh nh Slide)

    (defun upDateImage(sldNamekey)

    ;;;--- Get the width of the slide (Ly rng ca hnh nh)(setq width (dimx_tile key))

    ;;;--- Get the height of the slide(Ly cao ca hnh nh)(setq height (dimy_tile key))

  • 7/31/2019 The Dcl Autolisp Tutorial

    23/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    23

    ;;;--- Start the slide definition (Bt u nh ngha nh slide)(start_image key)

    ;;;--- Wipe out the background (Xa sch nn)(fill_image 0 0 width height 0)

    ;;;--- Put the slide in the image area (t nh Slide vo vng nh)(slide_image 0 0 width height sldName)

    ;;;--- Finish the slide definition (Kt thc nh ngha nh slide)(end_image)

    )

    This function can be used over and over to display any slide image in an image control. Use theupDateImage function after the new_dialog call and before the action_tile statements. You can also use this

    function while the dialog box is displayed in one of your action calls. We will use this later on in the tutorialso you can see it in action.

    Hm ny c th s dng ln lt hin th bt knh slide no trong mt kha iu khin. Vic s dnghm update hnh nh sau khi gi hp thoi mi v trc cc thng bo ca phn t hnh ng. Bn cng cth s dng hm ny trong khi hp thoi hin th trong mt ln gi hnh ng ca bn. Ta s s dng iuny trong phn sau v bn c th quan st n lm vic.

    Back

    Dialog Control Language Action (Hnh ng trong DCL)

    Action (Cc hnh ng)

    In order for the dialog box to respond to user interaction, you must set up an action event. A trigger, to firewhen the user selects an item from a list or presses a button. This works off of the KEY defined for eachcontrol inside the DCL file. If the key for a button is named "mybutton" then you must have an action named"mybutton". Otherwise the user will press the button and nothing will happen. Let's take a look at theaction_tile.

    hp thoi tng tc vi ngi s dng, bn phi to ra mt s kin hnh ng. Mt ci ly khai hakhi ngi s dng chn mt khon mc trong danh sch hoc nhn mt nt. iu ny s ngt mt kho cxc nh cho mi thao tc iu khin trong tp tin DCL. Nu kha cho mt nt c t tn l mybutton th

    bn phi c mt hnh ng c t tn l mybutton. Nu khng khi ngi s dng nhn nt s chngc g xy ra. Ta hy xem xt phn t hnh ng ny.

  • 7/31/2019 The Dcl Autolisp Tutorial

    24/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    24

    (action_tile "key" "action")

    The action_tile has two parameters. "Key" and "action". (Phn t hnh ng c hai tham s l key vaction)

    "Key" - The name of the key you defined with the control inside the DCL file. (Tn ca kho m bn xc nh vi kho iu khin trong tp tin DCL)

    "Action"- The action you want taken when the action event is fired. You can set a variable or run afunction. (Hnh ng m bn mun thc hin khi s kin hnh ng c khai ha. Bn c th to mt binhoc thc thi mt hnh ng)

    Examples: Cc v d:

    Let's take for example, you have a button named "test" and you want to set the variable named myTEST to 1if the user presses the button: (Ly v d, bn c mt nt t tn l test v bn mun t mt bin tn lmyTEST v 1 khi ngi s dng nhn nt ny: )

    (action_tile "test" "(setq myTest 1)")

    Notice the key is in quotes and the setq function is in quotes. This is standard procedure for the action_tilestatement. Next let's say you have the same thing except you want to run a function named "saveVars" whenthe user presses the test button. (Ch l tn kho v hm setq phi c t trong ngoc kp. l th tctiu chun cho cc hm thng bo phn t hnh ng. Tip theo bn c mt hm thng bo phn t hnhng tng t nh trn tr vic bn mun chy hm saveVar khi ngi s dng nhn nt test)

    (action_tile "test" "(saveVars)")

    Notice is is the same as the above. The (saveVars) is inside quotes. (Ch l cng nhtrn hm saveVarphi t trong ngoc kp)

    What if you wanted to do both, run the function and set a variable? (Vy khi bn mun thc hin c hai iutrn chy mt chng trnh v t mt bin th sao?)

    (action_tile "test" "(setq myTest 1)(saveVars)")

    Simply put both inside the quotes. (n gin l t c hai hm setq v saveVar vo trong ngoc kp.)

    One more thing....What if the button is set to be the cancel or accept button for the dialog box? No problem...

    Mt iu na, nu nt ny c t l nt xa hay chp nhn i vi hp thoi th sao? Khng c sao c

    (action_tile "test" "(setq myTest 1)(saveVars)(done_dialog)")

    Add the done_dialog statement to the end. [ Inside the quotes ](Thm thng bo done_dialog vo cui trc

    khi ng ngoc kp.)

  • 7/31/2019 The Dcl Autolisp Tutorial

    25/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    25

    That is about all there is to an action_tile. Remember, anything with a key can have an action call.

    l tt c v mt phn t hnh ng. Nhrng, bt k kha no cng c th c mt lnh hnh ng.

    Back

    Dialog Control Language - Set and Mode Tile (t v to chc nngcho phn ttrong DCL)

    Set_Tile and Mode_Tile (t phn tv to chc nng phn t)

    Set_Tile - is used to set the value of a control. (S dng t gi tr cho mt nt iu khin)

    (set_tile "key" "value")

    Set_Tile has two parameters. "Key" and "Value". (Hm Set_tile c hai tham s l key v value)

    "Key" - The name of the key you defined with the control inside the DCL file. (l tn ca kha m bnxc nh vi nt iu khin trong tp tin DCL)

    "Value"- The new value for the control. (Gi tr mi ca nt iu khin)

    Edit_box (Hp hiu chnh)o (set_tile "mybox" "Jeff") Displays Jeff in the edit box. (Hin th Jeff trong

    hp hiu chnh)o (set_tile "mybox" "4'-3 1/2") Displays 4'-3 1/2 in the edit box. (Hin th

    4-3 trong hp hiu chnh) List_box (Hp danh sch)

    o (set_tile "mylist" "0") Selects the first item in the list. (Chn khon mcth nht trong danh sch)

    o (set_tile "mylist" "5") Selects the sixth item in the list. (Chn khon mcth su trong danh sch)

    o (set_tile "mylist" "") No Items are selected. (Khng chn khon mc no) PopUp_List (Danh sch th xung)

    o (set_tile "mylist" "0") Selects the first item in the list. (Chn khon mcth nht trong danh sch)

    o (set_tile "mylist" "5") Selects the sixth item in the list. (Chn khon mcth su trong danh sch)o (set_tile "mylist" "") No Items are selected.(Khng chn khon mc no)

  • 7/31/2019 The Dcl Autolisp Tutorial

    26/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    26

    Toggle (Bt hoc tt kim sot hp iu khin)o (set_tile "mytog" "0") Removes the check from the box. (Xa kim sot)o (set_tile "mytog" "1") Checks the box. (Kim sot)

    Radio_Button (Nt la chn)o (set_tile "myradio1" "1") Moves the selection to the first radio button.

    (Chuyn vic la chn v nt la chn 1)o (set_tile "myradio2" "1") Moves the selection to the 2nd radio button.

    (Chuyn vic la chn v nt la chn 2)

    Use Set_Tile after the new_dialog and before the action_tiles. Set_Tile can also be used as a function whilethe dialog box is displayed. (S dng hm Set_Tilesau thng bo new_dialog v trc thng bo action_tile.Hm Set_tile cng c th s dng nh mt hm trong lc hp thoi c hin th)

    Mode_Tile - is used to enable or disable a control. (Hm c s dng bt hoc tt mt nt iu khin)

    (mode_tile "key" value)

    Mode_Tile has two parameters. "Key" and Value. (Hm c hai tham s l Key v Value)

    "Key" - The name of the key you defined with the control inside the DCL file. (l tn ca kha m bn xc nh vi nt iu khin trong tp tin DCL)

    Value- The new value for the control. 0 = Enabled 1 = Disabled (Gi tr mi ca nt iu khin 0 l bt1 l tt)

    (mode_tile "mylist" 0) Enables the list box (Kch hot hp danh sch)

    (mode_tile "mylist" 1) Disables the list box (Tt hp danh sch)

    Use Mode_Tile after the new_dialog call and before the action_tiles. Mode_Tile can also be used as afunction while the dialog box is displayed.(Hm Mode_tile s dng sau hm lnh new_dialog v trc hmaction_tile. HmMode_tile cng c thc s dng trong lc hp thoi c hin th)

    That is all there is to set_tile and mode_tile. We will used them later on in the tutorial so you can see themin action.( l tt c mi iu v hm Set_tile v hm Mode_tile. Ta s s dng chng trong phn sau v

    bn s thy chng hot ng ra sao.)

    Back

  • 7/31/2019 The Dcl Autolisp Tutorial

    27/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    27

    Dialog Control Language List (Danh sch trong DCL)

    List and how to handle them.(Danh sch v cch iu khin chng)

    List_box and popup_list handle a list of items in basically the same manner. You have to load your list intothe list box and at some point you have to decide which item or items were selected. In the "Saving data froma dialog box" I cover how to find the selected items from a list_box and a popup_list. How do we get the listinside the list_box? That is what we need to cover in this section. So let's get started.

    Hp danh sch v danh sch th xung v cbn iu khin mt danh sch theo cng mt phng cch.Bn ti danh sch ca bn thnh mt hp danh sch ti mt im no v bn phi quyt nh khonmc no hay nhng khon mc no sc la chn. Trong phn Lu gi liu t mt hp thoi ti s trnh

    by cch tm nhng khon mc c la chn t mt hp danh sch hay mt danh sch th xung. Lmsao ly cdanh sch bn trong mt hp danh sch? l iu bn s hc trong phn ny. Bt uthi.

    If you looked at the previous sections you know the AutoLisp and DCL basic model. Let's get a copy of thatin this section so we can look at it. I will replace the edit_box with a list_box control and add the code tohandle the list. The list handling code is shown in the "Saving data from a dialog box" section of thistutorial.

    cc phn trc, bn bit v cc mu cbn v Autolisp v DCL. phn ny ta s copy chng li xem xt. Ta s thay th hp hiu chnh bng mt hp danh sch v thm vo cc m iu khin danh sch .M iu khin danh sch ny c trnh by trong phn Lu d liu t mt hp thoi ca ti liu ny

    I will show the revised and new code in red below. All of the black code is unchanged from the BasicModel and the instructions from the "list_box multiple selection" area of the "Saving data from a Dialog box"

    page.

    Ti s trnh by cc phn m chnh sa v m mi bng mu . Tt c cc m mu en l khng thay iso vi mu cbn v so vi cc ch dn mc hp danh sch a la chn ca phn Lu d liu t mthp thoi

    The Basic Revised Model (Mu cbn c chnh sa)

    The DCL File: (Tp tin DCL)

    EXAMPLE : dialog {

    label = "EXAMPLE.lsp"; \\ Puts a label on the dialog box (Gn nhn hp thoi): column {: row {: boxed_column {

    : list_box { \\ I replaced the edit_box with a list_box. (Thayth hp hiu chnh bng hp danh sch)

  • 7/31/2019 The Dcl Autolisp Tutorial

    28/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    28

    label ="Choose Items";key = "mylist";height = 15;width = 25;

    multiple_select = true; \\ Multiple selection is on (Bt a la

    chn)fixed_width_font = true; \\ Fixed Width Font is on (Bt font m)value = "0";

    }}

    }: row {: boxed_row {

    : button {key = "accept";label = " Okay ";is_default = true;

    }: button {key = "cancel";label = " Cancel ";is_default = false;is_cancel = true;

    }}

    }}

    }

    The AutoLisp File: Tp tin Autolisp:

    ;;;--- EXAMPLE.lsp

    ;;;---I replaced the saveVars routine with the one from the "Save data from a list" section of this tutorial.;;; I also changed the things marked in red. (Ti thay th vng lu bin saveVar bng mt trong cc;;; cch c trnh by trong phn Lu d liu t mt hp thoi ca ti liu ny. Ti cng thay i;;;; nhng iu c nh du mu .)

    (defun saveVars(/ readlist count item)

    ;;;--- Notice the "mylist" is the action key associated with the DCL file and

    ;;; themyList is the variable for the list built below.(Ch rng mylist l kho hnh;;;; ng tng ng vi tp tin DCL v myList l bin cho danh sch c to thnh di y)

    ;;;--- Setup a list to hold the selected items (To mt danh sch gi cc khon mcchn)

    (setq retList(list))

    ;;;--- Save the list setting (Lu vic t danh sch)(setq readlist(get_tile "mylist"))

  • 7/31/2019 The Dcl Autolisp Tutorial

    29/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    29

    ;;;--- Setup a variable to run through the list (t bin chy qua ton b danh sch)(setq count 1)

    ;;;--- cycle through the list getting all of the selected items (Lp qua ton b danh sch

    ly cc khon mc c la chn)(while (setq item (read readlist))(setq retlist(append retList (list (nth itemmyList))))(while

    (and(/= " " (substr readlist count 1))(/= "" (substr readlist count 1))

    )(setq count (1+ count))

    )(setq readlist (substr readlist count))

    ))

    (defun C:EXAMPLE()

    ;;;--- I need to build a list of data ( Ti cn to mt danh sch cc d liu)(setqmyList(list "1" "2" "3" "4" "5" "6" "7" "8"))

    ;;;--- Load the dcl file (Ti tp tin DCL)(setq dcl_id (load_dialog "EXAMPLE.dcl"))

    ;;;--- Load the dialog definition if it is not already loaded (Ti nh ngha hp thoi nun cha c ti)

    (if (not (new_dialog "EXAMPLE" dcl_id) ) (exit))

    ;;;-- Here, I add the data to the list_box control(y ti thm d liu vo hp danh sch);;; I do this after the new_dialog call and before the action_tiles.(iu ny thc

    ;;;; hin sau hm new_dialog v trc hm action_tile.)(start_list "mylist" 3)(mapcar 'add_listmyList)(end_list)

    ;;;--- Notice the "mylist" is the action key associated with the DCL file and

    ;;; themyList is the variable for the list built above.(Ch rng mylist l kho

    ;;;; hnh ng tng ng vi tp tin DCL v myList l bin cho danh sch c to thnh di y);; If an action event occurs, do this function(Nu mt thao tc xy ra, thc hin hm ny)(action_tile "cancel" "(setq ddiag 1)(done_dialog)")(action_tile "accept" "(setq ddiag 2)(saveVars)(done_dialog)")

    ;;; If an action event occurs, do this function (Nu mt thao tc xy ra, thc hin hm ny)(action_tile "cancel" "(setq ddiag 1)(done_dialog)")(action_tile "accept" "(setq ddiag 2)(saveVars)(done_dialog)")

    ;;;--- Display the dialog box (Hin th hp thoi)

    (start_dialog)

  • 7/31/2019 The Dcl Autolisp Tutorial

    30/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    30

    ;;;- If the cancel button was pressed - display message (Hin th nu nt Cancel c nhn)(if (= ddiag 1)

    (princ "\n \n ...EXAMPLE Cancelled. \n "))

    ;;;--- If the "Okay" button was pressed (Nu nt Okay c nhn)(if (= ddiag 2)

    ;;;--- Here, I decide what to do with my data (y ti xc nh iu cn thc hin vibin)

    ;;; I'm going to print each selected item on the command line.(In tng khon mcc chn ra dng lnh)

    (foreach a retList(princ "\n")(princ a)

    ))

    ;;;--- Suppress the last echo for a clean exit (Trit tiu s lp li lnh cui v thot m)(princ)

    )

    So, in order to display a list in a dialog box, you must first build the list. Then use the start_list, add_list,and end_list functions just after the new_dialog call and before the action_tiles. This is the same for both alist_box and a popup_list. That wasn't so bad. Was it?

    Vy , hin th mt danh sch trong mt hp thoi, trc ht bn phi to danh sch . Sau s dngcc hm start_list, add_list, v end_list ngay sau hm lnh new_dialog v trc hm action_tile. iu ny lnh nhau i vi hp danh sch v danh sch th xung. Khng ti ch, phi khng?

    Back

    Dialog Control LanguageSaveVars(Lu bin trong DCL-SaveVars)

    In this section we are going to take a look at the SaveVars routine and disect it for each type of control.The types of controls that we sometimes need to save are :

    Trong phn ny, ta s xem xt vng lu bin SaveVars v disect n vi tng loi nt iu khin. Cc loint iu khin m i khi chng ta cn lu li l:

    Edit_box - List_box - Popup_List - Radio_Buttons - Radio_Column - Radio_Row - Toggle

    The SaveVars routine is executed just before the dialog box is issued a done_dialog call. You cannot get

  • 7/31/2019 The Dcl Autolisp Tutorial

    31/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    31

    the data from the dialog box once it has been shut down. If you remember our action calls in the AutoLispProgram, they looked something like this:

    Vng lu bin SaveVars c thc hin ngay trc khi hp thoi thc hin lnh gi done_dialog. Bnkhng th ly c d liu t hp thoi khi n ng li. Tr phi bn nhgi lnh hnh ng trong

    Autolisp. Chng trnh s ging nh sau:

    ;;;-- If an action event occurs, do this function (Nu mt thao tc xy ra, thc hin hmny)

    (action_tile "accept" "(saveVars)(done_dialog)")

    Notice the (saveVars) is just before the (done_dialog). This is the proper order.

    The settings from the dialog box will be saved just before it is shut down. (Ch y rnghm (saveVars) c t ngay trc hm (done_dialog). l trt tng. iu ny s khin hp thoi philu li bin trc khi ng li.)

    The SaveVars routine always has the same definition: (Vng lu bin saveVars lun c cngmt nh ngha l: )

    (defun saveVars()

    ;stuff here (Thc hin ba ci lng nhng y)

    )

    That's the shell. We need to work on the "stuffing". Let's get to it. ( mi ch lci v. Ta cn phi lm vic vi ba ci lng nhng na. Hy tip tc i na no.)

    Stuffing (Cc hnh ng thc hin trong vng lu bin SaveVars)

    There are three parts to a control with an action call back statement. (Mt nt iukhin vi mt hnh ng gi li hm thng bo c ba phn gm: )

    1. The DCL definition (nh ngha DCL)

    2. The action call in the AutoLisp file. (Lnh gi hnh ng trong tp tin Autolisp)

    3. The code inside the SaveVars routine. (M bn trong vng lu bin SaveVars)

    In each of the following controls I will show you all three parts so we know how toput it all together later. Although, all of the parts are important, try to focus onthe SaveVars routine.

    Trong mi nt iu khin di y, ti s trnh by c ba phn bn c th bit cch t chng kt hpvi nhau. Mc du c ba phn u quan trng nhng hy c gng tp trung vo vng lu bin SaveVars

    Edit_box - List_Box_Single_Selection - List_Box_Multiple_Selection - Popup_List

  • 7/31/2019 The Dcl Autolisp Tutorial

    32/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    32

    Radio_Column - Radio_Row - Toggle - Ok_Cancel

    Edit_Box(Hp hiu chnh)

    1

    : edit_box {key = "edbox";label = "Bolt Circle Diameter:";edit_width = 8;value = "10";

    }

    2

    No Action call required. We will get the data the user typed when we use the SaveVars routine. But,if your deadset about doing something when the user types in the edit box, here you go:

    Khng yu cu lnh gi hnh ng. Ta s ly d liu m ngi s dng nhp vo khi ta s dngvng lu bin Savevars. Nhng nu bn mun cnh thc hin mt iu g khi ngi s dngnhp vo hp hiu chnh , bn s thc hin:

    (action_tile "edbox" "(doThisFunction)")

    3

    To get the data as a real number:( c d liul mt s thc: )

    (defun saveVars()

    (setq edBox(distof(get_tile "edbox")))

    )

    To get the data as a string: ( c d liu l mtchui: )

    (defun saveVars()

    (setq edBox(get_tile "edbox"))

    )

    NOTE: Notice I used the key name as the variable to store the data in. You will find this methodmakes it easier to keep things straight. DCL is case sensitive so, I never capitalize key names, onlyautolisp variable names if they are made up of more than one word. Like: thisIsMyVariable.

    CH : Ti sdng tn kho nhmt bin lu dliu vo. Bn s tm thy phng php ny ld hn gitrc tip mi thca kho. DCL l rt nhy vi kiu chin hoa hay in thng, tikhng bao gisdng chin hoa cho cc tn kha, m ch sdng trong tn bin cA Autolisp khitn di hn mt t. V d: thisIsMyVariable.

    List_Box Single Choice Only(Hp danh sch vi sla chn n duy nht)

    1

    : list_box {key = "mylist";label = "Available Choices";

    multiple_select = "FALSE"; // Sets single selection (t s la chn n)

    width = 40;height = 20;

  • 7/31/2019 The Dcl Autolisp Tutorial

    33/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    33

    fixed_width_font = true; // Equally spaced characters (Cc k t cch u nhau)value = ""; // Start with no item selected (Khng chn trc)

    }

    2

    No Action call required. We will get the selected item when we use the SaveVars routine. But,

    sometimes you need to change things in the dialog box if the user makes a selection, so:Khng yu cu lnh gi hnh ng. Ta s ly khon mc c chn khi s dng vng lu binSaveVars. Nhng i khi bn cn thay i nhng iu trong hp thoi nu ngi s dng thc hins la chn, vy th:

    (action_tile "edbox" "(doThisFunction)") ;;; Ch ny chc c Jeff munkim tra my thng hc mt y, phi thay edbox bng mylist mi phi ch???

    3

    (defun saveVars()

    ;;;--Get the selected item from the list (Ly cc khon mc c chn t danh sch)(setq sStr(get_tile "mylist"))

    ;;;--- If the index of the selected item is not "" then something was selected

    (Nu s th t ca khon mc c chn l khc nil th chn mt iu g )(if(/= sStr "")

    (progn

    ;;;--- Something is selected, so convert from string to integer (C itng c chn, chuyn i tng t chui thnh s nguyn)

    (setq sIndex(atoi sStr))

    ;;;--- And get the selected item from the list (V ly i tng c chn tdanh sch)

    (setq sName(nth sIndex myList)))

    ;;;--- Else, nothing is selected (ngc li, khng c i tng no c chn)(progn

    ;;;--- Set the index number to -1 (t s th t v -1)(setq sIndex -1)

    ;;;--- And set the name of the selected item to nil (V t tn i tng cchn v nil)

    (setq sName nil))

    ))

    Notes: Ch :

    1. This should only be used when single selection is required. This will not work on multipleselection.(iu ny chc s dng vi vic la chn n. N khng lm vic nu l a la chn)

    2. This is assuming there is a list of items called myList. (Gi s c danh sch tn l myList)

  • 7/31/2019 The Dcl Autolisp Tutorial

    34/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    34

    List_Box Multiple Choice (Hp danh sch a la chn)

    1

    : list_box {key = "mylist";label = "Available Choices";

    multiple_select = "TRUE"; // Sets multiple selection (t a la chn)width = 40;height = 20;

    fixed_width_font = false; // Use default font [ no alignment ](S dng fontmc nh, Khng cn hng)value = "4"; // Start with item 5 selected.(Bt u vi khon mc th 5 c chn)

    }

    2

    No Action call required. We will get the selected items when we use the SaveVars routine. But,

    sometimes you need to change things in the dialog box if the user makes a selection, so:Khng yu cu lnh gi hnh ng. Ta s ly cc khan mc c chn khi s dng vng lubin SaveVars. Nhng i khi bn cn thay i nhng iu trong hp thoi khi ngi s dng tomt la chn, Th th:

    (action_tile "mylist" "(doThisFunction)")

    3

    (defun saveVars(/ readlist count item)

    ;;;--- Setup a list to hold the selected items (t mt danh sch gi cc khonmc c chn)

    (setq retList(list))

    ;;;--- Save the list setting (Lu li danh sch chn)(setq readlist(get_tile "files"))

    ;;;-- Setup a variable to run through the list (t bin lp qua ton b danh sch)(setq count 1)

    ;;;--- cycle through the list getting all of the selected items (Lp qua ton bdanh sch cc khon mc c chn)

    (while (setq item (read readlist))(setq retlist(append retList (list (nth item fileNames))))(while

    (and(/= " " (substr readlist count 1))(/= "" (substr readlist count 1))

    )(setq count (1+ count))

    )(setq readlist (substr readlist count))

    ))

    Note: This method can be used for a single or multiple selection list_box. (Ch rng phng phpny ch s dng cho hp danh sch a la chn)

  • 7/31/2019 The Dcl Autolisp Tutorial

    35/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    35

    PopUp_List(Danh sch th xung)

    1

    : popup_list {

    key = "mylist";// action key (Kho hnh ng)

    fixed_width_font = false; // fixed width font off(Tt font rng u)width = 20; // width of popup list ( rng danh sch th)height = 20; // height of popup list ( cao danh sch th)

    }

    2

    No Action call required. We will get the selected item when we use the SaveVars routine. But,sometimes you need to change things in the dialog box if the user makes a selection, so:Khng yu cu lnh gi hnh ng. Ta s ly cc khan mc c chn khi s dng vng lu

    bin SaveVars. Nhng i khi bn cn thay i nhng iu trong hp thoi khi ngi s dng tomt la chn, Th th:

    (action_tile "mylist" "(doThisFunction)")

    3

    (defun saveVars()

    ;;;--- Get the selected item from the list(Ly khan mc la chn t danh sch)(setq sStr(get_tile "mylist"))

    (if(= sStr "")(alert "No Item was Selected!"))

    )

    Radio_Column and Radio_Buttons (Ct la chn v cc nt la chn)

    1

    : radio_column { // Use boxed_radio_column if a box is required. (S dng cthp la chn nu yu cu mt hp)label = "Choices"; // Label for the column or boxed_column (Gn nhn cho ct hay

    ct hp)key = "choices"; // Action key for the radio column (Kho hnh ng ca ct la

    chn)

    : radio_button { // First radio button (Nt la chn th nht)label = "Choice 1";key = "choice1";

    }

    : radio_button { // Second radio button (Nt la chn th hai)label = "Choice 2";key = "choice2";

    }

    : radio_button { // Third radio button (Nt la chn th ba)

    label = "Choice 3";key = "choice3";

  • 7/31/2019 The Dcl Autolisp Tutorial

    36/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    36

    }

    : radio_button { // Fourth radio button (Nt la chn th t)label = "Choice 4";key = "choice4";

    }

    } // Close the radio_column (ng ct la chn)

    2

    No Action call required. We will get the selected item when we use the SaveVars routine. But,sometimes you need to change things in the dialog box if the user makes a selection, so:Khng yu cu lnh gi hnh ng. Ta s ly cc khan mc c chn khi s dng vng lu binSaveVars. Nhng i khi bn cn thay i nhng iu trong hp thoi khi ngi s dng to mt lachn, Th th:

    (action_tile "choices" "(doThisFunction)") // If any choice is made (Nu mt

    khon mc bt kc la chn)

    (action_tile "choice1" "(doThatFunction)") // If choice1 is made (Nu khonmc th nht c chn)

    (action_tile "choice2" "(doDisFunction)") // If choice2 is made (Nu khonmc th hai c chn)

    etc.

    3

    (defun saveVars()

    ;;;--- Get the key of the choice made(Ly kha ca s la chn thc hin);;; [ returns (Tr v gi tr) "choice1" "choice2" "choice3" or "choice4" ]

    (setq choicesVal(get_tile "choices"))

    )

    OR (Hoc)

    (defun saveVars()

    ;;;--- Get the value of each item (Ly gi tr ca mi mt khon mc)(setq choice1(atoi(get_tile "choice1"))) // 0 = not chosen 1 = chosen (0 l khng chn, 1 l

    chn)(setq choice2(atoi(get_tile "choice2"))) // 0 = not chosen 1 = chosen (0 l khng chn, 1 l chn)(setq choice3(atoi(get_tile "choice3"))) // 0 = not chosen 1 = chosen (0 l khng chn, 1 l chn)(setq choice4(atoi(get_tile "choice4"))) // 0 = not chosen 1 = chosen (0 l khng chn, 1 l chn)

    )

    Radio_Row And Radio_Buttons (Hng la chn v cc nt la chn)

  • 7/31/2019 The Dcl Autolisp Tutorial

    37/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    37

    1

    : radio_row { // Use boxed_radio_row if a box is required. (S dng hnghp la chn nu yu cu mt hp)label = "Choices"; // Label for the row or boxed_row (Gn nhn cho hng hay

    hng hp)

    key = "choices"; // Action key for the radio row (Kho hnh ng cho hng la chn): radio_button { // First radio button (Nt la chn th nht)label = "Choice 1";key = "choice1";

    }

    : radio_button { // Second radio button (Nt la chn th hai)label = "Choice 2";key = "choice2";

    }

    : radio_button { // Third radio button (Nt la chn th ba)label = "Choice 3";key = "choice3";

    }

    : radio_button { // Fourth radio button (Nt la chn th t)label = "Choice 4";key = "choice4";

    }

    } // Close the radio_row (ng hng la chn)

    2

    No Action call required. We will get the selected item when we use the SaveVars routine. But,sometimes you need to change things in the dialog box if the user makes a selection, so:Khng yu cu lnh gi hnh ng. Ta s ly cc khan mc c chn khi s dng vng lu

    bin SaveVars. Nhng i khi bn cn thay i nhng iu trong hp thoi khi ngi s dng tomt la chn, Th th:

    (action_tile "choices" "(doThisFunction)") // If any choice is made (Nu btk khon mc no c chn)

    (action_tile "choice1" "(doThatFunction)") // If choice1 is made (Nuchoice1 c chn)

    (action_tile "choice2" "(doDisFunction)") // If choice2 is made (Nuchoice2 c chn)

    etc.

    3

    (defun saveVars()

    ;;;--- Get the key of the choice made (Ly kho ca la chn c thc hin)

    ;;; [ returns (Tr v gi tr)"choice1" "choice2" "choice3" or "choice4" ]

  • 7/31/2019 The Dcl Autolisp Tutorial

    38/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    38

    (setq choicesVal(get_tile "choices"))

    )

    OR (HoC)

    (defun saveVars()

    ;;;--- Get the value of each item (Ly gi tr ca mi khon mc)(setq choice1(atoi(get_tile "choice1"))) // 0 = not chosen 1 = chosen (0 l khng chn, 1 l

    chn)(setq choice2(atoi(get_tile "choice2"))) // 0 = not chosen 1 = chosen (0 l khng chn, 1 l

    chn)(setq choice3(atoi(get_tile "choice3"))) // 0 = not chosen 1 = chosen (0 l khng chn, 1 l

    chn)(setq choice4(atoi(get_tile "choice4"))) // 0 = not chosen 1 = chosen (0 l khng chn, 1 l

    chn)

    )

    Toggle (Bt v tt kim sot nt iu khin)

    1

    : toggle {

    key = "tog1"; // Action key (Kho hnh ng)label = "Name"; // Label (Nhn)

    }

    2

    No Action call required. We will get the value of the toggle when we use the SaveVars routine.But, sometimes you need to change things in the dialog box if the user makes a selection, so:Khng yu cu lnh gi hnh ng. Ta s ly cc khan mc c chn khi s dng vng lu

    bin SaveVars. Nhng i khi bn cn thay i nhng iu trong hp thoi khi ngi s dng tomt la chn, Th th:

    (action_tile "tog1" "(doThisFunction)")

    3

    (defun saveVars()

    ;;;--- Get the selected item from the radio column (Ly khon mc la chn t ctla chn)

    (setq tog1Val(atoi(get_tile "tog1"))) // 0 = unchecked 1 = checked (0 l khng chn, 1 l chn)

    )

    Ok_Cancel

  • 7/31/2019 The Dcl Autolisp Tutorial

    39/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    39

    1

    ok_cancel; // Only one line required (Yu cu mt dng duy nht)

    Note: I usually define my own okay and cancel buttons using two standard buttons, but this worksfine. (Ch l ti thng xc nh hai nt Okay v Cancel ca mnh bng cch s dng hai nt tiu

    chun m chng lm vic rt tt)

    2

    You will need two action calls for this button...both close the dialog box but the accept or "Okay" keywill save the dialog box settings before shutting the dialog box down.Bn cn c hai lnh gi hnh ng cho nt ny, c hai u ng hp thoi nhng khaAccept hayOkay s lu vic t hp thoi li trc khi ng hp thoi.

    (action_tile "cancel" "(setq ddiag 1)(done_dialog)")

    (action_tile "accept" "(setq ddiag 2)(saveVars)(done_dialog)")

    Ahh....finally the SaveVars routine shows up. (A ha, vy l xong vi vng lu bin SaveVars ri)3

    Nothing to do in the saveVars routine for these buttons. (Ch cn g lm thm vi cc nt ny trongvng lu bin SaveVars)

    Back

    Dialog Control Language - Part 1 (Phn thc hnh 1 v DCL)

    Part 1 - Buttons(Phn 1- Cc nt)

    Let's build a working DCL file showing us exactly how to handle buttons. (Ta s xy dng tp tin DCL hotng thy c chnh xc cch iu khin cc nt)

    We will build a DCL file containing three buttons plus a Close [ Cancel ] button. Each of the three buttons

    will display a message when pressed. (Ta s to mt tp tin DCL gm ba nt v thm mt nt Close[Cancel]. Mi mt trong ba nt s hin th mt thng bo khi b nhn.)

    Layout thoughts: I will place the buttons in a column, (stacked on top of each other). Then I'll put theClose button at the bottom on a row of it's own. So...I'll need something like this:

    Thit k hp thoi: Ta st cc nt trong mt ct (Cc nt chng ln nhau). Sau ta st nt Close hng cui cng ca hp thoi. Do , ta s cn mt thi loi nh th ny:

    : column {: boxed_column {

  • 7/31/2019 The Dcl Autolisp Tutorial

    40/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    40

    : button {

    // Put code for button 1 here (t m cho nt 1 y)}: button { ]

    // Put code for button 2 here(t m cho nt 2 y)}: button { ]

    // Put code for button 3 here (t m cho nt 3 y)}

    }: boxed_row {

    : button {

    // Put code for the Close button here (t m cho nt Close y)}

    }}

    Let's copy in the code for the header and all of the controls above. I'll show them in red. Notice the keynames and labels had to be changed. (Ta s copy cc m phn tiu v tt c cc miu khin trn. Tis trnh by chng mu . Ch cc tn kho v cc nhn c s thay i.)

    SAMPLE1 : dialog {label = "Sample Dialog Box Routine - Part 1";: column {: boxed_column {: button {

    key = "but1";label = "Button 1";is_default = false;

    }: button {

    key = "but2";label = "Button 2";is_default = false;

    }: button {

    key = "but3";label = "Button 3";is_default = false;

    }}: boxed_row {: button {

    key = "cancel";label = "Close";is_default = true;is_cancel = true;

    }

  • 7/31/2019 The Dcl Autolisp Tutorial

    41/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    41

    }

    }

    Right click and copy the above. Open NotePad and paste it. Save the file as SAMPLE1.DCL Be sure to

    change the "Save as Type" drop down box to "All Files" before saving it or it will put a ".txt" extensionon the file name. Save this file somewhere in the AutoCAD search path.

    Click chut phi v copy tp tin trn. MNotePad v dn n vo. Lu li n di tn SAMPLE1.DCLPhim bo l bn thay i gi trca hp thoi Save as Type thnh All Files trc khi lu tptin, nu khng n sblu vi kiu tp tin l txt. Lu tp tin ny trongng dn tm kim ca AutoCad.

    Next we will get a copy of the AutoLisp model and revise it. All new code is shown in red. (Tip theo ta scopy chng trnh Autolisp mu v chnh sa li. Cc m mi sc trnh by mu .)

    (defun C:SAMPLE1()

    ;;;--- Load the dcl file (Ti tp tin DCL)(setq dcl_id (load_dialog "SAMPLE1.dcl"))

    ;;;--- Load the dialog definition if it is not already loaded (Ti nh ngha hp thoi nun cha c ti)

    (if (not (new_dialog "SAMPLE1" dcl_id))(progn

    (alert "The SAMPLE1.DCL file could not be loaded!")(exit)

    )

    )

    ;;;--- If an action event occurs, do this function (Nu c mt s hnh ng, thc hin hm)(action_tile "cancel" "(done_dialog)")

    ;;;--- Display the dialog box (Hin th hp thoi)(start_dialog)

    ;;;--- Unload the dialog box (Thot khi hp thoi)(unload_dialog dcl_id)

    ;;;--- Suppress the last echo for a clean exit (Trit tiu vic lp li lnh cui v thot m)

    (princ)

    )

    I removed several lines from the model. I took out the part that checked to see if we hit the Cancel or Okaybuttons. We don't need either in this program. I also removed the action tile for the okay button andremoved "(setq ddiag 1)" from the cancel button.

    Ti xa i mt vi dng t chng trnh mu. Ti ly i phn kim sot xem liu ta c kch cc nt Okayhay Cancel khng. Ta khng cn iu trong chng trnh ny. Ti cng xa i phn t hnh ng i vint Okay v xo i setq ddiag 1 t phn t hnh ng i vi nt Cancel.

    Right click and copy the above. Open NotePad and paste it. Save the file as SAMPLE1.LSP Be sure to

  • 7/31/2019 The Dcl Autolisp Tutorial

    42/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    42

    change the "Save as Type" drop down box to "All Files" before saving it or it will put a ".txt" extensionon the file name. Save this file somewhere in the AutoCAD search path.

    Click chut phi v copy tp tin trn. MNotePad v dn n vo. Lu li n di tn SAMPLE1.LSP .Phim bo l bn thay i gi trca hp thoi Save as Type thnh All Files trc khi lu tp

    tin, nu khng n sblu vi kiu tp tin l txt. Lu tp tin ny trongng dn tm kim ca AutoCad.

    Let's load the program and see what the DCL file looks like. On the command line type this: (Ta hy tichng trnh v xem tp tin DCL ra sao. Trn dng lnh nhp nh sau: )

    Command: (load "sample1") and press enter(Nhp (load sample1) ri nhn Enter)

    You should see this (Bn s thy nh sau: )

    C:Sample1Command:

    Now type Sample1 and press enter. If everything went according to plan you should see this on your screen:

    By ginhp Sample1 v nhn Enter. Nu mi thng nh k hoach, bn s thy trn mn hnh:

    The buttons do not work yet. Except for the close button. It should work fine. We need to add the functionto print three different messages when the user presses each button. Let's send a parameter to the function todecide which message to display. If the parameter equals 1 we will print the first message. If 2 then printthe second message. If 3 print the third message. Something like this:

    Cc nt vn cha lm vic ngoi tr nt Close. Chng s phi lm vic thi. Ta cn thm hm in bathng bo khc nhau khi ngi s dng nhn vo mi nt. Ta s gi thng s vo hm quyt nh thng

    bo no s hin th. Nu thng s bng 1 ta s in thng bo th nht. Nu thng s bng 2 ta s in thng both hai. Nu thng s l 3 ta s in thng bo th ba. Hm s nh sau:

    (defun doButton(a)

  • 7/31/2019 The Dcl Autolisp Tutorial

    43/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    43

    (cond((= a 1)(alert "Button 1 was pressed!"))((= a 2)(alert "Button 2 was pressed!"))((= a 3)(alert "Button 3 was pressed!"))

    ))

    Now we need to add the action calls for each of the buttons: (By gita cn phi thm cc lnh gi phn thnh ng cho mi mt nt)

    (action_tile "but1" "(doButton 1)")(action_tile "but2" "(doButton 2)")(action_tile "but3" "(doButton 3)")

    This will send a parameter of 1,2, or 3 to the doButton function depending on which button is pressed. (iuny s gi cc thng s 1,2 v 3 vo hm doButton ty theo nt no c nhn)

    Let's add the doButton function and the action calls to the autolisp program. It should look like this:

    Ta hy thm hm doButton v cc lnh gi hnh ng vo chng trnh Autolisp. N s ging nh sau:

    (defun doButton(a)(cond

    ((= a 1)(alert "Button 1 was pressed!"))((= a 2)(alert "Button 2 was pressed!"))((= a 3)(alert "Button 3 was pressed!"))

    )

    )

    (defun C:SAMPLE1()

    ;;;--- Load the dcl file(setq dcl_id (load_dialog "SAMPLE1.dcl"))

    ;;;--- Load the dialog definition if it is not already loaded(if (not (new_dialog "SAMPLE1" dcl_id))

    (progn(alert "The SAMPLE1.DCL file could not be loaded!")(exit)

    )

    )

    ;;;--- If an action event occurs, do this function(action_tile "but1" "(doButton 1)")(action_tile "but2" "(doButton 2)")(action_tile "but3" "(doButton 3)")(action_tile "cancel" "(done_dialog)")

    ;;;--- Display the dialog box(start_dialog)

    ;;;--- Unload the dialog box(unload_dialog dcl_id)

    ;;;--- Suppress the last echo for a clean exit

  • 7/31/2019 The Dcl Autolisp Tutorial

    44/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    44

    (princ)

    )

    Save it and test it out. Everything working okay? (Lu n li v th chy. Mi th lm vic tt ch?)

    When you get your program tested and everything is working, move the blue line above, [ (defunC:SAMPLE1() ] all the way to the top of the file. This will make all of your variables local and will reset themall to nil when the program ends.

    Khi bn chy th chng trnh v mi th u lm vic tt, hy chuyn dng ch mu xanh [(defunC:Sample1() ] ln trn cng ca tp tin. iu ny s lm cho tt c cc bin ca bn thnh bin cc b v tli chng v nil khi chng trnh kt thc.

    That's it. We're done. Vy , Ta hon thnh.

    Back

    Dialog Control Language - Part 2 (Phn thc hnh 2 v DCL)

    Part 2 - Edit_Box (Phn 2 - Hp hiu chnh)

    Let's build a working DCL file showing us exactly how to handle edit boxes. (Ta s xy dng tp tin DCL trnh by chnh xc cch iu khin hp hiu chnh)

    We will build a DCL file containing two edit boxes plus a Okay and Cancel button. The information in thetwo edit boxes will be displayed after selecting the Okay button. (Ta s to mt tp tin DCL cha hai hphiu chnh v b nt Okay v Cancel. Thng tin trong hai hp hiu chnh sc hin th sau khi nhn ntOkay.)

    Layout thoughts: I will place the edit boxes in a column, (stacked on top of each other). Then I'll put theOkay and Cancel buttons at the bottom in a row. So...I'll need something like this:

  • 7/31/2019 The Dcl Autolisp Tutorial

    45/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    45

    Thit k hp thoi: Ti st cc hp hiu chnh trong mt ct (cc hp chng ln nhau). Sau t ccnt Okay v Cancel vo hng di cng. Vy nn ti cn mt th nh sau:

    : column {: boxed_column {

    : edit_box {// Put code for edit_box 1 here (t m ca hp hiu chnh 1 y)

    }: edit_box {

    // Put code for edit_box 3 here(t m ca hp hiu chnh 3 y)}

    }: boxed_row {

    : button {

    // Put code for the Okay button here (t m ca nt Okay y)}: button {

    // Put code for the Cancel button here (t m ca nt Cancel y)}

    }}

    Let's copy in the code for the header and all of the controls above. I'll show them in red. Notice the keynames and labels had to be changed. (Ta s copy phn tiu v tt c cc m iu khin mu trn. Ti s

    th hin chng mu . Ch l cc tn kho v cc nhn c thay i.)

    SAMPLE2 : dialog {label = "Sample Dialog Box Routine - Part 2";: column {: boxed_column {: edit_box {

    key = "username";label = "Enter your Name:";edit_width = 15;value = "";

    initial_focus = true;}: edit_box {

    key = "userage";label = "Enter your Age:";edit_width = 15;value = "";

    }}: boxed_row {: button {

    key = "accept";label = " Okay ";

  • 7/31/2019 The Dcl Autolisp Tutorial

    46/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    46

    is_default = true;}: button {

    key = "cancel";label = " Cancel ";

    is_default = false;is_cancel = true;

    }}

    }

    }

    Right click and copy the above. Open NotePad and paste it. Save the file as SAMPLE2.DCL Be sure tochange the "Save as Type" drop down box to "All Files" before saving it or it will put a ".txt" extensionon the file name. Save this file somewhere in the AutoCAD search path.

    Next we will get a copy of the AutoLisp model and revise it. All new code is shown in red. (Tip theo ta scopy chung trnh Autolisp mu v chnh sa li. Cc m mi c th hin bng mu .)

    (defun C:SAMPLE2()

    ;;;--- Load the dcl file(setq dcl_id (load_dialog "SAMPLE2.dcl"))

    ;;;--- Load the dialog definition if it is not already loaded(if (not (new_dialog "SAMPLE2" dcl_id))

    (progn(alert "The SAMPLE2.DCL file could not be loaded!")(exit)

    ))

    ;;;--- If an action event occurs, do this function(action_tile "accept" "(setq ddiag 2)(saveVars)(done_dialog)") ;Ch thng SaveVars(action_tile "cancel" "(setq ddiag 1)(done_dialog)")

    ;;;--- Display the dialog box(start_dialog)

    ;;;--- Unload the dialog box(unload_dialog dcl_id)

    ;;;--- If the user pressed the Cancel button(if(= ddiag 1)

    (princ "\n Sample2 cancelled!"))

    ;;;--- If the user pressed the Okay button(if(= ddiag 2)

    (progn(princ "\n The user pressed Okay!")

    ))

  • 7/31/2019 The Dcl Autolisp Tutorial

    47/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    47

    ;;;--- Suppress the last echo for a clean exit(princ)

    )

    Right click and copy the above. Open NotePad and paste it. Save the file as SAMPLE2.LSP Be sureto change the "Save as Type" drop down box to "All Files" before saving it or it will put a ".txt"extension on the file name. Save this file somewhere in the AutoCAD search path.

    Let's load the program and see what the DCL file looks like. On the command line type this:

    Command: (load "sample2") and press enter

    You should see this

    C:Sample2Command:

    Now type Sample2 and press enter. If everything went according to plan you should see this on your screen:

    Looking good so far. We need to add the SaveVars function to save the strings in the edit boxes when theOkay button is pressed. Look at the blue text in the Sample2.lsp program above.(Trng qu p. Ta cn thmhm SaveVars lu li chui trong hp hiu chnh khi nt Okay c nhn.Hy xem dng chmu xanh

    trong chng trnh Autolisp Sample2.lsp trn)

    The edit box for the name needs to be a string. Dialog boxes return strings, so we do not need to modify it.All we have to do is use the get_tile function. (Hp hiu chnh dng cho tn cn nhp mt chui. Cc hpthoi tr v gi tr l cc chui, v th ta khng cn phi sa n. Mi th ta cn lm l s dng hm get_tile)

    (setq userName(get_tile "username")) ; C php ny t cha thng, tm hiu l hm;;get_tile tr v mt chuic nhp trong;;;edit_box c kha l i s ca n.

    The edit box for Age needs to be an integer. We will have to modify the get_tile results by using the ATOIfunction. This function converts a string to an integer. (Hp hiu chnh dng cho tui cn nhp mt s

  • 7/31/2019 The Dcl Autolisp Tutorial

    48/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    48

    nguyn. Ta s phi sa kt qu ca hm get_tile bng cch s dng hm ATOI. Hm ny i mt chui thnhmt s nguyn)

    (setq userAge( atoi (get_tile "userage")))

    If we needed to convert to an Real number we would use DISTOF function instead of the ATOI function.(Nu ta cn i thnh mt sthc th phi sdng hm DISTOF thay cho hm ATOI)

    Our SaveVars routine would look like this: (Vng lu bin ca ta s trng ging nh sau: )

    (defun saveVars()(setq userName(get_tile "username"))(setq userAge( atoi (get_tile "userage")))

    )

    Our program would now look like this: (Chng trnh lc ny s nh sau:)

    (defun saveVars()(setq userName(get_tile "username"))(setq userAge(atoi(get_tile "userage")))

    )

    (defun C:SAMPLE2()

    ;;;--- Load the dcl file(setq dcl_id (load_dialog "SAMPLE2.dcl"))

    ;;;--- Load the dialog definition if it is not already loaded(if (not (new_dialog "SAMPLE2" dcl_id))

    (progn(alert "The SAMPLE2.DCL file could not be loaded!")(exit)

    ))

    ;;;--- If an action event occurs, do this function(action_tile "accept" "(setq ddiag 2)(saveVars)(done_dialog)")(action_tile "cancel" "(setq ddiag 1)(done_dialog)")

    ;;;--- Display the dialog box

    (start_dialog)

    ;;;--- Unload the dialog box(unload_dialog dcl_id)

    ;;;--- If the user pressed the Cancel button(if(= ddiag 1)

    (princ "\n Sample2 cancelled!"))

    ;;;--- If the user pressed the Okay button(if(= ddiag 2)

    (progn

    (princ "\n The user pressed Okay!"))

  • 7/31/2019 The Dcl Autolisp Tutorial

    49/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    49

    )

    ;;;--- Suppress the last echo for a clean exit(princ)

    )

    Last item. We need to replace the line in the program: (princ "\n The user pressed Okay!") withsomething to modify and display the userName and userAge data. Let's do something simple. We will findout how many days old this person is. (T cui cng. Ta cn thay th dng thng bo (princ \n The user

    pressed Okay!) trong chng trnh bng t sa i v hin th Tn ngi s dng cng vi s ngy tui cahn. Ta s lm t n gin ny. Hy tm ra s ngy tui ca g.)

    ;;;--- If the user pressed the Okay button(if(= ddiag 2)

    (progn

    ;;;--- Multiply the users age x 365 to get the number of days.(Nhn s tui vi 365)(setq userAge(* userAge 365))

    ;;;--- Display the results (Hin th kt qu)(alert (strcat userName " is " (itoa userAge) " days old."))

    ))

    Add the above to the file, save it and test it out. Everything working okay? (Thm nhng iu trn vo

    chng trnh. Lu li v chy th. Mi th chy tt ch?)

    When you get your program tested and everything is working, move the blue line above, [ (defunC:SAMPLE2() ] all the way to the top of the file. This will make all of your variables local and will reset themall to nil when the program ends.

    That's it. We're done. (Bit ri, kh lm)

    Back

    Dialog Control Language - Part 3 (Phn thc hnh 3 v DCL)

  • 7/31/2019 The Dcl Autolisp Tutorial

    50/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    50

    Part 3 - List_Box (Phn 3 - Hp danh sch)

    Let's build a working DCL file showing us exactly how to handle list boxes. (Ta s xy dng mt tp tinDCL hot ng ch ra chnh xc cch iu khin mt hp danh sch)

    We will build a DCL file containing two list boxes plus an Okay and Cancel button. We will set the first listbox to single selection and the second to multiple selection. The selected items will be displayed on thescreen after the user presses the Okay button. ( ta s to ra mt tp tin DCL cha hai hp danh sch vi b ntOkay v Cancel. Ta st hp danh sch th nht l la chn n v hp danh sch th hai l a la chn.Cc khon mc c la chn sc hin th trn mn hnh sau khi ngi s dng nhn nt Okay.)

    Layout thoughts: I will place the list boxes in a row, (side by side). Then I'll put the Okay and Cancelbuttons in a row at the bottom of the dialog box. So...I'll need something like this:

    Thit k hp thoi: Ti st cc hp danh sch trong mt hng (Cc hp cnh nhau). Sau t cc ntOkay v Cancel vo hng di cng ca hp thoi. V th ti s cn mt th nh sau:

    : column {: boxed_row {

    : list_box {

    // Put code for list_box 1 here (t m cho hp danh sch 1 y)}: list_box {

    // Put code for list_box 2 here (t m cho hp danh sch 2 y)}

    }: boxed_row {

    : button {

    // Put code for the Okay button here (t m cho nt Okay y)}: button {

    // Put code for the Cancel button here (t m cho Nt Cancel y)}

    }}

    Let's copy in the code for the header and all of the controls above from the "Controls" section of this tutorial.I'll show them in red. Notice the key names and labels had to be changed. (Ta hy copy cc m tiu v ttc cc m iu khin trn t phn cc nt iu khin trong ti liu ny. Ti s th hin chng bng mu .Ch l cc tn kha v cc nhn c thay i)

  • 7/31/2019 The Dcl Autolisp Tutorial

    51/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    51

    SAMPLE3 : dialog {label = "Sample Dialog Box Routine - Part 3";: column {: boxed_row {: list_box {

    label ="Choose Item";key = "mylist1";height = 15;width = 25;multiple_select = false;fixed_width_font = true;value = "";

    }: list_box {

    label ="Choose Items";key = "mylist2";

    height = 15;width = 25;multiple_select = true;fixed_width_font = true;value = "";

    }}: boxed_row {: button {

    key = "accept";label = " Okay ";

    is_default = true;}: button {

    key = "cancel";label = " Cancel ";is_default = false;is_cancel = true;

    }}

    }

    }

    Right click and copy the above. Open NotePad and paste it. Save the file as SAMPLE3.DCL Be sure tochange the "Save as Type" drop down box to "All Files" before saving it or it will put a ".txt" extensionon the file name. Save this file somewhere in the AutoCAD search path.

    Next we will get a copy of the AutoLisp model and revise it. All new code is shown in red. (Tip theo ta sly mt phin bn ca chng trnh Autolisp mu v chnh sa li. Tt c m mi c th hin bi mu )

    (defun C:SAMPLE3()

  • 7/31/2019 The Dcl Autolisp Tutorial

    52/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    52

    ;;;--- Load the dcl file(setq dcl_id (load_dialog "SAMPLE3.dcl"))

    ;;;--- Load the dialog definition if it is not already loaded(if (not (new_dialog "SAMPLE3" dcl_id))

    (progn

    (alert "The SAMPLE3.DCL file could not be loaded!")(exit)

    ))

    ;;;--- If an action event occurs, do this function(action_tile "accept" "(setq ddiag 2)(saveVars)(done_dialog)") ;Ch hm SaveVars(action_tile "cancel" "(setq ddiag 1)(done_dialog)")

    ;;;--- Display the dialog box(start_dialog)

    ;;;--- Unload the dialog box

    (unload_dialog dcl_id)

    ;;;--- If the user pressed the Cancel button(if(= ddiag 1)

    (princ "\n Sample3 cancelled!"))

    ;;;--- If the user pressed the Okay button(if(= ddiag 2)

    (progn(princ "\n The user pressed Okay!")

    ))

    ;;;--- Suppress the last echo for a clean exit(princ)

    )

    Right click and copy the above. Open NotePad and paste it. Save the file as SAMPLE3.LSP Be sureto change the "Save as Type" drop down box to "All Files" before saving it or it will put a ".txt"extension on the file name. Save this file somewhere in the AutoCAD search path.

    Let's load the program and see what the DCL file looks like. On the command line type this:

    Command: (load "sample3") and press enter

    You should see this

    C:Sample3Command:

    Now type Sample3 and press enter. If everything went according to plan you should see this on your screen:

  • 7/31/2019 The Dcl Autolisp Tutorial

    53/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    53

    Looks good but, there is nothing to choose. Let's add some data to the list boxes. We will need two list.We will call the list for the first list box myList1 and the second myList2. (Trng p y nhng cha c g chn. Ta s thm d liu vo hp danh sch. Ta cn hai danh sch. Ta s gi hm list to danh sch thnht l myList1 v danh sch th hai l myList2.)

    (setq myList1(list "Electrical" "Structural" "Plumbing" "Foundation"))

    (setq myList2(list "Plastic" "Steel" "Aluminum" "Concrete"))

    Alrighty then, we have our list built. All we have to do is put them in the dialog box. We will use thestart_list, add_list, and end_list functions. Start_list tells DCL which list_box we are going to edit. Theidentification is made by using the list_box KEY. The first list has a key of "mylist1" and the second has akey of "mylist2". So the start_list function would look like this:

    Vy l ta to cc danh sch ca mnh. iu phi lm l t n vo trong hp danh sch. Ta s s dng cchm start_list, add_list v end_list. Hm start_list s bo DCL hp danh sch no s phi sa li. Vic chnh c thc hin bng cch s dng kha ca hp danh sch. Danh sch th nht c kho l mylist1 vdanh sch th hai c kho l mylist2. Do hm start_list s nh sau:

    (start_list "mylist1" 3) ;The 3 means we want to delete the old contents and start new. (Thams 3 c ngha l ta mun xo cc ni dung c v bt u danh sch mi)

    Next we use the add_list function to tell DCL which list to put in the list_box. We use the mapcar functionto apply add_list to each member in the list. Our list for the first list_box is named myList1. So... (Tip theos dng hm add_list bO DCL danh sch no sc t vo trong hp danh sch. Ta s dng hmmapcar p dng hm add_list cho mi thnh vin trong danh sch . Danh sch ca chng ta dng cho

    hp danh sch th nht l myList1. V th ta c:

  • 7/31/2019 The Dcl Autolisp Tutorial

    54/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    54

    (mapcar 'add_list myList1)

    Finally we use the end_list function to tell DCL to display the new contents because we are through editingthe list. (Cui cng ta dng hm End_list bO DCL hin th ni dung mi v ta sa xong danh sch )

    (end_list)

    To look at it all together: Hy xem ton b cng vic ny:

    (start_list "mylist1" 3)(mapcar 'add_list myList1)(end_list)

    (start_list "mylist2" 3)(mapcar 'add_list myList2)(end_list)

    Let's add all of this to the AutoLisp program and see what it looks like. (Thm cc m ny vo chng trnh)

    (defun C:SAMPLE3()

    (setq myList1(list "Electrical" "Structural" "Plumbing" "Foundation"))(setq myList2(list "Plastic" "Steel" "Aluminum" "Concrete"))

    ;;;--- Load the dcl file(setq dcl_id (load_dialog "SAMPLE3.dcl"))

    ;;;--- Load the dialog definition if it is not already loaded(if (not (new_dialog "SAMPLE3" dcl_id))(progn

    (alert "The SAMPLE3.DCL file could not be loaded!")(exit)

    ))

    (start_list "mylist1" 3)(mapcar 'add_list myList1)(end_list)

    (start_list "mylist2" 3)

    (mapcar 'add_list myList2)(end_list)

    ;;;--- If an action event occurs, do this function(action_tile "accept" "(setq ddiag 2)(saveVars)(done_dialog)") ;Ch hm SaveVars(action_tile "cancel" "(setq ddiag 1)(done_dialog)")

    ;;;--- Display the dialog box(start_dialog)

    ;;;--- Unload the dialog box(unload_dialog dcl_id)

    ;;;--- If the user pressed the Cancel button(if(= ddiag 1)

  • 7/31/2019 The Dcl Autolisp Tutorial

    55/100

    Gi Mr. DUN Hng dn vit v sdng Dialog trong AutoLisp

    55

    (princ "\n Sample3 cancelled!"))

    ;;;--- If the user pressed the Okay button(if(= ddiag 2)

    (progn

    (princ "\n The user pressed Okay!"))

    )

    ;;;--- Suppress the last echo for a clean exit(princ)

    )

    Notice the location of the red lines. We create the list before loading the dialog box. We add the list to thedialog box after it is loaded with new_dialog and before the action_tile statements. This is the order youshould use.

    Ch ti v tr ca cc dng mu . Ta to danh sch trc khi ti hp thoi. Ta thm danh sch vo hpthoi sau khi n c ti vi hm new_dialog v trc hm thng bo phn t hanh ng. y l trt t bnnn dng

    Looking good so far. We need to add the SaveVars function to save the selected items from the list boxeswhen the Okay