MstnVBA学习--Vol1.代码画点线--20220623

火影  金牌会员 | 2022-6-24 13:33:36 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 544|帖子 544|积分 1632

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
@
目录

前言

2022年6月23日,小白笔记,复习之前的MstnVBA代码,因为目前还做不到完全自己背诵或编写出来,虽然简单,温故知新~
Mstn中没有单独的圆的概念,圆实际上是椭圆的一种形态,只不过该形态下长半轴和短半轴的值相等: a=b
1. 绘制固定大小的简单图形--靶心
  1. Sub Main()
  2.         '声明变量
  3.         Dim MyLine As LineElement
  4.         Dim MyCir As EllipseElement
  5.         Dim CenPt As Point3d
  6.         Dim LineSt As Point3d
  7.         Dim LineEn As Point3d
  8.         Dim RotMatrix As Matrix3d
  9.        
  10.         '生成水平线
  11.         LineSt.X = -1
  12.         LineEn.X = 1
  13.         '上面只给了起点LineSt和终点LineEn的x值,y值默认是0,这里不给值的话那就是(-1,0)到(1,0)的一条线
  14.         Set MyLine = Application.CreateLineElement2(Nothing, LineSt, LineEn)
  15.         Application.ActiveModelReference.AddElement MyLine
  16.        
  17.         '生成垂直线
  18.         LineSt.X = 0: LineSt.Y = 1
  19.         LineEn.X = 0: LineEn.Y = -1
  20.         '上面给起点LineSt和终点LineEn赋值0是因为在生成水平线时给他赋值了
  21.         Set MyLine = Application.CreateLineElement2(Nothing, LineSt, LineEn)
  22.         Application.ActiveModelReference.AddElement MyLine
  23.        
  24.         '生成圆,用CreateEllipseElement2函数,CenPt是圆心,圆心不赋值的话默认就是(0,0,0),CenPt参数后面紧跟着是圆的a,b半径
  25.         Set MyCir = Application.CreateEllipseElement2(Nothing, CenPt, 0.25, 0.25, RotMatrix)
  26.         Application.ActiveModelReference.AddElement MyCir
  27.         Set MyCir = Application.CreateEllipseElement2(Nothing, CenPt, 0.5, 0.5, RotMatrix)
  28.         Application.ActiveModelReference.AddElement MyCir
  29. End Sub
复制代码
2.绘制不固定大小的简单图形--靶心

不知道是个人原因还是哪里不对,在原书中的这个例子有点问题,原封不动抄下来后却跑不起来,应该是在DrawTarget过程中没有定义输入参数,根据个人理解稍加调整后可以运行,改后代码如下:
  1. Sub DrawTarget(CenX As Long, CenY As Long, CenZ As Long)
  2.         '声明变量
  3.         Dim MyLine As LineElement                'LineElement是Mstn线元素
  4.         Dim MyCir As EllipseElement                'EllipseElement是Mstn椭圆元素
  5.         Dim CenPt As Point3d                        '声明原点
  6.         Dim LineSt As Point3d                        '声明起点
  7.         Dim LineEn As Point3d                        '声明终点
  8.         Dim RotMatrix As Matrix3d                '声明平面
  9.        
  10.         '生成水平线,这段代码中的x,y,z没有赋值,调用DrawTarget过程的时候,需要另外赋值才能画出来图形
  11.         LineSt.X = CenX - 1
  12.         LineSt.Y = CenY
  13.         LineSt.Z = CenZ
  14.         LineEn.X = CenX + 1
  15.         LineEn.Y = CenY
  16.         LineEn.Z = CenZ
  17.         Set MyLine = Application.CreateLineElement2(Nothing, LineSt, LineEn)
  18.         Application.ActiveModelReference.AddElement MyLine
  19.        
  20.         '生成垂直线,这段代码中的x,y,z没有赋值,调用DrawTarget过程的时候,需要另外赋值才能画出来图形
  21.         LineSt.X = CenX
  22.         LineSt.Y = CenY + 1
  23.         LineSt.Z = CenZ
  24.         LineEn.X = CenX
  25.         LineEn.Y = CenY - 1
  26.         LineEn.Z = CenZ
  27.         Set MyLine = Application.CreateLineElement2(Nothing, LineSt, LineEn)
  28.         Application.ActiveModelReference.AddElement MyLine
  29.         '生成圆,这段代码中的x,y,z没有赋值,调用DrawTarget过程的时候,需要另外给圆心赋值才能画出来固定半径的圆形
  30.         CenPt.X = CenX
  31.         CenPt.Y = CenY
  32.         CenPt.Z = CenZ
  33.         Set MyCir = Application.CreateEllipseElement2(Nothing, CenPt, 0.25, 0.25, RotMatrix)
  34.         Application.ActiveModelReference.AddElement MyCir
  35.         Set MyCir = Application.CreateEllipseElement2(Nothing, CenPt, 0.5, 0.5, RotMatrix)
  36.         Application.ActiveModelReference.AddElement MyCir
  37. End Sub
  38. '下面是用Main来调用DrawTarget函数来绘制图形
  39. Sub Main()
  40.         '绘制靶形图, 输入三个参数来绘制图形
  41.         DrawTarget 0, 0, 0
  42.         DrawTarget 3, 0, 0
  43.         DrawTarget -3, 0, 0
  44.         DrawTarget 0, 3, 0
  45.         DrawTarget 0, -3, 0
  46. End Sub
