我想看一级黄色大片_久久亚洲国产精品一区二区_久久精品免视看国产明星_91久久青青青国产免费

您的位置:網(wǎng)站首頁 > CAD新聞

AutoCAD利用VB交互創(chuàng)建應(yīng)用程序交互

時(shí)間:2012-01-29 08:27:48 來源:未知

可以使用COM技術(shù),在VB中直接操作AutoCAD,創(chuàng)建于VBA功能類似的程序,VB能夠直接打包生成安裝文件,這種形式比VBA更加方便,并且更容易保護(hù)自己的程序。VB連接到AutoCAD主要用到CreateObject和GetObject函數(shù),創(chuàng)建或者獲得對(duì)ActiveX對(duì)象的引用。

連接到AutoCAD之后,就可以使用acadApp對(duì)象對(duì)AutoCAD進(jìn)行操作,語法和操作方法與VBA完全一致。

下面這段代碼創(chuàng)建一個(gè)繪制樓梯剖面圖的程序。

Dim bcal As Boolean
Dim ptarr1() As Double
Dim ptarr2(19) As Double
Private Sub cmdcal_Click()
    Dim objcontrol As Control
    For Each objcontrol In Form1.Controls
        If TypeOf objcontrol Is TextBox Then
            If objcontrol.Text = "" Then
                MsgBox "缺少參數(shù),無法計(jì)算!", vbCritical
                Exit Sub
            End If
        End If
    Next
    Dim x0 As Double, y0 As Double
    Dim s As Double, t As Double, n As Double
    Dim b As Double, h As Double, h0 As Double
    x0 = txtptx.Text: y0 = txtpty.Text
    s = txtsteph.Text: t = txtstepw.Text: n = txtstepnum.Text
    b = txtgriderw.Text: h = txtgriderh.Text: h0 = txtboardt.Text
    If h0 >= h Or b > 80 Or s >= t Then
        MsgBox "輸入條件不符合要求,請(qǐng)檢查參數(shù)的合理性!", vbCritical
        Exit Sub
    End If
    ReDim ptarr1(2 * (2 * n + 2) – 1)
    ptarr1(0) = x0 – 100: ptarr1(1) = y0
    ptarr1(2) = x0: ptarr1(3) = y0
    ptarr1(4) = x0: ptarr1(5) = y0 + s
    Dim i As Integer
    For i = 6 To 2 * (2 * n + 2) – 3
        If i Mod 4 = 2 Then
            ptarr1(i) = ptarr1(i – 4) + t
        ElseIf i Mod 4 = 3 Then
            ptarr1(i) = ptarr1(i – 4) + s
        ElseIf i Mod 4 = 0 Then
            ptarr1(i) = ptarr1(i – 2)
        ElseIf i Mod 4 = 1 Then
            ptarr1(i) = ptarr1(i – 2) + s
        End If
    Next i
    ptarr1(2 * (2 * n + 2) – 2) = ptarr1(2 * (2 * n + 2) – 4) + 100
    ptarr1(2 * (2 * n + 2) – 1) = ptarr1(2 * (2 * n + 2) – 3)
    ptarr2(0) = x0 – 100: ptarr2(1) = y0 – h0
    ptarr2(2) = x0 – b: ptarr2(3) = y0 – h0
    ptarr2(4) = x0 – b: ptarr2(5) = y0 – h
    ptarr2(6) = x0: ptarr2(7) = y0 – h
    ptarr2(8) = x0: ptarr2(9) = y0 – h0
    ptarr2(10) = x0 + (n – 1) * t: ptarr2(11) = y0 + (n – 1) * s – h0
    ptarr2(12) = ptarr1(2 * (2 * n + 2) – 4): ptarr2(13) = ptarr1(2 * (2 * n + 2) – 3) – h
    ptarr2(14) = ptarr2(12) + b: ptarr2(15) = ptarr2(13)
    ptarr2(16) = ptarr2(14): ptarr2(17) = ptarr2(15) + (h – h0) #p#分頁標(biāo)題#e#
    ptarr2(18) = ptarr1(2 * (2 * n + 2) – 2): ptarr2(19) = ptarr1(2 * (2 * n + 2) – 1) – h0
    bcal = True
End Sub
Private Sub cmddraw_Click()
    If bcal = False Then
        MsgBox "請(qǐng)先進(jìn)行計(jì)算,再進(jìn)行繪圖!", vbCritical
        Exit Sub
    End If
    On Error Resume Next
    Dim acadapp As AcadApplication
    Set acadapp = GetObject(, "AutoCAD.Application.16")
    If Err Then
        Err.Clear
        ‘MsgBox "sssssssssssss"
        ‘Set acadapp = CreatObject("AutoCAD.Application.16")
        If Err Then
            MsgBox Err.Description
            Exit Sub
        End If
    End If
    Dim acaddoc As AcadDocument
    Set acaddoc = acadapp.ActiveDocument
    acaddoc.ModelSpace.AddLightWeightPolyline ptarr1
    acaddoc.ModelSpace.AddLightWeightPolyline ptarr2
    ZoomAll
    acadapp.Visible = True
    bcal = False
End Sub
Private Sub cmdexit_Click()
    End
End Sub
Private Sub Form_Load()
    txtptx.Text = 0
    txtpty.Text = 0
    txtptz.Text = 0
    txtsteph.Text = 20
    txtstepw.Text = 40
    txtstepnum.Text = 10
    txtgriderw.Text = 25
    txtgriderh.Text = 45
    txtboardt.Text = 15
    bcal = False
End Sub

因?yàn)閂B和AutoCAD之間是通過COM技術(shù)連接,這種數(shù)據(jù)交換對(duì)系統(tǒng)資源的消耗很大,同樣的計(jì)算在VB運(yùn)算消耗的時(shí)間比在VBA中的運(yùn)算時(shí)間要多5倍以上,在VB中按F5鍵運(yùn)行程序,可以在Form中對(duì)樓梯參數(shù)進(jìn)行設(shè)置,就可在CAD中繪制樓梯剖面圖。創(chuàng)建VB程序時(shí)要盡可能減少程序和AutoCAD之間的數(shù)據(jù)交換,所有計(jì)算盡量在VB中完成,直接傳遞給AutoCAD計(jì)算結(jié)果,使CAD可以直接根據(jù)結(jié)果進(jìn)行繪圖。在VB中按下F5鍵,單擊繪圖按鈕即可在CAD中看到繪制的樓梯剖面圖。