
! script name: vcontours.d/vcontinbox
! var_topic: create contours from elements picked in a box.

!A
!A NAME:
!A
!A 	Auto Link Contours from elements in a box.
!A
!A DESCRIPTION:
!A
!A 	This script creates a contour (a special type of group), that is a
!A 	chain of connected elements.
!A

stat=ChangeLabelStr("ScriptNameStatus","Function: auto link elements in a box ")

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

Var_object=CheckVarObject
IF ExitScript(Destination$) THEN Leave


DeActivateUndo
Start:!

Group_loop:!
Group=CreateGroup("TEMPGROUP")

Vnum= LastPickView
Stat=PickBox(Group,Vnum,WsPickPen)
IF (ExitScript(Destination$)) THEN Leave



Make_record:!
N1=GroupCount(Group)
IF (N1=0) THEN 
	Stat=DeleteEntity(Group)
	GOTO Leave
END IF
Stat=SetPickMask(WsPickAll)
Stat=SetInputFormat("n4","")

Cont_color= CurrentColor
Cont_layer= CurrentLayer

Stat=GenerateVarName(WsGroupEnt,Name$)
RecordFunctionOn
Stat=RecordDescr("CONTOUR: Auto link")
G1= CreateGroup(Name$,WsTempGroup)
V1= FreeParameter("Contour_color",Cont_color)
V2= FreeParameter("Contour_layer",Cont_layer)
RecordFunctionOff
Num_elements= GroupCount(Group)
FOR I= 1 TO Num_elements
        E1= GroupElement(Group,I)
        IF ValidGeometry(E1) THEN
	IF IsValidPathElement(E1) THEN
                Stat= EntityNameCheck(E1,Element$)
                RecordFunctionOn
                Stat= AddToGroup(G1,Element$)
                RecordFunctionOff
	END IF
        END IF
NEXT I
RecordFunctionOn
Stat= SetNoPathFlag(G1,WsFalse)
G2= JoinEntities(G1)
RecordFunctionOff
IF ValidGeometry(G2) THEN
	Make_record=1
ELSE
	Make_record=0
END IF
RecordFunctionOn
Stat= AutoNameGroup(G2,Name$,WsTrue)
Stat= SetNoPathFlag(G2,WsTrue)
Stat= SetColor(G2,V1)
Stat= SetLayer(G2,V2)
Stat= DeleteEntity(G1)
Stat= DeleteEntity(G2)
RecordFunctionOff
CloseVarRecord

IF NOT Make_record THEN
        DelLastVarRecord
END IF
Stat=DeleteEntity(Group)
GOTO Start


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


