c
c   Copyright (c) 1992  by  Dwight Meglan                                      
c                       
      subroutine SetOptions( CMND,OPT3D )
      implicit none       

      include 'show3d.inc'             
     
      include '..\anz\a_const.inc'
      include '..\anz\a_struct.inc'
      include '..\anz\a_names.inc'
      include '..\anz\a_prm.inc'

      record /CmndLine/    CMND
      record /options_3d/  OPT3D
      record /Plot3D_flag/ VU3D_FLG

      integer*4 NumOpt,i                       
      character OptTxt(32)*3,SegText*4,JntText*4
      logical*4 SetOnOff,OptOK,vnrm,GetSegNum,GetJntNum

      data NumOpt / 32 /
      data OptTxt / 'INC','STA','END','STE','ERA', ! #   #   #      on/off on/off
     &              'WIR','HLN','HFC','STK', ! on/off on/off on/off on/off
     &              'SCN','JCN',             ! on/off/# on/off/#
     &              'SGC','LCS','GCS','FPT', ! on/off on/off on/off on/off
     &              'ROT',                   ! on/off
     &              'PRI','SCR','PST',       ! on/off on/off on/off
     &              'MWN','WNW',             ! on/off #
     &              'VPT','VDR','VUP','VCN', ! #x3 #x3 #x3 #x3
     &              'CPR','GRF','GRM',       ! on/off on/off on/off
     &              'SCF','SCM','FRN','CON' /            ! # # on/off on/off

      if ( CMND.CmndText(2) .eq. 'RES' ) then
        call ResetOptions( OPT3D )
        return
      end if
c
c     Do a quick check to see if option is valid
c                           
      OptOK= .false.
      do i= 1, NumOpt
        if ( CMND.CmndText(2) .eq. OptTxt(i) ) OptOK= .true.
      end do
      if ( .not. OptOK ) goto 1000 ! error
