VERSION 5.00
Begin VB.Form FrmMain 
   Caption         =   "Form1"
   ClientHeight    =   7845
   ClientLeft      =   60
   ClientTop       =   600
   ClientWidth     =   12885
   Icon            =   "CommandLineInterface.frx":0000
   LinkTopic       =   "Form1"
   ScaleHeight     =   7845
   ScaleWidth      =   12885
   StartUpPosition =   3  'Windows Default
   Begin VB.CheckBox chkAutoClear 
      Caption         =   "AutoClear"
      Height          =   255
      Left            =   3720
      TabIndex        =   8
      Top             =   780
      Value           =   1  'Checked
      Width           =   1575
   End
   Begin VB.CheckBox chkAutoPush 
      Caption         =   "AutoPush"
      Height          =   375
      Left            =   240
      TabIndex        =   7
      Top             =   720
      Value           =   1  'Checked
      Width           =   1695
   End
   Begin VB.CheckBox chkAutoRefresh 
      Caption         =   "AutoRefresh"
      Height          =   375
      Left            =   2040
      TabIndex        =   6
      Top             =   720
      Value           =   1  'Checked
      Width           =   1575
   End
   Begin VB.CommandButton CmdClose 
      Caption         =   "Close"
      Height          =   375
      Left            =   10920
      TabIndex        =   5
      Top             =   720
      Width           =   1815
   End
   Begin VB.TextBox Text3 
      Height          =   6375
      Left            =   240
      MultiLine       =   -1  'True
      ScrollBars      =   2  'Vertical
      TabIndex        =   3
      Text            =   "CommandLineInterface.frx":08CA
      Top             =   1200
      Width           =   12500
   End
   Begin VB.CommandButton Command1 
      Caption         =   "Send to ZEMAX"
      Height          =   375
      Left            =   11000
      TabIndex        =   2
      Top             =   240
      Width           =   1815
   End
   Begin VB.TextBox Text2 
      Height          =   375
      Left            =   240
      ScrollBars      =   2  'Vertical
      TabIndex        =   1
      Text            =   "Text2"
      Top             =   240
      Width           =   10700
   End
   Begin VB.TextBox Text1 
      Height          =   285
      Left            =   9600
      TabIndex        =   0
      Text            =   "Text1"
      Top             =   720
      Visible         =   0   'False
      Width           =   855
   End
   Begin VB.Label lblVersion 
      Caption         =   "Version"
      Height          =   255
      Left            =   240
      TabIndex        =   4
      Top             =   7560
      Width           =   1095
   End
End
Attribute VB_Name = "FrmMain"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim mstrPushLensMode As String
' This is used to control the PushLens command, which is version-specific

Private Sub chkAutoRefresh_Click()
' Set PushLens depending on the value of this checkbox

If (chkAutoRefresh) Then
    mstrPushLensMode = "PushLens, 1"
Else
    mstrPushLensMode = "PushLens, 0"
End If

End Sub

Private Sub CmdClose_Click()
Unload Me

End Sub

Private Sub Command1_Click()
On Error GoTo Handle_Errors

' Send the text in Text 2 to ZEMAX, and echo it to Text 3
' The get the response from ZEMAX, and copy it to Text 3

' First send data to ZEMAX. This is a synchronous interface
' so we must wait for a response

Text1.LinkItem = Text2.Text    ' Set LinkItem.
Text1.LinkRequest              ' Send DDE request.
' The response is stored in Text1.text
                           
'Now write the data to text3

Text3.Text = Text3.Text & "Me> " & Text2.Text & vbCrLf
Text3.Text = Text3.Text & "ZEMAX> " & Text1.Text
Text3.SelStart = Len(Text3.Text) ' Move to end of the text box

' If Autopush is selected, send PushLens automatically

If (chkAutoPush) Then
    
    Text1.LinkItem = mstrPushLensMode   ' Set LinkItem.
    Text1.LinkRequest                   ' Send DDE request.

End If

' finally clear text2 if chkAutoClear is set
If (chkAutoClear) Then Text2.Text = ""

Exit Sub

Handle_Errors:

    Select Case Error_Handler
    
    Case 1 'Abort
        Unload Me
    Case 2 'Retry
        Resume
    Case 3 ' Ignore
        Text1.Text = "An error occurred and was ignored." & vbCrLf
        Text3.Text = Text3.Text & "Me> " & Text2.Text & vbCrLf
        Text3.Text = Text3.Text & "ZEMAX> " & Text1.Text
        Text3.SelStart = Len(Text3.Text)
    End Select
