You are on page 1of 96

BI GING :

LP TRNH TRONG AUTOCAD


Ging Vin : Trn anh Bnh

Sch tham kho :


Wileys & Sons George Omura John Wiley & Sons KTS.Lu Triu Nguyn. TS.Nguyn Hu Lc. TS.Nguyn Hu Lc. Nguyn Thanh Trung. AutoCAD 2004 (1,2) c bn v nng cao TS.Nguyn Hu Lc. AutoCAD 2004 Activex and VBA KS.Hong Thnh An. AutoCAD 2004 Bible Mastering in AutoCAD 2000 AutoCAD 2004 For Dummies AutoCAD 2000 (1,2) AutoCAD 2004 (1,2) c bn v nng cao Cc tin ch thit k trn AutoCAD

S tit ging : 45 Tit Lp trnh trong AutoCAD : 45 tit. L thuyt : 30 tit. Thc hnh : 5 bui (3x5=15 tit).

LP TRNH TRONG AUTOCAD (45 tit)


CHNG 1 : GII THIU CHUNG (3 TIT)
I. II. III. Ngn ng lp trnh trong CAD Tng quan v Activex Automation Ngn ng lp trnh AutoLisp 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. IV. V. VI. Gii thiu chung Cn bn v AutoLisp Bin trong Lisp File chng trnh Lisp Nhp d liu Mt s hm c bn X l danh sch Biu thc iu kin Vng lp Tp hp cc i tng c chn Lp trnh vi c s d liu ca AutoCAD. Phn tch v d :

Ngn ng lp trnh Visual Lisp C bn v ngn ng lp trnh Visual Basic Lm quen vi VBA. 1. 2. 3. 4. 5. VBA Projects To mi project T chc cc project vi VBA Manager Son tho project vi VBA IDE Lm vic vi cc Macro M hnh i tng ca AutoCAD. Object Hierarchy. i tng Collection. Property & Method (thuc tnh & phng thc). Truy cp n i tng trong Object Hierarchy. Truy cp n i tng Collection trong Object Hierarchy. Truy cp n Object Hierarchy bng VB, VBA trong cc mi trng khc.

VII.

Cn bn v VBA. 1. 2. 3. 4. 5. 6. 7.

CHNG 2 : LM VIC VI MI TRNG AUTOCAD (3 TIT)


I. M, ng v ghi li bn v (0.5t) 2

1. 2. 3. II. 1. 2. 3. III. 1. 2. 3. IV. V.

M bn v. To mi bn v. Lu bn v. iu khin ca s AutoCAD. iu khin ca s bn v. iu khin s hin th bn trong ca s bn v. Ly v thit lp cc bin h thng Grid v Snap. Ly v thit lp bin h thng trong Option.

iu khin ca s bn v (0.5t)

Ly v thit lp cc thng s h thng (0.5t)

S dng command line trong VBA (0.5t) Nhp d liu ngi dng (1t) 1. 2. 3. 4. 5. 6. Nhp Chui Nhp ta mt im Nhp mt k t in hnh cho Option Nhp s thc, s nguyn. GetCorner Method, GetAngle Method, GetDistance Method GetEntity Method, GetSubEntity Method

CHNG 3 : TO V SA CC THC TH HA (9 TIT)


I. To i tng bn v (1t) 1. 2. 3. 4. 5. II. 1. 2. 3. III. 1. 2. 3. IV. V. 1. Xc nh i tng cha thc th. V Line, Arc, Circle, and Ellipse objects To cc khi c To i tng hatch To i tng Region, cc php ton trn Region To cc TextStyle Chn Text vo bn v Chn cc k t c bit, cc k t Unicode. Cc php sa i c bn Cc php bin i nng cao Chnh sa PolyLine, SpLine Blocks v Block References

Thm Text vo bn v (1t)

Sa cc i tng bn v (1.5t)

Block v thuc tnh ca block (2 Tit) Chn i tng. (1 Tit) 3

1. 2. 3. VI. 1. 2. 3. 4. VII. 1. 2. 3. VIII. 1. 2. 3.

To Selectionset. Thm i tng vo selection set Lc i tng trong selection set To mt Group Object Truy cp ti cc Group Object Thm bt thc th vo Group Xa Group Object S dng layer, color S dng linetype Gn layer, color, linetype cho i tng Lm vic vi kch thc (2t) Lm vic vi DimStyle To cc ng o kch thc To cc leader.

Lm vic vi Group (0.5 Tit)

S dng layer, color v linetype (1t)

CHNG 4 : TY BIN MENUS V TOOLBARS (5 TIT)


I. C bn v menu group v toolbar (0.5t) 1. 2. II. 1. 2. 3. III. 1. 2. 3. 4. 5. 6. IV. 1. 2. 3. 4. 5. Menugroup collection Menugroup Object Chn thm menu vo menubar Xo b menu trn menubar Sp xp li cc menu trn menubar To mi Pull-down menu Chn mt menu Item vo Pull-down menu Chn mt khong trng vo Pull-down menu Thm mt Menu Item vo Shortcut Menu To Submenu cho menu item Xa bt menu item To mi toolbar Thm mt nt chn vo toolbar nh ngha hnh nh cho cc nt Toolbar Button. Thm mt khong trng vo toolbar Floating v Docking Toolbars 4

Thay i menu bar (0.5t)

To v chnh sa Pull-down v Shorcut menus (1,5t)

To v chnh sa Toolbars (1,5t)

6. 7. V. 1. 2.

To cc flyout toolbar Xa Toolbar v Toolbar Button Cc quy nh v macro Cc v d v macro

To cc macro (1t)

CHNG 5 : PHT TRIN NG DNG VI VBA (5 TIT)


I. AutoCAD Events (1.5 Tit) 1. 2. 3. II. 1. 2. III. 1. 2. 3. IV. 1. 2. 3. 4. V. 1. 2. 3. 4. Application-level events Document-level events Object-Level Events Lm vic vi Form v Macro Lm vic vi module v macro (0.5t) Tng tc vi Visual Lisp S dng c s d liu DAO Giao tip vi cc ng dng khc. Khi nim v XData Sets the extended data (XData) associated with an object. Gets the extended data (XData) associated with an object. Cc V d Khi nim v Xrecord Phng thc AddXRecord Phng thc SetXRecordData Phng thc GetXRecordData

S dng Form (1t)

Tng tc vi cc ng dng v cc c s d liu khc (0.5t)

Lm vic vi Xdata (1 tit)

Lm vic vi Xrecord (1 tit).

CHNG 1 : GII THIU CHUNG (3 TIT)


I. Ngn ng lp trnh trong CAD Cc cp ca ngi dung khi thao tc vi AutoCAD : Cp I : Bit s dng CAD Cp II : Bit qun l v lm ch mi trng CAD Cp III : Bit lp trnh t ng ha qu trnh v Autocad cho ta cc cch c th t ng qu qu trnh v nh sau : S dng cc ngn ng lp trnh, sau kit xut ra bn v di dng cc file vn bn DFX. T ng ha bng cc file Script. Lp trnh trong mi trng CAD nh Lisp, Object ARX, VBA. AutoDesk cung cp cho chng ta mt b cc phn m rng kim sot AutoCad t ngn ng. Nhng phn m rng ny c gi l Object ARX. y l mt phng php tip cn vi CAD mt cch chuyn nghip nht tuy nhin n li qu phc tp. AutoLisp l mt ngn ng lp trnh thng dch, N l mt phin bn mi nht v ngn ng lp trnh nhn to c nht m ngy nay vn cn c s dng. Autolisp nm trong b Common LISP. LISP vit tt ca LIST Processor !. Ni chung Lisp d hc bi c php ca n n gin nhng n khng tng tc c vi cc c s d liu nh Excel, access. Nn vic s dng n to ra cc ng dng phc tp l rt kh. Tuy nhin mc phc tp cng c gim bt i rt nhiu trong Visual LISP! VBA vit tt ca Visual Basic Application. Cng nh Object Arx th VBA cng l mt mi trng lp trnh hng i tng s dng ngn ng VB. u im ca VBA l S dng VB, mt ngn ng lp trnh tng i thng dng v d hc. VBA nm trong CAD nn tc chy cng tng i nhanh. D dng trong vic to ra cc giao din (hp thoi, menu). Tng tc vi cc ng dng khc v cc c s d liu khc. Cho php ta ghi Project ra file ring hoc tch hp lun vo bn v. To iu kin pht trin ng dng mt cch mm do trong vic chia s d liu gia cc ng dng khc trong mi trng Window. II. Tng quan v Activex Automation 2 u im ca AutoCAD ActiveX Activex automation l chun mc c to ra bi hng Microsoft, trc y c gi l OLE activex, cho php mt ng dng Windows ny kim sot mt ng dng Windows khc qua m hnh cc i tng r rng. AutoCAD ActiveX l giao din cho php ngi lp trnh lm vic vi cc i tng ca AutoCAD. AutoCAD Activex cho php bn s dng mt cch t ng khng ch trong phm vi AutoCAD m ngoi c AutoCAD. Cc i tng ca AutoCAD c th c truy nhp n bi nhiu ngn ng lp trnh khc nhau nh Ms word VBA, Ms Excel VBA,...

2 u im ca AutoCAD ActiveX Tt c cc ngn ng lp trnh u c th lm vic vi cc i tng trong AutoCAD (khng gii hn vi C++ v AutoLISP nh trc) Chia s d liu vi cc ng dng khc trn mi trng Windows (Excel, Word...)

M hnh client server. Mc d Activex lun lun bao gm mt cuc hi thoi gia hai ng dng, n khng phi l cuc hi thoi hai chiu gia cc thnh phn tng ng. Mi thnh phn chng trnh Activex Automation bao gm hai chng trnh vi cc vai tr khc nhau. Client l ng dng khi to cuc hi thoi. Server l ng dng hi p client. M Activex Automation chy trong client, trong khi cc hnh ng m ny c kim sot thc hin trn server. Hnh di y trnh by mi quan h gia Client v Server trong mt cuc trao i Activex Automation c th.
Starts conversation Request Object Supplies Object

Client
Invokes Method Supplies Object Close conversation

Server

Principle Activex Automation

Di y l mt s ng dng bn c th dung kim sot cc server activex, k c AutoCAD. Visual Basic Excel VBA Word VBA ..... M hnh i tng ca Automation. 7

Mt Server Activex Automation (chng hn nh AutoCAD) thc hin cc chc nng qua cc i tng. Mt i tng l mt i din ca thnh ng dng. Mt i tng c phn bit vi cc di tng khc b ba c tnh : Phn loi ca i tng Cc c tnh ca i tng Phng thc ca i tng V d i tng Line, c tnh ca i tng cho php bn xc nh : Mu sc Lp im khi u im kt thc di Phng thc i tng l Sao chp xa i xng qua gng di chuyn quay Ch , cc Autocad activex s khng hot ng nu cad ang thc hin lnh. III. Ngn ng lp trnh AutoLisp 1. Gii thiu chung u im : Tc chy nhanh Nhc im : L ngn ng lp trnh thng dch, ko cu trc Khng kt ni c vi cc c s d liu nh Access, Excel 2. Cn bn v AutoLisp 2.1. Xy dng biu thc AutoLisp Cu trc d liu c bn ca Lisp l danh sch (List) Danh sch l tp hp cc phn t cha trong cc du ngoc n, cc phn t c cch nhau bi mt hoc nhiu du cch Danh sch c 2 loi : Biu thc ton hc (expression) v danh sch d liu (data list). Phn t u tin ca ca mt biu thc lun lun l mt hm Mt biu thc bao gm tn hm v cc tham s cha trong cc du ngoc n. Khc vi biu thc ton hc, cc tham s trong biu thc Lisp l cc tham s c th t. Tham s l cc gi tr cung cp cho hm tnh ton AutoLisp tr v kt qu tnh ton t biu thc. V d : (+ 30 20 50) Tr v kt qu : 100 2.2. Cch nhp biu thc AutoLisp Biu thc Lisp c th c nhp nh cc dng lnh ca AutoCAD, ch rng biu thc ca Lisp lun lun nm trong du ngoc n ( ). Khi nhp trc tip bng dng lnh, bn nn ch ko di ca s AutoCAD Text Window ra c th theo di c kt qu cng nh li ca n. Command: (- ( + 140 10) 30) 120 2.3. Cc hm s hc Hm cng (+) : (+ [number 1] [number 2] [number 3].) Danh sch bt u bng du + bo cho lisp l hm, cc phn t ng sau n s l tham s. D liu s c chia lm 2 loi : 8

