'//
'//   File Name: ElecMultiStndInput.EBS
'//
'//  (c) Copyright  1997-2000, Bentley
'//   Description:
'//      This script file is used to create a Electrical Instrument Stand.
'//
'//   Local Functions:
'//      Support_setVariableString  - Set a variable in the support file
'//      Support_getVariable        - Get a variable from the support file
'//
'//   $Workfile:   ElecMultiStndInput.ebs  $
'//   $Author: umar.inam $
'//   $Revision: 1.7 $
'//   $Modtime:   Nov 09 2004 16:46:32  $ 
'//
'//   Notes: Function has been developed from ElecStndInput.ebs
'//

Const AT_CADUNITS_USECADDEFAULT  As Integer = 0
Const AT_CADPREC_USECADDEFAULT   As Integer = -1
Const AT_COMPONENT_CLASS         As String = "ClassName"
Dim   CLASSHOLD                  As String

Const AT_DB_TABLE_PIPE           As String   = "PIPING"
Const AT_CURRENT_DOCUMENT        As String   = ""
Const AT_DIALOG_FOREDIT          As Integer = 0
Const AT_DIALOG_FORINPUT         As Integer = 1

'/////////////////////////////////////////////////////////////////////////////////////////
'//
'// Function: getRealInput
'//           Convert the input from a text box into it's double precision equivalent.
'//           We need to use this function to support locales that use a comma
'//           decimal delimiter for numeric data.
'//
'/////////////////////////////////////////////////////////////////////////////////////////

Function getRealInput (dialogControl As String, errorMessage As String) As Double

   Dim textInput     As String
   Dim doubleValue   As Double
   Dim status        As Integer

   On Error GoTo GetTextInput
   realValue# = dialogControl
   If realValue# <= 0 Then
      Call msgbox (errorMessage, ebOKOnly Or ebInformation, at_Msg("AutoPLANT Error", "PIP", 0))
      Err = -1
      On Error Resume Next
      Exit Function
   Else
      getRealInput = realValue#
      Exit Function
   End If
'//  If the input cannot be translated to a number assume it's some other kind of text (architectural input etc..)
   GetTextInput:
   textInput = dialogControl
   status = at_CAD_strToDouble (doubleValue, textInput, AT_CADUNITS_USECADDEFAULT)
   If ((status <> AT_SUCCESS) Or (doubleValue < 0) Or (doubleValue = 0)) Then
      Call msgbox (errorMessage, ebOKOnly Or ebInformation, at_Msg("AutoPLANT Error", "PIP", 0))
      Err = -1
      On Error Resume Next
      getRealInput = 0.0
   Else
      getRealInput = doubleValue
   End If
   Exit Function

End Function
'////////////////////////////////////////////////////////////////////////////
'//
'//  Update a field in the instruments file
'//
'////////////////////////////////////////////////////////////////////////////

Sub Instrument_setVariableString (instrumentClass As String, fieldName As String, fieldValue As String)
 
    status% = at_ConfigurationTable_setString (fieldValue, "", "instrmnt", instrumentClass, fieldName)

   Exit Sub

End Sub

'////////////////////////////////////////////////////////////////////////////
'//
'//  Get a value from the instruments file
'//
'////////////////////////////////////////////////////////////////////////////

Function Instrument_getVariable (instrumentClass As String, fieldName As String) As String

   Dim   stringValue   As String
   
   status% = at_ConfigurationTable_getString (stringValue, "", "instrmnt", instrumentClass, fieldName)
   If (status% = AT_SUCCESS) Then
      Instrument_getVariable = stringValue
   Else
      Instrument_getVariable = 0.0
   End If

   Exit Function
End Function


'////////////////////////////////////////////////////////////////////////////
'//
'//  Input method for electrical Instruments component
'//
'////////////////////////////////////////////////////////////////////////////

