這個(gè)必須用控件的方法解決,VB6有個(gè)Line控件很容易解決這個(gè)問題,但是VB.NET沒有了,幸好微軟也想到了這個(gè)缺陷,提供免費(fèi)的Visual Basic PowerPacks控件箱,其中有Line控件。
成都創(chuàng)新互聯(lián)公司成立于2013年,我們提供高端成都網(wǎng)站建設(shè)、成都網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)公司、網(wǎng)站定制、營銷型網(wǎng)站建設(shè)、微信小程序、微信公眾號開發(fā)、網(wǎng)站推廣服務(wù),提供專業(yè)營銷思路、內(nèi)容策劃、視覺設(shè)計(jì)、程序開發(fā)來完成項(xiàng)目落地,為成都木托盤企業(yè)提供源源不斷的流量和訂單咨詢。
Visual Basic PowerPacks下載地址:
使用 LineShape 控件繪制直線:
你去查查書吧,書上挺詳細(xì)的,在這說不好說,你先在項(xiàng)目里引用。然后 Dim acadapp As AcadApplication Dim acaddoc As AcadDocument On Error Resume Next AcadApp = GetObject(, "AutoCAD.Application") If Err.Number Then Err.Clear() AcadApp = CreateObject("AutoCAD.Application") If Err.Number Then MsgBox("不能運(yùn)行AutoCAD,請檢查是否安裝了AutoCAD") Exit Sub End If End If AcadApp.Visible = True '界面可視
On Error Resume Next
Set ACADApp = GetObject(, "AutoCAD.Application")
If Err Then
Err.Clear
Set ACADApp = GetObject(, "AutoCAD.Application")
If Err Then
MsgBox Err.Description
Exit Sub
End If
End If
Set ACADApp = GetObject(, "AutoCAD.Application")
Set ThisDrawing = ACADApp.ActiveDocument
Dim Pline As AcadLine
Dim ptSt(0 To 2) As Double
Dim ptEn(0 To 2) As Double
ptSt(0) = 100
ptSt(1) = 100
ptSt(2) = 0
ptEn(0) = 150
ptEn(1) = 100
ptEn(2) = 0
Set Pline = ThisDrawing.ModelSpace.AddLine(ptSt, ptEn)
VBA繪制的話,我只有繪制橢圓的時(shí)候才使用,因?yàn)闄E圓分割后,發(fā)現(xiàn)VBA的計(jì)算角度才正確,autolisp計(jì)算的角度會(huì)有問題,查看屬性會(huì)發(fā)現(xiàn)有偏差。
autolisp里面的entmake函數(shù)繪制速度很快,
1、直線
(entmake
(list
'(0
.
"LINE")
(cons
10
pt1)
(cons
11
pt2)))
2、兩頂點(diǎn)多段線(多頂點(diǎn)類似)
(entmake
(list
'(0
.
"LWPOLYLINE")
'(100
.
"AcDbEntity")
'(100
.
"AcDbPolyline")
(cons
90
2)
(cons
10
pt1)
(cons
10
pt2)))
3、點(diǎn)表生成多段線
(entmake
(append
(list
'(0
.
"LWPOLYLINE")
'(100
.
"AcDbEntity")
'(100
.
"AcDbPolyline")
(cons
90
(length
lst)))
(mapcar
'(lambda
(pt)(cons
10
pt))
lst
)))
4、圓
(entmake
(list
'(0
.
"CIRCLE")
(cons
10
pt)
(cons
40
r)))
5、圓弧
(entmake
(list
'(0
.
"ARC")
(cons
10
pt)
(cons
40
r)
(cons
50
ang1)
(cons
51
ang2)))
必要的話,可以加入圖層,顏色等元素,不用關(guān)閉捕捉,改變圖層等。你可以試試。
繪圖是系統(tǒng)內(nèi)部操作的,不需要懂原理
方法就在那里,只有會(huì)用和不會(huì)用,你的代碼告訴它繪制,它就會(huì)繪制。它(方法)究竟如何去繪制的并不是重點(diǎn),反正它會(huì)繪制。
drawline(繪線)方法很簡單,第一個(gè)參數(shù)是pen,它確定線條的顏色、寬度和樣式。第二、第三個(gè)參數(shù)都是point類型,確定兩個(gè)點(diǎn)的位置,繪制直線。
Set LineObj = activeDoc.ModelSpace.AddLine(startpoint, endpoint) '畫線中的參數(shù)只需兩個(gè)點(diǎn),而你的數(shù)組是多點(diǎn)的,不匹配。要畫多點(diǎn)連續(xù)線可用Set myl = ThisDrawing.ModelSpace.AddLightWeightPolyline(p) '畫多段線 。
要分段線可以漸次畫出。
sub 劃線()'作為一模塊
Dim myAcadApp As AutoCAD.AcadApplication
Dim activeDoc As AutoCAD.AcadDocument
Dim acMS As AutoCAD.AcadModelSpace
On Error Resume Next
Set myAcadApp = GetObject(, "Autocad.Application") '檢查AutoCAD是否已經(jīng)打開
If Err 0 Then '沒有打開
Err.Clear
Set myAcadApp = CreateObject("Autocad.Application") '打開CAD
If Err Then
MsgBox Err.Number ":" Err.Description '打開失敗
Exit Sub
End If
End If
On Error GoTo prcERR
myAcadApp.Visible = True '顯示CAD
Set activeDoc = myAcadApp.ActiveDocument
Dim startpoint(0 To 2) As Double '12 改為2,AddLine首尾點(diǎn)坐標(biāo)要3維的,平面z不用賦值,默認(rèn)為0
Dim endpoint(0 To 2) As Double
Dim LineObj As AcadLine
For i = 0 To 5
startpoint(0 = 0
startpoint( 1) = 2 * i
endpoint(0) = 2 * i
endpoint( 1) = 2 * i
'Next i ‘移到下一行,
Set LineObj = activeDoc.ModelSpace.AddLine(startpoint, endpoint) '畫線
Next i '到此返回,實(shí)現(xiàn)漸次劃線
prcExit:
Set activeDoc = Nothing
Set myAcadApp = Nothing
Exit Sub
prcERR:
MsgBox Err.Number ":" Err.Description, vbCritical, "錯(cuò)誤"
Resume prcExit
End Sub
'祝你成功!