vb6 trong autocad_phantuhuong

Download VB6 Trong AutoCad_PhanTuHuong

Post on 15-Jul-2015

315 views

Category:

Documents

2 download

Embed Size (px)

TRANSCRIPT

Xy dng m hnh t ng ho thit k trong AutoCad bng ngn ng lp trnh VB6 Phan T Hng, Trng i hc Kin trc H Ni Tm tt: Trong lnh vc thit k cng trnh xy dng, phn mm AutoCad c s dng rt ph bin v nhiu tnh nng u vit. Vi yu cu thit k i hi cht lng ngy cng cao, nhiu cng vic phi giao tip vi cc phn mm ng dng bn ngoi hoc thc hin lp i lp li, mt nhiu cng sc v d mc sai st. Cng vic c th hon ton thc hin t ng vi thi gian ngn, chnh xc cao nh ngn ng lp trnh VB6 (Visual Basic 6) trong mi trng AutoCad. Bi bo ny trnh by m hnh t ng ho thit k trong AutoCad bng ngn ng lp trnh VB6. M hnh t ng ho ny c ng dng trong nhiu phn mm thit k xy dng hin nay, nhng ti liu vit v m hnh lin kt ny rt him, k c cc ti liu nc ngoi. Hy vng bi bo ny s p ng nhu cu nhng ai mun tm hiu lnh vc t ng ho trong AutoCad bng VB6. 1. t vn AutoCad l phn mm chuyn v lnh vc v k thut v ho, c ng dng rt ph bin trong thit k cng trnh. Bn thn AutoCad c tch hp ngn ng lp trnh Visual Lisp v VBA (Visual Basic for Applications) nhng u c nhng hn ch nht nh. V d nh kh nng bo mt km, khng c kh nng ng gi thnh ng dng chy c lp, h tr t iu khin ActiveX,... Hai ngn ng ny thng c s dng vi cc ng dng quy m va v nh, hoc ng vai tr kt ni ng dng khc trong AutoCad. VB6 khc phc c nhng mt hn ch trn, c th to ra ng dng trong AutoCad hon ton chy c lp (khng ph thuc vo phin bn), sn phm c kh nng thng mi. Nhn chung cu trc, m lnh trong th tc ca VB6 v VBA rt ging nhau nn d dng tham kho v so snh. Ngun ti liu hng dn VBA trong 1

AutoCad cng kh phong ph. V vy, nhng ngi am hiu v VBA th c th nhanh chng tip cn, s dng VB6 v ngc li. Hin nay, tuy c thay th bng VB.NET nhng VB6 vn c s dng rng ri nh nhng u im trn. 2. iu khin AutoCad t VB6 VB6 c th to ra hn 6 kiu ng dng, nhng ch c hai kiu iu khin c vi AutoCad (v c cc phn mm trong Office) l Standard EXE v ActiveX DLL. Standard EXE l ng dng c bin dch chy hon ton c lp, iu khin AutoCad thng qua i tng Automation. Automation l mt dng cng ngh ActiveX trong gia nh Microsoft, cho php iu khin cc ng dng (AutoCad) t mi trng bn ngoi (nh VB6).VB6

AUTOMATION

Word

Excel

PowerPoi nt

AutoCad

Hnh 1: M hnh trao i d liu vi cc ng dng khc t VB6

Hnh 2: To ng dng Standard EXE 2

ActiveX DLL c hiu l th vin lin kt ng (Dynamic Link Library - DLL) cc ng dng c th tham chiu, s dng c. Khc vi VB6, VBA l mi trng lp trnh c sn trong AutoCad nn iu khin khng thng qua i tng Automation (c gi l AutoCAD.Application). iu c ngha Automation l i tng trung gian VB6 t bn ngoi c th iu khin cc ng dng khc nh AutoCad, Excel, Word,... Bi bo ny gii thiu 2 ng dng iu khin AutoCad t VB6 qua Standard EXE v ActiveX DLL mt cch n gin nht. 2.1. iu khin AutoCad bng Standard EXE Khi ng VB6, chn Standard EXE trong ca s New Project (hnh 2). D n (project) mi mc nh sn c Form1, chng ta i tn ca d n thnh VB6exeAcad, i tn Form thnh ControlAcad v lu li d n .

Hnh 3: Xy dng cc iu khin trong d n VB6exeAcad Sau , xy dng cc iu khin chnh nh hnh 3. iu khin Text Box gm c txtFirstX, txtFirstY (to im u), txtSecondX, txtSecondY (ta im cui). iu khin CommandButton gm c cmdFirstP, cmdSecondP (chn im u, im cui), cmdOK (thc hin lnh v), cmdClose (ng ca s),... Th tc di y s khi ng AutoCad khi Form ControlAcad c ti: 3