Sub at_elect_multi_dialog ( dialogtype As Integer )

   Dim compID                 As At_ComponentID
   Dim InstrPicturesFile      As String
   Dim dialog_status          As Integer
   Dim longDescription        As String
   Dim shortDescription       As String
   Dim mainSize               As String
   Dim pieceMark              As String
   Dim supportClass           As String
   Dim status                 As Integer
   Dim height                 As String
   Dim supdepth               As Double
   Dim supwidth               As Double
   Dim HELP_FILE              As String        'String variable to store HELP FILE path
   Dim supOD                  As String
   Dim class                  As String
   Dim PipeLoc                As String
   Dim alt_Conn               As String
   Dim Diameter				  As Double  '\ArtiomJ
   HELP_FILE = at_app_getHelpFile("INSTR")    'Specify the help file

   status = at_Component_getCurrID(compID)
   status = at_Component_getString(class, compID, "CLASS")

   InstrPicturesFile = at_ComponentDialog_getPictureLibrary (compID)  
      
   If InStr(1, class, "AT_INSTTSTND", 1) Then
        PictureName = "T-STAND"

   ElseIf InStr(1, class, "AT_PLTMNTSTND", 1) Then
        PictureName = "PLTSTAND"

   ElseIf InStr(1, class, "AT_WALMNTSTND", 1) Then
        PictureName = "WALLSTAND"
                     
   End If

   CLASSHOLD = CLASS

   Begin Dialog At_Instrument_Dialog ,,330,235,"Electrical Instruments and Enclosures",.callBackProc,InstrPicturesFile
      Picture 120,2,203,160,PictureName,10,.Picture1,1
      TextBox 71,20,42,12,.supdepth
      Text 8,21,32,8,"Dist A:",.depthText
      TextBox 71,37,42,12,.supwidth
      Text 8,39,32,8,"Dist B:",.widthText
	  TextBox 71,54,42,12,.diameter  '\ArtiomJ new text field for Diameter input
	  Text 8,57,32,8,"OD:",.diamText '\ArtiomJ new field name
      Text 14,172,60,8,"Long Description",.longdescriptiontext
      TextBox 79,170,245,12,.longDescription
      Text 14,188,56,8,"Short Description:",.shortDescriptionText
      TextBox 79,186,108,12,.shortDescription
      Text 232,188,20,8,"Tag:",.TagText
      TextBox 265,186,60,12,.Tag
      OKButton 65,210,50,14
      CancelButton 140,210,50,14
      HelpButton 215,210,50,14,HELP_FILE,5016
   End Dialog

   Dim thisDialog  As At_Instrument_Dialog

   '//  Initialize the dialog
   If (dialogtype) Then
      thisDialog.supdepth = Instrument_getVariable (classhold, "supdepth")
      thisDialog.supwidth = Instrument_getVariable (classhold, "supwidth")
	  thisDialog.diameter = Instrument_getVariable (classhold, "diameter")  '\ArtiomJ Get value from Dialog
      thisDialog.longDescription = Instrument_getVariable (classhold, "LONG_DESCR")
      thisDialog.shortDescription = Instrument_getVariable (classhold, "SHORT_DESC")
      thisDialog.tag = Instrument_getVariable (classhold, "TAG")
   Else
      status = at_Component_getReal (supdepth, compID, "supdepth")
      thisDialog.supdepth = cstr(supdepth)
      status = at_Component_getReal (supwidth, compID, "supwidth")
      thisDialog.supwidth = cstr(supwidth)
	  '\\ArtiomJ shows diameter value in text box
	  status = at_Component_getReal (diameter, compID, "diameter")
      thisDialog.diameter = cstr(diameter)
	  '\\ArtiomJ
      status = at_component_getstring(thisDialog.longDescription,compID,"LONG_DESCR")
      status = at_component_getstring(thisDialog.shortDescription,compID,"SHORT_DESC")
      status = at_component_getstring(thisDialog.tag,compID,"TAGNUMBER")
   End If 

   dialog_status = 1
   status = 0
   dialog_status = Dialog(thisDialog)

      Select Case dialog_status

      '//  Cancel button pressed

      Case 0
         status = at_ScriptBuffer_putInteger (0, "REDRAW_COMPONENT")
         status = at_ScriptBuffer_putInteger (0, "EXECUTE_PORT_METHOD")
         status = AT_ERROR
         Call at_Basic_setUserReturnStatus(status)

      '//  OK Button was pressed

      Case -1
         If thisDialog.SUPDEPTH <> "" Then
            SUPDEPTH = getRealInput (thisDialog.SUPDEPTH, at_Msg("Invalid value specified", "PIP", 0))
         End If
         If thisDialog.SUPWIDTH <> "" Then
            SUPWIDTH = getRealInput (thisDialog.SUPWIDTH, at_Msg("Invalid value specified", "PIP", 0))
         End If
		 '\ArtiomJ
		 If thisDialog.DIAMETER <> "" Then
            DIAMETER = getRealInput (thisDialog.DIAMETER, at_Msg("Invalid value specified", "PIP", 0))
         End If
		 '\ArtiomJ
         If InStr(1, class, "AT_INSTSTND", 1) Then SUPWIDTH = 1
		 
         Call Instrument_setVariableString (classhold, "supdepth", thisDialog.supdepth)
         Call Instrument_setVariableString (classhold, "supwidth", thisDialog.supwidth)
		 Call Instrument_setVariableString (classhold, "diameter", thisDialog.diameter)  '\ArtiomJ
         Call Instrument_setVariableString (classhold, "LONG_DESCR", thisDialog.longDescription)
         Call Instrument_setVariableString (classhold, "SHORT_DESC", thisDialog.shortDescription)
         Call Instrument_setVariableString (classhold, "TAG", thisDialog.tag)
         status = at_Component_putModifiedReal (SUPDEPTH, compID, "SUPDEPTH")
         status = at_Component_putModifiedReal (SUPWIDTH, compID, "SUPWIDTH")
		 status = at_Component_putModifiedReal (DIAMETER, compID, "DIAMETER")  '\ArtiomJ
 
         If (dialogtype) Then
            status = at_Component_putString (thisDialog.longDescription, compID, "LONG_DESCR")
            status = at_Component_putString (thisDialog.shortDescription, compID, "SHORT_DESC")
            status = at_Component_putString (thisDialog.tag, compID, "TAGNUMBER")
         Else
            status = at_DBExt_commit ( AT_DB_TABLE_PIPE, AT_CURRENT_DOCUMENT)
            status = at_DbExt_put (thisDialog.longDescription, AT_DB_TABLE_PIPE, AT_CURRENT_DOCUMENT, compID, "LONG_DESCR")      
            status = at_DbExt_put (thisDialog.shortDescription, AT_DB_TABLE_PIPE, AT_CURRENT_DOCUMENT, compID, "SHORT_DESC")      
            status = at_DbExt_put (thisDialog.tag, AT_DB_TABLE_PIPE, AT_CURRENT_DOCUMENT, compID, "TAGNUMBER") 
            status = at_DBExt_commit ( AT_DB_TABLE_PIPE, AT_CURRENT_DOCUMENT)
         End If            

         status = at_ScriptBuffer_putInteger (1, "REDRAW_COMPONENT")
         status = at_ScriptBuffer_putInteger (1, "EXECUTE_PORT_METHOD")

         status = AT_SUCCESS
         Call at_Basic_setUserReturnStatus(status)

      End Select

