永发信息网

AUTOCAD二次开发 VB、 VBA 的疑问

答案:2  悬赏:0  手机版
解决时间 2021-02-02 11:36
  • 提问者网友:遮云壑
  • 2021-02-02 08:47
我找到了一个让我钻牛角尖的问题:
举例:添加一个图层名字叫ABC的图层,并定义他的颜色:
Dim obj As AcadLayer
Set obj = ThisDrawing.Layers.Add(ABC)
obj.color = acBlue

那么问题来了,为什么我不能在这里用ABC.color = acBlue
而必须用变量obj.color = acBlue呢?
对ABC直接操作为什么不行,非得通过变量obj呢?

对不起,没财富值了,我去挣,采纳后一定追加!
最佳答案
  • 五星知识达人网友:洒脱疯子
  • 2021-02-02 08:56
您好,这样的:
VB,VBA或其它工具如c#,C++等都是利用AutoCAD提供的ARX接口进行开发
VB开发的程序相对独立,不能查看源码;运行速度一般;可能会因为CAD正在运行的命令或其它冲突!
VBA必需集成于CAD之下,源代码可以被看到,运行速度快;运行错误率更低!
AutoCAD 64版本,在安装使用VBA后(CAD2010及以后版本需单独安装);保存低版本会很慢;需要等上约30秒左右!
如果你是初学开发,个人建议学习VBA(就是学习CAD的ARX接口用法),不用引用,调试很快;在CAD开发人员帮助文件中有很详细的帮助说明!学会VBA后,可以很轻松的转到VB,c#等其它工具进行开发!
全部回答
  • 1楼网友:詩光轨車
  • 2021-02-02 09:44
