提示:文章寫完後,目錄可以自動生成,如何生成可參考右邊的幫助文檔 @(MstnVBA學習--Vol1.代碼畫點線--20220623) 前言 2022年6月23日,小白筆記,複習之前的MstnVBA代碼,因為目前還做不到完全自己背誦或編寫出來,雖然簡單,溫故知新~ Mstn中沒有單獨的圓的概念,圓實 ...
提示:文章寫完後,目錄可以自動生成,如何生成可參考右邊的幫助文檔
@
目錄前言
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
總結
上面的都是最最簡單的基礎內容, 感覺對於沒啥經驗的人來說,還是需要練手的, 對於有經驗的人,那這內容應該是太小兒科了~