End Sub

'////////////////////////////////////////////////////////////////////////////
'//
'//  Callback function for the dialog
'//
'////////////////////////////////////////////////////////////////////////////

Function callBackProc (ControlName$, Action%, SuppValue%)

   Dim status1             As Integer
   Dim status2             As Integer
   Dim styleTxt            As String
   Dim length              As Double
   Dim lenTxt              As String
   Dim supOD               As Double
   Dim supODtxt            As String
   Dim inputTxt            As String
   Dim compID              As At_ComponentID
   Dim strClassDescription As String

   If Action% = 1 Then

'// Initialize labels

      status = at_Component_getCurrID(compID)
      status = at_Component_getString (strClassDescription, compID, "CLASS_DESCRIPTION")
      '//  If there's no class description then use a generic caption
      If (Len(strClassDescription) = 0) Then
         Call DlgCaption (at_Msg("Electrical Stands and Supports", "PIP", 0))  'PIP_1527
      Else
         Call DlgCaption (strClassDescription)
      End If
      Call DlgText ("longDescriptionText", at_Msg("Long Description:", "PIP", 0))
      Call DlgText ("shortDescriptionText", at_Msg("Short Description:", "PIP", 0))
      Call DlgText ("tagText", at_Msg("Tag:", "PIP", 0))
      Call DlgText ("OK", at_Msg("OK", "PIP", 0))
      Call DlgText ("Cancel", at_Msg("Cancel", "PIP", 0))
      Call DlgText ("Help", at_Msg("Help", "PIP", 0))

   End If

   If ((ControlName$ = "OK") And (Action% = 2 Or Action% = 4))  Then


      inputTxt = DlgText$ ("longDescription")
      If inputTxt = "" Then
         Call msgbox (at_Msg("Description field cannot be empty", "PIP", 0),_
                    ebOKOnly Or ebInformation, at_Msg("AutoPLANT Error", "PIP", 0))
         DlgFocus "longDescription"
         callBackProc = 1
         Exit Function
      End If

      inputTxt = DlgText$ ("shortDescription")
      If inputTxt = "" Then
         Call msgbox (at_Msg("Description field cannot be empty", "PIP", 0),_
                    ebOKOnly Or ebInformation, at_Msg("AutoPLANT Error", "PIP", 0))
         DlgFocus "shortDescription"
         callBackProc = 1
         Exit Function
      End If

      inputTxt = DlgText$ ("SUPDEPTH")

      If inputTxt = "" Then
         Call msgbox (at_Msg("Length of support field cannot be empty", "PIP", 0),_
                    ebOKOnly Or ebInformation, at_Msg("AutoPLANT Error", "PIP", 0))
         DlgFocus "SUPDEPTH"
         callBackProc = 1
         Exit Function
      End If

      inputTxt = DlgText$ ("SUPWIDTH")
      If CLASSHOLD = "AT_INSTSTND" Then INPUTTXT = "1"  
      If inputTxt = "" Then
         Call msgbox (at_Msg("Width of support field cannot be empty", "PIP", 0),_
                    ebOKOnly Or ebInformation, at_Msg("AutoPLANT Error", "PIP", 0))
         DlgFocus "SUPWIDTH"
         callBackProc = 1
         Exit Function
	  End If	 
		 '\ArtiomJ
		 inputTxt = DlgText$ ("DIAMETER")
      If CLASSHOLD = "AT_INSTSTND" Then INPUTTXT = "1"  
      If inputTxt = "" Then
         Call msgbox (at_Msg("Diameter of support cannot be empty", "PIP", 0),_
                    ebOKOnly Or ebInformation, at_Msg("AutoPLANT Error", "PIP", 0))
         DlgFocus "DIAMETER"
         callBackProc = 1
         Exit Function
	  End If
		 'ArtiomJ      
   	End If

End Function
'//////////////////////////////////////////////////////////////////////////////////////
'//
'//  Main Entry point for displaying Input dialog
'//
'//////////////////////////////////////////////////////////////////////////////////////

Sub at_elect_multi_input()
   Call at_elect_multi_dialog(AT_DIALOG_FORINPUT)
End Sub

'//////////////////////////////////////////////////////////////////////////////////////
'//
'//  Main Entry point for displaying Edit dialog
'//
'//////////////////////////////////////////////////////////////////////////////////////

Sub at_elect_multi_edit()
   Call at_elect_multi_dialog(AT_DIALOG_FOREDIT)
End Sub