c                        
c     A valid option is present so go on and interpret the rest of the command
c
      if ( CMND.CmndText(2) .eq. 'STA' ) then ! set start frame number
        if ( (.not. CMND.CmndNumOK(3)) .or.
     &       (CMND.CmndIntVal(3) .lt. 0) .or. 
     &       (CMND.CmndIntVal(3) .gt. MP.nfrm) ) goto 1010 ! error
        if ( CMND.CmndIntVal(3) .gt. OPT3D.EndFrm ) goto 1017 ! error
        OPT3D.StartFrm= CMND.CmndIntVal(3)

      else if ( CMND.CmndText(2) .eq. 'INC' ) then ! set frame increment
        if ( (.not. CMND.CmndNumOK(3)) .or.
     &       (CMND.CmndIntVal(3) .lt. 0) .or. 
     &       (CMND.CmndIntVal(3) .gt. MP.nfrm) ) goto 1020 ! error
        OPT3D.FrmInc= CMND.CmndIntVal(3)
                                          
      else if ( CMND.CmndText(2) .eq. 'END' ) then ! set end frame number
        if ( (.not. CMND.CmndNumOK(3)) .or.
     &       (CMND.CmndIntVal(3) .lt. 0) .or. 
     &       (CMND.CmndIntVal(3) .gt. MP.nfrm) ) goto 1020 ! error
        if ( CMND.CmndIntVal(3) .lt. OPT3D.StartFrm ) goto 1160
        OPT3D.EndFrm= CMND.CmndIntVal(3)
                                                               
      else if ( CMND.CmndText(2) .eq. 'STE' ) then ! set step mode 
        if ( CMND.NumCmnd .lt. 3 ) goto 1030 ! error
        if ( .not. SetOnOff(CMND.CmndText(3),OPT3D.StepControl) ) goto 1040 ! error
                                                              
      else if ( CMND.CmndText(2) .eq. 'CON' ) then ! set continuous mode
        if ( CMND.NumCmnd .lt. 3 ) goto 1030 ! error
        if ( .not. SetOnOff(CMND.CmndText(3),OPT3D.Continuous) ) goto 1040 ! error

      else if ( CMND.CmndText(2) .eq. 'ERA' ) then ! set erase frame mode 
        if ( CMND.NumCmnd .lt. 3 ) goto 1030 ! error
        if ( .not. SetOnOff(CMND.CmndText(3),OPT3D.EraseFrm) ) goto 1040 ! error

      else if ( CMND.CmndText(2) .eq. 'WIR' ) then ! show segment wire frame
        if ( CMND.NumCmnd .lt. 3 ) goto 1030 ! error
        if ( .not. SetOnOff(CMND.CmndText(3),OPT3D.WireFrame) ) goto 1040 ! error
        if ( OPT3D.WireFrame ) OPT3D.HiddenLine= .true. ! default

      else if ( CMND.CmndText(2) .eq. 'STK' ) then ! show stick figure
        if ( CMND.NumCmnd .lt. 3 ) goto 1030 ! error
        if ( .not. SetOnOff(CMND.CmndText(3),OPT3D.StickFigure) ) goto 1040 ! error
        if ( OPT3D.StickFigure ) then
          OPT3D.WireFrame= .false.
          OPT3D.HiddenLine= .false.
        else
          OPT3D.HiddenLine= .true.
        end if

      else if ( CMND.CmndText(2) .eq. 'HLN' ) then ! set hiddenline mode
        if ( CMND.NumCmnd .lt. 3 ) goto 1030 ! error
        if ( .not. SetOnOff(CMND.CmndText(3),OPT3D.HiddenLine) ) goto 1040 ! error
        if ( OPT3D.HiddenLine ) OPT3D.HideFace= .false.
                         
      else if ( CMND.CmndText(2) .eq. 'HFC' ) then ! set hiddenline mode
        if ( CMND.NumCmnd .lt. 3 ) goto 1030 ! error
        if ( .not. SetOnOff(CMND.CmndText(3),OPT3D.HideFace) ) goto 1040 ! error
        if ( OPT3D.HideFace ) OPT3D.HiddenLine= .false.

      else if ( CMND.CmndText(2) .eq. 'SCN' ) then ! center view on segment
        if ( CMND.NumCmnd .lt. 3 ) goto 1050 ! error
        if ( .not. SetOnOff(CMND.CmndText(3),OPT3D.SegCentered) ) then
          SegText= CMND.Text(1)(CMND.CmndStart(3):CMND.CmndStart(3)+3)
          if ( .not. GetSegNum(SegText,OPT3D.CntrSegNum) ) goto 1060 ! error
          OPT3D.SegCentered= .true.
        end if
        if ( OPT3D.SegCentered ) OPT3D.JntCentered= .false.

      else if ( CMND.CmndText(2) .eq. 'JCN' ) then ! center view on segment
        if ( CMND.NumCmnd .lt. 3 ) goto 1070 ! error
        if ( .not. SetOnOff(CMND.CmndText(3),OPT3D.JntCentered) ) then
          JntText= CMND.Text(1)(CMND.CmndStart(3):CMND.CmndStart(3)+3)
          if ( .not. GetJntNum(JntText,OPT3D.CntrJntNum) ) goto 1080 ! error
          OPT3D.JntCentered= .true.
        end if
        if ( OPT3D.JntCentered ) OPT3D.SegCentered= .false.

      else if ( CMND.CmndText(2) .eq. 'SGC' ) then ! show segment LCS center
        if ( CMND.NumCmnd .lt. 3 ) goto 1030 ! error
        if ( .not. SetOnOff(CMND.CmndText(3),OPT3D.ShowSegCntr) ) goto 1040 ! error

      else if ( CMND.CmndText(2) .eq. 'LCS' ) then ! show segment LCS 
        if ( CMND.NumCmnd .lt. 3 ) goto 1030 ! error
        if ( .not. SetOnOff(CMND.CmndText(3),OPT3D.ShowSegLCS) ) goto 1040 ! error

      else if ( CMND.CmndText(2) .eq. 'GCS' ) then ! show GCS
        if ( CMND.NumCmnd .lt. 3 ) goto 1030 ! error
        if ( .not. SetOnOff(CMND.CmndText(3),OPT3D.ShowGCS) ) goto 1040 ! error

      else if ( CMND.CmndText(2) .eq. 'FPT' ) then ! show forceplate outline
        if ( CMND.NumCmnd .lt. 3 ) goto 1030 ! error
        if ( .not. SetOnOff(CMND.CmndText(3),OPT3D.ShowFrcPlt) ) goto 1040 ! error

      else if ( CMND.CmndText(2) .eq. 'ROT' ) then ! Remove initial rotation
        if ( CMND.NumCmnd .lt. 3 ) goto 1030 ! error
        if ( .not. SetOnOff(CMND.CmndText(3),OPT3D.UseInitRotCor) ) goto 1040 ! error

      else if ( CMND.CmndText(2) .eq. 'PRI' ) then ! generate print file
        if ( CMND.NumCmnd .lt. 3 ) goto 1030 ! error
        if ( .not. SetOnOff(CMND.CmndText(3),OPT3D.Print) ) goto 1040 ! error
        if ( .not. OPT3D.Print ) OPT3D.Screen= .true.

      else if ( CMND.CmndText(2) .eq. 'SCR' ) then ! send graphics to screen
        if ( CMND.NumCmnd .lt. 3 ) goto 1030 ! error
        if ( .not. SetOnOff(CMND.CmndText(3),OPT3D.Screen) ) goto 1040 ! error
        if ( .not. OPT3D.Screen ) OPT3D.Print= .true.

      else if ( CMND.CmndText(2) .eq. 'PST' ) then ! using postscript printer
        if ( CMND.NumCmnd .lt. 3 ) goto 1030 ! error
        if ( .not. SetOnOff(CMND.CmndText(3),OPT3D.UseMonoDisplay) ) goto 1040 ! error

      else if ( CMND.CmndText(2) .eq. 'MWN' ) then ! use manual window
        if ( CMND.NumCmnd .lt. 3 ) goto 1030 ! error
        if ( .not. SetOnOff(CMND.CmndText(3),OPT3D.UseManWin) ) goto 1040 ! error
                                           
      else if ( CMND.CmndText(2) .eq. 'WNW' ) then ! set manual window width
        if ( CMND.NumCmnd .lt. 3 ) goto 1030 ! error
        if ( (CMND.CmndRealVal(3) .lt. 0.0) .or.
     &       (.not. CMND.CmndNumOK(3))          ) goto 1090
        OPT3D.WindowWidth= CMND.CmndRealVal(3)

      else if ( CMND.CmndText(2) .eq. 'VPT' ) then ! set manual viewpoint
        if ( CMND.NumCmnd .lt. 5 ) goto 1100 ! error
        do i= 3, 5
          if ( .not. CMND.CmndNumOK(i) ) goto 1110 ! error
          OPT3D.ManVwPnt(i-2)= CMND.CmndRealVal(i)
        end do
       
      else if ( CMND.CmndText(2) .eq. 'VDR' ) then ! set manual view direction
