
! script name: vgroups.d/groupinbox
! var_topic: group  elements in a box

!A
!A NAME:
!A
!A 	GROUP ELEMENTS IN A BOX
!A
!A DESCRIPTION:
!A	
!A	Makes 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 Choice$[256]


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

Highlight_color=2

Start:!
ActivateUndo
Group_name:!
Stat=SetInputFormat("a99=","")
Stat= SetEqualsValue(0,"A")
Stat=GetValue("GROUP NAME, Autoname ",Name$)
IF (ExitScript(Destination$)) THEN Leave
IF Name$= "A" OR Name$="a" OR POS(Name$,"Autoname") OR POS(Name$,"autoname") THEN
        Stat=GenerateVarName(WsGroupEnt,Name$)
END IF
Group= FindNamedGrp(Name$)
IF ValidGeometry(Group) THEN
        Stat= SetInputFormat("a1",",N,")
        Stat= GetValue("GROUP EXISTS: New name",Answer$)
        IF (ExitScript(Destination$)) THEN Leave
        GOTO Group_name
END IF

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:!
IF Name$= "" THEN
	Stat= GenerateVarName(WsGroupEnt,Name$)
END IF

RecordFunctionOn
Stat= RecordDescr("GROUP: Elements")
G1= CreateGroup(Name$)
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= AddToGroup(G1,Element$)
		RecordFunctionOff
	END IF
NEXT I
RecordFunctionOn
Stat= NameEntity(G1,Name$)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF NOT ValidGeometry(G1) THEN
        DelLastVarRecord
ELSE
	ActivateUndo
	Stat= AdvanceUndoNumber("append")
	Stat= AddItemToUndo(WsUBCreateMode,G1)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
	Stat= DeleteEntity(Group1)
END IF
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