dim blocknamest as string sub dimdimaligned() on error goto err dim po(0 to 2) as double dim pr(0 to 2) as double dim var as variant dim dimdimalign as acaddimrotated dim blokname as string dim ang as double dim ldob as double dim xdob as double dim pth as string var = thisdrawing.utility.getpoint(, vbcr & "指定第一条尺寸界线原点:") po(0) = var(0): po(1) = var(1): po(2) = var(2) var = thisdrawing.utility.getpoint(po, vbcr & "指定第二条尺寸界线原点:") pr(0) = var(0): pr(1) = var(1): pr(2) = var(2) xdob = pr(0) - po(0) ldob = sqr(((pr(0) - po(0)) * (pr(0) - po(0))) + ((pr(1) - po(1)) * (pr(1) - po(1)))) ang = xdob / ldob if pr(1) > po(1) then ang = atn(-ang / sqr(-ang * ang + 1)) + 2 * atn(1) end if if pr(1) < po(1) then ang = -atn(-ang / sqr(-ang * ang + 1)) + 2 * atn(1) - 180 * 3.1415926 / 180 end if if pr(1) = po(1) and pr(0) < po(0) then ang = 180 * 3.1415926 / 180 end if if pr(1) = po(1) and pr(0) > po(0) then ang = 0 end if set dimdimalign = thisdrawing.modelspace.adddimrotated(po, pr, pr, ang) err: end sub sub linkblok() on error goto err dim po(0 to 2) as double dim pr(0 to 2) as double dim var as variant dim blokin as acadblockreference dim blokname as string dim ang as double dim ldob as double dim xdob as double dim pth as string dim ucsobj as acaducs dim origin(0 to 2) as double dim xaxispo(0 to 2) as double dim yaxispo(0 to 2) as double origin(0) = 0#: origin(1) = 0#: origin(2) = 0# xaxispo(0) = 3: xaxispo(1) = 0: xaxispo(2) = 0 yaxispo(0) = 0: yaxispo(1) = 3: yaxispo(2) = 0 set ucsobj = thisdrawing.usercoordinatesystems.add(origin, xaxispo, yaxispo, "wucs") thisdrawing.activeucs = ucsobj blokname = thisdrawing.utility.getstring(false, vbcr & "输入的块名<" + blocknamest + ">: ") if blokname = "" then blokname = blocknamest pth = "d:\cad块\" + blokname + ".dwg" if dir(pth) <> "" then blocknamest = blokname do var = thisdrawing.utility.getpoint(, vbcr & "选取图块放置点:") po(0) = var(0): po(1) = var(1): po(2) = var(2) set blokin = thisdrawing.modelspace.insertblock(po, pth, 1, 1, 1, 0) var = thisdrawing.utility.getpoint(po, vbcr & "指定图块方向:") pr(0) = var(0): pr(1) = var(1): pr(2) = var(2) xdob = pr(0) - po(0) ldob = sqr(((pr(0) - po(0)) * (pr(0) - po(0))) + ((pr(1) - po(1)) * (pr(1) - po(1)))) ang = xdob / ldob if pr(1) > po(1) then ang = atn(-ang / sqr(-ang * ang + 1)) + 2 * atn(1) end if if pr(1) < po(1) then ang = -atn(-ang / sqr(-ang * ang + 1)) + 2 * atn(1) - 180 * 3.1415926 / 180 end if if pr(1) = po(1) and pr(0) < po(0) then ang = 180 * 3.1415926 / 180 end if if pr(1) = po(1) and pr(0) > po(0) then ang = 0 end if blokin.rotate po, ang loop else thisdrawing.utility.prompt vbcr & pth + "的文件路径不存在!" end if err: end sub sub linkblokr0() on error goto err dim po(0 to 2) as double dim pr(0 to 2) as double dim var as variant dim blokin as acadblockreference dim blokname as string dim ang as double dim ldob as double dim xdob as double dim pth as string dim ucsobj as acaducs dim origin(0 to 2) as double dim xaxispo(0 to 2) as double dim yaxispo(0 to 2) as double origin(0) = 0#: origin(1) = 0#: origin(2) = 0# xaxispo(0) = 3: xaxispo(1) = 0: xaxispo(2) = 0 yaxispo(0) = 0: yaxispo(1) = 3: yaxispo(2) = 0 set ucsobj = thisdrawing.usercoordinatesystems.add(origin, xaxispo, yaxispo, "wucs") thisdrawing.activeucs = ucsobj blokname = thisdrawing.utility.getstring(false, vbcr & "输入的块名<" + blocknamest + ">: ") if blokname = "" then blokname = blocknamest pth = "d:\cad块\" + blokname + ".dwg" if dir(pth) <> "" then blocknamest = blokname do var = thisdrawing.utility.getpoint(, vbcr & "选取图块放置点:") po(0) = var(0): po(1) = var(1): po(2) = var(2) set blokin = thisdrawing.modelspace.insertblock(po, pth, 1, 1, 1, 0) 'var = thisdrawing.utility.getpoint(po, vbcr & "指定图块方向:") 'pr(0) = var(0): pr(1) = var(1): pr(2) = var(2) pr(0) = 0#: pr(1) = 0#: pr(2) = 0# xdob = pr(0) - po(0) ldob = sqr(((pr(0) - po(0)) * (pr(0) - po(0))) + ((pr(1) - po(1)) * (pr(1) - po(1)))) ang = xdob / ldob if pr(1) > po(1) then ang = atn(-ang / sqr(-ang * ang + 1)) + 2 * atn(1) end if if pr(1) < po(1) then ang = -atn(-ang / sqr(-ang * ang + 1)) + 2 * atn(1) - 180 * 3.1415926 / 180 end if if pr(1) = po(1) and pr(0) < po(0) then ang = 180 * 3.1415926 / 180 end if if pr(1) = po(1) and pr(0) > po(0) then ang = 0 end if blokin.rotate po, ang loop else thisdrawing.utility.prompt vbcr & pth + "的文件路径不存在!" end if err: end sub sub plinelenx() on error goto err dim plx as string dim obj as acadentity dim lentxt as acadtext dim po(0 to 2) as double dim var as variant thisdrawing.utility.getentity obj, var, vbcr & "选取polyline对象:" if obj.objectname = "acdbpolyline" then plx = cstr(int(obj.length * 100) / 100) var = thisdrawing.utility.getpoint(, vbcr & "选取文字放置点:") po(0) = var(0): po(1) = var(1): po(2) = var(2) set lentxt = thisdrawing.modelspace.addtext("heater len", po, 4) lentxt.stylename = "heatertxt": lentxt.layer = "3" po(0) = po(0): po(1) = po(1) - 6: po(2) = po(2) set lentxt = thisdrawing.modelspace.addtext(plx, po, 4) lentxt.stylename = "heatertxt": lentxt.layer = "3" else thisdrawing.utility.prompt vbcr & "选取对象无效!" end if err: end sub
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