c        if ( CMND.NumCmnd .lt. 5 ) goto 1100 ! error
c        do i= 3, 5
c          if ( .not. CMND.CmndNumOK(i) ) goto 1110 ! error
c          OPT3D.ManVwDir(i-2)= CMND.CmndRealVal(i)
c        end do                             
c        if ( .not. vnrm(OPT3D.ManVwDir,OPT3D.ManVwDir) ) goto 1120 ! error
	write(6,311)
 311    format(' Manual view direction no longer works- use VCN & VPT instead')

      else if ( CMND.CmndText(2) .eq. 'VUP' ) then ! set manual view up vector
        if ( CMND.NumCmnd .lt. 5 ) goto 1100 ! error
        do i= 3, 5
          if ( .not. CMND.CmndNumOK(i) ) goto 1110 ! error
          OPT3D.ManUpVec(i-2)= CMND.CmndRealVal(i)
        end do                                                   
        if ( .not. vnrm(OPT3D.ManUpVec,OPT3D.ManUpVec) ) goto 1130 ! error
                                           
      else if ( CMND.CmndText(2) .eq. 'VCN' ) then ! set manual view center
        if ( CMND.NumCmnd .lt. 5 ) goto 1100 ! error
        do i= 3, 5
          if ( .not. CMND.CmndNumOK(i) ) goto 1110 ! error
          OPT3D.ManVwCntr(i-2)= CMND.CmndRealVal(i)
        end do                             
       
      else if ( CMND.CmndText(2) .eq. 'CPR' ) then ! show the center of press.
        if ( CMND.NumCmnd .lt. 3 ) goto 1030 ! error
        if ( .not. SetOnOff(CMND.CmndText(3),OPT3D.ShowCP) ) goto 1040 ! error
                                           
      else if ( CMND.CmndText(2) .eq. 'GRF' ) then ! show force of GRL
        if ( CMND.NumCmnd .lt. 3 ) goto 1030 ! error
        if ( .not. SetOnOff(CMND.CmndText(3),OPT3D.ShowGRF) ) goto 1040 ! error
                                           
      else if ( CMND.CmndText(2) .eq. 'GRM' ) then ! show normal moment of GRL
        if ( CMND.NumCmnd .lt. 3 ) goto 1030 ! error
        if ( .not. SetOnOff(CMND.CmndText(3),OPT3D.ShowGRM) ) goto 1040 ! error
                                           
      else if ( CMND.CmndText(2) .eq. 'SCF' ) then ! set force scaling
        if ( CMND.NumCmnd .lt. 3 ) goto 1030 ! error
        if ( .not. CMND.CmndNumOK(3) ) goto 1140
        OPT3D.ScaleFRC= CMND.CmndRealVal(3)
                                           
      else if ( CMND.CmndText(2) .eq. 'SCM' ) then ! set moment scaling
        if ( CMND.NumCmnd .lt. 3 ) goto 1030 ! error
        if ( .not. CMND.CmndNumOK(3) ) goto 1150
        OPT3D.ScaleMOM= CMND.CmndRealVal(3)

      else if ( CMND.CmndText(2) .eq. 'FRN' ) then ! show frame number
        if ( CMND.NumCmnd .lt. 3 ) goto 1030 ! error
        if ( .not. SetOnOff(CMND.CmndText(3),OPT3D.ShowFrmNum) ) goto 1040 ! error
                                           
      end if
      return           
                                    