End Sub




Private Sub Form_Load()
On Error GoTo Handle_Errors
Dim intDummy As Integer

' This program written by Mark Nicholson, November 2004
' For teaching and information purposes only
' No warranty, express or implied etc
' Comments and suggestions to mark.nicholson@optima-research.com

Me.ScaleMode = 1 ' use twips as unit of measure
Me.Height = 8500
Me.Width = 13000


lblVersion = "Version " & App.Major & "." & App.Minor & "." & App.Revision
Text3.Text = ""
Text2.Text = ""
Text1.Visible = False
Me.Caption = "Connecting to ZEMAX..."
Text2.FontSize = 12
Text2.FontBold = True
Text3.FontSize = 12
Text3.FontBold = True

With Text1                      ' This is the part of the code that talks to ZEMAX

    .Text = ""                  ' The Text control is a DDE client
    .LinkMode = vbLinkNone      ' Turn off DDE communication
                                ' Now set the application name and topic name.
    .LinkTopic = "ZEMAX| Any non-null string"
    .LinkMode = vbLinkManual    ' Set LinkMode to Manual.
    .LinkItem = "GetSerial"     ' Set LinkItem.
    .LinkRequest                ' Send DDE request.
                                ' The response is stored in Text1.text
 
End With

Me.Caption = "Connected to ZEMAX key " & Left$(Text1.Text, Len(Text1.Text) - 2)

' Get ZEMAX version number

With Text1
    .LinkItem = "GetVersion"
    .LinkRequest
End With

If (Val(Text1.Text) <= 41101) Then ' If ZEMAX version on or before 1 Nov 2004
    mstrPushLensMode = "PushLens"
    chkAutoRefresh.Enabled = False
    chkAutoRefresh.Visible = False
    Me.Visible = True
    intDummy = MsgBox("Please make sure that extensions are allowed to push lenses to" & vbCrLf & "the Lens Data Editor, under File...Preferences...Editors", vbOKOnly, "Communications with ZEMAX")
    
Else
    mstrPushLensMode = "PushLens, 1"
    chkAutoRefresh.Enabled = True
    chkAutoRefresh.Value = 1
    chkAutoRefresh.Visible = True
    Me.Visible = True
    ' now test to see if pushing be extensions is valid
    
    With Text1
        .LinkItem = "PushLensPermission"
        .LinkRequest
        If (.Text <> "1" & vbCrLf) Then
            intDummy = MsgBox("Please make sure that extensions are allowed to push lenses to" & vbCrLf & "the Lens Data Editor, under File...Preferences...Editors", vbOKOnly, "Communications with ZEMAX")
        End If
    End With
    
End If

Exit Sub

Handle_Errors:

    Select Case Error_Handler
    
    Case 1 'Abort
        Unload Me
    Case 2 'Retry
        Resume
    Case 3 ' Ignore
        Resume Next
    End Select

End Sub

Private Sub Label1_Click()

End Sub

Private Sub Form_Resize()
' Define how the form should behave  when resized.
' The form has a minimum width and height equal to its start width and height.

' First check if the from has been minimized, and quit if it is

If (FrmMain.WindowState = vbMinimized) Then Exit Sub

FrmMain.ScaleMode = 1 ' use twips as unit of measure

If (FrmMain.Height < 3000) Then FrmMain.Height = 3000 ' enforce minimum height
If (FrmMain.Width < 7000) Then FrmMain.Width = 7000   ' enforce minimum width
    
' When resized, the following happens:

' the command buttons move to start the same distance away horizontally from the RHS edge.

Command1.Left = FrmMain.Width - 2000
CmdClose.Left = FrmMain.Width - 2000

' Text 2 widens to fill the extra horizontal space

Text2.Width = FrmMain.Width - 2300

' Text3  widens to fill the extra horizontal and vertical space

Text3.Width = FrmMain.Width - 500
Text3.Height = FrmMain.Height - 2200

' The version label stays at the same place at the bottom of the form

lblVersion.Top = FrmMain.Height - 900
' no other controls move.
End Sub

Private Sub Text2_KeyPress(KeyAscii As Integer)

' If the user presses RETURN, treat it like clicking the command button
If KeyAscii = 13 Then Call Command1_Click

End Sub

