VB6 trong AutoCad

Download VB6 trong AutoCad

Post on 26-Jul-2015

235 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

B n c th download ti li u ny v t http://ghic.co.cc Ho c blog: http://chunguv.blogspot.com website cung c p y ebook v : acad, 3dsmax, etabs, sap2000, ms project, photoshop..

ghi ch: http://ghic.co.cc khng ch u trch nhi m v b n quy n c a ti li u c pht hnh. T t c ti li u do chng ti pht hnh u c ngu n g c r rng v c ch n l c m t cch kh t khe mang l i cho b n c nh ng ti li u t t nh t. Trong th i gian t i chng ti s chuy n ton b d li u sang blog: http://chunguv.blogspot.com Chc m i ng i thnh cng !

http://ghic.co.cc khng ch u trch nhi m v b n quy n c a ti li u c pht hnh. T t c ti li u do chng ti pht hnh u c ngu n g c r rng v c ch n l c m t cch kh t khe mang l i cho b n c nh ng ti li u t t nh t. Chc m i ng i thnh cng !

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, phn mm AutoCad c s dng rt ph bin v nhiu tnh nng u vit. Vi yu cu thit k xy dng 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 gy nhm chn, mt nhiu cng sc v d 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 khm ph 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 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 hay khi ng ng dng khc. VB6 khc phc c nhng mt hn ch trn, c th to ra cc ng dng trong AutoCad hon ton chy c lp (khng ph thuc vo phin bn), khng cn cc chng trnh nn h tr, 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 chuyn i. 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 (lin kt) c vi AutoCad (v c cc phn mm trong b Office) l Standard EXE v ActiveX DLL. Standard EXE l ng dng c bin dch chy hon ton c lp, iu khin

http://ghic.co.cc website cung c p y ebook v : acad, 3dsmax, etabs, sap2000, ms project, photoshop..

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). 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,...

VB6

AUTOMATION

Word

Excel

PowerPoint

AutoCad

Hnh 1: M hnh trao i d liu vi cc ng dng khc t VB6 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

http://ghic.co.cc website cung c p y ebook v : acad, 3dsmax, etabs, sap2000, ms project, photoshop..

Hnh 2: To ng dng Standard EXE Khi ng VB6, chn Standard EXE trong ca s New Project (hnh 2). D n (project) mi mc nh sn c Form1, 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

http://ghic.co.cc website cung c p y ebook v : acad, 3dsmax, etabs, sap2000, ms project, photoshop..

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: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

http://ghic.co.cc website cung c p y ebook v : acad, 3dsmax, etabs, sap2000, ms project, photoshop..

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 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 'Gn to im u

i tng AcadApp c khai bo trn chnh l AutoCAD.Application. Nu AutoCad ang c m, s dng hm GetObject chng trnh tham chiu n i tng AutoCAD.Application. Trong trng hp AutoCad ang ng, 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). Chi tit ca d n ny trong www.cadviet.com... 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).

http://ghic.co.cc website cung c p y ebook v : acad, 3dsmax, etabs, sap2000, ms project, photoshop..

Hnh 5: Ti Form, Module vo d n Dll_VB6_Project

Hnh 6: Cu trc 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: 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)

http://ghic.co.cc website cung c p y ebook v : acad, 3dsmax, etabs, sap2000, ms project, photoshop..

-

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 ti 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.

http://ghic.co.cc website cung c p y ebook v : acad, 3dsmax, etabs, sap2000, ms project, photoshop..

Hnh 7: Cu trc Class Module HelloDllVB6 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 nh. Hm SetWindowLongA s thay i thuc tnh ca ca s ch nh (xem thm trong trang www.microsoft.com).

http://ghic.co.cc website cung c p y ebook v : acad, 3dsmax, etabs, sap2000, ms project, photoshop..

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 d n 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 trong VBA ca AutoCad. u tin 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 ti file Dll in AutoCad.dll va c bin dch. Sau khi chn xong, AutoCad xc nhn d n Dll_VB6_Project c ti vo chng trnh (hnh 10). Sau bn 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).

http://ghic.co.cc website cung c p y ebook v : acad, 3dsmax, etabs, sap2000, ms project, photoshop..

Hnh 9: Ca s Microsoft Visual Basic trong AutoCad

Hnh 10: Ca s Microsoft Visual Basic trong AutoCad

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

http://ghic.co.cc website cung c p y ebook v : acad, 3dsmax, etabs, sap2000, ms project, photoshop..

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