Private Sub Form_Load() On Error Resume Next 'B qua li khi AutoCad cha m sn '"Bt" i tng AutoCad nu ang m Set AcadApp = GetObject(, "AutoCAD.Application") 'Xo li nu AutoCad cha m If Err 0 Err.Clear End If AppActivate AcadApp.Caption 'Kch hot AutoCad AcadApp.Visible = True End Sub 'Hin ca s AutoCad 'Xo li pht sinh Set AcadApp = CreateObject("AutoCAD.Application")

Th tc thi hnh mt s nt lnh cmdClose, cmdFirstP, cmdSecondP nh hnh 4.

Hnh 4: Ni dung th tc ca nt lnh cmdClose, cmdFirstP, cmdSecondP Ni dung th tc ca nt cmdOK (v on thng t im LineP1 v LineP2):Private Sub cmdOK_Click() Dim LineL As Object, LineP1(0 To 2) As Double, LineP2(0 To 2) As Double With Me LineP1(0) = Val(.txtFirstX) LineP1(1) = Val(.txtFirstY) LineP2(0) = Val(.txtSecondX) 'Gn to im cui 'Gn to im u

4

LineP2(1) = Val(.txtSecondY) End With 'V on thng v chuyn sang mu Set LineL = AcadApp.ActiveDocument.ModelSpace.AddLine(LineP1, LineP2) LineL.Color = 1 Unload Me Set AcadApp = Nothing End Sub 'Xo bin i tng

i tng AcadApp c khai bo trn chnh l AutoCAD.Application. Nu AutoCad ang m, s dng hm GetObject chng trnh tham chiu n i tng AutoCAD.Application. Trong trng hp AutoCad cha m, s dng hm CreateObject to ra v tr v tham chiu n i tng AutoCAD.Application. Sau khi xy dng hon chnh, bn vo menu File v chn Make VB6exeAcad.exe... to file chy c lp nh cc chng trnh khc. ng dng ny c th thc hin trn cc phin bn AutoCad bt u t 2000 (phin bn u tin h tr Automation). 2.2. iu khin AutoCad bng ActiveX DLL Khi ng VB6, chn ActiveX DLL trong ca s New Project (hnh 2). D n mi mc nh c mt Class Module, i tn ca d n thnh Dll_VB6_Project v lu li d n . Sau thm Form, Module, Class Module bng cch vo menu Project v chn Add Form, Add Module, Add Class Module (hnh 5).

Hnh 5: Ti Form, Module vo d n Dll_VB6_Project D n Dll_VB6 c ba thnh phn l Form FDllVb6, Module Dll_in_AutoCad, Class Module HelloDllVB6 nh sau: 5

-

Module Dll_in_AutoCad: Khai bo bin i tng CadApp, chnh l i tng Acad.Application cc th tc c th truy cp.

Public CadApp As Object (gi chung)

-

Form FDllVb6: L Form s hin th trong AutoCad, Form ny cha cc iu khin theo mun. Phng php xy dng cc iu khin tng t nh mc 2.1 nn khng trnh by li na.

Private Sub cmdLine_Click() Dim LineObj As Object Dim StartPoint As Variant Dim EndPoint As Variant On Error GoTo Thoat 'T thot khi gp li hay bm phm Esc Unload Me 'Chn im u ca ng thng StartPoint = CadApp.ActiveDocument.Utility.GetPoint(, "Chon diem dau:") Do 'Chn im cui ca ng thng, c lp ging lnh Line trong AutoCad EndPoint = CadApp.ActiveDocument.Utility. _ GetPoint(StartPoint, "Chon diem tiep theo:") 'V on thng Set LineObj = CadApp.ActiveDocument.ModelSpace. _ AddLine(StartPoint, EndPoint) 'Gn bin i tng StartPoint = EndPoint Loop Set LineObj = Nothing Thoat: End Sub

-

Class Module HelloDllVB6: To i tng mi c tn HelloDllVb6, trong HelloDllVb6 xy dng th tc ShowVB6Form (hnh 7). Th tc ShowVB6Form c chc nng hin th form FDllVb6 trong AutoCad.

ng ch trong d n ny c s dng hai hm API l FindWindowA v SetWindowLongA. Hm FindWindowA ly handle ca ca s c tn (title) c ch

6

nh. Hm SetWindowLongA s thay i thuc tnh ca ca s ch nh (xem thm trong trang www.microsoft.com).

Hnh 6: Cu trc D n Dll_VB6_Project

Hnh 7: Cu trc Class Module HelloDllVB6 7