复制代码
3.按照数组值绘制不固定大小的简单图形--靶心

事先并不知道参数数组中有多少个半径值,有一个半径值就绘制一个靶心, 所以在程序中使用了一个 For…Next 循环来查看每一个值并分别以之为半径来生成新的圆。
  1. Sub DrawCircle4(X As Double, Y As Double, Z As Double, ParamArray Radii() As Variant)
  2.         '声明变量
  3.         Dim MyCir As EllipseElement
  4.         Dim CenPt As Point3d
  5.         Dim RotMatrix As Matrix3d
  6.         Dim I As Long
  7.         '生成圆
  8.         CenPt.X = X
  9.         CenPt.Y = Y
  10.         CenPt.Z = Z
  11.         'LBound函数返回一个 Long 型值,其中包含指示的数组维度的最小可用下标
  12.         'UBound函数返回一个 Long 型值,其中包含指示的数组维度的最大可用下标
  13.         For I = LBound(Radii) To UBound(Radii)               
  14.                 Set MyCir = Application.CreateEllipseElement2(Nothing, CenPt, Radii(I),Radii(I),RotMatrix)
  15.                 Application.ActiveModelReference.AddElement MyCir
  16.         Next I
  17. End Sub
  18. '调用上面的DrawCircle4过程
  19. Sub Main()
  20.         DrawCircle4 1,1,0,0.25,0.5,0.75,1,1.25,1.5
  21.         '1,1,0是确定圆心位置的,后面的两个数是数组来绘制6个圆
  22. End Sub
复制代码
4.返回对象

函数出了返回值和类型外,还能返回对象,下面例子就是:
如果打开的excel中sheet里对应的B2,C2,D2有内容,那么在立即窗口里会立刻显示相应的内容
  1. Function GetExcelWS() As Object                '取得Excel中的当前工作表
  2.         Dim ExcelApp As Object
  3.         Set ExcelApp = GetObject(,"Excel.Application")
  4.         Set GetExcelWs = ExcelApp.activesheet
  5. End Function
  6. Sub TestGetExcelWS()
  7.         Dim MyWS As Object
  8.         Dim Cell1 As Double
  9.         Dim Cell2 As Double
  10.         Dim Cell3 As Double
  11.         Set MyWS = GetExcelWS
  12.         Cell1 = MyWS.Range("B2")
  13.         Cell2 = MyWS.Range("C2")
  14.         Cell3 = MyWS.Range("D2")
  15. End Sub
复制代码
总结

上面的都是最最简单的基础内容, 感觉对于没啥经验的人来说,还是需要练手的, 对于有经验的人,那这内容应该是太小儿科了~

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

火影

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表