
! script name: vdatatable.d/exchangerows
! var_topic: exchange two rows in a table

!A
!A NAME:
!A
!A 	Exchange Rows	VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script exchanges the position of two rows in a table.
!A
!B INSTRUCTIONS:
!B
!B 	PICK FIRST ROWS TO BE EXCHANGED: Pick the first row to be exchanged.
!B
!B 	PICK SECOND ROWS TO BE EXCHANGED: Pick the row that should be
!B 		exchanged with the first row picked.
!B
!# see also - htdocs/help.d/datatables.htm

stat=ChangeLabelStr("ScriptNameStatus","Function: exchange two rows ")

DIM Answer$[256]
DIM A$[256]
DIM Destination$[256]
DIM Table$[256]

Var_object= CheckVarObject
IF (ExitScript(Destination$)) THEN Leave
DEG

First_pen=3
Second_pen=4


ActivateUndo
Start:!
Element_loop:!
Plot_table=0
stat= SetInputFormat("n4","")
Table1=PickEntity("PICK FIRST ROW TO BE EXCHANGED")
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidDependency(Table1) THEN Element_loop
Stat= EntityNameCheck(Table1,Table$)
T1= LastPickPara
R1=TableParaRow(Table1,T1)
C1=TableParaColumn(Table1,T1)
Stat= PlotDataTable(Table1,R1,0,First_pen)
Plot_table=1

Pick_second:stat= SetInputFormat("n4","")
Table2=PickEntity("PICK SECOND ROW TO BE EXCHANGED")
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidGeometry(Table2) THEN Pick_second

IF NOT AreEntitiesSame(Table1,Table2) THEN Pick_second

T1= LastPickPara
R2=TableParaRow(Table1,T1)
C2=TableParaColumn(Table1,T1)
Stat= PlotDataTable(Table1,R2,0,Second_pen)

ActivateUndo
Stat= AdvanceUndoNumber("exchange rows")
Stat= AddItemToUndo(WsUBModifyMode,Table1)

Stat= EraseEntity(Table1)
RecordFunctionOn
Stat= RecordDescr("TABLE: Exchange rows")
V1= FreeParameter("First row",R1)
V2= FreeParameter("Second row",R2)
Stat= ExchangeTableRows(Table$,V1,V2)
RecordFunctionOn
Curr_record= CurrentVarRecord
CloseVarRecord
Stat= PlotEntity(Table1)
Plot_table=0

Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
GOTO Element_loop

Leave:!
IF Plot_table THEN
        IF ValidGeometry(Table1) THEN
                Stat= PlotEntity(Table1)
        END IF
END IF
stat= SetInputFormat("n4","")
IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 LOAD Destination$
END IF
END
