
! script name: vgroups.d/manualgrp
! var_topic: manual group ny elements 

!A
!A NAME:
!A
!A 	MANUAL GROUP BY ELEMENTS
!A
!A DESCRIPTION:
!A	
!A	Makes a group by picking the individual elements.
!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 by elements ")

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


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


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:!
Group= CreateGroup("TEMPGROUP")
Pick_loop:!
Stat=SetPickMask(WsPickAll)
Stat=SetInputFormat("a1",",P,U,C,")
E1=PickEntity("PICK ELEMENT, Unpick, Clear, Proceed",Answer$)
IF ExitScript(Destination$) THEN Leave
Choice$=UPC$(Answer$)
SELECT Choice$
   CASE "P"
      Stat= SetSelectedFlag(Group,WsFalse)
      Stat= PlotEntity(Group)
      GOTO Make_record
   CASE "U"
      Element_num=GroupCount(Group)
      Temp_e1=GroupElement(Group,Element_num)
      IF NOT ValidGeometry(Temp_e1) THEN Pick_loop
      Stat= SetSelectedFlag(Temp_e1,WsFalse)
      Stat=PlotEntity(Temp_e1)
      Stat=RemoveFromGrp(Group,Element_num)
   CASE "C"
      Stat= SetSelectedFlag(Group,WsFalse)
      Stat= PlotEntity(Group)
      Stat=DeleteEntity(Group)
      GOTO Make_group
   CASE ELSE
      IF NOT ValidGeometry(E1) THEN Pick_loop
      Stat= SetSelectedFlag(E1,WsTrue)
      N1= ElementGrpNum(Group,E1)
      IF N1 = 0 THEN
              Stat=AppendToGroup(Group,E1)
      END IF
      Stat=PlotEntity(E1)
END SELECT
GOTO Pick_loop



Make_record:!
IF Name$= "" THEN
	Stat= GenerateVarName(WsGroupEnt,Name$)
END IF
RecordFunctionOn
Stat= RecordDescr("GROUP: Elements")
G1= CreateGroup(Name$)
RecordFunctionOff
Num_elements= GroupCount(Group)
FOR I= 1 TO Num_elements
	E1= GroupElement(Group,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("make group")
	Stat= AddItemToUndo(WsUBCreateMode,G1)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF
Stat= DeleteEntity(Group)
GOTO Start

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