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

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

@


前言

2022年6月23日,小白笔记,复习之前的MstnVBA代码,因为目前还做不到完全自己背诵或编写出来,虽然简单,温故知新~

Mstn中没有单独的圆的概念,圆实际上是椭圆的一种形态,只不过该形态下长半轴和短半轴的值相等: a=b


1. 绘制固定大小的简单图形--靶心

Sub Main()
	'声明变量 
	Dim MyLine As LineElement 
	Dim MyCir As EllipseElement 
	Dim CenPt As Point3d 
	Dim LineSt As Point3d 
	Dim LineEn As Point3d 
	Dim RotMatrix As Matrix3d 
	
	'生成水平线 
	LineSt.X = -1 
	LineEn.X = 1 
	'上面只给了起点LineSt和终点LineEn的x值,y值默认是0,这里不给值的话那就是(-1,0)到(1,0)的一条线
	Set MyLine = Application.CreateLineElement2(Nothing, LineSt, LineEn) 
	Application.ActiveModelReference.AddElement MyLine 
	
	'生成垂直线 
	LineSt.X = 0: LineSt.Y = 1 
	LineEn.X = 0: LineEn.Y = -1 
	'上面给起点LineSt和终点LineEn赋值0是因为在生成水平线时给他赋值了
	Set MyLine = Application.CreateLineElement2(Nothing, LineSt, LineEn) 
	Application.ActiveModelReference.AddElement MyLine 
	
	'生成圆,用CreateEllipseElement2函数,CenPt是圆心,圆心不赋值的话默认就是(0,0,0),CenPt参数后面紧跟着是圆的a,b半径
	Set MyCir = Application.CreateEllipseElement2(Nothing, CenPt, 0.25, 0.25, RotMatrix) 
	Application.ActiveModelReference.AddElement MyCir 
	Set MyCir = Application.CreateEllipseElement2(Nothing, CenPt, 0.5, 0.5, RotMatrix) 
	Application.ActiveModelReference.AddElement MyCir 
End Sub

2.绘制不固定大小的简单图形--靶心

不知道是个人原因还是哪里不对,在原书中的这个例子有点问题,原封不动抄下来后却跑不起来,应该是在DrawTarget过程中没有定义输入参数,根据个人理解稍加调整后可以运行,改后代码如下:

Sub DrawTarget(CenX As Long, CenY As Long, CenZ As Long)
	'声明变量
	Dim MyLine As LineElement		'LineElement是Mstn线元素
	Dim MyCir As EllipseElement		'EllipseElement是Mstn椭圆元素
	Dim CenPt As Point3d			'声明原点
	Dim LineSt As Point3d			'声明起点
	Dim LineEn As Point3d			'声明终点
	Dim RotMatrix As Matrix3d		'声明平面
	
	'生成水平线,这段代码中的x,y,z没有赋值,调用DrawTarget过程的时候,需要另外赋值才能画出来图形
	LineSt.X = CenX - 1 
	LineSt.Y = CenY 
	LineSt.Z = CenZ 
	LineEn.X = CenX + 1 
	LineEn.Y = CenY 
	LineEn.Z = CenZ 
	Set MyLine = Application.CreateLineElement2(Nothing, LineSt, LineEn) 
	Application.ActiveModelReference.AddElement MyLine 
	
	'生成垂直线,这段代码中的x,y,z没有赋值,调用DrawTarget过程的时候,需要另外赋值才能画出来图形
	LineSt.X = CenX 
	LineSt.Y = CenY + 1 
	LineSt.Z = CenZ 
	LineEn.X = CenX 
	LineEn.Y = CenY - 1 
	LineEn.Z = CenZ 
	Set MyLine = Application.CreateLineElement2(Nothing, LineSt, LineEn) 
	Application.ActiveModelReference.AddElement MyLine 

	'生成圆,这段代码中的x,y,z没有赋值,调用DrawTarget过程的时候,需要另外给圆心赋值才能画出来固定半径的圆形
	CenPt.X = CenX
	CenPt.Y = CenY 
	CenPt.Z = CenZ 
	Set MyCir = Application.CreateEllipseElement2(Nothing, CenPt, 0.25, 0.25, RotMatrix) 
	Application.ActiveModelReference.AddElement MyCir 
	Set MyCir = Application.CreateEllipseElement2(Nothing, CenPt, 0.5, 0.5, RotMatrix) 
	Application.ActiveModelReference.AddElement MyCir 
End Sub

'下面是用Main来调用DrawTarget函数来绘制图形
Sub Main() 
	'绘制靶形图, 输入三个参数来绘制图形
	DrawTarget 0, 0, 0 
	DrawTarget 3, 0, 0 
	DrawTarget -3, 0, 0 
	DrawTarget 0, 3, 0 
	DrawTarget 0, -3, 0 
End Sub 
 

3.按照数组值绘制不固定大小的简单图形--靶心

事先并不知道参数数组中有多少个半径值,有一个半径值就绘制一个靶心, 所以在程序中使用了一个 For…Next 循环来查看每一个值并分别以之为半径来生成新的圆。

Sub DrawCircle4(X As Double, Y As Double, Z As Double, ParamArray Radii() As Variant)
	'声明变量
	Dim MyCir As EllipseElement
	Dim CenPt As Point3d
	Dim RotMatrix As Matrix3d
	Dim I As Long
	'生成圆
	CenPt.X = X
	CenPt.Y = Y
	CenPt.Z = Z
	'LBound函数返回一个 Long 型值,其中包含指示的数组维度的最小可用下标
	'UBound函数返回一个 Long 型值,其中包含指示的数组维度的最大可用下标
	For I = LBound(Radii) To UBound(Radii)		
		Set MyCir = Application.CreateEllipseElement2(Nothing, CenPt, Radii(I),Radii(I),RotMatrix)
		Application.ActiveModelReference.AddElement MyCir
	Next I
End Sub

'调用上面的DrawCircle4过程
Sub Main()
	DrawCircle4 1,1,0,0.25,0.5,0.75,1,1.25,1.5 
	'1,1,0是确定圆心位置的,后面的两个数是数组来绘制6个圆
End Sub


4.返回对象

函数出了返回值和类型外,还能返回对象,下面例子就是:
如果打开的excel中sheet里对应的B2,C2,D2有内容,那么在立即窗口里会立刻显示相应的内容

Function GetExcelWS() As Object		'取得Excel中的当前工作表
	Dim ExcelApp As Object
	Set ExcelApp = GetObject(,"Excel.Application")
	Set GetExcelWs = ExcelApp.activesheet
End Function

Sub TestGetExcelWS()
	Dim MyWS As Object
	Dim Cell1 As Double
	Dim Cell2 As Double
	Dim Cell3 As Double
	Set MyWS = GetExcelWS
	Cell1 = MyWS.Range("B2")
	Cell2 = MyWS.Range("C2")
	Cell3 = MyWS.Range("D2")
End Sub

总结

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

posted @ 2022-06-23 14:58  芋头y  阅读(24)  评论(0编辑  收藏  举报