1000  write(6,1005) CMND.CmndText(2)
1005  format(' Error- ',a3,' is not a valid option'/
     &       ' Valid options are:'/      
     &       '  RESet '/
     &       '  INCrement #       STArt      #    END   #'/
     &       '  STEpmode  ON/OFF  ERAse ON/OFF    CONtinuous ON/OFF'//
     &       '  WIReframe ON/OFF  HiddenLiNe ON/OFF  HiddenFaCe ON/OFF'//
     &       '  SegmentCeNtered ON/OFF/segname'/
     &       '  JointCeNtered   ON/OFF/jntname'//
     &       '  ShowseGCenter ON/OFF  showLCS        ON/OFF'/
     &       '  showGCS       ON/OFF  showForcePlaTe ON/OFF'//
     &       '  removeinitialROTation ON/OFF'//
     &       '  PRInt     ON/OFF  SCReen ON/OFF     PoSTscript ON/OFF'//
     &       '  WiNdowWidth #       ViewPoinT  # # #'/
c     &       '  WiNdowWidth #       ViewPoinT  # # #   ViewDiRection # # #'/
     &       '  ViewUPvector # # #  ViewCeNter # # #'//
     &       '  showCntrPRes ON/OFF  showGRndFrc ON/OFF  showGRndMom ON/OFF'//
     &       '  showFRameNumber ON/OFF'//
     &       '  SCaleForce #   SCaleMom #')
      return                                                                 
                                           
1010  write(6,1015) CMND.CmndText(3),MP.nfrm
1015  format(' Error- ',a3,' is not a valid starting frame number'/
     &       '        Valid frame numbers are from 1 to ',i5)
      return           
  
1017  write(6,1019) CMND.CmndIntVal(3),OPT3D.EndFrm
1019  format(' Error- The start frame (',i4,') cannot be greater than the end',
     &       ' frame (',i4,')')
      return                                              
 
1020  write(6,1025) CMND.CmndText(3),MP.nfrm
1025  format(' Error- ',a3,' is not a valid frame increment number'/
     &       '        Valid frame numbers are from 1 to ',i5)
      return           
      
1030  write(6,1035) CMND.CmndText(2)
1035  format(' Error- The ',a3,' option must be followed by either ON or OFF')
      return

1040  write(6,1045) CMND.CmndText(3)
1045  format(' Error- ',a3,' is not a valid parameter'/
     &       '        The only valid parameters are ON and OFF')
      return                                                               
     
1050  write(6,1055) 
1055  format(' Error- The SegmentCeNter option must be followed by ON, OFF, or'/
     &       '        the name of a segment upon which the display is centered')
      return

1060  write(6,1065) CMND.CmndText(3) 
1065  format(' Error- ',a3,' is not a valid option for SegmentCeNter'/
     &       '        The SegmentCeNter option must be followed by ON, OFF, or'/
     &       '        the name of a segment upon which the display is centered')
      OPT3D.CntrSegNum= 1
      return

