本文主要是介绍TYPE(用户定义类型)类型在CAD-vba中的应用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
type类型可以是任何用 Type 语句定义的数据类型。用户自定义类型可包含一个或多个某种数据类型的数据元素、数组或一个先前定义的用户自定义类型。例如:
在cad vba 中应用如下(生成点和线):
部分代码:
Option Explicit' 定义一个自定义类型 PointData,包含点的X Y坐标,颜色,层名称
Type PointDataX As Double ' X 坐标Y As Double ' Y 坐标Color As Long ' 颜色(用颜色索引表示)Layer As String ' 层名称
End Type
Type point2d
X As Double ' X 坐标
Y As Double ' Y 坐标
End Type' 创建点并设置颜色和层,返回点对象
Function CreatePoint(Pt As PointData) As AcadPoint' 创建一个点对象Dim newPoint As AcadPointDim PtCoords(0 To 2) As DoublePtCoords(0) = Pt.XPtCoords(1) = Pt.YPtCoords(2) = 0 ' Z坐标默认为0Set newPoint = ThisDrawing.ModelSpace.AddPoint(PtCoords)' 设置点的颜色newPoint.Color = Pt.Color' 设置点的层newPoint.Layer = Pt.Layer' 返回新创建的点对象Set CreatePoint = newPoint
End Function
Function CreatePL(Pt() As PointData)' 创建一个点对象Dim newpl As AcadLWPolylineDim PtCoords() As DoubleDim i As IntegerFor i = 1 To UBound(Pt)ReDim Preserve PtCoords(2 * i - 1)PtCoords(2 * i - 2) = Pt(i).XPtCoords(2 * i - 1) = Pt(i).YNext iSet newpl = ThisDrawing.ModelSpace.AddLightWeightPolyline(PtCoords)' 设置点的颜色newpl.Color = Pt(1).Color' 设置点的层newpl.Layer = Pt(1).Layer' 返回新创建的点对象Set CreatePL = newpl
End Function
Sub ManagePoints()
'yngqq@2024年8月29日15:48:55' 声明一个 PointData 类型的数组来存储多个点的信息Dim Points(1 To 3) As PointData' 初始化点的数据ThisDrawing.Layers.Add ("Layer1")ThisDrawing.Layers.Add ("Layer2")ThisDrawing.Layers.Add ("Layer3")Points(1).X = 100: Points(1).Y = 200: Points(1).Color = 1: Points(1).Layer = "Layer1"Points(2).X = 150: Points(2).Y = 250: Points(2).Color = 2: Points(2).Layer = "Layer2"Points(3).X = 200: Points(3).Y = 300: Points(3).Color = 3: Points(3).Layer = "Layer3"' 遍历数组,并在CAD中创建这些点Dim i As IntegerDim Pt As AcadPointFor i = 1 To UBound(Points)Set Pt = CreatePoint(Points(i))' 可以在这里进一步操作 pt 对象,例如移动或旋转Next iCall CreatePL(Points)ZoomExtentsMsgBox "OK,CAD二次开发qq:业务合作443440204", , "qq443440204"End Sub
这篇关于TYPE(用户定义类型)类型在CAD-vba中的应用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!