
! script name: vdatatable.d/boxcopy
! var_topic: copy table elements

!A
!A NAME:
!A
!A 	Copy Entries	VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	Copy table elements in a box.
!A
!B INSTRUCTIONS:
!B
!B 	PICK ORIGIN TABLE: Pick the source table.
!B
!B 	SELECT VIEW FOR SELECTION BOX: Pick window view for selection.
!B
!B 	PICK DESTINATION ENTRY: This is the starting point for the copy's
!B 		destination.	
!B
!B NOTES:
!B
!B 	The origin table and the destination table may be the same.
!B 	In this case the copied data must not overlap.
!B
!B 	Make sure the selection box is contained within the
!B 	table. Selecting outside the table will cancel the
!B 	command.
!B
!B 	When tables are used in variational design the variational
!B 	attachments are not copied.
!B
!B      In the FreeParameters specifing Row and Column numbers.
!B      If End row is set to 0 it will default internally to the number
!B      of rows in the table.
!B      If End column is set to 0 it will default internally to the number
!B      of columns in the table.
!B
!# see also - htdocs/help.d/datatables.htm

stat=ChangeLabelStr("ScriptNameStatus","Function: Box copy entries ")

DIM Answer$[256]
DIM A$[256]
DIM Destination$[256]
DIM Table1$[256]
DIM Table2$[256]

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

ActivateUndo
Highlight_pen=2
Start:!

Element_loop:!
N1= DataTableCount
IF N1=0 THEN Leave
IF N1 > 1 THEN
Pick_table:stat= SetInputFormat("n4","")
        Table1=PickEntity("PICK ORIGIN TABLE")
        IF (ExitScript(Destination$)) THEN Leave
	IF NOT ValidDependency(Table1) THEN Pick_table
        Stat= EntityNameCheck(Table1,Table1$)
ELSE
        Table1= DataTableByNumber(1)
	IF NOT ValidDependency(Table1) THEN Pick_table
        Stat= EntityNameCheck(Table1,Table1$)
END IF


Pick_loop:!
Vnum= LastPickView
L1=WorkplaneBox(Vnum)
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidGeometry(L1) THEN Pick_loop

Len1= LineLength(L1)
IF Len1< 0.0001 THEN
        Stat= DeleteEntity(L1)
        GOTO Start
END IF

Pt1= ParameterPt(L1,0.0)
T1= ParameterInTable(Table1,Pt1)
Stat= DeleteEntity(Pt1)
IF T1 <= 0 THEN Pick_loop
R1=TableParaRow(Table1,T1)
C1=TableParaColumn(Table1,T1)

Pt2= ParameterPt(L1,1.0)
T2= ParameterInTable(Table1,Pt2)
Stat= DeleteEntity(Pt2)
Stat= DeleteEntity(L1)
IF T2 <= 0 THEN Pick_loop
R2=TableParaRow(Table1,T2)
C2=TableParaColumn(Table1,T2)
Stat= DeleteEntity(Pt1)
Stat= DeleteEntity(Pt2)
Stat= DeleteEntity(L1)

Stat= PlotDataTable(Table1,R1,C1,R2,C2,Highlight_pen)


Pick_dest:!
stat= SetInputFormat("n4","")
Table2=PickEntity("PICK DESTINATION ENTRY")
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidDependency(Table2) THEN Pick_dest
Stat= EntityNameCheck(Table2,Table2$)
T1= LastPickPara

R3=TableParaRow(Table2,T1)
C3=TableParaColumn(Table2,T1)

ActivateUndo
Stat= AdvanceUndoNumber("copy entries")
Stat= AddItemToUndo(WsUBModifyMode,Table2)
Stat= EraseEntity(Table2)

RecordFunctionOn
Stat= RecordDescr("TABLE: Copy Entries")
V1= FreeParameter("Start row",R1)
V2= FreeParameter("Start column",C1)
V3= FreeParameter("End row",R2)
V4= FreeParameter("End column",C2)
V5= FreeParameter("Destination row",R3)
V6= FreeParameter("Destination column",C3)
Stat= CopyTableRows(Table1$,Table2$,V1,V2,V3,V4,V5,V6)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
Stat= PlotEntity(Table2)
Stat= PlotDataTable(Table1,R1,C1,R2,C2,0)

Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
GOTO Start

Leave:!
Stat= DeleteEntity(Pt1)
Stat= DeleteEntity(Pt2)
Stat= DeleteEntity(L1)
stat= SetInputFormat("n4","")
IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 LOAD Destination$
END IF
END