1070  write(6,1075) 
1075  format(' Error- The JointCeNter option must be followed by ON, OFF, or'/
     &       '        the name of a joint upon which the display is centered')
      return

1080  write(6,1085) CMND.CmndText(3) 
1085  format(' Error- ',a3,' is not a valid option for JointCeNter'/
     &       '        The JointCeNter option must be followed by ON, OFF, or'/
     &       '        the name of a joint upon which the display is centered')
      return                                              

1090  write(6,1095) CMND.CmndText(3) 
1095  format(' Error- ',a3,' is not a valid window width')
      return                                              

1100  write(6,1105) 
1105  format(' Error- The ',a3,' option must be followed by 3 numbers')
      return                                              

1110  write(6,1115) CMND.CmndText(i),CMND.CmndText(2)
1115  format(' Error- ',a3,' is not a valid number for the ',a3,' option')
      return                                              

1120  write(6,1125) 
1125  format(' Error- The manual view direction could not be normalized')
      return                                              

1130  write(6,1135) 
1135  format(' Error- The manual view up vector could not be normalized')
      return                                              

1140  write(6,1145) CMND.CmndText(3)
1145  format(' Error- ',a3,' is not a valid number for the force scaling factor')
      return                                              

1150  write(6,1155) CMND.CmndText(3)
1155  format(' Error- ',a3,' is not a valid number for the moment scaling factor')
      return                                              

1160  write(6,1165) CMND.CmndIntVal(3),OPT3D.StartFrm
1165  format(' Error- The end frame (',i4,') cannot be smaller than the start',
     &       ' frame (',i4,')')
      return                                              
      end                           
c
c
c
      function SetOnOff( text,flag )
      implicit none

      character text*3
      logical*4 flag,SetOnOff

      if ( text .eq. 'ON ' ) then
        flag= .true.
        SetOnOff= .true.
      else if ( text .eq. 'OFF' ) then
        flag= .false.
        SetOnOff= .true.
      else
        SetOnOff= .false.
      end if
      return
      end
c
c
c
      subroutine ResetOptions( OPT3D )
      implicit none

      include 'show3d.inc'             
 
      include '..\anz\a_const.inc'
      include '..\anz\a_struct.inc'
      include '..\anz\a_names.inc'
      include '..\anz\a_prm.inc'

      record /options_3d/  OPT3D

      integer*4 i

      OPT3D.StartFrm= 1
      OPT3D.FrmInc=   1
      OPT3D.EndFrm=   MP.nfrm
      OPT3D.EraseFrm=    .true.
      OPT3D.StepControl= .true.
      OPT3D.Continuous=  .false.

      OPT3D.WireFrame=   .true.
      OPT3D.HideFace=    .false.
      OPT3D.HiddenLine=  .true.
      OPT3D.StickFigure= .false.

      OPT3D.SegCentered= .false.
      OPT3D.CntrSegNum=  1
      OPT3D.JntCentered= .false.
      OPT3D.CntrJntNum=  1

      OPT3D.ShowSegCntr= .false.
      OPT3D.ShowSegLCS=  .false.
      OPT3D.ShowGCS=     .false.
      OPT3D.ShowFrcPlt=  .true.
      OPT3D.ShowFrmNum=  .false.

      OPT3D.UseInitRotCor=  .false.
                           
      OPT3D.Print=         .false.
      OPT3D.Screen=        .true.
      OPT3D.UseMonoDisplay=  .false.

      OPT3D.UseManWin=   .false.
      OPT3D.WindowWidth= 3.0         
      do i= 1, 3
        OPT3D.ManVwPnt(i)=  0.0
        OPT3D.ManVwDir(i)=  0.0
        OPT3D.ManUpVec(i)=  0.0
        OPT3D.ManVwCntr(i)= 0.0
      end do
      OPT3D.ManVwPnt(1)=  3.0
      OPT3D.ManVwDir(1)= -1.0
      OPT3D.ManUpVec(3)=  1.0

      OPT3D.ShowCP=   .false.
      OPT3D.ShowGRF=  .true.
      OPT3D.ShowGRM=  .false.
      OPT3D.ScaleFRC=  -1.0
      OPT3D.ScaleMOM=  -0.1
      return
      end                       
