
! script name: vgroups.d/appendinbox
! var_topic: append elements in a box to an existing group

!A
!A NAME:
!A
!A 	APPEND ELEMENTS IN A BOX
!A
!A DESCRIPTION:
!A	
!A	Appends to a group by picking elements in a box.
!A
!B INSTRUCTIONS:
!B
!B 	GROUP NAME, Autoname: Enter the name to be used for the group, or
!B 		enter "A" to have the group automatically named.
!B

stat=ChangeLabelStr("ScriptNameStatus","Function: group in box  ")

DIM Destination$[256]
DIM Answer$[256]
DIM Name$[256]
DIM Element$[256]
DIM Group$[256]


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

Highlight_color=2

Start:!
ActivateUndo
Stat= SetInputFormat("n4","")
Group=PickGroupEnt("PICK EXISTING GROUP")
IF ExitScript(Destination$) THEN Leave
IF NOT ValidDependency(Group) THEN Start
IF IsEntityDependent(Group)= WsFalse THEN
        stat= SetInputFormat("a99","")
        Stat= GetValue("NOT A VARIATIONAL GROUP, Proceed ",Answer$)
        IF ExitScript(Destination$) THEN Leave
        GOTO Start
END IF
Stat= EntityNameCheck(Group,Group$)
Stat=Highlight(Group,Highlight_color,WsDirArrowOff)
IF NOT ValidGeometry(Group) THEN Start


Make_group:!
Group1= CreateGroup("TEMPGROUP")
Vnum= LastPickView
Stat=PickBox(Group1,Vnum,WsPickPen)
IF ExitScript(Destination$) THEN Leave
IF NOT ValidGeometry(Group1) THEN
        GOTO Start
ELSE
        Group2=CreateGroup("TEMP")
        Num_elements= GroupCount(Group1)
        FOR I=1 TO Num_elements
                E1= GroupElement(Group1,I)
                IF ValidDependency(E1) THEN
                        Stat= EntityNameCheck(E1,Element$)
                        Stat= AddToGroup(Group2,Element$)
                END IF
        NEXT I
        Stat= DeleteEntity(Group1)
        Group1= Group2
        IF NOT GroupCount(Group1) THEN
                Stat= DeleteEntity(Group1)
                GOTO Start
        END IF
END IF


Make_record:!

ActivateUndo
Stat= AdvanceUndoNumber("append to group")
Stat= AddGroupToUndo(WsUBModifyMode,Group,1,WsTrue)

RecordFunctionOn
Stat= RecordDescr("GROUP: Elements")
RecordFunctionOff
Num_elements= GroupCount(Group1)
FOR I= 1 TO Num_elements
	E1= GroupElement(Group1,I)
	IF ValidGeometry(E1) THEN
		Stat= EntityNameCheck(E1,Element$)
		RecordFunctionOn
		Stat= AppendToGroup(Group$,Element$)
		RecordFunctionOff
	END IF
NEXT I
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord

Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
Stat= PlotEntity(Group)
Stat= DeleteEntity(Group1)
GOTO Start

Leave:!
Stat= SetPickMask(WsPickAll)
Stat= DeleteEntity(Group1)
IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 LOAD Destination$
END IF
END
