用CAD内嵌的VBA组件是实现包装结构自动设计(3)
5、编写功能构素绘图程序
5. 1 建立交互界面
在AutoCAD 命令行执行vbaide 命令打开VBA 集成开发环境,该环境与VB 的开发环境接近。将设计、运行、调试集成,以Thisdrawing 文档组织,根据需要添加模块、实现人机交互的窗体等对象,以dvb 文件保存工程所有信息。右键点击工程,在快捷菜单中选添加,选择窗体命令,即可建立一个新的窗体。根据人机交互需要建立相应的对象。
5. 2 初始化图层
右键点击工程,在快捷菜单中选添加,选择模块命令,即可建立一个新的模块。图层初始化方法如下:
Public l1csx As AcadLayer ’定义图层对象:
Sub italize()’初始化图层
ThisDrawing. Linetypes. Load " center" ," acadiso. lin"
Set l1csx = ThisDrawing. Layers. Add(" l1csx" )
l1csx. color = acblack
ThisDrawing. ActiveLayer = l1csx
ThisDrawing. ActiveLayer. Linetype = " continuous"
End Sub‘其它图层略
5. 3 在Thisdrawing 文档进行绘图编程
5. 3. 1 创建图形对象;(以盒盖为例)
Sub js-01hega(i )’计算盒盖各点坐标值
pthega(i 0)= 0:pthega(i 1)= 0 ’1
pthega(i 2)= 0:pthega(i 3)= b - 2 ’2
. . .‘其它点坐标表达式省略
End Sub
Sub draw-01hega(i )’画盒盖
Dim points(0 To 29)As Double
Dim points1(0 To 3)As Double
’定义盒盖图形对象
Dim plobj-hegai-cx As AcadLWPolyline
Dim plobj-hegai-xx As AcadLWPolyline
’画2 - 5 虚线段略
For i = 0 To 29 ’画实线
point(s i) = pthega(i i)
Next i
Set plobj-hegai-cx = ThisDrawing. ModelSpace.
AddLightWeightPolyline(points)
End Sub
5. 3. 2 创建标注对象;(以盒身为例)
Sub dim-hes()’水平标注
Dim dimobj As AcadDimAligned
Dim point1(2)As Double:Dim point2(2)As Double:
Dim location(2)As Double
’水平总尺寸
point1(0)= pthes(8):point1(1)= pthes(9)- kjt
point2(0)= pthes(22):point2(1)= pthes(23)
location(0)=(point1(0)+ point2(0))/ 2:location(1)
= b + 50
Set dimobj = ThisDrawing. ModelSpace. AddDimAligned
(point1,point2,location)
’水平其它尺寸略
End Sub