Hnh 8: Bin dch d n Dll_VB6_Project sang DLL Khi d n hon thnh, chng ta tin hnh bin dch bng cch chn menu File\ Make Dll in AutoCad.dll... (hnh 8). By gi, cng vic tip theo ca chng ta l s dng file "Dll in AutoCad.dll" nh th no? Khc vi VB6exeAcad.exe c th iu khin trc tip AutoCad, s dng ActiveX DLL cn phi c th tc trong AutoCad gi ng dng . Th tc c xy dng bng VBA hoc AuoLisp trong AutoCad.

Hnh 9: Ca s Microsoft Visual Basic trong AutoCad u tin chng ta khi ng AutoCad, sau vo menu Tools\ Macro\ Visual Basic Editer (hoc bm Alt+F11). Ca s Microsoft Visual Basic hin ra (hnh 9), chn menu Insert\ Module thm module mi. Sau vo menu Tools\ References..., ca s References - ACADProject hin ra nh hnh 10. Bm vo nt Browse... tm file "Dll in AutoCad.dll" va c bin dch. Khi chn xong, AutoCad xc nhn d n Dll_VB6_Project c ti vo chng trnh (hnh 10). 8

Hnh 10: Dll in AutoCad.dll c np trong AutoCad Sau chng ta tin hnh xy dng th tc DisplayDLLForm trong VBA, d n va ri c np vo Auto List Members nh cc thnh phn sn c trong AutoCad (hnh 11).

Hnh 11: D n Dll_VB6_Project c xc nhn trong Auto List Members Th tc DisplayDLLForm hon chnh nh di:Public Sub DisplayDLLForm() 'Khai bo bin HelloDllVb6 trong Dll_VB6_Project Dim HelloDllVb6 As Dll_VB6_Project.HelloDllVb6 Set HelloDllVb6 = New Dll_VB6_Project.HelloDllVb6 Set HelloDllVb6.AcadApp = Application 'Hin thi hnh th tc ShowVB6Form ti form FDllVb6 HelloDllVb6.ShowVB6Form Set HelloDllVb6 = Nothing End Sub

9

Hnh 12: Thi hnh th tc DisplayDLLForm trong AutoCad

Hnh 13: Form xy dng trong VB6 trong AutoCad Trong th tc trn, HelloDllVb6 ng vai tr l mt i tng nm trong Dll_VB6_Project. i tng HelloDllVb6 c phng thc ShowVB6Form. Sau thi hnh th tc trn bng cch chuyn sang ca s AutoCad, vo menu Tools\ Macro\ Macros... (hoc phm tt Alt+F8). Ca s Macros hin ra nh hnh 12, chn th tc DisplayDLLForm v bm nt Run. Kt qu th hin nh hnh 13 thi hnh cc lnh.

10

Hnh 14: Form xy dng trong VBA ca AutoCad 3. Quan h gia VB6 v VBA trong AutoCad Nh cp trn, VB6 v VBA rt gn gi nhau v chng u l ngn ng lp trnh Visual Basic. Khi lm vic vi i tng ging nhau, cch thc thc hin tng t nhau. tin so snh, chng xy dng thm Form v on thng trong VBA ging nh trong VB6 (hnh 14). Cc i tng bn trong Form cng nh thuc tnh ca chng thit lp gn tng t nhau. Giao din gia 2 chng trnh ging nhau n mc c th gy nn s xo trn khi lm vic vi chng (hnh 6 v 14). V mt thut ton cng nh m lnh (code) gia chng gn tng t nhau. tin so snh chng ta copy ton b code v on thng trong VB6 (hnh 6) sang VBA (hnh 14). Do VBA c sn trong AutoCad (khng phi can thip t bn ngoi nh VB6) nn ch cn b i on code CadApp., cn ton b gi nguyn. Th tc v on thng trong VBA sa li nh sau:Private Sub cmdLine_Click() Dim LineObj As Object Dim StartPoint As Variant Dim EndPoint As Variant

11

On Error GoTo Thoat ' T thot khi gp li hay bm phm Esc Unload Me 'Chn im u ca ng thng StartPoint = ActiveDocument.Utility.GetPoint(, "Chon diem dau:") Do 'Chn im cui ca ng thng, c lp ging lnh Line trong AutoCad EndPoint = ActiveDocument.Utility. GetPoint(StartPoint, "Chon diem tiep theo:") 'V on thng Set LineObj = ActiveDocument.ModelSpace.AddLine(StartPoint, EndPoint) 'Gn bin i tng StartPoint = EndPoint Loop Set LineObj = Nothing Thoat: End Sub

Hy kim tra th tc trn, chng ta s thy kt qu tng t nh vi VB6. l cch chuyn code t VB6 sang VBA, cn nu mun chuyn t VBA sang VB6 th thc hin ngc li bng cch b sung ng dn CadApp.