S nguyn (ko c du chm) S thp phn (c du chm) Kt qu tr v s c kiu l kiu rng nht trong cc kiu ca tham s. V d : Command: ( + 140 10 30) 180 Command: ( + 140 10 30.0) 180.0 Command: (+ 140 10.10 30) 180.1 Command: (+ 10 a) ; error: bad argument type: numberp: nil Hm tr (-) Hm nhn (*) Hm chia (/) Command: ( / 120 10 3) 4 Command: ( * 3.75 3.775) 14.0625 Lisp lu tr ti 14 s thp phn nhng kt qu tr v trn mn hnh s ch c 6 ch s c ngha tnh t tri sang phi. V d : 18-[(3+6+9):(9-6)-12] Command: (- (- 18 (/ (+ 3 6 9) (- 9 6))) 12) 0 3. Bin trong Lisp K hiu Tn bin cng nh tn hm ko phn bit ch hoa, ch thng. Bin khng nht thit cn khai bo Gn gi tr cho bin S dng hm Setq gn gi tr cho mt bin. C php nh sau : (SetQ Symbol1 value1 [symbol2 value2] ) Ging nh cc hm khc, hm SetQ tr v mt gi tr. Gi tr ny c th l nill (rng), T (True) hoc cc s, chui, danh sch. V d : Command (Setq x 3) 3 Command (Setq x 3 y 4) 4 Command (Setq z (+ x y) 7 Command (Setq A Xyabg) Xyabg Phm vi bin Bin ch c tc dng trong phm vi bn v S dng bin trong dng lnh Command : (setq x 10) 10 Command : (!X) 10 Command : circle 9

3P/..<Center point> : Nhp ta tm Diameter <Radius> : !x 4. File chng trnh Lisp 4.1. File lisp Quy nh chung : File Lisp c phn m rng l *.lsp Mt biu thc c th vit trn nhiu dng Cc biu thc khng phn bit ch hoa, ch thng Chui ch thch bt u bng du chm phy Cc ti file lisp vo trong AutoCAD S dng hm Load Command : (Load E:/autoLisp/CHT.lsp) Hoc Command : (Load E:\\toLisp\\T.lsp) S dng hm Appload (Application Load)

4.2. Hm t to Ngoi cc hm AutoCAD cung cp, ta cn c th to ra cc hm t to. nh ngha hm t to Bng cch s dng hm Defun (define function). C php nh sau : (Defun Function_Name Argument_List expresstion) Function_Name : tn hm t to. Tun theo quy tc t tn bin Argument_List : gm hai phn ngn cch nhau bi du /, Phn th nht cha cc tham s cn thit khi gi l hm. Phn th hai ch cc bin cc b ca hm. 10

V d :

Expression : cc biu thc tnh ton ca hm. Cc biu thc ny s ln lt c tnh ton theo th t t trn xung di. (Defun ZA() (command Zoom all) )

Bin ton cc v bin cc b Bin ton cc l cc bin hot ng trong phm vi bn v Bin cc b l bin c nh ngha trong phm vi hm v gi tr ca n s mt i khi hm kt thc. 4.3. To lnh AutoCAD mi. Tham s C: c th s dng hm t to nh l mt lnh trong AutoCAD, ta t k hiu C: vo trc tn hm trong phn nh ngha hm t to. V d : (Defun C: (/PT1 PT2) (Setq PT1 (getpoint \n nhap diem thu nhat)) (Setq PT2 (getpoint \n nhap diem thu hai)) (command Line PT1 PT2 ); (Princ) ) ;ket thuc Tham s S::Startup Khi khi ng AutoCAD, hm S::startup nh ngha trong file ACADRx.lsp s c t ng gi v thi hnh. y l hm duy nht c tnh cht ny. 5. Nhp d liu 5.1. Nhp d liu ngi dng Nhp ta mt im : Hm getpoint (GetPoint [pt] [prompt]) Hm ny s tr v mt danh sch. Danh sch ny thuc sng danh sch lu tr d liu (Data Storage list). Loi danh sch ny khc vi biu thc ch, phn t u tin ca danh sch khng phi l mt hm. Khi ta nhp d liu vo, AutoCAD nhn bit c kiu d liu danh sch ta dng hm Quote (hoc du ). V d ta v ng thng i qua mt im c ta (2,2,0) v mt im nhn c t ngi dng. (Defun C: (/PT1) (setq PT1 (getpoint \n Nhap diem thu nhat)) (Command Line PT1 Quote(2 2 0)) ) Hoc : (Defun C: (/PT1) (setq PT1 (getpoint \n Nhap diem thu nhat)) (Command Line PT1 2,2,0) ) Trnh t cc tham s hm Command tng ng vi trnh t nhp lnh ti dng nhc. C php y : (Command [argument]) Nhp s nguyn (interger) : Hm getint C php : (Getint [prompt]) V d : Command : (Getint \n Enter an integer) 12.0 Requires an integer value Try again : 23 11

23 Nhp d liu s thc (real) : Hm Getreal C php nh sau : (Getreal [Prompt]) Nhp d liu kiu chui (string) C php nh sau : (Getstring [Prompt]) 5.2. Kim sot d liu nhp vo Hm getint cung cp danh sch cc gi tr nhp vo hp l bng cch gn cc bit kim tra (bit code) v danh sch cc t kha. Cc loi hm nhp d liu nh Getpoint, getcorner, getint, Getreal, (ngoi tr hm GetstringO u b kim sot bi hm initget. Hm c tc dng i vi hm nhp d liu tip theo sau n. C php ca hm nh sau : (Initget [bits] [string]) Bits l mt s nguyn. Gi tr tham s ny bng tng cc bit code tng ng vi cc ch kim sot m t among mun Tham s String cha danh sch cc t kha. Bit code Ch kim sot 1 Gi tr phi c nhp vo; khng chp nh gi tr null 2 Gi tr nhp vo phi khc khng 4 Gi tr nhp vo khng c l s m 128 Cho php nhp chui k t khng c trong danh sch cc t kha. Cc bit code khc s c u tin trc. Hm GetKword Hm ny yu cu nhp d liu dng t kha. C php nh sau : (GetKWord [Prompt]) Hm getkword ch chp nhn 2 bit code trong hm initget l 1 v 128. V d : Command : (initget 1 Y N) Nil Command : (setq abc (getkword \n ban co ghi lai khong ? <Y/N>)) 5.3. Bin h thng Ly gi tr bin h thng (Getvar Varname) Gn gi tr bin h thng (Setvar varname value) Bin h thng quan trng CmdEcho Value =1 : Kt qu tnh ton trung gian s c hin ln trn mn hnh. Value =0 : Kt qu tnh ton trung gian s ko c hin ln trn mn hnh. 6. Mt s hm c bn 6.1. Hm chuyn kiu d liu Chuyn i mt s thnh s thc : Hm (Atof String) Command : (Atof 15.4a) 15.4 Chuyn i mt chui thnh mt s nguyn : hm (Atoi String) Command : (Atof 15.4a) 15 Chuyn i mt s thnh mt chui (real to string):Hm (Rtos Number [mode [precision]]) Precision : s ch s thp phn Mode l kiu s (scientific, decimal,) 12

V d

Mode Format 1 Scientific 2 Decimal 3 Engineering 4 Architectural Number : s s c chuyn qua kiu (mode) vi s ch s thp phn c quy nh trong (precision) sau c chuyn thnh chui tng ng. Nu khng c 2 tham s ny th AutoCAD s ly bin h thng trong Units thc hin php ton. (Rtof 215 2) tr v : 2.15E+2

Chuyn i mt s nguyn thnh chui : hm (Itoa integer) V d : (Itoa 21) tr v 21 (Itoa 30.2) tr v li Hm ASCII C php : (Ascii String) : chuyn i k t u tin ca chui thnh m k t ascii tng ng. V d : (Ascii Abc) tr v 65. (Ascii 9Ac) tr v 57. Hm CHR C php : (Chr Integer) : chuyn i m ascii thnh k t tng ng trong bn m ASCII. Cc m ascii chun c gi tr t 32 n 126. 6.2. Hm ton hc Hm kim sot dng s (Fix Number) : tr v phn nguyn ca mt s thc. (Float Number) : Chuyn s Number thnh kiu s thc. (Abs Number) : tr v tr tuyt i ca mt s. Hm max,min (Max Number1 Number 2 ) (Mim Number1 Number 2 ) Ch khng chp nhn chui.

Hm lng gic (Sin Angle) Tr v gi tr ca mt gc, n v Angle l radians. (Cos Angle) (Atan Angle). Gi tr tr v t pi/2 n pi/2 Hm ly tha, khai cn, logarit (Expt Base Power) (Expt 4.0 4) tr v gi tr 64.0 (SQRT Number) (Log Number) tr v logarit ca mt s. (Exp Number) Tr v e m n. 6.3. Cc hm v khong cch v gc o Hm Cvunit (convert units) C php : (cvunit Value From To) Value : s nguyn, s thc, hoc ta im 2D, 3D From n v o hin ti (kiu chui) To n v o s chuyn sang (kiu chui) V d : (Cvunit Pi RADIANS DEGREE) tr v 3.14159 (Cvunit (1 3) FT IN tr v (12.0 36.0) 13

Hm Angle C php (Angle PT1 PT2) : Tr v gc (Radians) gia ng thng i qua 2 im vi trc X trong mt phng XY. Nu 2 im ny khng nm trn mt phng XY, n s c chiu ln mt phng XY v tnh gc. V d Command : (Angle (5 6.10) (quote (10 5))) 6.06664 6.4. Cc hm v chui Hm hin th thng tin kiu chui C php : (Princ [Expr [file]] - Hm ny in ra mn hnh hoc in ra file C php : (Print [Expr [file]] - Hm ny in ra mn hnh hoc in ra file trn mt dng mi. C php : (Prin1 [Expr [file]] - Hm ny in ra mn hnh hoc in ra file trn mt dng mi. V d : Command: (princ "\nabc \nabc\n") abc abc "\nabc \nabc\n" Command: (prin1 "\nabc \nabc\n") "\nabc \nabc\n""\nabc \nabc\n" Command: (print "\nabc \nabc\n") "\nabc \nabc\n" "\nabc \nabc\n" Cc k t c bit cho hm Princ \n : xung dng \t : cch ra nh mt khong Tab Hm StrCase C php : (StrCase String [switch]) Nu switch <> nill hm s tr v chui String trong cc k t hoa c chuyn thnh ch thng Nu Switch ko c hoc bng nill th hm s tr v chui String trong cc k thng s c chuyn thnh k t hoa. Hm StrCat C php : (StrCat String [string2]) : kt ni cc chui tham s. Hm StrLen C php : (StrLen String [string]) : tr v chiu di ca mt xu. Nu c nhiu xu hm s tr v tng chiu di ca cc xu tham s. Hm SubStr C php : (SubStr String Start [length]) : Tr v mt xu con ca xu String bt u t v tr Start v di length k t. Nu khng c length, n s ly n tn cui xu. 7. X l danh sch 7.1. Tng quan Danh sch (List) c phn lm 3 loi chnh Biu thc (Expression list) : cha tn hm v cc tham s ca hm. Biu thc tr v gi tr Ta dim (Point Coordinate list) : c hm quote hoc du ng trc. Cha ta X, Y, Z ca mt im. Kho d liu (Data storage List) : Cng nh Point coordinate list. Nhng n cha d liu bt k. 7.2. To danh sch 14

Tt cc d liu ca AutoCAD (Auotcad database) u c lu di dng danh sch v c nh s th t theo m (DXF Code). Khi vit chng trnh, qun l d liu, thng thng ta lu vo cc bin. Nhng khi s lng lu tr d liu tng ln. Lisp khng cung cp kiu d liu ng v mng ng. gii quyt vn ny, ta s dng kiu d liu List. Mt trong nhiu phng php to ra danh sch l s dng hm List C php : (List expression) V d Command : (set q L1 (list abc 10 30.0 hoang) (abc 10 30.0 hoang) Trong v d trn L1 c 4 phn t. Ngoi ra ta c th s dng hm Quote hoc du ng trc. V d : Command: (setq a1 (quote("abc" 1 30 40))) ("abc" 1 30 40) Hoc Command: (setq a1 '("abc" 1 30 40)) ("abc" 1 30 40) S khc nhau c bn gia List v quote (hoc ). List to ra danh sch nh gi tr, khi to ra danh sch, lisp s nh gi tr v kiu d liu tng ng cho cc phn t trong danh sch. Cn quote to ra danh sch cha nh gi tr. Chng ta xem xt v d sau : Command: (setq a2 '( a b c)) (A B C) Command: (setq a2 (list a b c)) (nil nil nil) (V a b c l 3 bin cha c gi tr) 7.3. Hm x l danh sch c bn Hm Car C php : (Car List) : Dng ly gi tr u tin ca danh sch. V d : Command : (Car a1) abc Hm CDR C php : (CDR list) : To ra mt danh bng cch loi b phn t u tin ca danh sch gc. V d : Command : (setq a3 (CDR a1) (1 30 40) Hm CADR, CADDR (CADR list) tr v phn t th 2 ca danh sch. (CADDR list) tr v phn t th 3 ca danh sch. Hm Last C php : (Last list) : Tr v phn t cui cng ca danh sch. Hm Length C php : (Length List) : tr v s lng phn t c trong danh sch. 7.4. Hm x l danh sch nng cao Hm Assoc (association) C php : (Assoc Item AList) : Danh sch Alist phi cha phn t Item, v l danh sch phc hp. Hm s tr v mt danh sch con ca Alist m phn t u tin l Item. Nu khng tm thy phn t Item trong Alist th hm s tr v gi tr nill. V d : Command: (Setq Alist '((1 "Name" "NGuyen hoang anh") (2 "Toan" 10) (3 "Ly" 6))) ((1 "Name" "NGuyen hoang anh") (2 "Toan" 10) (3 "Ly" 6)) 15

Command: (setq Toan (assoc 2 Alist)) (2 "Toan" 10) Command: !toan (2 "Toan" 10) Command: (last (assoc 2 Alist)) 10 Hm Cons (construct) C php : (Cons Item List) b xung phn t Item vo v tr u tin ca danh sch. V d : Command : (Setq AL (list (Cons COLOR 4) (Cons LAYER 0) Hm Member C php : (Member Item list) : tr v mt danh sch bt u bng phn t Item. V d : Command : (Setq L (1 2 3 4 5) ( 1 2 3 4 5) Command : (member 3 L) (3 4 5) Hm Append C php : (Append list1 list2 ) : Gp nhiu danh sch thnh mt danh sch. Command : (setq L2 (Append (member 3 L) (list 6))) (3 4 5 6) 8. Biu thc iu kin 8.1. Biu thc iu kin Cc hm so snh Hm = /= < <=; >=; > EQ Equal C php (= Atom Atom ..) (/= Atom Atom ..) (< Atom Atom ..) Tng t (EQ Expr1 Expr2) Gii thch Tr v gi tr T nu tt c cc phn t bng nhau. Tham s kiu s hoc kiu chui. Tr v T nu cc phn t i mt khc nhau Tr v T nu mi phn t nh hn phn t ng bn phi n Tng t So snh s trng nhau ca 2 danh sch.

(Equal Expr1 Expr2 Hm nh gi tr cc biu thc v kim tra cc gi [fuzz] tr ny c bng nhau hay khng. i vi d liu kiu s Fuzz quy nh sai s trong php so snh C php (Atom Item.) (Listp Item) (numberp Item) (minusp Item) (Zerop Item) (Null Item) (Type Item) Gii thch Tr v gi tr Nil nu Item l list Tr v T nu Item l danh sch hoc gi tr Nill Tr v T nu gi tr l s nguyn Tr v T nu gi tr l s m Tr v T nu d liu =0 or 0.0 Kim tra mt bin c rng hay ko Tr v kiu d liu ca bin 16

Cc hm kim tra d liu Hm Atom Listp Numberp Minusp Zerop Null Type

8.2. Hm If v Progn Cu trc r nhnh vi If C php : (If LogicExpr ThenExpr) (If LogicExpr ThenExpr ElseExpr) Ch : Hm If ch chp nhn mt biu thc ThenExpr v ElseExpr. Nu s dng nhiu hn mt biu thc th phi s dng cu trc Progn (Progn Expression .) V d : (DEFUN C:11 () (if (= (getvar "pickstyle") 0) (setvar "pickstyle" 1) (setvar "pickstyle" 0)) (PRINC) ) Bi tp Gii phng trnh bc nht Nhp vo 2 im, kim tra xem chng c nm trong gii hn Limmax v Limmin hay khng. Nu c hy a ra khong cch gia 2 im . S dng hm (Distance PT1 PT2). 8.3. Hm logic (Setq A 10 b 20 c 30) Hm And Or Not C php (And Expression .. ) (or Expression .. ) (Not Item) V d (And (> 10 0) (< 10 20)) tr v gi tr T (Or (= A 10) (< b 0)) tr v git tr T (Not nill) = T. (Not T) = nill

8.4. Hm Cond (Condition) Hm if cho php ta r ti a 2 nhnh. Hm Cond gip ta r nhiu hn 2 nhnh. C php hm nh sau : (Cound (Test1 result1 ) (Test2 result2 ) (Testn resultn ) ) Bi tp Gii phng trnh bc 2. 9. Vng lp 9.1. Vng lp c bn Vng lp Repeat. Hm Repeat to ra vng lp vi s ln nht nh C php : (Repeat Number Expr .. ) V d : (Setq I 10) (setq j 1) (Repeat 10 (setq i (+ 2 i) (setq j (* 2 j) ) ; kt thc hm repeat Lp 10 ln khi I = 10+2*10 = 30; j= 2^10. 17

Vng lp While. Hm while to ra vng lp c iu kin. Vng lp ny s jt thc khi iu kin Testexpr khng tha mn. C php : (While testexpr Expr ..) V d : V ng thng i qua 2 im (defun c:L2P (/ ch PT1 pt2) (setq ch "Y") (while (or (= ch "Y") (= ch "y") ) (setq PT1 (getpoint "\nnh?p vo ?i?m th? nh?t : ")) (setq PT2 (getpoint "\nnh?p vo ?i?m th? hai : ")) (command "Line" PT1 PT2 "") (initget 1 "Y N y n") (Setq ch (getkword "\n b?n c v? n?a khng (Y/N) : ")) ); ket thuc while ); ket thuc defun 9.2. Hm foreach C php : (Foreach Name List Expr ) Hm Foreach duyt tng phn t trong danh sch LIST. Ti mi thi im, gi tr ca tng phn t trong danh sch s c gn cho bin Name. Sau cc biu thc Expr s c nh gi tr. V d : (setq I 0) (foreach So (list 1 2 3 4 5 6 7 8 9 10) (Setq I (1+ i)) (Princ (Strcat \nCc gi tr th (itoa i) trong danh sch l (itoa so))) ) 10. Tp hp cc i tng c chn 10.1. Hm Ssget Hm SSget C php : (Ssget [Mode][PT1][PT2][PT-List][Filter-List]) Mode None <Point> L P W C F WP CP X V d : Phng php chn S dng mi phng php chn (hay dng) Chn i tng i qua im Point Last: chn i tng c to ra cui cng Previous Window Crossing window Fence Window polygon Crossing polygon All Setq PT1 (0 0 0) PT2 (4 0 0) PT3 (4 4 0) PT4 (0 4 0)) (setq SS (ssget WP (list pt1 pt2 pt3 pt4))) Bng Group code. S dng filter V d : 18 C php (Ssget) (Ssget point) (Ssget l) (ssget p) (Ssget W PT1 PT2) (Ssget C PT1 PT2) (Ssget F PT-list (ssget wp PT-list (ssget cp PT-list (ssget x)

(ssget ( (0 . text) (40 . 2.5))) (ssget ( (0 . line) (62 . 4))) S dng cc php so snh. Ch cc php so snh ch p dng i vi cc group code c kiu s (nguyn hoc thc). Cc php so snh bao gm * = /= != > < >= <= <> V d : (Ssget ((0 . Circle) (-4 . <>) (40 . 50) (-4 . >, <,*) (10 0.0 0.0 0.0))) ng trn bn knh group code 40, to tm group code 10 S dng cc php ton logic. Cc php ton logic And Or Not V d : (Ssget X ((-4 . <OR) (0 . Text) (0 . Line) (0 . Circle) (-4 . Or>))) Mt vi group code : to im u, cui ca Line : 10,11. Gi tr Text : 12. 10.2. Hm SSLength C php : (SSlength ss) 10.3. SSName C php : (Ssname ss index) : tr v tn ca i tng th index trong tp hp chn SS. (lu : s u tin ca tp hp chn index=0). V d xa phn t th nht ca danh sch chn (command erase (Ssname SS 0) ) Hm EntGet (entity get) C php : (ENTGET entname) (SETQ DS (ENTGET (SSNAME SS DEM))) (SETQ KDL (CDR (ASSOC 0 DS))) 11. Lp trnh vi c s d liu ca AutoCAD. 11.1. Ly Record d liu i tng Hm EntGet (entity get) C php : (ENTGET entname) : tr v danh sch biu din Record d liu ca i tng c m l entname. 11.2. Hiu chnh record i tng Cc bc hiu chnh To ra record mi cha cc field thay i bng hm Subst. Thay th Record c ca i tng bng record mi bng hm Entmod. Cp nht s thay i ca i tng ln mn hnh (thay cho lnh regen). S dng hm EntUpd Hm Subst to ra mt record mi bng cch s i mt s phn t ca record c. C php : (Subst new_Item Old_Item list) Nu khng tm thy old_Item, hm s tr v danh sch ging danh sch ban u. V d : (Setq al (A 22 34 yes B 22)) (Subst 22 11 al) tr v A 11 34 yes B 22 (setq El ((-1.<entity name 2d3314>) (0.Line) (5. 20) (100. ACDB entity) (67 . 0) (8 . 0) (100 . acdbline) (10 0 0 0) (11 5.0 5.0 0)) (setq el (subst (8 . Layer Dim) (8 . 0) EL)) Hm Entmode Hm entmode thay th record c trong c s d liu bng mt record mi. C php : (Entmode Elist) Elist l mt danh sch c dng mt record i tng trong m i tng nm group code -1. Hm ny s tm trong c s d liu i tng c m nh trong code -1 v thc hin vic thay th. Cc trng hp khng th thay th c hm s tr v gi tr nill : Khng tm thy i tng Thay i m i tng 19

Thay i m handle Hiu chnh i tng viewport Thay i kiu i tng,,,

Hm Entity update C php : (EntUpd Ename) : Dng cp nht s thay i record ca i tng c tn l Ename ln mn hnh ha. 11.3. To i tng mi. Hm (entmake Elist) to ra i tng mi Cc quy nh cho elist Tham s elist khng nht thit phi c cc thng s Field th nht bt buc phi l group code 0 cha kiu i tng M i tng AutoCAD s t t khi i tng c to ra 12. Phn tch v d : ;******************************************************************************* ;* TRAN ANH BINH CDC-HUCE * ;******************************************************************************* (defun myerror (s) ; If an error (such as CTRL-C) occurs ; while this command is active... (cond ((= s "quit / exit abort") (princ)) ((/= s "Function cancelled") (princ (strcat "\nError: " s))) ) (setvar "cmdecho" CMD) ; Restore saved modes (setvar "osmode" OSM) (setq *error* OLDERR) ; Restore old *error* handler (princ) ) ;******************************************************************************* (DEFUN C:CD (/ CMD SS LTH DEM PT DS KDL N70 GOCX GOCY PT13 PT14 PTI PT13I PT14I PT13N PT14N O13 O14 N13 N14 OSM OLDERR PT10 PT11) (SETQ CMD (GETVAR "CMDECHO")) (SETQ OSM (GETVAR "OSMODE")) (SETQ OLDERR *error* *error* myerror) (PRINC "Please select dimension object!") (SETQ SS (SSGET)) (SETVAR "CMDECHO" 0) (SETQ PT (GETPOINT "Point to trim or extend:")) (SETQ PT (TRANS PT 1 0)) ; chuyen tu current UCS sang WCS (world) (COMMAND "UCS" "W") (SETQ LTH (SSLENGTH SS)) (SETQ DEM 0) (WHILE (< DEM LTH) (PROGN (SETQ DS (ENTGET (SSNAME SS DEM))) (SETQ KDL (CDR (ASSOC 0 DS))) (IF (= "DIMENSION" KDL) (PROGN (SETQ PT10 (CDR (ASSOC 10 DS))) (SETQ PT11 (CDR (ASSOC 11 DS))) (SETQ PT13 (CDR (ASSOC 13 DS))) (SETQ PT14 (CDR (ASSOC 14 DS))) 20

(SETQ N70 (CDR (ASSOC 70 DS))) (IF (OR (= N70 32) (= N70 33) (= N70 160) (= N70 161)) (PROGN (SETQ GOCY (ANGLE PT10 PT14)) (SETQ GOCX (+ GOCY (/ PI 2))) ) ) (SETVAR "OSMODE" 0) (SETQ PTI (POLAR PT GOCX 2)) (SETQ PT13I (POLAR PT13 GOCY 2)) ; tao ra mot diem moi (SETQ PT14I (POLAR PT14 GOCY 2)) ; (polar PT angle distance) (SETQ PT13N (INTERS PT PTI PT13 PT13I NIL)) ; nill thi cac duong thang se dc keo dai, (SETQ PT14N (INTERS PT PTI PT14 PT14I NIL)); kha nill thi se khong dc keo dai (SETQ O13 (ASSOC 13 DS)) (SETQ O14 (ASSOC 14 DS)) (SETQ N13 (CONS 13 PT13N)) (SETQ N14 (CONS 14 PT14N)) (SETQ DS (SUBST N13 O13 DS)) (SETQ DS (SUBST N14 O14 DS)) (ENTMOD DS) ) ) (SETQ DEM (+ DEM 1)) ) ) (COMMAND "UCS" "P") (SETVAR "CMDECHO" CMD) (SETVAR "OSMODE" OSM) (setq *error* OLDERR) ; Restore old *error* handler (PRINC) ) ;******************************************************************************

21

IV. V. VI.

Ngn ng lp trnh Visual Lisp C bn v ngn ng lp trnh Visual Basic Lm quen vi VBA.

1. VBA Projects Chng trnh VBA c t chc trong cc project, 1 Project l tp hp cc module: m, lp, form VBA Project c th c lu trong file bn v DWG (ch nhng) hoc c lu trong 1 file ring (*.DVB) 2. To mi project M VBA Manager, Chn menu Tools/Macro/VBA Manager. Hoc trn command line, g lnh VBAMAN. To mi VBA project M VBA Manager, chn New, project mi s c tn l ACADProject. i tn project. Bn phi s dng VBA IDE, nhn Alt+F11, click phi chut vo tn project, nhn Rename Lu ct (save) project Project nhng c lu khi lu bn v Project ko nhng phi lu qua VBA IDE (chc nng File/Save) hoc VBA Manager

Ti (load) 1 project c Project nhng c load ngay khi bn m bn v cha n Project khng nhng c lu li di dng file *.DVB, load project ny: trn ca s VBA Manager, nhn nt Load. Trn hp thoi OpenFile, chn file DVB can mo xem ni dung cua Project, nhan Alt+F11 hoac dung lenh VBAIDE tren dong command-line 3. T chc cc project vi VBA Manager Gii thiu chc nng cc nt trong hp thoi VBA Manager

4. Son tho project vi VBA IDE Gii thiu cc ca s ca VBA IDE 22

Ca s Project Manager Objects (i tng) Forms Gm cc hp thoi do ngi lp trnh to ra, s dng trong project Modules Cc hm dng chung trong ton b Project c t chc trong cc module ring (ph thuc vo chc nng ca chng) Class Modules nh ngha cc lp i tng ca ngi dng thm 1 component (form, module, class module) vo project Chn project cn thm thnh phn - component Trn menu [Insert], chn [UserForm], [Module], [Class Module] thm cc thnh phn ny vo Project Module, Class module c son tho trn ca s Code UserForm c son tho trn ca s UserForm son tho cc thnh phn Trn ca s Project Explorer, chn thnh phn cn son tho Nhn nt [View code] m ca s Code Nhn nt [View object] m ca s UserForm S dng Option Explicit

Thm cc ActiveX :

23

S dng trnh son tho Code : Editor.

Text

5. Lm vic vi cc Macro m ca s Macro Chn menu Tools/Macro/Macro. Hoc s dng lnh VBARUN trn dng command-line Hp thoi Macro lit k tt c cc hm ca bn v, project (tu theo la chn ti mc [Macro in]) chy Macro M hp thoi Macro Nhn nt [Run] sa Macro M hp thoi Macro Nhn nt [Edit] to Macro mi Trn mc [Macro name], nhp vo tn ca Macro Nhn nt [Create] Trn hp thoi [Select project] chn project to Macro

VII. Cn bn v VBA. 1. M hnh i tng ca AutoCAD. Hu nh mi Server Activex Automation cung cp nhiu hn mt i tng cho cc client. AutoCAD cung cp cho cc client Activex Automation khong 100 i tng, vi tng s 2500 phng thc v thuc tnh. Do vy CAD c mt khung lm vic n gin d hiu qun l tt c cc i tng v mi quan h gia chng. 2. Object Hierarchy. Gi thiu :

24

3. i tng Collection. 25

AutoCAD nhm hu ht cc i tng (Object) trong mt collections. Cho d cc collections ny cha nhiu loi d liu khc nhau, nhng chng c th c chng c tao ra bng cch s dng cc k thut tng t nhau. Mi mt collection c mt method thm mt object vo collection. Hu ht cc collections s dng Add method cho mc ch ny. V d, thm mt thc th ta s dng method Add<Entityname>. For exam-ple, to add a line you would use the AddLine method. 4. Property & Method (thuc tnh & phng thc). Mi mt Object cha nhiu thuc tnh v phng thc khc nhau. 5. Truy cp n i tng trong Object Hierarchy. Ta c th truy cp n objects mt cch trc tip hoc thng qua mt bin c nh ngha trc. truy cp n i tng mt cch trc tip. V d, dng code sau thm mt ng line trn model space. Ch rng cy ph h bt u bng ThisDrawing. Dim startPoint(0 To 2) As Double, endPoint(0 To 2) As Double Dim LineObj as AcadLine startPoint(0) = 0: startPoint(1) = 0: startPoint(2) = 0 endPoint(0) = 30: endPoint(1) = 20: endPoint(2) = 0 Set LineObj = ThisDrawing.ModelSpace.AddLine(startPoint,endPoint) truy cp i tng thng qua mt bin c khai bo trc. u tin ta phi khai bo bin vi kiu tng ng, sau t bin ln cp trn ca i tng trong cy ph h. V d : on code sau khai bo bin (moSpace) thuc kiu AcadModelSpace v gn bin ny cho lp model space hin hnh: Dim moSpace As AcadModelSpace Set moSpace = ThisDrawing.ModelSpace The following statement then adds a line to the model space using the userdefined variable: Dim startPoint(0 To 2) As Double, endPoint(0 To 2) As Double Dim LineObj as AcadLine startPoint(0) = 0: startPoint(1) = 0: startPoint(2) = 0 endPoint(0) = 30: endPoint(1) = 20: endPoint(2) = 0 Set LineObj = moSpace.AddLine(startPoint,endPoint) Truy cp n Application Object Bi v ThisDrawing object cung cp link ti Document object, bn cng c th tr dn ti th mc gc ca cy ph h (Application object), Chng ta cng c th truy cp n Document object trong object hierarchy thng qua ng dn vng. Document object c mt thuc tnh gi l Application, n cung cp link ti Application object. V d : ThisDrawing.Application.Update 6. Truy cp n i tng Collection trong Object Hierarchy. Hu ht cc collection objects c th c truy cp n trong Document object. Document object cha thuc tnh cho mi i tng trong Collection objects. V d, V d sau nh ngha mt bin v gn n cho tp hp i tng layer ca bn v hin hnh: Dim layerCollection as AcadLayers Set layerCollection = ThisDrawing.Layers Documents collection, MenuBar collection, and MenuGroups collection c truy cp thng qua Application object. The Application object cha mt thuc tnh cho mi i tng nm trong cc collection ny. V d, on code sau nh ngha mt bin v gn chng cho tp hp i tng MenuGroups cho ng dng. Dim MenuGroupsCollection as AcadMenuGroups 26

Set MenuGroupsCollection = ThisDrawing.Application.MenuGroups Thm mt i tng vo Collection. Dim newLayer as AcadLayer Set newLayer = ThisDrawing.Layers.Add("MyNewLayer") Truy cp n mt i tng trong collection. Truy cp n mt i tng thuc tp hp, s dng Item method. Ta c th s dng tn ca Item hoc ch s (s th t ca Item) trong tp hp. Lu l Item method l method mc nh ca collection. V d, 2 cch vit sau l nh nhau : ThisDrawing.Layers.Item("ABC") ThisDrawing.Layers("ABC") Xo mt i tng khi Collection Object S dng Delete method. V d, on m sau xo layer ABC: Dim ABCLayer as AcadLayer Set ABCLayer = ThisDrawing.Layers.Item("ABC") ABCLayer.Delete 7. Truy cp n Object Hierarchy bng VB, VBA trong cc mi trng khc. Ta cng c th truy cp n Object Hierarchy ca cad bng cc ngn ng khc nh VB, VBA trong Excel, Word,. Vic u tin ta phi Tham chiu n AutoCAD Type Library. lm vic ny ta lm nh sau : Chn References option t Project menu, hp thoi Reference dialog hin ln. Trong References dialog box, chn Type library for AutoCAD, and then click OK. Khi nim v Type library for AutoCAD. Cc i tng, thuc tnh v phng thc c a ra bi k thut t ng ho i tng c cha mt type library. Mt type library l mt file hoc mt phn ca file miu t kiu ca mt hoc nhiu i tng.Type libraries khng cha Object; chng cha cc thng tin v Object. truy cp n type library, applications v browsers c th xc nh c c trng ca tng object, nh l giao din ca i tng, tn v a ch ca tng thnh phn ca dao din .Trc khi bn s dng, Bn phi tham chiu n type library. AutoCAD VBA s t tham chiu n AutoCAD type library.i cc mi trng khc bn phi to tham chiu ti AutoCAD type library, acax16enu.tlb. File ny mc nh nm trong th mc C:\program files\common files\autodesk shared. Tuy nhin, s tt hn nu bn add type library reference v nhng u im sau : Chng trnh s chay nhanh hn, n nh hn. Function, properties, methods c th c kim tra khi bn nh code. Chc nng tm kim v th xung sau du chm ca cc i tng. Sub Ch2_ConnectToAcad() Dim acadApp As AcadApplication On Error Resume Next Set acadApp = GetObject(, "AutoCAD.Application.16") If Err Then Err.Clear Set acadApp = CreateObject("AutoCAD.Application.16") If Err Then MsgBox Err.Description Exit Sub End If End If MsgBox "Now running " + acadApp.Name + " version " + acadApp.Version End Sub Dim acadDoc As AcadDocument 27

Set acadDoc = acadApp.ActiveDocument acadApp.visible = True Nu AutoCad ang chy GetObject s khng sinh ra li. Ngc li nu sinh ra li, tc l CAD cha chy. Khi CreatObject s khi ng CAD. Nu c nhiu session ca CAD ang chy th chng trnh s ly Session u tin trong Windows Running Object Table.

28

CHNG 2 : LM VIC VI MI TRNG AUTOCAD (3 TIT)


I. M, ng v ghi li bn v (0.5t) 1. M bn v. m bn v, s dng phng thc Open method. S dng hm DIR ca Visual Basic kim tra s tn ti ca bn v trc khi m.Bn cng c th thay i tn v ng dn ca bn v trong ng dn mc nh ca AutoCAD (Mc Option ca CAD). Sub Ch3_OpenDrawing() Dim dwgName As String dwgName = "c:\campus.dwg" If Dir(dwgName) <> "" Then ThisDrawing.Application.Documents.Open dwgName Else MsgBox "File " & dwgName & " does not exist." End If End Sub 2. To mi bn v. To mi mt bn v, s dng phng thc Add. Sub Ch3_NewDrawing() Dim docObj As AcadDocument Set docObj = ThisDrawing.Application.Documents.Add End Sub 3. Lu bn v. ghi bn v hin hnh. c 2 phng thc sau : Sub Ch3_SaveActiveDrawing() ThisDrawing.Save ghi bn v hin hnh. ThisDrawing.SaveAs "MyDrawing.dwg" ghi bn v vi tn mi. End Sub kim tra xem bn v c ghi hay cha, ta s dng hm Saved : Sub Ch3_TestIfSaved() If Not (ThisDrawing.Saved) Then If MsgBox("Do you wish to save this drawing?", vbYesNo) = vbYes Then ThisDrawing.Save End If End If End Sub II. iu khin ca s bn v (0.5t) 1. iu khin ca s AutoCAD. Mc ch : Khi bn lm vic vi 1 ng dng khc, bn cn nhp s liu t ngi dng chng hn. Bn cn thu nh hoc kim tra tnh trang ca ca s AutoCAD. S dng methods v properties c trong Application object, bn c th thay i position, size v visibility ca ca s AutoCAD. Bn cng c th s dng WindowState property minimize, maximize v c th kim tra tnh trng hin ti ca ca s AutoCAD. Cc thuc tnh ca Position v size ca Application window l WindowTop, WindowLeft, Width, and Height properties iu chnh ca s Autocad : 29

Sub Ch3_PositionApplicationWindow() ThisDrawing.Application.WindowTop = 0 ThisDrawing.Application.WindowLeft = 0 ThisDrawing.Application.width = 400 ThisDrawing.Application.height = 400 End Sub Maximize the Application window Sub Ch3_MaximizeApplicationWindow() ThisDrawing.Application.WindowState = acMax End Sub Minimize the Application window Sub Ch3_MinimizeApplicationWindow() ThisDrawing.Application.WindowState = acMin End Sub Ly tnh trng ca Application window Sub Ch3_CurrentWindowState() Dim CurrWindowState As Integer Dim msg As String CurrWindowState = ThisDrawing.Application.WindowState msg = Choose(CurrWindowState, "normal","minimized", "maximized") MsgBox "The application window is " + msg End Sub Make the Application window invisible Sub Ch3_HideWindowState() ThisDrawing.Application.Visible = False End Sub 2. iu khin ca s bn v. iu khin ca s bn v cng ging nh iu khin ca s AutoCAD, v d nh bn c th minimize, maximize, reposition, resize, v kim tra tnh trng ca bt k Document window no. Document window c th minimized hoc maximized bng cch s dng thuc tnh WindowState, v bn c th ly tnh trng ca Document window thng qua thuc tnh WindowState. V d nh ta gn cho Width and Height ca bn v hin hnh l 400 x 400 pixel. Sub Ch3_SizeDocumentWindow() ThisDrawing.Width = 400 ThisDrawing.Height = 400 End Sub Maximize the active Document window Sub Ch3_MaximizeDocumentWindow() ThisDrawing.WindowState = acMax End Sub Minimize the active Document window Sub Ch3_MinimizeDocumentWindow() ThisDrawing.WindowState = acMin End Sub Find the current state of the active document window Sub Ch3_CurrentWindowState() Dim CurrWindowState As Integer Dim msg As String CurrWindowState = ThisDrawing.WindowState msg = Choose(CurrWindowState, "normal","minimized", "maximized") MsgBox "The document window is " + msg 30

End Sub 3. iu khin s hin th bn trong ca s bn v. Bn cng c th thay i s hin th bn trong ca s bn v bng cch s dng phng thc views, viewports, v zooming. AutoCAD ActiveX cung cp rt nhiu cch iu khin s hin th bn trong ca s bn v. Di chuyn n cc vng khc nhau trn bn v . Phng to thu nh hay di chuyn n cc v tr khc nhau trn bn v. Ghi li cc khung nhn v ly ra khi cn thit Hin th nhiu khung nhn ca mt bn v bng cch s dng splitting the screen trong nhiu tiled viewports. Position v Size the Document WindowUse the Document object to modify the position and size of any document window. .

Define a Zoom Window S dng ZoomWindow or ZoomPickWindow method. Sub Ch3_ZoomWindow() ' ZoomWindow MsgBox "Perform a ZoomWindow with:" & vbCrLf &"1.3, 7.8, 0" & vbCrLf & _ 2.6, 0", , "ZoomWindow" Dim point1(0 To 2) As Double Dim point2(0 To 2) As Double point1(0) = 1.3: point1(1) = 7.8: point1(2) = 0 point2(0) = 13.7: point2(1) = -2.6: point2(2) = 0 ThisDrawing.Application.ZoomWindow point1, point2 ' ZoomPickWindow MsgBox "Perform a ZoomPickWindow", , "ZoomPickWindow" ThisDrawing.Application.ZoomPickWindowEnd Sub III. Ly v thit lp cc thng s h thng (0.5t)

"13.7, -

1. Ly v thit lp cc bin h thng Trong cy ph h, Document object cung cp 2 phng thc SetVariable v GetVariable thit lp v ly gi tr ca cc bin h thng ca AutoCAD. V d : ThisDrawing.SetVariable "MAXSORT", 100 2. Grid v Snap. S dng Snap, Grid Alignment Cc iu khin Snap v Grid nm trong class Viewport. Sub textsnap() Dim Vp As AcadViewport Set Vp = ThisDrawing.ActiveViewport Vp.SnapOn = True Bt ch snap ln Dim newBasePoint(0 To 1) As Double newBasePoint(0) = 1: newBasePoint(1) = 1 Vp.SnapBasePoint = newBasePoint ' Thay i gc ca snap Dim Xspacing as Double, ySpacing as Double xSpacing =20: ySpacing=20 VP.SetSnapSpacing(xspacing,ySpacing) ' Thay bc nhy ca chut Dim rotationAngle As Double rotationAngle = 0.575 Vp.SnapRotationAngle = totationangle ' Thay i gc quay ca Snap sang 30 o hay 0.575 radians ' reset the viewport 31

ThisDrawing.ActiveViewport = ThisDrawing.ActiveViewport End Sub Ch : Vi cc lnh hiu chnh cc active Object nh active layer, active linetype, ActiveTextStyle, ta khng th dng phng thc update nh khi ta hiu chnh cc i tng thng thng c. Thay i cc active object s khng c hin th ngay trn bn v. Mun quan st s thay i ny, ta phi dng phng thc Regen li viewport nh sau : ThisDrawing.Regen (acNameViewports) Ring i vi ActiveUCS hoc ActiveViewport ThisDrawing.ActiveViewport ThisDrawing.ActiveViewport. Phng thc ny p dng c cho tt c cc active object. Cc iu khin Grid cng tng t nh Snap. S dng Ortho Mode ThisDrawing.ActiveViewport.OrthoOn = True =

3. Ly v thit lp bin h thng trong Option. Trong mc options c 9 Objects, mi mt object c trng cho mt tab ca Options dialog box. Cc object ny cung cp ng dn ti tt c cc registry lu trong options trong Options dialog box. Bn c th ty bin tt c cc thit lp ca AutoCAD bng cch s dng properties trong cc Object ny. Cc Object bao gm : PreferencesDisplay PreferencesDrafting PreferencesFiles PreferencesOpenSave PreferencesOutput PreferencesProfiles PreferencesSelection PreferencesSystem PreferencesUser truy xut n Preferences object, s dng cc thuc tnh ca Application object: Dim acadPref as AcadPreferences Set acadPref = ThisDrawing.Application.Preferences V d sau thit lp 2 ru ca chut di kn mn hnh : Sub Ch2_PrefsSetCursor() Dim acadPref As AcadPreferences Set acadPref = ThisDrawing.Application.Preferences acadPref.Display.CursorSize = 100 End Sub S dng command line trong VBA (0.5t) S dng SendCommand method gi lnh mt cch trc tip cho AutoCAD. Phng thc SendCommand s gi mt chui n ti dng lnh command line. Chuit phi cha cc tham s cho lnh, th t vit trong chui nh vit trong macro hc trong phn Customize menu. . Phm cch hoc m ASCII tng ng s tng ng vi phm ENTER trn bn phm keyboard. V d sau v mt ng trn tm A(2, 2, 0), bn knh R= 4. sau s dng lnh Zoom All. Sub Ch3_SendACommandToAutoCAD() ThisDrawing.SendCommand "_Circle 2,2,0 4 " ThisDrawing.SendCommand "_zoom a " End Sub V. Nhp d liu ngi dng (1t) Cc iu khin nhp d liu ngi dng nm trong class Utility. 1. Nhp Chui GetString method prompts cho php ngi dng nhp mt chui t bn dng lnh. C php nh sau : 32 IV.

RetVal = UtilityObject.GetString(HasSpaces[, Prompt]) HasSpaces iu khin phm spaces khi bn nhp chui. 0 : Spaces Bar khng cho php (SPACEBAR s kt thc nhp chui) 1 : Chui nhp vo c th cha du cch (ENTER s kt thc nhp chui). Prompt khng bt buc, l chui s c a ra ti dng lnh vi mc ch thng bo. RetVal gi tr tr v, kiu Variant vi 3 ta (3D). V d : Sub Ch3_GetStringFromUser() Dim retVal As String retVal = ThisDrawing.Utility.GetString(1, vbCrLf & "Enter your name: ") MsgBox "The name entered was: " & retVal End Sub 2. Nhp ta mt im GetPoint method prompts cho php ngi dng nhp ta mt im t bn bn phm hoc bng mt kch chut tri trn bn v C php nh sau : RetVal = UtilityObject.GetPoint([Point][, Prompt]) Point khng bt buc, nu c s xut hin dy thun t im ny. Point kiu Variant vi 3 ta (3D). Prompt khng bt buc, l chui s c a ra ti dng lnh vi mc ch thng bo. RetVal gi tr tr v, kiu Variant vi 3 ta (3D). V d : Sub Ch3_GetPointsFromUser() Dim startPnt As Variant Dim endPnt As Variant Dim prompt1 As String Dim prompt2 As String prompt1 = vbCrLf & "Enter the start point of the line: " prompt2 = vbCrLf & "Enter the end point of the line: " ' Get the first point without entering a base point startPnt = ThisDrawing.Utility.GetPoint(, prompt1) ' Use the point entered above as the base point endPnt = ThisDrawing.Utility.GetPoint(startPnt, prompt2) ' Create a line using the two points entered ThisDrawing.ModelSpace.AddLine startPnt, endPnt ThisDrawing.Application.ZoomAll End Sub 3. Nhp mt k t in hnh cho Option GetKeyword Method prompts cho php ngi dng nhp mt k t in hnh t bn bn phm cho mt mc la chn C php nh sau : RetVal = UtilityObject.GetKeyword([Prompt]) Prompt khng bt buc, l chui cha la chn s c a ra dng lnh vi mc ch thng bo la chn. RetVal gi tr tr v, kiu chui. V d : Sub Ch3_KeyWord() Dim keyWord As String ThisDrawing.Utility.InitializeUserInput 1, "Line Circle Arc" 33

keyWord = ThisDrawing.Utility.GetKeyword(vbCrLf (Line/Circle/Arc): ") MsgBox keyWord, , "GetKeyword Example" End Sub 4. Nhp s thc, s nguyn.

&

"Enter

an

option

The GetInteger Method Phng thc GetInteger cho php ngi dung nhp vo mt s nguyn (gi tr nhp vo t 32,768 to +32,767). C php nh sau : intUserIntegerInput = UtilityObject.GetInteger([Prompt]) V d : Public Sub TestGetInteger() Dim intInput As Integer With ThisDrawing.Utility intInput = .GetInteger(vbCr & "Enter an integer: ") .Prompt vbCr & "You entered " & intInput End With End Sub The GetReal Method Phng thc GetReal cho php ngi dung nhp vo mt s thc (gi tr nhp vo t 32,768 to +32,767). C php nh sau : dblUserRealInput = UtilityObject.GetReal([Prompt]) 5. GetCorner Method, GetAngle Method, GetDistance Method The GetCorner Method Tr v im gc ca mt hnh ch nht C Php nh sau : varUserCornerInput = UtilityObject.GetCorner(BasePoint [,Prompt]) NAME
BasePoint Prompt

TYPE Variant String

DESCRIPTION Mng 3 gi tr kiu doubles m t im gc ca hnh ch nht. Optional. A prompt for input. Gi tr tr v 3D

varUserCornerInput Variant

The GetDistance Method Phng thc GetDistance nhp khong cch t ngi dng. Khc vi GetReal, GetDistance c th nhp vo mt s thc, s thc ny tng ng vi n v ang s dng, hoc ta cng c th pick 2 im trn bn v. C php ca phng thc nh sau : dblUserDistanceInput = UtilityObject.GetDistance([BasePoint] [,Prompt]) NAME BasePoint TYPE Variant DESCRIPTION L mt mng 3 pt kiu double th hin ta 3D ca im u m t ta bt u o (trong WCS). Nu bn khng cung cp im ny, bn phi nhp vo 2. Chui thng bo ti dng lnh

Prompt dblUserDist

String Double

34

NAME anceInput

TYPE

DESCRIPTION

Ch : Hm cho php ta nhp vo mt s m. Nhng khi ta pick 2 im trn bn v th gi tr tr v s mt s dng (khong cch gia 2 im ) Khi ta nhp khong cch bng cch pick 2 im, ti v tr con chut s xut hin dy thun. Mc nh l ta 3D. C th dng l InitializeUserInput vi Bit code 16 chiu cc ta sang 2D. AutoCAD s tnh khong cch 2 im khi chiu sang 2D. 6. GetEntity Method, GetSubEntity Method GetEntity Method S dng phng thc GetEntity chn mt i tng AutoCAD bng cch pick mt thc th t bn v. Phng thc c c php nh sau : UtilityObject.GetEntity PickedEntity, PickedPoint[, Prompt] NAME PickedEntity PickPoint Prompt TYPE DESCRIPTION AcadEntity L d liu truyn ra. i tng ny s tham chiu ti object i tng va c pick trn bn v. Variant String D liu truyn ra. L mt mng 3 phn t kiu double, l ta im bn va pick trong h ta WCS. Chui thng bo trn dng lnh

V d sau ly mt thc th bng cch pick i tng trn bn v: Public Sub TestGetEntity() Dim objEnt As AcadEntity Dim varPick As Variant On Error Resume Next With ThisDrawing.Utility .GetEntity objEnt, varPick, vbCr & "Pick an entity: " If objEnt Is Nothing Then 'check if object was picked. .Prompt vbCrLf & "You did not pick as entity" Exit Sub End If .Prompt vbCr & "You picked a " & objEnt.ObjectName .Prompt vbCrLf & "At " & varPick(0) & "," & varPick(1) End With End Sub GetEntity tr v mt li nu gi tr nhp vo l null, nh khi bn pick khng trng mt thc th ha no c, hoc khi bn n Enter m khng chn bt k mt thc th no The GetSubEntity Method S dng GetSubEntity nhp mt thc th phc. Mt thc th phc l mt thc th cha nhiu thc th n khc v d nh ng polyline, block. Phng thc c c php nh sau : UtilityObject.GetSubEntity PickedEntity, PickPoint, Matrix, Context[, Prompt] NAME TYPE DESCRIPTION Object Output. Tr v tham chiu ti i mt i tng va c pick. Output. L mt mng 3 phn t kiu double, l ta im bn va pick trong h ta WCS. Output. Tr v 1 mng n 4x4 phn t kiu doubles 35 PickedEntity AcadEntity PickPoint Matrix Variant Variant

NAME Context

TYPE Variant

DESCRIPTION Cha ma trn chuyn i ca i tng va c chn. Output. Tr v mt mng kiu long integer cha ObjectIds cho mi block cha, cha cc i tng c chn, nu thc th l Bloock. Chui thng bo tai dng lnh

Prompt

String

Thc th va chn trong Model ca block, tham s Matrix l ma trn chuyn t h ta block sang h to WCS ca bn v. N bao gm tt c cc php bin i t mt thc th c lu trong block sang mt thc th bn v, nh scale, rotation php bin i ta . Tham s Context output l mt mng cc ObjectIds ca cc i tng cha i tng va c chn. V d c mt ng thng, nm trong block c tn l BL1. BL1 li nm trong block chao (Block nng) c tn l BL2. Th Context s l mng 2 phn t cha ID ca BL1 v BL2. V d : Ch : Thc th nhp vo bng GetEntity hoc bng GetSubEntity c th l i tng n (Invisible). V khi bn chn, thay v pick object bn c th nhp vo k t L (Last Object selection). Last Object Selection c th b ng bng hay thuc layer va b tt i.

36

CHNG 3 : TO V SA CC THC TH HA (9 TIT)


I. To i tng bn v (1t) 1. Xc nh i tng cha thc th. i tng ha c th c to trong ModelSpace collection, PaperSpace Collection v Block object. to thm mt thc th ha, ta dng phng thc Addxxx. V d, v mt ng thng trong ModelSpace. Set lineObj = ThisDrawing.ModelSpace.AddLine(startPoint,endPoint) Cch khc, khai bo bin : Dim moSpace As AcadModelSpace Dim paSpace As AcadPaperSpace Set moSpace = ThisDrawing.ModelSpace Set paSpace = ThisDrawing.PaperSpace Set lineObj = moSpace.AddLine(startPoint,endPoint) 2. V Line, Arc, Circle, and Ellipse objects V Line Set LineObject = Object.AddLine(StartPoint, EndPoint) NAME StartPoint EndPoint DATA TYPE Variant Variant DESCRIPTION Mng 3 phn t kiu Double l ta 3D im u ca ng thng trong h ta WCS Mng 3 phn t kiu Double l ta 3D im u ca ng thng trong h ta WCS

V polyline Set LWPolylineObject = Object.AddLightWeightPolyline(Vertices) NAME Vertices DATA TYPE Variant DESCRIPTION Mt mng kiu doubles ch ra danh sch ta cc im 2-D trong h ta WCS c dng nh sau (i.e., p1x, p1y, p2x, p2y, etc.). Mng ny ti thiu phi c 4 phn t (2 im).

Ch : Polyline khng bao cao Z. Nu bn mun v ti mt cao no , bn phi Set Elevation

37

The Spline Object Set SplineObject = Object.AddSpline(FitPoints, StartTangent, EndTangent) NAME FitPoints DATA TYPE Variant DESCRIPTION L mng mt chiu kiu double ch ra danh sch cc im m SPLine s i qua bao gm ta X, Y v Z c dng (i.e., p1x, p1y, p1z, p2x, p2y, p2z, etc.). Mng gm 3 pt kiu doubles xc nh tip tuyn ca spline ti im u. Mng gm 3 pt kiu doubles xc nh tip tuyn ca spline ti im u.

StartTangent EndTangent

Variant Variant

V cung trn (Arc) Set ArcObject = Object.AddArc(CenterPoint, Radius, StartAngle, EndAngle) NAME CenterPoint Radius StartAngle EndAngle DATA TYPE Variant Double Double Double DESCRIPTION Mng 3 phn t kiu Double l ta 3D tm cung trn trong h ta WCS Bn knh Gc bt u ca cung trn (radians), l gc hp vi trc X trong h ta WCS Gc kt thc ca cung trn (radians), l gc hp vi trc X trong h ta WCS

The Circle Object 38

Set CircleObject = Object.AddCircle(CenterPoint, Radius) NAME CenterPoint Radius DATA TYPE Variant Double DESCRIPTION Mng 3 phn t kiu Double l ta 3D tm ng trn trong h ta WCS Bn knh ng trn

The Ellipse Object Set EllipseObject = Object.AddEllipse(CenterPoint, MajorAxis, RadiusRatio) NAME CenterPoint MajorAxis RadiusRatio DATA TYPE Variant Variant Double DESCRIPTION Mng 3 phn t kiu Double l ta 3D tm Ellipse trong h ta WCS. Mng 3 phn t kiu Double l ta 3D mt im m t vector ca trc ln Ellipse tnh t tm. T l chiu di ca trc b v trc ln : 0 < RadiusRatio 1.

3. To cc khi c The Solid Object Set SolidObject = Object.AddSolid(Point1, Point2, Point3, Point4) NAME DATA TYPE Point1 Point2 Point3 Point4 Variant Mng 3 phn t kiu Double l ta 3D im u, im cui ca ng th hai trong h ta WCS. Variant DESCRIPTION Mng 3 phn t kiu Double l ta 3D im u, im cui ca ng th nht trong h ta WCS.

39

Point 1

Point 3

Point 2

Point 4

4. To i tng hatch The Hatch Object Set HatchObject = Object.AddHatch(PatternType, PatternName, Associativity) NAME PatternType PatternName Associativity DATA TYPE Long String Boolean DESCRIPTION Loi ca Pattern, c 3 gi tr c lit k bng i. Tn ca Hatch c nh ngha trong file *.pat. True : associative hatch. Flase : disassociative hatch.

Bng cc gi tr ca PatternType CONSTANT AcHatchPatternTypeUserDefined AcHatchPatternTypePredefined VALUE 0 1 DESCRIPTION Cho php nh ngha Pattern da trn kiu Line hin. S dng tn pattern t nhng Pattern nh ngha trc trong file acad.pat. S dng tn pattern t nhng Pattern nh ngha trc trong file acad.pat.

AcHatchPatternTypeCustomDefined

Sau khi bn to Hatch object, bn phi xc nh boundary or loop bng cch s dng phng thc AppendOuterLoop. C th xc nh thm cc o nh bn trong min bn trong min ln bng cch s dng phng thc AppendInnerLoop. Phng thc ny c c php nh sau : Object.AppendOuterLoop loop Object.AppendInnerLoop loop NAME DATA TYPE Object Loop AcadHatch Array of AcadEntity i tng Hatch L mng thc th. Ch l cc thc th ny bt buc phi to thnh chu trnh khp kn, nu khng Autocad s bo li
Outer Inner

DESCRIPTION

Sau khi nh ngha hatch, bn cn dng phng thc Object.Evaluate v hatch, trong : Object l i tng hatch thuc kiu AcadHatch. 5. To i tng Region, cc php ton trn Region The Region Object 40

RegionArray = Object.AddRegion(ObjectsArray) NAME ObjectsArray DATA TYPE Array of Entity DESCRIPTION L mng cc thc th. Ch l cc thc th ny bt buc phi to thnh chu trnh khp kn, nu khng Autocad s bo li.

The Region Boolean Object.Boolean(Operation, Object1) NAME Operation DATA TYPE AcBooleanType DESCRIPTION acUnion : ni 2 Region acIntersection : ly phn giao nhau ca 2 Region acSubtraction : ly phn khng giao nhau.

Region mi va to thnh s c gn cho bin Object. II. Thm Text vo bn v (1t) 1. To cc TextStyle TextStyle nmt trong classe Textstyles collection. Thm mt style dng phng thc ThisDrawing.TextStyles.Add (NameStyle). Bn cng c th chnh s thuc tnh ca TextStyle. Cc thuc tnh gm : FontFile : File font. BigFontFile : Font shape. Height : Chiu cao ca font. Width : T l chiu rng ca ch. ObliqueAngle : Gc nghing ca ch. TextGenerationFlag : Backward text, upside-down text, or both. 2. Chn Text vo bn v The Text Object Set TextObject = Object.AddText(TextString, InsertionPoint, Height) NAME TextString InsertionPoint Height The MText Object Set MTextObject = Object.AddMText(InsertionPoint, Width, TextString) NAME InsertionPoint Width TextString DATA TYPE Variant Double String DESCRIPTION Mng 3 phn t kiu Double l ta 3D im chn Mtext trn bn v. Chiu rng ca Mtext Ni dung text DATA TYPE String Variant Double Ni dung text Mng 3 phn t kiu Double l ta 3D im chn Mtext trn bn v. Chiu cao ch (phi l mt s dng) DESCRIPTION

3. Chn cc k t c bit, cc k t Unicode. The Specail characters S dng %%nnn chn k t c bit. V d : 41

Dim percent as Long percent = ASC("%") TextString = chr(percent) + chr(percent) + "nnn" Cc Code c bit nh sau %%o Toggles overscore mode on and off %%u Toggles underscore mode on and off %%d Draws degree symbol %%p Draws plus and minus tolerance symbol %%c Draws diameter dimensioning symbol %%% Draws single percent sign The Unicode characters \U+00B0 Degree symbol \U+00B1 Plus/minus tolerance symbol \U+2205 Diameter dimensioning symbol III. Sa cc i tng bn v (1.5t) AutoCAD cung cp cho bn mt s phng thc v thuc tnh gip bn c th chnh sa cc thc th ha. Vi cc thuc tnh ny bn c th : Copy, delete, explode, highlight, mirror, move, offset, rotate, v scale objects Lm vic vi polar v rectangular arrays Thay i color, layer, linetype, v visibility ca cc thc th ha Khi bn thay i mt thc th ha bng code, s thay i khng hin th ngay trn bn v cho n khi bn s dng phng thc Update ca Object, hoc s dng phng thcRegen ca Document Object. Trong mt s trng hp, AutoCAD s update trn bn v khi macro hoc program ca bn hon thnh. Trong mc ny, ta thng xuyn s dng phng thc Update nh sau : DrawingObject.Update 1. Cc php sa i c bn Copying Objects Khi s dng phng thc Copy, i tng mi c to ra c cng v tr vi thc th gc v c v ln trn thc th gc. C php ca phng thc nh sau : Set DrawingObject = DrawingObject.Copy Deleting Objects xa mt thc th ra khi bn v, ta dung phng thc Delete. Phng thc c c php nh sau : Object.Delete Phng thc Erase tng t nh phng thc Delete. Nh phng thc ny ch p dng cho selectionset groups. Bn khng th p dng n xa mt Object. Exploding Objects S dng phng thc Explode tch mt thc th gp thnh cc thc th n l. Phng thc ny tr v mt mng cc objects va c tch ra. Phng thc c c php nh sau: varObjectArray = Object.Explode Lu : T phin bn CAD 2004 tr i, lnh Explode s tch MText thnh cc i tng Text ring l. Highlighting Entities Phng thc Highlight s lm cho i tng bin thnh nt t nh khi bn chn i tng . C php nh sau : Object.Highlight Highlighted 42

Tham s Highlighted kiu Boolean, Nhn gi tr (True) khi mun Object Highlight v (False) nu mun i tng tr v trng thi bnh thng. Mirroring Objects C php nh sau : Set DrawingObject = DrawingObject.Mirror(Point1, Point2) NAME DATA TYPE Point1 Variant Point2 Variant DESCRIPTION Mng 3 phn t kiu Double l ta 3D im th nht ca trc i xng. Mng 3 phn t kiu Double l ta 3D im th hai ca trc i xng.

Moving Objects DrawingObject.Move Point1, Point2 NAME DATA TYPE Point1 Point2 Variant Variant DESCRIPTION Mng 3 phn t kiu Double l ta 3D im th nht ca Vector dch chuyn trong h ta WCS. Mng 3 phn t kiu Double l ta 3D im th hai ca Vector dch chuyn trong h ta WCS.

Rotating Objects DrawingObject.Rotate BasePoint, RotationAngle 43

NAME BasePoint

DATA TYPE Variant

DESCRIPTION Mng 3 phn t kiu Double l ta 3D im tm quay trong h ta WCS. im ny phi cng ta Z vi thc th gc Gc quay tnh bng radian.

RotationAngle Double

Scaling Objects DrawingObject.ScaleEntity BasePoint, ScaleFactor NAME BasePoint DATA TYPE Variant DESCRIPTION Mng 3 phn t kiu Double l ta 3D im tm phng trong h ta WCS. Gi tr dng, l t l tng i kch thc ca hnh mi cho hnh c.

ScaleFactor Double

2. Cc php bin i nng cao Offsetting Objects 44

Phng thc ny c th p dng cho cc thc th nh Arc, Circle, Ellipse, Line, LightweightPolyline, Polyline, Spline, v Xline. Phng thc ny tr v mt mng cc thc th mi c to. varObjectArray = Object.Offset(OffsetDistance) Tham s OffsetDistance kiu double khc khng th hin hng v khong cch Offset. Gi tr m ngha l offset to mt Object b hn thc th gc. i vi ng thng, gi tr m th hin hng ca thc th mi so vi thc th gc.

Object Arrays Use the ArrayPolar and ArrayRectangular methods to create an array of objects based on an existing object. Both methods copy the base object into a regular pattern at a specified distance from one another. Creating a Polar Array of Objects varObjectArray = DrawingObject.ArrayPolar (NumberofObjects, ArrayCenter) NAME NumberofObjects AngleToFill ArrayCenter DATA TYPE Long Double Variant DESCRIPTION S lng cc thc th phi ln hn 1. Gi tr khc khng tnh bng radians. Mng 3 phn t kiu Double l ta 3D im tm array trong h ta WCS. AngleToFill,

Creating a Rectangular Array of Objects varObjectArray = DrawingObject.ArrayRectangular (NumberOfRows, NumberOfColumns, NumberOfLevels, DistBetweenRows, DistBetweenColumns, DistBetweenLevels) 45

NAME NumberofRows

DATA TYPE Long

DESCRIPTION S dng, l s lng dng trong rectangular array. Nu gi tr l 1, th NumberofColumns phi ln hn 1. S dng, l s lng ct trong rectangular array. Nu gi tr l 1, th NumberofRows phi ln hn 1. S dng, l s lng cc cp theo trc Z trong rectangular 3D array. Khong cch gia cc dng. Gi tr ny dng th dng s m rng theo hng ln trn. Ngc li, nu l gi tr m, dng s c m rng xung di. Nu bng khng, i tng s c v ln trc thc th gc. Khong cch gia cc ct Gi tr ny dng th ct s m rng theo hng sang tri. Ngc li, nu l gi tr m, dng s c m rng sang phi. Nu bng khng, i tng s c v ln trc thc th gc. Khong cch gia cc Level theo trc Z. Gi tr ny dng th dng s m rng theo hng ln trn. Ngc li, nu l gi tr m, dng s c m rng xung di. Nu bng khng, i tng s c v ln trc thc th gc.

NumberOfColumns

Long

NumberOfLevels DistBetweenRows

Long Double

DistBetweenColumns Double

DistBetweenLevels

Double

Nu bn lm vic trong 2D, tt nht bn t tham s NumberOfLevels bng 1. Transform Objects Bn c th move, scale, hoc rotate mt object thng qua ma trn chuyn i 4 4 bng cch s dng phng thc TransformBy. C php ca phng thc nh sau : anObj.TransformBy tMatrix Ma trn ny c dng nh sau :, R00 R01 R02 T0 R10 R11 R12 T1 R20 R21 R22 T2 0 0 0 1 Trong R = Rotation v T = Translation: V d : ma trn quay mt thc th mt gc 90 s c dng nh sau : tMatrix(0,0) = 0.0 tMatrix(0,1) = -1.0 tMatrix(0,2) = 0.0 46

tMatrix(0,3) = 0.0 tMatrix(1,2) = 0.0 tMatrix(2,1) = 0.0 tMatrix(3,0) = 0.0 tMatrix(3,3) = 1.0

tMatrix(1,0) = 1.0 tMatrix(1,3) = 0.0 tMatrix(2,2) = 1.0 tMatrix(3,1) = 0.0

tMatrix(1,1) = 0.0 tMatrix(2,0) = 0.0 tMatrix(2,3) = 0.0 tMatrix(3,2) = 0.0

Rotation Matrix: 90 degrees about point (0, 0, 0)

3. Chnh sa PolyLine, SpLine Edit Polylines Bn c th chnh sa Closed property : Opens hoc closes mt polyline. Coordinates property : Thay i tng nh ca mt polyline. AddVertex method : Thm mt nh vo polyline. S dng cc phng thc sau cp nht ch li lm v chiu rng ca mt polyline: SetBulge : Thit lp cong ca polyline. SetWidth : Thit lp b rng ca im u v im kt thc polyline. Edit Splines Vi splines bn c th thay i cc thuc tnh sau : Closed : Open hoc close ng spline. ControlPoints : iu khin cc im ca spline. EndTangent : iu khin php tuyn im cui ca spline. FitPoints : Specifies all the fit points of a spline. FitTolerance : Thau i tolerance cho Spline. Knots : iu chnh nt vertor cho spline. StartTangent : iu khin php tuyn im u ca spline. Ngoi ra, bn cn c th thm mi nh, nh sau AddFitPoint : Thm mt fit point vo Spline ti v tr Index. DeleteFitPoint : Xa mt fit point khi spline ti v tr Index. GetFitPoint : Ly im fit point ca spline ti v tr Index. Reverse : nghch o hng ca Spline. SetControlPoint : Thit lp control point ca spline ti v tr index. SetFitPoint : thit lp fit point ca spline ti v tr index. SetWeight : Thit lp weight ca control point ti v tr index. Cc thuc tnh ch c : Area : Din tch khp kn ca spline. NumberOfControlPoints : s lng cc im iu khin ca spline. NumberOfFitPoints : S lng cc im Fit ca spline. IV. Block v thuc tnh ca block (2 Tit) Trong mc ny chng ta s nghin cu To Block objects Chn Block object vo trong bn v AutoCAD Lm vic vi external reference file To Attribute object 47

Chn Block objects c cha attribute 1. Blocks v Block References Block object i din cho mt block definition, n cha tn v mt tp hp cc thc th ha. Block objects bao gm 2 loi : Mt Block definition l mt kiu d liu tru tng nh ngha cc thc th ha nm trong Block. Mt Block reference (hay block insertion), l nhng v tr m ta chn Block vo bn v. Thay i mt block definition tng ng vi vic tat hay i tt c cc block reference trn bn v. C 3 loi block : Simple block : L nhng Block n gin c nh ngha t cc thc th c sn trn bn v hoc blok c nh ngha bng cch bn insert mt bn v khc vo trong bn v hin hnh. Externally referenced block : l kiu tham kho ngoi, bn c th chnh s chng bng lnh REFEDIT. Layout block : . cha dng hnh hc ca mt Layout object. S dng thuc tnh IsLayout v IsXRef nhn dng kiu ca block definition. Nu c hai thuc tnh trn u tr v gi tr False, th Block object s l simple block. Truy cp Block Objects AutoCAD Document objects c Blocks collection cha tt c cc Block definition objects trong bn v. V d sau y truy cp n cc block object :
Dim objBlocks As AcadBlocks Set objBlocks = ThisDrawing.Blocks MsgBox "There are " & objBlocks.Count & " Block objects"

Tham chiu n Block object c sn trong bn v, ta s dng phng thc Item, y cng l phng thc mc nh ca Blocks collection.
Dim objBlock As AcadBlock Set objBlock = ThisDrawing.Blocks.Item(Index) Set objBlock = ThisDrawing.Blocks.Item(NameBlock)

Duyt qua cc Block Object ca Blocks Collection


Public Sub ListBlocks() Dim objBlock As AcadBlock Dim strBlockList As String strBlockList = "List of blocks: " For Each objBlock In ThisDrawing.Blocks strBlockList = strBlockList & vbCr & objBlock.Name Next MsgBox strBlockList End Sub

To Blocks S dng phng thc Add Phng thc Add ca Blocks collection dung thm mt Block object mi vo bn v. Phng thc ny s tr v mt Simple block. Phng thc c c php nh sau Set BlockObject = BlocksCollection.Add(InsertionPoint, BlockName) NAME DATA TYPE DESCRIPTION 48

NAME InsertionPoint BlockName

DATA TYPE Variant String

DESCRIPTION Mng 2 phn t kiu doubles m t ta im chn trong h ta WCS. Tn ca Block mi

AddXXX Methods S dng phng thc AddXXX thm cc thc th ha vo Block object ging nh thm cc thc th vo bn v. CopyObject Method Mt cch khc thm mt Object vo trong Block object l dng phng thc Document object's CopyObject. Phng thc ny s nhn bn thm mt thc th na. C php nh sau : varCopies = Owner.CopyObjects(Objects [, NewOwner] [, IdMap]) NAME Owner DATA TYPE Document, PaperSpace, ModelSpace or Block objects Variant DESCRIPTION i tng cha Object cn copy.

Objects

Mng objects copy. Cc Object ny phi thuc Owner object. ch ti ca phng thc copy. Nu l null th s Copy vo Owner object. Xem IDPair objects.

NewOwner Variant IdMap Variant

i tn Block Object i tn Block, bn gn mt chui mi cho thuc tnh Name. Khi thay i tn th tn ca cc Block reference s t ng thay i theo. Ch : Khi bn thay i Layout Block hoc khi bn thay i nhng Block khng c tn c th (bt u bng du *) c th ph hng AutoCad. Xa Block Object BlockObject.Delete Ch : Bn khng th xa Block khi : C BlockReference object tham chiu n n. Nu mt BlockReference object tham chiu n block definition, bn khng th xa chng. xa Block definition ny, bn phi s dng phng thc Purge trong Document Object. Khi n l mt Xref. Bn cng khng cn thit phi xa n v thc cht AutoCAD khng lu tr Xref trong bn v. The InsertBlock Method Nh ni trn, mt BlockReference object c to ra khi : Bn chn mt Block vo bn v t Block Definition Bn chn mt block vo bn v t mt bn v khc ngoi a ca bn Thao tc vi Block The Item Method S dng phng thc Item truy cp cng nh duyt qua cc i tng trong block definition object. Phng thc c c php nh sau : Set objEntity = BlockObject.Item(Index) Index l v tr ca thc th trong trong Block. Item l phng thc mc nh ca BlockObject. 49

Set objEntity = BlockObject(Index) The InsertBlock Method S dng InsertBlock them mt BlockReference object vo bn v hoc vo mt Block lng. C php nh sau : Set BlockReferenceObject = Object.InsertBlock(InsertionPoint, BlockName, _ Xscale, Yscale, ZScale, RotationAngle) NAME InsertionPoint BlockName Xscale DATA TYPE Variant String Double DESCRIPTION Mng 3 phn t kiu Double l ta im chn vo i tng (i tng c th l Block hay Document Object). Tn ca Block object trong Blocks collection, hoc ng dn v tn file ca bn v chn vo bn v hin hnh. L t l phng theo trc X, gi tr phi khc khng. Nu nhn gi tr m th s ly i xng theo trc ti im chn. L t l phng theo trc Y, gi tr phi khc khng. Nu nhn gi tr m th s ly i xng theo trc ti im chn. L t l phng theo trc Z, gi tr phi khc khng. Nu nhn gi tr m th s ly i xng theo trc ti im chn. Gc quay tng i hp vi trc X trong h ta WCS, tnh bng radian.

Yscale

Double

Zscale

Double

RotationAngle

Double

Deleting a Block Reference Ging nh cc Object khc, bn s dng phng thc Delete xa block references. Phng thc ch xa BlockReference object, khng xa Block definition object. C php nh sau : BlockReferenceObject.Delete The Explode Method varArray = BlockReferenceObject.Explode Ch : Phng thc ch c BlockReferenceObject Phng thc s to ra mt bn copy ca block definition v vn li block reference cha ph v. Bn phi xa n nu bn khng mun s dng chng. Ghi Block ra file (phng thc Wblock) i tng Document object c phng thc ghi li nhng g c trong mt SelectionSet object vo a thnh mt bn v mi. Bn c th nhp file ny nh mt block definition bng cch s dng phng thc InsertBlock. C php phng thc nh sau : DocumentObject.WBlock FileName, SelectionSet NAME FileName DATA TYPE String DESCRIPTION Tn ca file s c ghi. Bn cng khng cn thit phi ghi r c tn phn m rng ca file, phng thc s s dng phn m rng l .dwg. selection set cha cc thc th cn ghi ra file.

SelectionSet SelectionSet object

50

Phng thc mc nh s dng im gc ca bn v mi l gc to WCS ca bn v hin hnh. Bn c th sa li trc khi bn s dng Wblock bng cch sau : Public Sub TestWBlock() Dim objSS As AcadSelectionSet Dim varBase As Variant Dim dblOrigin(2) As Double Dim objEnt As AcadEntity Dim strFilename As String 'choose a selection set name that you only use as temporary storage and 'ensure that it does not currently exist On Error Resume Next ThisDrawing.SelectionSets("TempSSet").Delete Set objSS = ThisDrawing.SelectionSets.Add("TempSSet") objSS.SelectOnScreen With ThisDrawing.Utility .InitializeUserInput 1 strFilename = .GetString(True, vbCr & "Enter a filename: ") .InitializeUserInput 1 varBase = .GetPoint(, vbCr & "Pick a base point: ") End With " WCS origin dblOrigin(0) = 0: dblOrigin(1) = 0: dblOrigin(2) = 0 " move selection to the origin For Each objEnt In objSS objEnt.Move varBase, dblOrigin Next " wblock selection to filename ThisDrawing.Wblock strFilename, objSS " move selection back For Each objEnt In objSS objEnt.Move dblOrigin, varBase Next " clean up selection set objSS.Delete End Sub Using MInsertBlock Objects Set MInsertBlockObject = Object.AddMInsertBlock(InsertionPoint, BlockName, XScale, YScale, ZScale, RotationAngle, Rows, Columns, RowSpacing, ColumnSpacing) NAME InsertionPoint BlockName Xscale DATA TYPE Variant String Double DESCRIPTION Ta im chn. Tn ca Block object trong Blocks collection, hoc ng dn y ca bn v c sn trn a. T l phng theo phng X. Phi nhn gi tr khc 0. Gi tr m s ly i xng theo phng X ti im chn. 51

NAME Yscale Zscale RotationAngle Rows Columns RowSpacing ColumnSpacing External References

DATA TYPE Double Double Double Long Long Double Double

DESCRIPTION T l phng theo phng Y. Phi nhn gi tr khc 0. Gi tr m s ly i xng theo phng Y ti im chn. T l phng theo phng Z. Phi nhn gi tr khc 0. Gi tr m s ly i xng theo phng Z ti im chn. The rotation angle relative to the WCS X-axis, expressed in radians. S dng, l s lng dng. S dng, l s lng ct. S khc khng, l khong cch gia cc dng. Gi tr m s to ra cc dng theo hng ngc vi trc X. S khc khng, l khong cch gia cc dng. Gi tr m s to ra cc dng theo hng ngc vi trc Y.

External references, or Xrefs, are blocks that are not permanently loaded into the current drawing file. Instead, Xrefs refer to an external drawing file for their geometry (hence their name). External references share many properties and methods with simple blocks, and for many purposes you can treat them as simple blocks. But sometimes you might also need to use external references' special capabilities. This section explains the following Xref methods:

Attaching and detaching Loading and unloading Binding

Attaching External References


The AttachExternalReference method works much like InsertBlock, except that the resulting entity is an external reference instead of a block reference. Just like InsertBlock, the PaperSpace, ModelSpace, and Block objects expose this method and let you specify the insertion point, scale, and rotation angle in the drawing.
Set ExternalReferenceObject = Object.AttachExternalReference(FileName, _ BlockName, InsertionPoint, Xscale, Yscale, Zscale, RotationAngle, Overlay)

Table 13-7 explains this method's parameters. Table 13-7: AttachExternalReference Method Parameters DATA DESCRIPTION TYPE String The external AutoCAD drawing file's name. You must specify the .dwg extension. Optionally, you can specify a path to the file. If you don't, AutoCAD tries to find the file in the system search path. A name for the internal Block object that will point to the external drawing file. A three-element array of doubles that specifies the 3D WCS coordinates where the Xref will be inserted into the Object. A non-zero number representing the scaling factor for the Xref's X direction. Negative numbers mirror the insertion on this axis. A non-zero number representing the scaling factor for the Xref's Y direction. 52

NAME
FileName

BlockName

String

InsertionPoint Variant Xscale Yscale

Double Double

NAME

Table 13-7: AttachExternalReference Method Parameters DATA DESCRIPTION TYPE Negative numbers mirror the insertion on this axis. Double A non-zero number representing the scaling factor for the Xref's Z direction. Negative numbers mirror the insertion on this axis. The rotation angle relative to the WCS X-axis, expressed in radians.

Zscale

RotationAngle Double Overlay

Boolean Controls how the Xref is attached. If True, the Xref is brought in as an overlay. Overlay external references aren't visible if the current drawing is attached as an Xref to another drawing. In this way, overlay Xrefs can reduce the need to detach Xrefs before sharing drawings. If this parameter is False, the Xref is an attachment.

The following example creates an Xref based on user input: Detaching External References You can detach an external reference from the current drawing using the Block object's Detach method. It has this syntax:
BlockObject.Detach

Note You detach an Xref's block definition, the method removes all associated ExternalReference objects from the drawing too. This includes linetypes, textstyles, dimstyles, nested block definitions, and layers. Unloading External References You can also unload external references without detaching them from the current drawing. Use the Block object's Unload method. It has this syntax:
BlockObject.Unload

Though not visible, unloaded Xrefs are still associated with the current drawing. To regenerate them, reload them. Reloading External References Use the Block object's Reload method to reload an external reference whenever you want, even if the Xref is already loaded. Reload an already-loaded Xref when you modify the underlying drawing and then want to update the in-memory copy in the current drawing. This method has the following syntax:
BlockObject.Reload

Binding External References Use the Block object's Bind method to convert external references to simple blocks. This operation builds an internal copy of the external drawing file in much the same way the InsertBlock method does using an external filename. Instead of referring to the external drawing database, Bind converts any former ExternalReference objects to simple block references. This method has the following syntax:
BlockObject.Bind(Merge)

53

This method has one parameter, Merge, a Boolean. When it's True, the method merges dependent symbol table entries in the external file with the current drawings entries. When it's False, the method prefixes them to avoid collision with any other entry name in the current drawing. The prefix has the form BlockName$X$EntryName, where
BlockName is the block definition name for the current drawing's external reference X is an automatically generated integer that makes the name unique in the current drawing EntryName is the name of the symbol table entry in the externally referenced drawing file.

Note If Merge is set to True and an entry is already present in the current drawing, the method maps the external entry to the current drawing entry. This is identical behavior to inserting block definitions that contain duplicate layers, linetypes, or textstyles in the current drawing. The following example binds the specified external reference using either style

V.

Chn i tng. (1 Tit) 1. To Selectionset. Selectionset nm trong class Selections collection.

Thm mt Selectionset Set SelectionSetObject = SelectionSetsCollection.Add(SelectionSetName) NAME


SelectionSetName

DATA TYPE String

DESCRIPTION Tn ca Selectionset, tn ny phi khng c trng vi cc Selectionset c.

Truy sut n collection Nu bn c nhiu Selectionset trong selections collection. Bn c th truy cp n tng selectionset theo tn hoc s th t (Index) ca n. V d nh sau : Dim objSelections As AcadSelectionSets Set objSelections = ThisDrawing.SelectionSets Dim objSelection As AcadSelectionSet Set objSelection = objSelections.Item(2) Set objSelection = objSelections.Item("My SelectionSet") Ch s ca Selection nm trong khong t 0 n (SelectionSets.Count 1). Trong Autocad Selection collection th phng thc Item l phng thc mc nh. Do vy 2 v d trn c th vit li nh sau : Set objSelection = objSelections(2) Set objSelection = objSelections("My SelectionSet") 2. Thm i tng vo selection set Selecting Entities Khi bn to mi mt SelectionSet th s l mt Slectionset rng. thm cc i tng vo SelectionSet ta dng phng thc SelectXXX. Ging nh trong AutoCAD, ta c VBA cho php ta 54

chn cc i tng theo : chn tng i tng (pick vo i tng), chn theo Window, theo fence (n ch f, v chn 1 ng thng ct qua cc i tng c chn) hoc theo mt a gic (polygon). The Select Method Phng thc Select cho php bn thm i tng vo selection set, ngoi ra n cng cho php bn s dng Last SelectionSet. Phng thc c c php nh sau : SelectionSetObject.Select Mode [, Point1, Point2] [, FilterCodes, FilterValues] NAME Mode Point1 Point2 DATA TYPE Long Variant Variant DESCRIPTION Kiu chn, c lit k bng di. Mng 3 phn t kiu Double l 3 ta ca gc u tin ca hnh ch nht. Mng 3 phn t kiu Double l 3 ta ca gc u tin ca hnh ch nht. Point1, Point2 th phi dng ng thi vi nhau. Dng lc thc th. Nghin cu trong mc sau. Dng lc thc th. Nghin cu trong mc sau. Bng cc kiu chn VALUE DESCRIPTION 0 1 3 4 Tt c cc thc th nm trong ca s c ch ra bi im Point1 v Point2 c chn. Tt c cc thc th nm trong hoc ct Window c ch ra bi im Point1 v Point2 c chn. Selection set gn nht s c chn. Point1 v Point2 khng c s dng. Thc th c to cui cng tnh n thi im chn s c chn. Point1 v Point2 khng c s dng. Chn tt c cc thc th. Point1 v Point2 khng c s dng.

FilterCodes FilterValues

Variant Variant

CONSTANT acSelectionSetWindow acSelectionSetCrossing acSelectionSetPrevious acSelectionSetLast

acSelectionSetAll

Lu l phng thc Select c th chn tt c cc thc th thuc tt c cc layer ngay c khi n c ng bng hay c kha. V d : Public Sub TestSelectionSetFilter() Dim objSS As AcadSelectionSet Dim intCodes(0) As Integer Dim varCodeValues(0) As Variant Dim strName As String On Error GoTo Done With ThisDrawing.Utility strName = .GetString(True, vbCr & "Layer name to filter: ") If "" = strName Then Exit Sub " create a new selectionset Set objSS = ThisDrawing.SelectionSets.Add("TestSelectionSetFilter") 55

" set the code for layer intCodes(0) = 8 " set the value specified by user varCodeValues(0) = strName " filter the objects objSS.Select acSelectionSetAll, , , intCodes, varCodeValues " highlight the selected entities objSS.Highlight True " pause for the user .Prompt vbCr & objSS.Count & " entities selected" .GetString False, vbLf & "Enter to continue " " dehighlight the entities objSS.Highlight False End With Done: " if the selection was created, delete it If Not objSS Is Nothing Then objSS.Delete End If End Sub The SelectOnScreen Method y l kiu chn chun ca Autocad nh Window, Crossing v Last. SelectionSetObject.SelectOnScreen [, FilterCodes, FilterValues] The SelectAtPoint Method Cho php chn mt thc th bng cch chn 1 im. SelectionSet.SelectAtPoint Point [,FilterCodes, FilterValues] NAME DATA TYPE
Point

DESCRIPTION Mng 3 phn t kiu Double l 3 ta mt im m thc th c chn i qua.

Variant

The SelectByPolygon Method SelectionSetObject.SelectByPolygon Mode, Vertices [, FilterType, FilterData] NAME DATA TYPE DESCRIPTION Mode Long Kiu chn, c lit k bng di. L mng mt chiu kiu double ch ra danh sch cc im m a gic s i qua bao gm ta X, Y v Z trong WCS c dng (i.e., p1x, p1y, p1z, p2x, p2y, p2z, etc.). Bng gi tr Modes VALUE Vertices Variant

CONSTANT

DESCRIPTION 56

CONSTANT acSelectionSetFence

Bng gi tr Modes VALUE 2

DESCRIPTION

Vertices s m t cc ng thng lin tip nhau. Tt c cc thc th ct ng thng ny u c chn. Vertices cha t nht 2 im Vertices m t cc nh mt a gic. Tt c cc thc th nm trong a gic ny s c chn. Vertices cha t nht 3 im. Vertices m t cc nh mt a gic. Tt c cc thc th nm trong hoc ct qua a gic ny s c chn. Vertices cha t nht 3 im.

acSelectionSetWindowPolygon 6

acSelectionSetCrossingPolygon 7

Adding and Removing Items SelectionSetObject.AddItem(Entities) SelectionSetObject.RemoveItem(Entities) NAME


Entities

DATA TYPE Array of AcadEntity objects

DESCRIPTION Mng cc thc th thm hay bt i khi tp chn.

The Clear, Delete, and Erase Methods SelectionSetObject.Clear Phng thc Clear xa tt c cc i tng trong SelectionSet object. Nhng cc thc th nm trong Seclection set vn cn trong bn v. C php nh sau : SelectionSetObject.Delete Phng thc Delete dung xo mt SelectionSet object trong SelectionSets collection. Thc th nm trong SelectionSet vn tn ti trong bn v. SelectionSetObject.Erase Phng thc Erase xa tt c cc thc th nm trong SelectionSet object v xa khi bn v. Cc thc th b xa khi bn v, nhng SelectionSet object vn tn ti trong Selection Collection v ta c th thm cc thc th mi vo trong chng. 3. Lc i tng trong selection set Selection Set Filters Bn c th s dng 2 tham s FilterCodes and FilterValues c mt s iu kin lc. V d ta ch chn cc i tng c mu, layer hay lintype, theo mun. FilterCodes mt mng kiu integers, l Group DXF code. Bn c th xem thm AutoCAD DXF. Sau y mt s code hay dng nht : DXF code Filter type 0
2

Object Type (String) nh Line, Circle, Arc, Object Name (String) Layer Name (String) nh Layer 0. Object Visibility (Integer). Gm 0 = visible, 1 = invisible. Color Number (Integer) L mt s t 0 ti 256. Gi tr 0 : BYBLOCK. Gi tr 256 l BYLAYER. Gi tr m l layer tt. 57

8 60 62

DXF code 67

Filter type Ch s Model/paper space (Integer). Gi tr 0 hoc khng gn = model space, 1 = paper space.

FilterValues l mt mng kiu variant, Mi phn t ca mng FilterValues phi tng ng vi mt phn t ca mng FilterCodes. Do vy hai mng ny phi c cng chiu di. V d : FilterType(0) = 0 FilterData(0) = "Circle" Sub Ch4_FilterMtext() Dim sstext As AcadSelectionSet Dim FilterType(0) As Integer Dim FilterData(0) As Variant Set sstext = ThisDrawing.SelectionSets.Add("SS2") FilterType(0) = 0 FilterData(0) = "Circle" sstext.SelectOnScreen FilterType, FilterData End Sub Khi bn s dng nhiu hn 2 iu kin lc. Bn phi s dng cc php ton Logic i km vi cc ton t lc l cc group code c bit l 4. Cc cp ton t c th s dng l. FILTER START AND END VALUE NUMBER OF OPERANDS OPERATOR "<AND" "<OR" "<XOR" "<NOT" "AND>" "OR>" "XOR>" "NOT>" One or more One or more Exactly two Exactly one

V d :
Public Sub TestSelectionSetOperator() Dim objSS As AcadSelectionSet Dim intCodes() As Integer Dim varCodeValues As Variant Dim strName As String On Error GoTo Done With ThisDrawing.Utility strName = .GetString(True, vbCr & "Layer name to exclude: ") If "" = strName Then Exit Sub " create a new selectionset Set objSS = ThisDrawing.SelectionSets.Add("TestSelectionSetOperator") " using 9 filters ReDim intCodes(9): ReDim varCodeValues(9) " set codes and values - indented for clarity intCodes(0) = -4: varCodeValues(0) = "<and" intCodes(1) = -4: varCodeValues(1) = "<or" intCodes(2) = 0: varCodeValues(2) = "line" intCodes(3) = 0: varCodeValues(3) = "arc" intCodes(4) = 0: varCodeValues(4) = "circle"

58

intCodes(5) = -4: intCodes(6) = -4: intCodes(7) = 8: intCodes(8) = -4: intCodes(9) = -4:

varCodeValues(5) = "or>" varCodeValues(6) = "<not" varCodeValues(7) = strName varCodeValues(8) = "not>" varCodeValues(9) = "and>"

" filter the objects objSS.Select acSelectionSetAll, , , intCodes, varCodeValues " highlight the selected entities objSS.Highlight True " pause for the user .Prompt vbCr & objSS.Count & " entities selected" .GetString False, vbLf & "Enter to continue " " dehighlight the entities objSS.Highlight False End With Done: " if the selection was created, delete it If Not objSS Is Nothing Then objSS.Delete End If End Sub

Lm vic vi Group (0.5 Tit) Khi mt group c to, theo mc nh khi ta chn mt thc th trong group , tt c cc thc th khc trong group s c chn. Nu bn mun chn tng thc th trong group, hy thay i bin h thng PICKSTYLE nhn gi tr 0 (mc nh nhn gi tr 1) 1. To mt Group Object Cc goup Object nm trong Group Collection, ta dng phng thc add thm mt Group Object v Group Collection, c php nh sau : Set GroupObject = GroupsCollection.Add(Name) NAME Name DATA TYPE String DESCRIPTION Tn ca Group s c to

VI.

2. Truy cp ti cc Group Object Truy cp n group Collection Dim objGroups As AcadGroups Set objGroups = ThisDrawing.Groups thit lp tham chiu n group object, ta s dng phng thc Item ca Groups collection : Dim objGroup As AcadGroup Set objGroup = objGroups.Item(Index) Set objGroup = objGroups.Item(NameGroup) Phng thc item l phng thc mc nh ca Group collection, do vy ta c th duyt qua cc Group Object nh sau : Public Sub ListGroups() Dim objGroup As AcadGroup Dim strGroupList As String For Each objGroup In ThisDrawing.Groups strGroupList = strGroupList & vbCr & objGroup.Name Next 59

MsgBox strGroupList, vbOKOnly, "List of Groups" End Sub 3. Thm bt thc th vo Group Bn c tht thm hay bt thc th trong group bng phng thc AppendItem v RemoveItem GroupObject.AppendItem(Entities) GroupObject.RemoveItem(Entities) NAME DATA TYPE DESCRIPTION Mng cc thc th s bt ra khi group Entities Array of AcadEntity objects

Ch : phng thc RemoveItem khng xa thc th khi bn v m ch xa lin kt thc th vi group. 4. Xa Group Object Xa group Object khi group collection. Cc thc th trong group object s khng b xa, chng s c tr v bn v. GroupObject.Delete VII. S dng layer, color v linetype (1t) 1. S dng layer, color Trong mc ny chng ta s nghin cu : Truy cp n Layers Collection v Layer Objects. Kim tra s tn ti ca mt Layer. To mt Layer v chuyn mt layer thnh layer hin hnh. Thit lp cc thuc tnh ca layer nh : On /Off, Thawed /Frozen, Locked /Unlocked i tn, xa mt layer Thit lp hay ly cc thng s Color v Linetype ca layer. Lm vic vi Layers AutoCAD cung cp Layers collection cha tt c cc i tng Layer c trong bn v. Bn c th to ra cc layer bng cc thn layer object vo Layers collection. Bn c th truy sut vo Layers collection bng cch nh sau : Dim objLayers As AcadLayers Set objLayers = ThisDrawing.Layers tham chiu n mt Layer c sn trong bn v. Bn s dng phung thc Item ca layer Collection nh sau : Dim objLayer As AcadLayer Set objLayer = objLayers.Item(Index) Set objLayer = objLayers.Item("Name of Layer") Index l mt s integer i din cho v tr ca layer trong Layers collection. Name Layer l mt chui, l tn ca Layer m ta mun truy cp n n. Nu bn s dng index th n phi nm trong khong t 0 n ( Layers.Count 1). Ging nh cc AutoCAD collections khc, Item l phng thc mc nh ca Layer collection. C ngha l tn phng thc ny c th khng c vit vo nhng cad vn hiu l phng thc Dim objLayer As AcadLayer Set objLayer = objLayers(index) Set objLayer = objLayers("Name of Layer") Duyt qua cc Layers trong Layer Collection S dng For Each loop duyt qua cc layer trong Layer Collection: 60

Public Sub ListLayers() Dim objLayer As AcadLayer For Each objLayer In ThisDrawing.Layers Debug.Print objLayer.Name Next End Sub Ngoi ra ta c th duyt qua cc Layer thng qua ch s Index. S dng thuc tnh objLayers.Count 1 nh sau : Public Sub ListLayersManually() Dim objLayers As AcadLayers Dim objLayer As AcadLayer Dim intI As Integer Set objLayers = ThisDrawing.Layers For intI = 0 To objLayers.Count - 1 Set objLayer = objLayers(intI) Debug.Print objLayer.name Next End Sub Kim tra s tn ti ca mt Layer Public Sub CheckForLayerByIteration() Dim objLayer As AcadLayer Dim strLayerName As String strLayername = InputBox("Enter a Layer name to search for: ") If "" = strLayername Then Exit Sub ' exit if no name entered For Each objLayer In ThisDrawing.Layers ' iterate layers If 0 = StrComp(objLayer.name, strLayername, vbTextCompare) Then MsgBox "Layer '" & strLayername & "' exists" Exit Sub ' exit after finding layer End If Next objLayer MsgBox "Layer '" & strLayername & "' does not exist" End Sub Ngoi ra bn c th kim tra s tn ti ca mt Layer bng cch by Layer Public Sub CheckForLayerByException() Dim strLayerName As String Dim objLayer As AcadLayer strLayerName = InputBox("Enter a Layer name to search for: ") If "" = strLayerName Then Exit Sub ' exit if no name entered On Error Resume Next ' handle exceptions inline Set objLayer = ThisDrawing.Layers(strLayerName) If objLayer Is Nothing Then ' check if obj has been set MsgBox "Layer '" & strLayerName & "' does not exist" Else MsgBox "Layer '" & objLayer.Name & "' exists" End If End Sub Creating a New Layer 61

to mi mt Layer, ta s dng phng thc Add trong Layer Collection nh sau : Set LayerObject = LayerCollection.Add(LayerName) NAME
LayerName

DATA TYPE String

DESCRIPTION Nu bn Add mt Layer c tn trng vi Layer c sn. AutoCAD s sinh ra mt li.

Public Sub AddLayer() Dim strLayerName As String Dim objLayer As AcadLayer strLayerName = InputBox("Name of Layer to add: ") If "" = strLayerName Then Exit Sub ' exit if no name entered On Error Resume Next ' handle exceptions inline 'check to see if layer already exists Set objLayer = ThisDrawing.Layers(strLayerName) If objLayer Is Nothing Then Set objLayer = ThisDrawing.Layers.Add(strLayerName) If objLayer Is Nothing Then ' check if obj has been set MsgBox "Unable to Add '" & strLayerName & "'" Else MsgBox "Added Layer '" & objLayer.Name & "'" End If Else MsgBox "Layer already existed" End If End Sub Chuyn Layer thnh Layer hin hnh. C php nh sau : DocumentObject.ActiveLayer = LayerObject V d : ThisDrawing.ActiveLayer = ThisDrawing.Layers("Walls") ly Layer hin hnh nh sau : ThisDrawing.ActiveLayer.Name V d : If ThisDrawing.ActiveLayer.Name = "Walls" Then ... Turning a Layer On/Off LayerObject.LayerOn = blnLayerOn Bn cng c th kim tra layer ang bt bng on code sau : If objLayer.LayerOn Then ... 'Thi hnh nu Layer l On. Thit lp Layer : Frozen/Thawed C php nh sau : objLayer.Freeze = True : Frezee objLayer.Freeze = True : Thaw Kim tra tnh trang ng bng ca Layer : If objLayer.Freeze Then ... 'Thi hnh nu layer ng bng 62

Thuc tnh Locking/Unlocking ca Layer Bn khng th chn cc thc th b kha bng chnh s. Tuy nhin, in\ tng vn hin th nu layer khng b ng bng, v bn vn c th s dng cc phng thc truy bt im vi chng. Trong lc layer b kha, bn vn c th thm cc thc th ha vo ayer . objLayer.Lock = False : Unlock objLayer.Lock = False : Lock Kim tra tnh trng ca Layer : If objLayer.Lock Then ... ' Thi hnnh nu layer ang b kha. Thit lp ch in cho Layer (Plottable or Not) C php nh sau : objlayer.Plottable = False : Khng in khi bn thc hin lnh in objlayer.Plottable = False : S in khi bn thc hin lnh in Ch : Mt s Layer to bi ACIS hoc ShapeManager solid-modeling engine nh layer DEFPOINTS s lun lun khng c in ra. i tn cho Layer (Plottable or Not) C php nh sau : objLayer.Name = strLayerName Deleting a Layer C php : LayerObject.Delete Phng thc Layer.Delete xa mt i tng Layer object ra khi Layers collection. Trong mt s trng hp sau, layer s khng c xa : L layer hin hnh. L layer "0" (zero). L layer c cha thc th. L mt Xref-dependent layer. Nu bn c tnh xa n, Autocad s sinh ra mt li. Ly Handle ca mt Layer AutoCAD gn cho mi object mt gi tr handle hoc ID duy nht. N tn ti v khng thay i cng vi s tn ti Object . Bn c th truy cp n handle ca cc Object thng qua phng thc Handle nh sau : Dim objLayer As AcadLayer Dim strLayerHandle As String Set objLayer = ThisDrawing.Layers("0") strLayerHandle = objLayer.Handle Handles c s dng rng ri khi bn lm vic vi extended entity data. Extended entity data, or Xdata, l nhng thng tin khng phi l ha c gn vi Object bi application program. Layer Colors Mi layer c thuc tnh Color cung cp mu cho tt c cc thc th c v trong layer nu thuc tnh Color c thit lp l ByLayer. Mc nh, mu ca mt layer mis l trng hoc en ty thuc vo mu nn ca AutoCAD. Ta c th gn gi tr mu ca Layer t 0 n 256. 9 trong s 9 c lit k trong AutoCAD VBA constant nh sau: 63

objLayer.Color = acRed Table 6-2: AutoCAD-Defined Color Constants CONSTANT COLOR INDEX COLOR acByBlock acRed acYellow acGreen acCyan acBlue acMagenta acWhite acByLayer 0 1 2 3 4 5 6 7 256 ByBlock Red Yellow Green Cyan Blue Magenta White/Black (depending on the screen background color) ByLayer

Ly gi tri mu ca mt layer : intColor = objLayer.Color Tng t bn c th thay i mu ca tng thc th vi c php nh sau : objLayer.Color = acColor Layer Linetypes Mi Layer c mt thuc tnh Linetype. Mc nh kiu nt ca cc i tng c v trong layer s c kiu nt l Linetype ca layer. Tr khi thc th c ngi dng thit lp ti mt kiu nt khc. Mc nh, kiu ng ca mt layer mi l Continuous, tc l mt solid line. Bn c th thay i chng da trn thuc tnh Layer.Linetype V d nh sau : Public Sub Layer0Linetype() Dim objLayer As AcadLayer Dim strLayerLinetype As String Set objLayer = ThisDrawing.Layers("0") objLayer.Linetype = "Continuous" strLayerLinetype = objLayer.Linetype End Sub Lu , khi ta s dng Linetype, bn phi m bo rng Linetypes m bn s dng c Load vo trong bn v. Layer Lineweights C php ca thuc tnh nh sau : objLayer.Lineweight 2. S dng linetype Trong phn ny chng ta s nghin cu nhng mc sau : Truy cp Linetype collection v Linetype object. Kim tra s tn ti ca mt linetype. Ti mt linetype vo trong bn v v chuyn mt Linetype thnh dng ng hin hnh. i tn v xa mt dng ng. Thit lp v ly cc thng s v linetype's scale v description. Truy cp Linetypes trong VBA Dim objLinetypes As AcadLineTypes 64

Set objLinetypes = ThisDrawing.Linetypes tham chiu n mt i tng LineTypeTo c sn, s dng phng thc Item: Dim objLinetype As AcadLineType Set objLinetype = objLinetypes.Item(Index) Set objLinetype = objLinetypes.Item(NameLinetype) NameLinetype kiu string l tn dng ng c load vo trong bn v Index kiu interger, cng ging nh trong Layer Collection, Index nm t 0 n (Linetypes.Count 1). Ging nh tt c cc collection khc trong AutoCAD, Phng thc Item l phng thc mc nh trong Linetypes collection. Kim tra s tn ti ca mt Linetype Public Sub CheckForLinetypeByIteration() Dim objLinetype As AcadLineType Dim strLinetypeName As String strLinetypeName = InputBox("Enter a Linetype name to search for: ") If "" = strLinetypeName Then Exit Sub ' exit if no name entered For Each objLinetype In ThisDrawing.Linetypes If 0 = StrComp(objLinetype.Name, strLinetypeName, vbTextCompare) Then MsgBox "Linetype '" & strLinetypeName & "' exists" Exit Sub ' exit after finding linetype End If Next objLinetype MsgBox "Linetype '" & strLinetypeName & "' does not exist" End Sub Ngoi ra ta c th by li kim tra s tn ti ca mt LineType : Public Sub CheckForLinetypeByException() Dim strLinetypeName As String Dim objLinetype As AcadLineType strLinetypeName = InputBox("Enter a Linetype name to search for: ") If "" = strLinetypeName Then Exit Sub ' exit if no name entered On Error Resume Next ' handle exceptions inline Set objLinetype = ThisDrawing.Linetypes(strLinetypeName) If objLinetype Is Nothing Then ' check if obj has been set MsgBox "Linetype '" & strLinetypeName & "' does not exist" Else MsgBox "Linetype '" & objLinetype.Name & "' exists" End If nd Sub Ti mt Linetype vo trong bn v C php Load lintype nh sau : Set LinetypeObject = LinetypesCollection.Load(LinetypeName, LinetypeFilename) NAME
LineTypeName LinetypeFilename

DATA TYPE String String

DESCRIPTION Tn ca linetype ng dn ca file cha LineType cn Load 65

V d : ThisDrawing.Linetypes.Load Hidden, "acad.lin" Chuyn Linetype thnh dng ng hin hnh S dng phng thc ActiveLinetype nh sau : DocumentObject.ActiveLinetype = LinetypeObject V d sau bin dng ng "TRACKS" thnh dng ng hin thnh ca bn v hin hnh : ThisDrawing.ActiveLinetype = ThisDrawing.Linetypes("TRACKS") i tn Linetype S dng thuc tnh Linetype.Name property, bn c th thay i tn ca mt dng ng. Deleting a Linetype Phng thc Linetype.Delete cho php bn xa mt i tng Linetype t Linetypes collection. LinetypeObject.Delete Ta khng th xa mt dng ng ra khi bn v khi : N l linetype hin hnh. N l ByLayer, ByBlock, or Continuous linetype. N l mt Xref-dependent linetype. Ly Hanhdle ca Linetype Dim objLinetype As AcadLinetype Dim strLinetypeHandle As String Set objLinetype = ThisDrawing.Linetypes("Center") strLinetypeHandle = objLinetype.Handle Thay i Description ca Linetype. AutoCAD Cho php bn read, add hoc modify description ca LineType bng cch s dng phng thc Description ca Linetype object. Dim strLineTypeDescription As String objLineType.Description = "Linetype Description: -.-.-." strLineTypeDescription = objLineType.Description The following example changes a Linetype description based on user input: Scaling Linetypes Bn c th s dng hai loi t l phng : global linetype scale (LTSCALE) v individual linetype scale (CELTSCALE).

Global Scale Dim dblNewLTScale As Double ThisDrawing.SetVariable "LTSCALE", 2# dblNewLTScale = ThisDrawing.GetVariable("LTSCALE") Individual Scale 66

Dim dblNewCELTScale As Double ThisDrawing.SetVariable "CELTSCALE", 2# dblNewCELTScale = ThisDrawing.GetVariable("CELTSCALE") 3. Gn layer, color, linetype cho i tng S dng cc thuc tnh sau : Object.Layer Object.Color Object.LineType VIII. Lm vic vi kch thc (2t) 1. Lm vic vi DimStyle Thm mt DimStyle Object. DimStyle object thit lp s xut hin ca mt nhm cc kch thc v cc leader. Cc DimStyle objects nm trong DimStyles collection, do vy chng ta c th truy cp n n thng qua phng thc Item ca DimStyles collection's. to mt DimStyle, chng ta s dng phng thc Add nh sau : Set DimStyleObject = DimStylesCollection.Add(DimStyleName) NAME DimStyleName V d : Dim objDimStyle As AcadDimStyle Set objDimStyle = ThisDrawing.DimStyles.Add("NewDimStyle") Thit lp mt Dimension Style thit lp mt DimStyle trong VBA, bn phi nm vng tt c cc bin h thng m thng qua n bn c th iu khin tt c cc TAB trong Dimension Style. Phng thc CopyFrom. Bn c th s dng phng thc CopyFrom copy mt DimStyle object, c php nh sau : DimStyleObject.CopyFrom SourceObject Thng s SourceObject l i tng DimStyle object c copy. Kiu Copy ph thuc vo ngunc Copy m bn s dng c lit k ra trong bng di y : OBJECT Dimension, Tolerance, Leader Document DimStyle STYLES COPIED VBA s copy tt c cc d liu v Dimension style ca kch thc, dung sai, du dn, k c cc d liu ghi (override) c trong cc dimension ny voDimStyleObject Copy Dimension style v override ca Dimension style hin hnh ca bn v c ch nh trong Document Copy Dimension style ca bn v hin hnh c ch nh trong DimStyle DATA TYPE String DESCRIPTION Tn ca DimStyle

V d sau to mt DimStyle mi, ly tn l NewDimStyle. DimStyle ny tha hng tt c cc thuc tnh ca DimStyle hin hnh, tr mu ca dimension line, extension line, v dimension text c t theo th t l red, blue, and white. Public Sub NewDimStyle Dim objDimStyle As AcadDimStyle Set objDimStyle = ThisDrawing.DimStyles.Add("NewDimStyle") SetVariable "DIMCLRD", acRed 67

SetVariable "DIMCLRE", acBlue SetVariable "DIMCLRT", acWhite SetVariable "DIMLWD ", acLnWtByLwDefault objDimStyle.CopyFrom ThisDrawing End Sub Gii thch nh sau : khi thay i cc thng s h thng ca Dimension style, Cad s to ra mt kiu kch thc ghi (override) da trn kiu kch thc hin hnh. on m objDimStyle.CopyFrom ThisDrawing s copy ton b thit lp ca Dimstyle hin hnh v override ca n vo NewDimStyle S dng Dimension Style Gn mt Dimstyle cho mt Dimension (mt bin kch thc) Object.StyleName = DimStyleName NAME
Object DimStyleName

DATA TYPE
Dimension, Leader, or Tolerance object

DESCRIPTION L object m bn mun p t Dimstyle cho n. Tn ca Dimstyle

String

Chuyn mt DimStyle thn Dimstyle hin hnh chuyn mt DimStyle thnh DimStyle hin hnh, bn s dng phng thc ActiveDimStyle nh sau : Set DocumentObject.ActiveDimStyle = DimStyleObject NAME
DimStyleObject

DATA TYPE
DimStyle object

DESCRIPTION

L bin kiu Dimstyle m bn mun chuyn n thnh Dimstyle hin hnh. V d sau hin th kiu kch thc hin hnh ca bn v hin hnh. Msgbox Kiu kch thc hin hnh : & ThisDrawing.ActiveDimStyle.Name Dim objDimStyle As AcadDimStyle Duyt qua cc Dimstyle c trong bn v hin hnh. For Each objDimStyle In ThisDrawing.DimStyles Msgbox objDimStyle.Name Next 2. To cc ng o kch thc To cc Dimensions Cng nh cc thc th ha khc, to mt ng o kch thc, ta s dng phng thc AddDimXXX. Phng thc ny c th p dng trong ModelSpace, PaperSpace v Block objects. Trong mc ny, chng ta s nghin cu cc loi kch thc sau : Dim3PointAngular Object DimAligned Object DimAngular Object DimDiametric Object DimOrdinate Object DimRadial Object DimRotated Object Dim3PointAngular Object Set Dim3PointAngularObject = Object.AddDim3PointAngular(VertexPoint, Point1, Point2, TextPosition) 68

NAME VertexPoint Point1 Point2 TextPosition

DATA TYPE Variant Variant Variant Variant

DESCRIPTION VertexPoint l mng 3 phn t kiu Double m t ta mt nh ca gc cn o trong h ta WCS. Point1 l mng 3 phn t kiu Double m t ta mt trong 2 im cui trong h ta WCS. Point2 l mng 3 phn t kiu Double m t ta mt trong 2 im cui trong h ta WCS. TextPosition l mng 3 phn t kiu Double m t ta v tr m Text (gc o c) s hin th h ta WCS.

DimAligned Object Set DimAlignedObject = Object.AddDimAligned(Point1, Point2, TextPosition) NAME Point1 Point2 TextPosition DATA TYPE Variant Variant Variant DESCRIPTION Point1 l mng ta 3 phn t kiu Double m t ta mt trong 2 im cui trong h ta WCS. Point2 l mng 3 phn t kiu Double m t ta mt trong 2 im cui trong h ta WCS. TextPosition l mng 3 phn t kiu Double m t ta v tr m Text s hin th h ta WCS.

DimAngular Object Set DimAngularObject = Object.AddDimAngular(Vertex, Point1, Point2, TextPosition) 69

NAME
Vertex Point1 Point2 TextPosition

DATA TYPE Variant Variant Variant Variant

DESCRIPTION VertexPoint l mng 3 phn t kiu Double m t ta mt nh ca gc cn o trong h ta WCS. Point1 l mng 3 phn t kiu Double m t ta mt trong 2 im cui trong h ta WCS. Point2 l mng 3 phn t kiu Double m t ta mt trong 2 im cui trong h ta WCS. TextPosition l mng 3 phn t kiu Double m t ta v tr m Text (gc o c) s hin th h ta WCS.

DimDiametric Object DimDiametricObject = Object.AddDimDiametric (ChordPoint1, ChordPoint2, LeaderLength) NAME ChordPoint1 ChordPoint2 DATA TYPE Variant Variant DESCRIPTION ChordPoint1 l mng 3 phn t kiu Double m t ta mt trong 2 im cui ca dy cung trong h ta WCS. ChordPoint2 l mng 3 phn t kiu Double m t ta mt trong 2 im cui ca dy cung trong h ta WCS. Khong cch t im cui ca dy cung ChordPoint1 ti v tr ca gi tr Text o c.

LeaderLength Double

DimOrdinate Object 70

Set DimOrdinateObject = Object.AddDimOrdinate(DefinitionPoint, LeaderEndPoint, UseXAxis) NAME DefinitionPoint DATA TYPE Variant DESCRIPTION DefinitionPoint l mng 3 phn t kiu Double m t ta trong h ta WCS. DefinitionPoint l mng 3 phn t kiu Double m t ta v tr ca Text trong h ta WCS. Xc nh DefinitionPoint c o theo phng trc X hay trc Y. Nu gi tr ny l True th, trc X ch s dng nh l mt tham chiu.

LeaderEndPoint Variant UseXAxis Boolean

DimRadial Object Set DimRadialObject = Object.AddDimRadial (CenterPoint, ChordPoint, LeaderLength) NAME CenterPoint DATA TYPE Variant DESCRIPTION CenterPoint l mng 3 phn t kiu Double m t ta tm ca ng trn hay cung trn cn o trong h ta WCS. ChordPoint l mng 3 phn t kiu Double m t ta im nm trn dy cung trong h ta WCS. Khong cch t Text n im ChordPoint

ChordPoint

Variant

LeaderLength Double

The DimRotated Object Set DimRotatedObject = Object.AddDimRotated(Point1, Point2, DimLocationPoint, RotationAngle)

71

NAME Point1

DATA TYPE Variant

DESCRIPTION Point1 l mng 3 phn t kiu Double m t ta mt trong 2 im cui ca on thng trong h ta WCS. Point2 l mng 3 phn t kiu Double m t ta mt trong 2 im cui ca on thng trong h ta WCS. DimLocationPoint l mng 3 phn t kiu Double m t ta im m ti CAD s v ng thng cha mi tn v Text trong h ta WCS. Gc hp bi ng ghi kch thc v trc X, n vi Radian.

Point2

Variant

DimLocationPoint Variant

RotationAngle

Double

3. To cc leader.

72

CHNG 4 : TY BIN MENUS V TOOLBARS (5 TIT)


I. C bn v menu group v toolbar (0.5t) Trong mc ny, chng ta s nghin cu nhng mc sau : Load, save, v unload mt menu group Gn cc phm tt (accelerator key) Thao tc vi cc menu bar To v chnh sa menu To v chnh sa toolbar Toolbar ng v chc nng Docking.

MenuGroups collection cha cc MenuGroup object, n cha tt c cc i tng thuc ToolBars v PopupMenus collections. MenuBar collection s cha tt c PopupMenu object ang xut hin trn AutoCAD menu bar. 1. Menugroup collection MenuGroups Collection Tt c cc menu c load trong session hin tai ca AutoCAD c lu ct trong MenuGroup collection. Cc menu ny c th c hin th hoc khng c hin th trn menu bar ca Autocad. Ngi dng c th iu khin s hin th ca cc menu thng qua lnh MenuLoad trong Autocad. Mi mt menu MenuGroup object cung cp tt c cc toolbar and pop-up menu c trong Group . Loading Menu Groups Phng thc Load ca MenuGroup collection dng ti mt menu group c cha trong cc file ( .mnc, .mns, or .mnu ) vo Autocad. C php ca lnh ny nh sau : Set MenuGroupObject = MenuGroupsCollection.Load (MenuFileName [,BaseMenu]) NAME DATA TYPE Boolean DESCRIPTION ng dn v tn File s c ti vo AutoCad. Thng s ny xc nh menu group c ti l menu c bn hay menu tng phn. Gia tr True l base menu. Ngc li s l partial menu. Mc nh l False.

MenuFileName String BaseMenu

S dng tham s BaseMenu vi gi tr True s tng ng vi vic thc hin lnh MENU trong AutoCAD (hoc thi hnh lnh MENULOAD v check vo la chn Replace All). Ch MenuGroup mi mi c Load v n s thay th tt c cc menu c trong AutoCad. Nh mt la chn, s dng phng thc Load Vi tham s BaseMenu l False, tng ng vi bn thi hnh lnh MENULOAD trong AutoCAD m khng check vo la chn. Replace All. Menu group c ti thm vo AtutoCad. Kiu ca Menu Groups ly thng s kiu ca menu groups c ti vo trong AutoCAD, bn c th s dng thuc tnh Type cho mi MenuGroup object. C php nh sau : lngMenuGroupType = MenuGroupObject.Type 73

CONSTANT AcBaseMenuGroup AcPartialMenuGroup

VALUE 0 1

DESCRIPTION Menu group l base menu group. Menu group l partial menu group.

V d sau s lit k tt c cc menu group oc ti vo trong AutoCad v kiu ca chng: Public Sub ListMenuGroups() Dim objMenuGroup As AcadMenuGroup Dim strMenuGroupNames As String strMenuGroupNames = "The following menu groups are currently loaded, " For Each objMenuGroup In Application.MenuGroups If objMenuGroup.Type = acBaseMenuGroup Then strMenuGroupNames = strMenuGroupNames & vbCrLf & _ objMenuGroup.Name & ": Base menu" Else strMenuGroupNames = strMenuGroupNames & vbCrLf & _ objMenuGroup.Name & ": Partial menu" End If Next MsgBox strMenuGroupNames End Sub Lu : MenuGroups collection khng c phng thc Add. Tuy nhin bn c th to mt bn copy file .mns vo mt file mi, sau ti v sa cha n theo ca bn. Bn cng c th to mt file .mnu hoc .mns mi v ti chng t VBA. Bn khng th chnh sa c cc menu hnh nh, menu mn hnh v cc bng s ha. Tuy nhin, bn vn c th Load v UnLoad chng bng VBA. 2. Menugroup Object Thnh phn ca MenuGroup Object Bn Load mt MenuGroup vo trong AutoCAD, tng ng vi mt MenuGroup object c thm vo MenuGroup collection. Mi MenuGroup object cha hai collection l PopupMenus and Toolbars. C php truy cp n 2 collection trn nh sau : Set PopupMenusCollection = MenuGroupObject.Menus Set ToolbarsCollection = MenuGroupObject.Toolbars Saving Menu Groups Bn C hai phng thc ghi lai : Save v SaveAs. MenuGroupObject.Save MenuFileType MenuGroupObject.SaveAs FileName, MenuFileType NAME MenuFileType FileName DATA TYPE Long String DESCRIPTION Xc nh kiu file s c ghi : File ngun hoc file c bin dch (chi tit bng di). ng dn y v tn file s ghi li.

CONSTANT

Cc hng s AcMenuFileType VALUE DESCRIPTION compiled menu file (.mnc extension) 74

acMenuFileCompiled 0

CONSTANT
acMenuFileSource

Cc hng s AcMenuFileType VALUE DESCRIPTION 1 source menu file (.mns extension)

Unloading Menu Groups Tng ng vi lnh MENULOAD hoc MENUUNLOAD trong AutoCad : MenuGroupObject.Unload II. Thay i menu bar (0.5t) Bn c th : Thm, bt, sp xp li cc menu trn menu bar. 1. Chn thm menu vo menubar Phng thc InsertInMenuBar ca PopupMenu object. PopupMenuObject.InsertInMenuBar(Index) NAME DATA TYPE Index Variant DESCRIPTION V tr m pop-up menu s c thm vo MenuBar. Index l kiu Integer t 0 n N. N l s lng object trn menu bar, hoc kiu string l tn ca mt menu tn ti (bao gm c h t & ca accelerator key), pop-up menu s c thm vo trc menu c tn nh trong index. Nu menu trong index khng tn ti th mt menu mi s c thm vo ti v tr cui cng ca menu bar.

InsertMenuInMenuBar method of a PopupMenus collection. PopupMenusCollection.InsertMenuInMenuBar MenuName, Index NAME DATA TYPE Variant Ging nh trn. DESCRIPTION Tn ca menu s c thm vo menu bar.

MenuName String Index

2. Xo b menu trn menubar Ging nh thm menu vo menu bar, bn cng c 2 cch xa menu khi chng : Phng thc RemoveFromMenuBar PopupMenuObject.RemoveFromMenuBar Sub RemoveMenus() Dim objMenu As AcadPopupMenu For Each objMenu In ThisDrawing.Application.MenuBar If MsgBox("Remove " & objMenu.Name & "?", vbYesNo) = vbYes Then objMenu.RemoveFromMenuBar Next End Sub PopupMenusCollection.RemoveMenuFromMenuBar Index NAME DATA TYPE
Index

DESCRIPTION Ging nh trong Phng thc InsertMenuInMenuBar.

Variant

75

3. Sp xp li cc menu trn menubar III. To v chnh sa Pull-down v Shorcut menus (1,5t) 1. To mi Pull-down menu

Set PopupMenuObject = PopupMenusCollection.Add(MenuName) NAME DATA TYPE DESCRIPTION Tn ca PopupMenu object MenuName String

MenuName c th cha k t (&), dng dnh ngha accelerator key. 2. Chn mt menu Item vo Pull-down menu Set PopupMenuItemObject = PopupMenuObject.AddMenuItem(Index, Label, Macro) NAME DATA TYPE Index Label Variant String DESCRIPTION Ging nh trong Phng thc InsertMenuInMenuBar. Label ca menu item. Label c th cha ngn ng DIESEL. Macro s thc hin khi menu c click.

Macro String

3. Chn mt khong trng vo Pull-down menu Set PopupMenuItemObject = PopupMenuObject.AddSeparator(Index) NAME DATA TYPE Index Variant DESCRIPTION Ging nh trong Phng thc InsertMenuInMenuBar. Nu menu item c xc nh bi tham s index khng tn ti, th khong trng s c t cui ca menu.

Ch : Bn khng th thm khong trng vo u ca Popup menu. 4. Thm mt Menu Item vo Shortcut Menu Gi tr ca thuc tnh ShortcutMenu ca PopupMenu object nhn gi tr True nu menu l shortcut menu. T , bn c th them mt item vo trong shortcut menu nh i vi drop-down menu. 5. To Submenu cho menu item Set PopupMenuObject = PopupMenuObject.AddSubMenu(Index, Label) NAME DATA TYPE Index Label Variant String DESCRIPTION Ging nh trong Phng thc InsertMenuInMenuBar Label ca menu item. Label c th cha ngn ng DIESEL. 76

6. Xa bt menu item S dng phng thc Delete nh sau : PopupMenuItemObject.Delete IV. To v chnh sa Toolbars (1,5t) 1. To mi toolbar Set ToolbarObject = ToolbarsCollection.Add(ToolbarName) NAME DATA TYPE DESCRIPTION Tn ca Toolbar object s c to

ToolbarName String

2. Thm mt nt chn vo toolbar Mt Toolbar button c i din bi ToolbarItem object. Bn c th s dng phng thc AddToolbarButton method thm mt Toolbar item vo mt v tr xc nh trn toolbar. Set ToolbarItemObject = ToolbarObject.AddToolbarButton(Index, ButtonName, HelpString, Macro[, FlyoutButton]) NAME Index DATA TYPE Variant DESCRIPTION V tr m pop-up menu s c thm vo MenuBar. Index l kiu Integer t 0 n N. N l s lng object trn menu bar, hoc kiu string l tn ca mt Toolbar button tn ti, button mi s c thm vo trc button c tn nh trong index. Nu menu trong index khng tn ti th mt button mi s c thm vo ti v tr cui cng ca toolbar. Tn ca toolbar button s c to. (ch chp nhn cc k t : alphanumeric, dashes (-), v underscores (_)). L chui ch thch Macro s thi hnh khi ta chn nut lnh. Nu thit lp l True th s to mt flyout button. Mc nh l False.

ButtonName String HelpString Macro String String

FlyoutButton Boolean

Ch : Toolbar button cng ging nh menu item. Khi bn mun thay i ch s Index ca chng. Bn khng c php thay i lun gi tr Index. M u tin, bn phi xa toolbar button. Sau bn thm n vo Toolbar ti v tr mi, v tr m bn mun thay i. 3. nh ngha hnh nh cho cc nt Toolbar Button. thit lp hay ti v bitmap ca icons gn vi nt lnh ca toolbar, bn c th s dng phng thc SetBitmaps v GetBitmaps. c 2 u c c php tng t nhau nh sau : ToolbarItemObject.SetBitmaps SmallIconName, LargeIconName ToolbarItemobject.GetBitmaps SmallIconName, LargeIconName NAME DATA TYPE DESCRIPTION Path v tn file ca small bitmap (1615 pixels) Path v tn file ca large bitmap(2422 pixels)
SmallIconName String LargeIconName String

4. Thm mt khong trng vo toolbar Set ToolbarItemObject = ToolbarObject.AddSeparator(Index) NAME DATA TYPE DESCRIPTION 77

NAME DATA TYPE


Index

DESCRIPTION

Variant V tr ca khong trng trn Toolbar Ch : Bn khng th thm khong trng vo u ca ToolBar 5. Floating v Docking Toolbars Floating Toolbars ToolbarObject.Float Top, Left, NumberOfRows NAME Top Left DATA TYPE Long Long DESCRIPTION V tr bin trn ca Toolbar (pixel) tnh t mn hnh pha trn. V tr bin di tnh t vi tr bin trn S dng m toolbar buttons c th phn b trn . Nu NumberOfRows nhiu hn s lng buttons c trong toolbar, tham s ny s b b qua.

NumberOfRows Long

V d :
Public Sub FloatDrawToolbar() Dim objToolBarDraw As AcadToolbar Set objToolBarDraw = ThisDrawing.Application.MenuGroups.Item("ACAD").Toolbars.Item("Draw") objToolBarDraw.Float 0, 0, 3 End Sub

Docking Toolbars ToolbarObject.Dock DockStatus NAME DockStatus DATA TYPE Long DESCRIPTION Gi tr ca n xem trong bng di y. Hng s : AcToolbarDockStatus VALUE DESCRIPTION 0 1 2 3 Toolbar c bm vo pha trn ca bn v. Toolbar c bm vo pha di ca bn v. Toolbar c bm vo bin tri di ca bn v. Toolbar c bm vo bin phi ca bn v.

CONSTANT acToolbarDockTop acToolbarDockBottom acToolbarDockLeft acToolbarDockRight

V d minh ha acToolbarDockBottom 78

6. To cc flyout toolbar S dng Set ToolbarItemObject = ToolbarObject.AddToolbarButton(Index, ButtonName, HelpString, Macro[, FlyoutButton]) vi tham s FlyoutButton l True. V d sau to hai toolbar. Ci u tin cha flyout button. Toolbar th 2 c gn vo flyout button ca toolbar u tin. Sub Ch6_AddFlyoutButton() Dim currMenuGroup As AcadMenuGroup ' Create the first toolbar Set currMenuGroup = ThisDrawing.Application.MenuGroups.Item(0) Dim FirstToolbar As AcadToolbar Set FirstToolbar = currMenuGroup.Toolbars.Add("FirstToolbar") ' Add a flyout button to the first menu on the menu bar Dim FlyoutButton As AcadToolbarItem ' Create the second toolbar. This will be attached to ' the first toolbar via the flyout button. Set FlyoutButton = FirstToolbar.AddToolbarButton("", "Flyout", "Demonstrates a flyout button","OPEN", True) Dim SecondToolbar As AcadToolbar ' Add a button to the next toolbar Set SecondToolbar = currMenuGroup.Toolbars.Add("SecondToolbar") Dim newButton As AcadToolbarItem Dim openMacro As String ' Assign the macro the VB equivalent of "ESC ESC _open " openMacro = Chr(vbKeyEscape) + Chr(vbKeyEscape) + "_open " Set newButton = SecondToolbar.AddToolbarButton ("", "NewButton", "Open a file.", openMacro) ' Attach the second toolbar to the flyout button on the first toolbar FlyoutButton.AttachToolbarToFlyout currMenuGroup.Name,SecondToolbar.Name ' Display the first toolbar, hide the second toolbar FirstToolbar.Visible = True SecondToolbar.Visible = False End Sub 7. Xa Toolbar v Toolbar Button Bn c th xa c toolbars v toolbar buttons bng phng thc Delete nh sau : 79

ToolbarObject.Delete ToolbarItemObject.Delete V. To cc macro (1t) 1. Cc quy nh v macro Bng k t dng trong Macro v m ASCII tng ng.

Character ; ^M ^|

ASCII chr(59) chr(13) chr(94) + chr(124) SPACEBAR chr(32) \ _ + =* *^C^C $ ^B ^C ESC ^D ^E ^G ^H ^O ^Q ^T ^V ^Z chr(92) chr(95) chr(43) chr(61) + chr(42) chr(42) + chr(3) + chr(3) chr(36) chr(2) chr(3) chr(3) chr(4) chr(5) chr(7) chr(8) chr(15) chr(17) chr(20) chr(22) chr(26)

M t ENTER ENTER TAB Khong trng trong Macro s tng ng vi bn n phm SPACEBAR Dng li nhp s liu Chuyn lnh ng sau n sang Ting Anh Tip tc macro dng lnh tip theo(t cui dng lnh trc) Hin th hin hnh top-level image, pull-down, v shortcut menu Dng lp li cc lnh ng sau n cho n khi bn n Esc ti mt on menu section hoc thm mt on biu thc DIESEL Bt hoc tt Snap (CTRL+B) Cancels command (CTRL+C) Cancels command (ESC) Bt tt Coords (CTRL+D) Thit lp mt phng cng kch thc (CTRL+E) Bt tt Grid (CTRL+G) Tr v phm backspace Bt tt ch Ortho (CTRL+O) Khng hin th tt c cc thng bo, cc dng tnh trng lm v d liu nhp vo (CTRL+Q) Bt tt menu s ha Tablet (CTRL+T) Changes current viewport (CTRL+V) Null character t cui Macro xa cc k t pht sinh cui ca menu Item.

Ch v k t kt thc macro nh sau. Nu bn kt thc macro m khng c cc k t c bit kt thc macro nh ; th AutoCAD s t ng thm vo cui macro mt phm cch. Tuy nhin, i vi cc lnh Text th phm cch ny s tr v mt k t trng ca text nhp vo. Do vy ta cn nm chc quy tc sau : Khi c mt du chm phy trong macro, AutoCAD s thay th n bng phm ENTER. Nu dng macro kt thc bng cc k t (\), (+), hoc (;), AutoCAD s khng thm mt khong trcngs sau n. 2. Cc v d v macro

80

CHNG 5 : PHT TRIN NG DNG VI VBA (5 TIT)


AutoCAD Events (1.5 Tit) Event xut hin nh mt kt qu ca mt s kin xy ra khi chng trnh ang chy. Autocad cung cp 3 cp Event : application, document, and object. Event handlers (b qun l s kin) l mt Sub procedures, th tc ny s c chy mt cch t ng khi cc s kin gn vi n xy ra. Application-Level Events : Xut hin khi c s thay i trong mi trng ng dng AutoCAD. N bao gm cc s kin m bn v, ng bn v, thc thi dng lnh, thay i cc bin h thng v thay i ca s ng dng AutoCAD. Document-Level Events : Xut hin khi c s thay i trong bn v nh thm, sa mt i tng, ti sinh li bn v. Object-Level Events : xut hin khi c s thay i cp thc th trong bn v, nh s kin thay i (Modified). 1. Application-level events Application-level events khng mc nh c trong VBA khi bn load mt Project. s dng cc Event ny, bn phi lm qua cc bc sau : u tin, bn insert mt class module, v khai bo mt declare an object vi kiu AcadApplication vi KeyWord Withevents. V d : Bn to mt Class module c tn l EventClassModule Public WithEvents objApp As AcadApplication I.

Sau khi bn khai bo Object With event, v bn c th vit event procedures cho object mi trong class module. (khi bn chn object by trong Object box, tt c cc events hp l s c hin th trong Procedure drop-down list box nh hnh v trn) Sau bn connect ti Application object 1. Trong ca s cha main module, bn thm cc dng khai bo sau : Dim X As New EventClassModule 2. Cng trong ca s ny, bn thm th tc sau : Sub InitializeEvents() Set X.ObjApp = ThisDrawing.Application End Sub 3. Trong main module, bn gi th tc InitializeEvents : Call InitializeEvents V d sau : Public WithEvents ACADApp As AcadApplication Sub Example_AcadApplication_Events() ' This example intializes the public variable (ACADApp) ' which will be used to intercept AcadApplication Events ' 81

' Run this procedure FIRST! ' We could get the application from the ThisDocument ' object, but that would require having a drawing open, ' so we grab it from the system. Set ACADApp = GetObject(, "AutoCAD.Application.16") End Sub Private Sub ACADApp_BeginFileDrop _ (ByVal FileName As String, Cancel As Boolean) ' This example intercepts an Application BeginFileDrop event. ' ' This event is triggered when a drawing file is dragged ' into AutoCAD. ' ' To trigger this example event: ' 1) Make sure to run the example that initializes ' the public variable (named ACADApp) linked to this event. ' ' 2) Drag an AutoCAD drawing file into the AutoCAD ' application from either the Windows Desktop ' or Windows Explorer ' Use the "Cancel" variable to stop the loading of the ' dragged file, and the "FileName" variable to notify ' the user which file is about to be dragged in. If MsgBox("AutoCAD is about to load " & FileName & vbCrLf _ & "Do you want to continue loading this file?", _ vbYesNoCancel + vbQuestion) <> vbYes Then Cancel = True End If End Sub Danh sch cc Event trong application level: AppActivate AppDeactivate ARXLoaded ARXUnloaded BeginCommand BeginFileDrop BeginLisp BeginModal BeginOpen BeginPlot BeginQuit BeginSave EndCommand EndLisp EndModal EndOpen EndPlot EndSave LispCancelled NewDrawing SysVarChanged WindowChanged WindowMovedOrResized 82

2. Document-level events Khng ging nh application-level events, document-level events mc nh c khi bn ti Project vo bn v. Nu bn chn AcadDocument Object trong Object List box ca Thisdrawing module ca AutoCAD project, the document-level events c lit k trong danh sch bn phi nh hnh v di y :

Cc Event c s dng trong document level nh sau : Activate BeginClose BeginCommand BeginDoubleClick BeginLisp BeginPlot BeginRightClick BeginSave BeginShortcutMenuCommand BeginShortcutMenuDefault BeginShortcutMenuEdit BeginShortcutMenuGrip BeginShortcutMenuOsnap Deactivate EndCommand EndLisp EndPlot EndSave EndShortcutMenu LayoutSwitched LispCancelled ObjectAdded ObjectErased ObjectModified SelectionChanged WindowChanged WindowMovedOrResized 3. Object-Level Events s dng object-level events, u tin bn phi to mi mt class module v khai bo bin tham chiu n i tng m bn mun by s kin Modified. Class module mi cha khai bo i tng bng cch s dng VBA keyword WithEvents, v d : 83

Public WithEvents objLine As AcadLine t tn Class module l EventClassModule, sau vit Code cho Event : 1. Trong ca s cha main module, bn thm cc dng khai bo sau : Dim X As New EventClassModule 2. Cng trong ca s ny, bn thm th tc sau : Public Sub InitializeEvent() Dim dblStart(2) As Double Dim dblEnd(2) As Double dblEnd(0) = 1: dblEnd(1) = 1: dblEnd(2) = 0 Set X.objLine = ThisDrawing.ModelSpace.AddLine(dblStart, dblEnd) End Sub 3. Trong main module, bn gi th tc InitsializeEvents : Call InitializeEvents V d sau y to mt polyline vi Event Public WithEvents PLine As AcadLWPolyline Sub CreatePLineWithEvents() ' This example creates a light weight polyline Dim points(0 To 9) As Double points(0) = 1: points(1) = 1 points(2) = 1: points(3) = 2 points(4) = 2: points(5) = 2 points(6) = 3: points(7) = 3 points(8) = 3: points(9) = 2 Set PLine = ThisDrawing.ModelSpace. AddLightWeightPolyline(points) PLine.Closed = True ThisDrawing.Application.ZoomAll End Sub Private Sub PLine_Modified (ByVal pObject As AutoCAD.IAcadObject) ' This event is triggered when the polyline is resized. ' If the polyline is deleted the modified event is still ' triggered, so we use the error handler to avoid ' reading data from a deleted object. On Error GoTo ERRORHANDLER MsgBox "The area of " & pObject.ObjectName & " is: " _ & pObject.Area Exit Sub ERRORHANDLER: MsgBox Err.Description End Sub II. S dng Form (1t) 1. Lm vic vi Form v Macro To mi mt form, chy form trong ch run_mode Ging ht VB Chn controls vo form Khng khc VB n, hin th form 84

Ging ht VB Load v Unload form Ging y nh VB S dng Modal form y l im khc ca VBA vi VB. Ch Modal form cho php bn cng mt lc lm vic vi c Form v c mn hnh ca CAD nh vic Pick chut, nh lnh, 2. Lm vic vi module v macro (0.5t) Chy macro t toolbar v menu Bn c th chy macro t AutoCAD toolbar hoc menu bng cch thay i menu Macro property cho toolbar hoc menu . Macro property c dng : -VBARUN filename.dvb!modulename.macroname Trong : - filename l tn ca project file. - modulename l tn ca module cha macro cn chy. - macroname l tn ca macro. V d : -VBARUN Project1.dvb!module1.thongkethep Ti Project mt cch t ng C 2 cch Load Project mt cch t ng : 1. Khi VBA c ti, n s tm kim trong AutoCAD mt project c tn l acad.dvb. File ny s c ti mt cch mc nh nh mt default project 2. Cng ging nh VBA, Autolisp cng c mt file c tn l acad.lsp cng c ti mt cch t ng khi bn v mi xut hin. Dng lnh sau trong file acad.lsp dng ti project myproj.dvb vo trong bn v mi khi mt bn v mi c m (defun S::STARTUP() (command "_VBALOAD" "myproj.dvb") ) Chy macro mt cch t ng C 2 cch chay macro mt cch t ng : 1. Bn c th chy cc macro mt cch t ng, bng cch son tho AutoCAD startup ca acad.lsp. V d, t ng chy Macro c tn drawline, u tin bn copy on macro vo trong file project acad.dvb . Sau , m notepad.exe v son on sau : (defun S::STARTUP() (command "_-vbarun" "drawline") ) 2. Trong Project acad.dvb, macro c tn AcadStartup s t ng c chay khi VBA load. III. Tng tc vi cc ng dng v cc c s d liu khc (0.5t) 1. Tng tc vi Visual Lisp T c 2. S dng c s d liu DAO T c, ging VB 3. Giao tip vi cc ng dng khc. trao i thng tin gia cc Activex Model, ta lm theo cc bc sau : 1. Tham chiu n ActiveX Object Model ca ng dng cn giao tip vi. 85

2. To mt instance ca ng dng 3. Vit Code bng cch s dng c AutoCAD Object Model v Object Model ca ng dng ngoi. Tham chiu n ActiveX Object Library ca ng dng khc Vic u tin khi bn trao i thng tin vi cc ng dng ngoi l tham chin n ActiveX Object Library, v n cha tt c cc i tng, phng thc, thuc tnh, hng s v s kin do ng dng nh ngha Thng thng, kt ni vi cc Activex Library, bn chn Tools menu References. , sau mt danh sch cc object librarie VBA tm thy trong h thng ca bn s c hin ln. Sau bn chn nhng Library m bn cn trao i d liu ti n. V d, bn thm object library ca Microsoft Excel, chn Microsoft Excel object library entry trong danh sch. Khi bn to tham chiu n object library ca ng dng ngoi, bn c th s dng VBA Object Browser xem danh sch cc object ca ng dng m bn tham chiu n. C th nh sau : 1. In the VBA IDE, open the Tools menu and select the References menu option. 2. Find and select the entry in the list of Available References for the application you want to access. 3. Select OK to close the dialog box with your changes.

To mt Instance ca ng dng ngoi Sau khi tham chiu n Object library, bn phi to mt instance ca ng dng . Cc bc thc hin nh sau : 1. Bn khai bo mt bin i din cho application. V d, khai bo mt bin i din cho Excel.Application : Dim ExcelAppObj as Excel.Application 2. sau bn to mt instance cho application. V d : Set ExcelAppObj = New Excel.Application Vit Code cho Objects t ng dng ngoi V d sau y iu khin s hin h ca Excel sau khi tao Instance cho n : ExcelAppObj.Visible = TRUE Bn c th s dng VBA Object Browser tm cc i tng v s dng help file c thm v bt k mt Object Model m bn tham chin n. Thot khi ng dng 86

Khi ng dng ngoi chy, n chim mt b nh nht nh trong my tnh. Sau khi khng s dng n chng trnh na, tt nht bn nn tt tt c cc ng dng m bn to Instance cho n. V d, thot session Excel nh sau : ExcelAppObj.Application.Quit List AutoCAD attributes on an Excel spreadsheet V d : th tc sai tm tt c cc block references trong bn v hin hnh. Lit k tt c cc attributes ca cc block references : Sub Ch12_Extract() Dim Excel As Excel.Application Dim ExcelSheet As Object Dim ExcelWorkbook As Object Dim RowNum As Integer Dim Header As Boolean Dim elem As AcadEntity Dim Array1 As Variant Dim Count As Integer ' Launch Excel. Set Excel = New Excel.Application ' Create a new workbook and find the active sheet. Set ExcelWorkbook = Excel.Workbooks.Add Set ExcelSheet = Excel.ActiveSheet ExcelWorkbook.SaveAs "Attribute.xls" RowNum = 1 Header = False ' Iterate through model space finding ' all block references. For Each elem In ThisDrawing.ModelSpace With elem ' When a block reference has been found, ' check it for attributes If StrComp(.EntityName, "AcDbBlockReference", 1) = 0 Then If .HasAttributes Then ' Get the attributes Array1 = .GetAttributes ' Copy the Tagstrings for the ' Attributes into Excel For Count = LBound(Array1) To UBound(Array1) If Header = False Then If StrComp(Array1(Count).EntityName, _ "AcDbAttribute", 1) = 0 Then ExcelSheet.Cells(RowNum, Count + 1).value = _ Array1(Count).TagString End If End If Next Count RowNum = RowNum + 1 For Count = LBound(Array1) To UBound(Array1) ExcelSheet.Cells(RowNum, Count + 1).value = Array1(Count).textString Next Count Header = True End If End If End With Next elem 87

Excel.Application.Quit End Sub S dng GetObject, v NewObject Nh trnh by trng I Cch lp trnh AutoCAD VBA trong Excel, Bn cng th GetObject, v NewObject connect ti cc ng dng khc

Bng lp ng dng APPLICATION CLASS IDENTIFICATION Excel Word Excel.Application.x (x l product version) Word.Application.x (x l version index)

Microsoft Product Versions PRODUCT VERSION EXAMPLE Office 95 Office 97 Office 2000 Office XP Office 2003 7 8 9 10 11 Word.Application.7 Excel.Application.8 Word.Application.9 Powerpoint.Application.10 Outlook.Application.11

V d : Public Sub StartExcel(App As Excel.Application, Visible As Boolean) 'handle errors inline On Error Resume Next Set App = GetObject(, "Excel.Application") 'depends on application 'check to see if application is running If Err Then 'no, application will need to be started Err.Clear Set App = CreateObject("Excel.Application") 'depends on application 'check to see if application was started If Err Then 'no, application could not be started - exit Exit Sub End If End If 'set the application visibility App.Visible = Visible End Sub Kim tra xem connect c vi Excel hay cha :
Public Sub Start() Dim oExcel As Excel.Application 'attempt to start Excel StartExcel oExcel, True If Not oExcel Is Nothing Then MsgBox "Success" Else MsgBox "Could not start Excel, exiting ...", vbCritical

88

Exit Sub End If End Sub

IV.

Lm vic vi Xdata (1 tit)

1. Khi nim v XData Xdata l d liu nh km vi cc thc th. Xdata c th c hay khng c. Ngi dng thng dng n ghi thm cc thng tin m rng v thc th. V d, mt s hiu thp ghi trong bn v b tng thng c s hiu thp, phi, khong cch gia cc thanh. Nhng khng c di thanh . Ta c th dng Xdata ghi thm di thp. Extended data gii hn 16K cho mi thc th entity. Extended data gm cc group code DXF t 1000 n 1071. AutoCAD cha cc goup v d liu trong n nhng khng s dng chng. Bn c th tham kho Group code v kiu d liu ca tng Group code trong bng DFX Code. Sau y l mt s Group code in hnh : String Group Code th 1000. Strings trong extended data c di ti a l 255 bytes (vi byte th 256 dnh cho null character). Application Name Group Code th 1001 (cng l gi tr kiu string). Application names c di ti a l 31 bytes (byte th 32 dnh cho null character) v phi tun theo quy tc nh tn (nh tn ca layer). Mt application name c th cha cc ch ci nh : s, v cc k t c bit nh $, -, v _. Nhng khng c php cha khong trng. Layer Name Group code th 1003. Tn ca mt layer gn lin vi Xdata. Database -Handle Group code th 1005. Handle ca entity trong drawing database. 3D Point Group code th 1010. 3 gi tr kiu Real, l ta mt im. Real Group code th 1040. Mt s thc. Integer Group code th 1070. Mt s 16-bit integer. Long Group code th 1071. Mt s 32-bit long. Nu gi tr ny c gn cho mt s short integer hoc real, th n s c quy i v s kiu long integer. Nu gi tr khng hp l(v d nh kiu string), n c quy i v s long zero (0L). Control String Group code th 1002. Xdata control string can be either "{" or "}". These braces enable the application to organize its data by subdividing it into lists. The left brace begins a list, and the right brace terminates the most recent list. Lists can be nested. Note If a 1001 group appears within a list, it is treated as a string and does not begin a new application group. Binary Data 1004. Binary data that is organized into variable-length chunks, which can be handled in ObjectARX with the ads_binary structure. The maximum length of each chunk is 127 bytes. Note AutoLISP cannot directly handle binary chunks, so the same precautions that apply to long (1071) groups apply to binary groups as well. 89

World Space Position 1011. Unlike a simple 3D point, the WCS coordinates are moved, scaled, rotated, and mirrored along with the parent entity to which the extended data belongs. The WCS position is also stretched when the STRETCH command is applied to the parent entity and when this point lies within the select window. World Space -Displacement 1012. A 3D point that is scaled, rotated, or mirrored along with the parent, but not stretched or moved. World -Direction 1013. A 3D point that is rotated or mirrored along with the parent, but not scaled, stretched, or moved. The WCS direction is a normalized displacement that always has a unit length. Distance 1041. A real value that is scaled along with the parent entity. Scale Factor 1042. Also a real value that is scaled along with the parent. 2. Sets the extended data (XData) associated with an object. C php nh sau object.SetXData XDataType, XData Trong : Object : Tt c cc thc th bn v , AttributeReference, Block, Dictionary, DimStyle, Group, Layer, Linetype, PlotConfigurations, RegisteredApplication, TextStyle, UCS, View, Viewport; Xrecord. XdataType : Variant (array of short) input-only Xdata : Array of Variant; input-only 3. Gets the extended data (XData) associated with an object. C php nh sau object.GetXData AppName, XDataType, XDataValue

Object : Tt c cc thc th bn v , AttributeReference, Block, Dictionary, DimStyle, Group, Layer, Linetype, PlotConfigurations, RegisteredApplication, TextStyle, UCS, View, Viewport; Xrecord. AppName : String; input-only, chui rng s ta v tt c cc d liu c gn vi i tng. XdataType : Variant (array of short) Output-only Xdata : Array of Variant; Output -only

4. Cc V d V d 1 : Sub Ch10_AttachXDataToSelectionSetObjects() ' Create the selection set Dim sset As Object Set sset = ThisDrawing.SelectionSets.Add("SS1") ' Prompt the user to select objects sset.SelectOnScreen ' Define the xdata Dim appName As String, xdataStr As String appName = "MY_APP" xdataStr = "This is some xdata" 90

Dim xdataType(0 To 1) As Integer Dim xdata(0 To 1) As Variant ' Define the values for each array 1001 indicates the appName xdataType(0) = 1001 xdata(0) = appName '1000 indicates a string value xdataType(1) = 1000 xdata(1) = xdataStr ' Loop through all entities in the selection set and assign the xdata to each entity Dim ent As Object For Each ent In sset ent.SetXData xdataType, xdata Next ent End Sub V d 2 : Sub Example_SetXdata() ' V d sau to ra mt ng line v gn extended data cho n. ' Create the line Dim lineObj As AcadLine Dim startPt(0 To 2) As Double, endPt(0 To 2) As Double startPt(0) = 1#: startPt(1) = 1#: startPt(2) = 0# endPt(0) = 5#: endPt(1) = 5#: endPt(2) = 0# Set lineObj = ThisDrawing.ModelSpace.AddLine(startPt, endPt) ZoomAll ' Initialize all the xdata values. Note that first data in the list should be ' application name and first datatype code should be 1001 Dim DataType(0 To 9) As Integer Dim Data(0 To 9) As Variant Dim reals3(0 To 2) As Double Dim worldPos(0 To 2) As Double DataType(0) = 1001: Data(0) = "Test_Application" DataType(1) = 1000: Data(1) = "This is a test for xdata" DataType(2) = 1003: Data(2) = "0" ' layer DataType(3) = 1040: Data(3) = 1.23479137438413E+40 ' real DataType(4) = 1041: Data(4) = 1237324938 ' distance DataType(5) = 1070: Data(5) = 32767 ' 16 bit Integer DataType(6) = 1071: Data(6) = 32767 ' 32 bit Integer DataType(7) = 1042: Data(7) = 10 ' scaleFactor reals3(0) = -2.95: reals3(1) = 100: reals3(2) = -20 DataType(8) = 1010: Data(8) = reals3 ' real worldPos(0) = 4: worldPos(1) = 400.99999999: worldPos(2) = 2.798989 DataType(9) = 1011: Data(9) = worldPos ' world space position ' Attach the xdata to the line lineObj.SetXData DataType, Data ' Return the xdata for the line Dim xdataOut As Variant Dim xtypeOut As Variant lineObj.GetXData "", xtypeOut, xdataOut 91

End Sub V d 3 : V d sau hin th tt c cc xdata trong selection set Sub Ch10_ViewXData() ' Find the selection created in previous example Dim sset As Object Set sset = ThisDrawing.SelectionSets.Item("SS1") ' Define the xdata variables to hold xdata information Dim xdataType As Variant Dim xdata As Variant Dim xd As Variant 'Define index counter Dim xdi As Integer xdi = 0 ' Loop through the objects in the selection set and retrieve the xdata for the object Dim msgstr As String Dim appName As String Dim ent As AcadEntity appName = "MY_APP" For Each ent In sset msgstr = "" xdi = 0 ' Retrieve the appName xdata type and value ent.GetXData appName, xdataType, xdata ' If the xdataType variable is not initialized, there ' was no appName xdata to retrieve for that entity If VarType(xdataType) <> vbEmpty Then For Each xd In xdata msgstr = msgstr & vbCrLf & xdataType(xdi) _ & ": " & xd xdi = xdi + 1 Next xd End If ' If the msgstr variable is NULL, there was no xdata If msgstr = "" Then msgstr = vbCrLf & "NONE" MsgBox appName & " xdata on " & ent.ObjectName & ":" & vbCrLf & msgstr Next ent End Sub V d 4 : V d sau, lc tt c cc vng trn cha xdata ca MY_APP application: Sub Ch4_FilterXdata() Dim sstext As AcadSelectionSet Dim mode As Integer Dim pointsArray(0 To 11) As Double mode = acSelectionSetWindowPolygon pointsArray(0) = -12#: pointsArray(1) = -7#: pointsArray(2) = 0 pointsArray(3) = -12#: pointsArray(4) = 10#: pointsArray(5) = 0 pointsArray(6) = 10#: pointsArray(7) = 10#: pointsArray(8) = 0 92

pointsArray(9) = 10#: pointsArray(10) = -7#: pointsArray(11) = 0 Dim FilterType(1) As Integer Dim FilterData(1) As Variant Set sstext = ThisDrawing.SelectionSets.Add("SS9") FilterType(0) = 0 FilterData(0) = "Circle" FilterType(1) = 1001 FilterData(1) = "MY_APP" sstext.SelectByPolygon mode, pointsArray, FilterType, FilterData V. Lm vic vi Xrecord (1 tit).

1. Khi nim v Xrecord Xrecord l i tng thuc Collection Dictionaries. Khi nim v Xrecord cng tng t nh Xdata, nhng Xrecord khng b gii hn v kch thc cng nh th t v n c th cha bt k loi d liu no. Khc vi Xdata, XRecords cha d liu trong standard AutoCAD group codes, tc l khng phi cha trong phn m rng nh Xdata. Cc group code ny nh hn 1000. Tt c cc standard AutoCAD group codes u c s dng cha d liu. Mt iu quan trng na l ta c th qun l Xrecord bng object IDs. V d : Cc bc AutoCAD ghi li cc thng tin ca mt Layer nh sau : To ra mt t in m rng (Extension dictionary) trong layer collection. To mt Dictionary object ly tn l ACAD_LAYERSTATE trong extension dictionary. Lu tt c cc thuc tnh ca layer vo mt XRecord object trong ACAD_LAYERSTATE dictionary. AutoCAD lu tt c cc thit lp ca cc layer vo Xrecord nu bn chn chc nng Save. Khi bn khi phc li (restore the layer setting, AutoCAD s ly d liu t XRecord khi phc li).

Nh vy, ta cng c th ghi li cc d liu cn thit trong qu trnh lp trnh s l bn v bng cch s dng Xrecord. Nhng Group code sau c th c s dng trong XRecord objects: 100 : Subclass marker (AcDbXrecord) 1-369 : (except 5 and 105) XRecord obects c ghi li cng vi bn v, v chng c th c truy cp mt cch trc tip bi ObjectARX v LISP. Do vy, nu bn cn bo mt d liu, bn phi m ha chng. : AcadXRecord VBA class name: Create using: Access via: 2. Phng thc AddXRecord RetVal = object.AddXRecord(Keyword) 93 Dictionary.AddXRecord Dictionary.Item

Object : kiu Dictionary, i tng s cha XRecord. Keyword : String, input-only. L tn ca XRecord trong dictionary. RetVal : XRecord object Sub Example_AddXRecord() ' This example creates a new XRecord if one doesn't exist, ' appends data to the XRecord, and then reads it back. To see data being added, ' run the example more than once. Dim TrackingDictionary As AcadDictionary, TrackingXRecord As AcadXRecord Dim XRecordDataType As Variant, XRecordData As Variant Dim ArraySize As Long, iCount As Long Dim DataType As Integer, Data As String, msg As String ' Unique identifiers to distinguish this XRecordData from other XRecordData Const TYPE_STRING = 1 Const TAG_DICTIONARY_NAME = "ObjectTrackerDictionary" Const TAG_XRECORD_NAME = "ObjectTrackerXRecord" ' Connect to the dictionary in which to store the XRecord On Error GoTo CREATE Set TrackingDictionary = ThisDrawing.Dictionaries(TAG_DICTIONARY_NAME) Set TrackingXRecord = TrackingDictionary.GetObject(TAG_XRECORD_NAME) On Error GoTo 0 ' Get current XRecordData TrackingXRecord.GetXRecordData XRecordDataType, XRecordData ' If there is no array yet then create one If VarType(XRecordDataType) And vbArray = vbArray Then ArraySize = UBound(XRecordDataType) + 1 ' Get the size of the data elements returned ArraySize = ArraySize + 1 ' Increase to hold new data ReDim Preserve XRecordDataType(0 To ArraySize) ReDim Preserve XRecordData(0 To ArraySize) Else ArraySize = 0 ReDim XRecordDataType(0 To ArraySize) As Integer ReDim XRecordData(0 To ArraySize) As Variant End If ' Append new XRecord Data ' For this sample we only append the current time to the XRecord XRecordDataType(ArraySize) = TYPE_STRING: XRecordData(ArraySize) = _ CStr(Now) TrackingXRecord.SetXRecordData XRecordDataType, XRecordData ' Read back all XRecordData entries TrackingXRecord.GetXRecordData XRecordDataType, XRecordData ArraySize = UBound(XRecordDataType) ' Retrieve and display stored XRecordData For iCount = 0 To ArraySize ' Get information for this element DataType = XRecordDataType(iCount) Data = XRecordData(iCount) If DataType = TYPE_STRING Then msg = msg & Data & vbCrLf 94

End If Next MsgBox "The data in the XRecord is: " & vbCrLf & vbCrLf & msg, vbInformation Exit Sub CREATE: ' Create the objects that hold this XRecordData If TrackingDictionary Is Nothing Then ' Make sure to have tracking object Set TrackingDictionary = _ ThisDrawing.Dictionaries.Add(TAG_DICTIONARY_NAME) Set TrackingXRecord = _ TrackingDictionary.AddXRecord(TAG_XRECORD_NAME) End If Resume End Sub 3. Phng thc SetXRecordData object.SetXRecordData XRecordDataType, XRecordData Object : kiu XRecord Object, i tng s cha d liu. XrecordDataType : Variant (array of short); input-only. The following group codes are common to all XRecord objects: Group codes 100 Description Subclass marker (AcDbXrecord)

1-369 (except 5 and 105) Gi tr c th c s dng bi bt k ng dng no. 4. Phng thc GetXRecordData object.GetXRecordData XRecordDataType, XRecordDataValue Object : kiu XRecord Object, i tng s cha d liu. XrecordDataType : Variant (array of short); Output-only. Sub Example_SetXRecordData() ' This example creates a new XRecord if one doesn't exist, ' appends data to the XRecord, and reads it back. To see data being added, ' run the example more than once. Dim TrackingDictionary As AcadDictionary, TrackingXRecord As AcadXRecord Dim XRecordDataType As Variant, XRecordData As Variant Dim ArraySize As Long, iCount As Long Dim DataType As Integer, Data As String, msg As String ' Unique identifiers to distinguish our XRecordData from other XRecordData Const TYPE_STRING = 1 Const TAG_DICTIONARY_NAME = "ObjectTrackerDictionary" Const TAG_XRECORD_NAME = "ObjectTrackerXRecord" ' Connect to the dictionary in which the XRecord is stored On Error GoTo CREATE Set TrackingDictionary = ThisDrawing.Dictionaries(TAG_DICTIONARY_NAME) Set TrackingXRecord = TrackingDictionary.GetObject(TAG_XRECORD_NAME) 95

On Error GoTo 0 ' Get current XRecordData TrackingXRecord.GetXRecordData XRecordDataType, XRecordData ' If there is no array already, create one If VarType(XRecordDataType) And vbArray = vbArray Then ArraySize = UBound(XRecordDataType) + 1 ' Get the size of the data elements returned ArraySize = ArraySize + 1 ' Increase to hold new data ReDim Preserve XRecordDataType(0 To ArraySize) ReDim Preserve XRecordData(0 To ArraySize) Else ArraySize = 0 ReDim XRecordDataType(0 To ArraySize) As Integer ReDim XRecordData(0 To ArraySize) As Variant End If ' Append new XRecord Data ' ' For this sample, we only append the current time to the XRecord XRecordDataType(ArraySize) = TYPE_STRING: XRecordData(ArraySize) = _ CStr(Now) TrackingXRecord.SetXRecordData XRecordDataType, XRecordData ' Read back all XRecordData entries TrackingXRecord.GetXRecordData XRecordDataType, XRecordData ArraySize = UBound(XRecordDataType) ' Retrieve and display stored XRecordData For iCount = 0 To ArraySize ' Get information for this element DataType = XRecordDataType(iCount) Data = XRecordData(iCount) If DataType = TYPE_STRING Then msg = msg & Data & vbCrLf End If Next MsgBox "The data in the XRecord is: " & vbCrLf & vbCrLf & msg, vbInformation Exit Sub CREATE: ' Create the objects that hold the XRecordData If TrackingDictionary Is Nothing Then ' Make sure the tracking object is there Set TrackingDictionary = _ ThisDrawing.Dictionaries.Add(TAG_DICTIONARY_NAME) Set TrackingXRecord = _ TrackingDictionary.AddXRecord(TAG_XRECORD_NAME) End If Resume End Sub

96

You might also like