Send Topic Feedback | See Object Hierarchy Chart
PC-DMIS 2022.2 Object Library
Sample Automation Script 3

Sample Automation Script 3 - Create a Custom Comment Dialog Box

This script is more involved than the previous examples as it involves dialog box creation and reading and writing data. Specifically, it recreates a customized version of the Comment dialog box found in PC-DMIS, showing that you can create a customized dialog box that matches your exact specifications to perform specific tasks.

My Comment dialog box

 

This script functions nearly identically to the Comment dialog box in PC-DMIS, with one exception. It allows you to change and store the default comment text that appears, thereby demonstrating how to write and read text strings to and from a file.

Custom Dialog Box Example
Copy Code
 Sub Main
  Dim App As Object
  Set App = CreateObject ("PCDLRN.Application")
  Dim Part As Object
  Set Part = App.ActivePartProgram
  Dim Cmds As Object
  Set Cmds = Part.Commands
  Dim Cmd As Object

  Dim strText As String
  Dim optVal As Integer
  Dim lngCommentType As Long
  Dim buttonval As Integer
  Dim strDefaultText As String
  Dim strLine As String

Start: ' This label is used to quickly redisplay the Dialog after changing the default text.

' Reads in stored comment text.
' This uses text from a text file named default_comment.txt and reads it.
' This creates the file to read from and write to, if it doesn't exist. If it exists, it simply opens the file and then
' closes it.
Open "default_comment.txt" For Append As #1 ' This creates it if it doesn't exist.
Close #1

' This opens the file for writing, and puts the info into strLine variable
  Open "default_comment.txt" For Input As #1
    Do While Not EOF(1)
      Line Input #1, strLine
    Loop

    If len(strLine)>0 Then
   ' Removes automatic quotation marks
     strLine = mid(strLine,2,len(strLine)-3)
     strDefaultText = strLine
   Else
    ' If there isn't any default value to read in, it uses and sets this
    strDefaultText = "Type your comment text here."
    Open "default_comment.txt" For Output As #2
    Write #2, strDefaultText
    Close #2
   End If

Close #1 ' Closes the Open file handler

 'Creates the dialog box
Begin Dialog DLG_CUSTOM_COMMENT 106,15, 143, 177, "My Comment"
  OptionGroup .GROUP_1
    OptionButton 20,12,48,24, "Operator"
    OptionButton 20,28,40,24, "Report"
    OptionButton 20,44,48,24, "Document"
    OptionButton 76,12,32,24, "Input"
    OptionButton 76,28,40,24, "Yes/No"
    OptionButton 76,44,44,24, "ReadOut"
  GroupBox 12,4,116,68, "Comment Type"
  TextBox 12,88,116,44, .EditBox_1
  Text 12,76,56,12, "Comment Text"
  OKButton 20,160,44,12
  CancelButton 72,160,52,12
  PushButton 20,140,104,12, "Change Default Text", .PushButton_1
End Dialog

Dim dlg1 As DLG_CUSTOM_COMMENT
dlg1.EditBox_1 = strDefaultText

 buttonval = Dialog (dlg1)
  strCommentText = dlg1.EditBox_1
  optValue = dlg1.GROUP_1

' If OK is clicked, run this routine
If buttonval = -1 Then
    optVal = dlg1.GROUP_1

' If it's an Operator Comment
    If optVal = 0 Then    
        ' strCommentType is For testing only.
        strCommentType = "Operator"
        ' This variable determines the Type of comment
        lngCommentType = 0
    End If

' If it's a Report Comment
    If optVal = 1 Then
        strCommentType = "Report"
        lngCommentType = 2
   End If
   
' If it's a Document Comment
    If optVal = 2 Then
        strCommentType = "Document"
        lngCommentType = 4
    End If

' If it's a a Input Comment
    If optVal = 3 Then
        strCommentType = "Input"
        lngCommentType = 3
    End If

'If it's a Yes/No Comment
    If optVal = 4 Then
        strCommentType = "Yes/No"
        lngCommentType = 5
    End If
   
' If it's a Readout Comment
    If optVal = 5 Then
        strCommentType = "Readout"
        lngCommentType = 6
    End If

    ' This statement adds the initial comment
    Set Cmd = Cmds.Add(SET_COMMENT, True)
    ' This statement changes the comment Type To lngCommentType
    Cmd.SetToggleString lngCommentType, COMMENT_TYPE, 0
    ' This statement displays the comment text.
    Cmd.PutText strCommentText, COMMENT_FIELD, 1

    ' Redraws the Edit window
    Part.Refreshpart

' If the pushbutton to set new default text is clicked, then collect and write the new default
ElseIf buttonval = 1 Then
  strDefaultText = InputBox ("Type the new default comment text","Change Default Comment Text","")
  Open "default_comment.txt" For Output As #1
    Write #1, strDefaultText
  Close #1
  GoTo Start ' After setting the new default comment text, the program flow goes the Start: label

' If Cancel is clicked...
Else
    '...the dialog closes without doing anything
End If
End Sub