VB程式碼說明
上一頁 前言 量測概念 MIL量測原理 VB控制項說明 VB程式碼說明 VB執行流程 參考文獻

模組1
模組2

VB程式碼說明  


Const STRIPE_COLOR = 128          '定義目標線段的顏色、寛度、角度為定值

Const STRIPE_WIDTH = 61       

Const STRIPE_ANGLE = 2#

Const IMAGE_WIDTH = 512           '定義影像的寬度、高度、 

Const IMAGE_HEIGHT = 480

Const MEAS_BOX_WIDTH = IMAGE_WIDTH / 2  '定義灰色矩形塊的寬度、高度、位置座標 

Const MEAS_BOX_HEIGHT = 101

Const MEAS_BOX_POS_X = IMAGE_WIDTH / 4

Const MEAS_BOX_POS_Y = IMAGE_HEIGHT / 2 - MEAS_BOX_HEIGHT / 2

Const REFERENCE_POS_X = 60          '定義參考點的位置座標

Const REFERENCE_POS_Y = 45

.

Dim StripeMarkerID As Long     '定義MilID為長整數

Dim PointMarkerID As Long

────────────────────────────────────────────────

  Private Sub Form_Load()      ‘表單主程式

AdjustForm Me    '呼叫AdjustForm模組,自動調整表單顯示大小

         '步驟1:建立旋轉線條

ImageBuffer.Clear 0  '清除影像

             ' 模擬一個旋轉垂直線條的數位影像

GraphicContext.ForegroundColor = STRIPE_COLOR      '設定 GraphicContext的前景色(128)

With GraphicContext.DrawingRegion                   '設定GraphicContext的繪圖區域

 .StartX = MEAS_BOX_POS_X + ((MEAS_BOX_WIDTH / 2) - (STRIPE_WIDTH / 2))     '起始xy座標

 .StartY = MEAS_BOX_POS_Y - ((MEAS_BOX_HEIGHT / 2))                          

 .EndX = MEAS_BOX_POS_X + (((MEAS_BOX_WIDTH / 2) + (STRIPE_WIDTH / 2)) - 1)  '終點xy座標

 .EndY = MEAS_BOX_POS_Y + (((MEAS_BOX_HEIGHT / 2)) * 3)

End With

GraphicContext.Rectangle True                 '填滿矩形的顏色

   

    Call MimRotate(ImageBuffer.MilID, ImageBuffer.MilID, STRIPE_ANGLE, _  '呼叫MimRotate副程式

    M_DEFAULT, M_DEFAULT, M_DEFAULT, M_DEFAULT, M_BILINEAR + M_CLEAR)

    

    Call MimConvolve(ImageBuffer.MilID, ImageBuffer.MilID, M_SMOOTH)   '呼叫MimConvolve副程式

   

lblComment = "This program determines the position, width and angle of the " & _   '在標籤上印出提示

            "stripe in the displayed image and marks its center and edges.  " & _

            "It also determines the length and angle of a line going from " & _

            "a reference point in the image to the center of the stripe.  " & _

            "Click Next to continue.  "

End Sub     '結束主程式

────────────────────────────────────────────────

Private Sub cmdNext_Click()

' Variables global to all example steps.

Static dStripeCenterX As Double  '定義線條X中心位置為雙精度的靜態變數

Static dStripeCenterY As Double  '定義線條Y中心位置為雙精度的靜態變數

   

AdjustStepNumber Me       ' 呼叫AdjustStepNumber副程式調整步驟數並且顯示提示的文字

Select Case cmdNext.Tag      'tag預設為1

  '步驟2:使用MIL應用程式量測線條

Case 2              '如果tag2,執行本區段的程式

                               '定義區域變數.

Dim dStripeWidth As Double        '定義線寬為雙精度小數

Dim dStripeAngle As Double        '定義線角度為雙精度小數

Dim dStripeFirstEdgeX As Double    '定義線條左邊的X座標為雙精度小數

Dim dStripeFirstEdgeY As Double     '定義線條左邊的Y座標為雙精度小數

Dim dStripeSecondEdgeX As Double    '定義線條右邊的X座標為雙精度小數

Dim dStripeSecondEdgeY As Double    '定義線條右邊的Y座標為雙精度小數

           

            ''呼叫MmeasAllocMarke副程式,配置點的位置

Call MmeasAllocMarker(System.MilID, M_STRIPE, M_DEFAULT, StripeMarkerID)

            '呼叫MmeasSetMarkeMmeasSetMarker副程式,指示找出影像中的矩形方塊線條位置

Call MmeasSetMarker(StripeMarkerID, M_BOX_ORIGIN, MEAS_BOX_POS_X, MEAS_BOX_POS_Y)

Call MmeasSetMarker(StripeMarkerID, M_BOX_SIZE, MEAS_BOX_WIDTH, MEAS_BOX_HEIGHT)

            '呼叫MmeasSetMarkeMmeasSetMarker副程式,指示線條大約的方向和對比

Call MmeasSetMarker(StripeMarkerID, M_ORIENTATION, M_VERTICAL, M_NULL)

Call MmeasSetMarker(StripeMarkerID, M_CONTRAST, STRIPE_COLOR, M_NULL)

           

            '呼叫MmeasFindMarker副程式,求得線條並且測量寬度和角度

Call MmeasFindMarker(M_DEFAULT, ImageBuffer.MilID, StripeMarkerID, _

                    M_POSITION + M_WIDTH + M_ANGLE)

            '呼叫MmeasGetResult副程式,得到線條的位置、寬度和角度

Call MmeasGetResult(StripeMarkerID, M_POSITION,dStripeCenterX, dStripeCenterY)

Call (StripeMarkerID, M_POSITION + M_EDGE_FIRST, dStripeFirstEdgeX, dStripeFirstEdgeY)

Call MmeasGetResult(StripeMarkerID, M_POSITION + M_EDGE_SECOND,dStripeSecondEdgeX, dStripeSecondEdgeY)  

Call MmeasGetResult(StripeMarkerID, M_WIDTH, _dStripeWidth, M_NULL)

Call MmeasGetResult(StripeMarkerID, M_ANGLE, _ dStripeAngle, M_NULL)

              

With CrossGraphicContext.DrawingRegion    '在找到左邊線條、右邊線條的中心和中心點各畫出一個十字記號

 .CenterX = dStripeCenterX                '設定十字中心的XY座標(中心點)

 .CenterY = dStripeCenterY

 CrossGraphicContext.Cross               '畫出十字記號

 .CenterX = dStripeFirstEdgeX             '設定十字中心的XY座標(第一個邊)

 .CenterY = dStripeFirstEdgeY

  CrossGraphicContext.Cross              '畫出十字記號

 .CenterX = dStripeSecondEdgeX           '設定十字中心的XY座標(第二個邊)

 .CenterY = dStripeSecondEdgeY

  CrossGraphicContext.Cross              '畫出十字記號

End With

 

           lblComment = "The stripe in the image is at position (" & _   '印出步驟2的提示文字

            Format(dStripeCenterX, "####0.00") & ", " & _

            Format(dStripeCenterY, "####0.00") & ") and is " & _

          Format(dStripeWidth, "####0.00") & " pixels wide with an angle of " & _

            Format(dStripeAngle, "####0.00") & " degrees.  " & _

            "Its center and edges have been marked.  " & _

            "Click Next to continue.  "

                       '步驟3:從參考點(矩形中心)求得距離

Case 3         '如果tag3,執行本區段的程式

                                 '定義區域變數

Dim dReferenceDistance As Double    '定義參考線距離為雙精度小數

Dim dReferenceAngle As Double      '定義參考線角度為雙精度小數

           

             '呼叫MmeasAllocMarke副程式,配置點的位置

Call MmeasAllocMarker(System.MilID, M_POINT, M_DEFAULT, PointMarkerID)

           

              '呼叫MmeasSetMarker副程式,指出影像中參考點的位置

Call MmeasSetMarker(PointMarkerID, M_POSITION, REFERENCE_POS_X, REFERENCE_POS_Y)

              '呼叫MmeasAllocResult副程式,配置計算結果的暫存區

Call MmeasAllocResult(System.MilID, M_CALCULATE, CalcResultsID)

             ' 呼叫MmeasCalculate 副程式,計算參考點和線條之間的距離和角度

Call (M_DEFAULT, PointMarkerID, StripeMarkerID,CalcResultsID, M_DISTANCE + M_ANGLE)

             ' 呼叫MmeasGetResultMmeasGetResult,得到線條的距離和角度

Call MmeasGetResult(CalcResultsID, M_DISTANCE, dReferenceDistance, M_NULL)

Call MmeasGetResult(CalcResultsID, M_ANGLE, dReferenceAngle, M_NULL)

           

With CrossGraphicContext.DrawingRegion      '設定十字記號的中心位置座標(XY

 .CenterX = REFERENCE_POS_X      

 .CenterY = REFERENCE_POS_Y

End With

CrossGraphicContext.Cross                   '畫一個十字記號

           

With GraphicContext.DrawingRegion           '從參考點畫一條線到找到線條的中心

 .StartX = REFERENCE_POS_X

 .StartY = REFERENCE_POS_Y

 .EndX = dStripeCenterX

 .EndY = dStripeCenterY

End With

GraphicContext.ForegroundColor = 250    '設定前景色為250

GraphicContext.LineSegment

lblComment = "The distance and angle from the drawn reference point are " & _      '印出步驟3的提示文字

            Format(dReferenceDistance, "#####0.00") & " pixels and " & _

            Format(dReferenceAngle, "#####0.00") & " degrees.  " & _

          "Click Next to end.  "

Case Else

                     '最後步驟:結束程式的應用

Unload Me      '將表單從記憶體中釋放掉

End Select

End Sub       '結束程式

────────────────────────────────────────────────

Private Sub Form_Unload(Cancel As Integer)  '釋放記憶體時觸發的程式

If StripeMarkerID Then        

Call MmeasFree(StripeMarkerID)        '呼叫MmeasFree副程式釋放MILID

End If

 

If PointMarkerID Then

Call MmeasFree(PointMarkerID)     '呼叫MmeasFree副程式釋放MILID

End If

End Sub              '結束程式