
! script name: vdraft.d/fillinbox
! var_topic: fill the area of curves inside a box.

!A
!A NAME:
!A
!A 	Fill In Box	VARIATIONAL
!A
!A DESCRIPTION:
!A
!A	This script Fills the area of curves inside a box.
!A

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

DIM Destination$[256]
DIM Answer$[256]
DIM Name$[256]
DIM Bgroup$[256]
DIM Gname$[256]
DEG


Start:!
Var_object= CheckVarObject
IF (ExitScript(Destination$)) THEN Leave

DeActivateUndo
Group_loop:!
RaiseColorBox
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","")

Fill_pen= CurrentColor

Stat= GenerateVarName(WsGroupEnt,Gname$)
Stat= GenerateVarName(WsGroupEnt,Bgroup$)
Stat= GenerateVarName(WsGroupEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("DIM: Area Fill")
Stat= SetRecDelLastExec
G1= CreateGroup(Name$,WsTempGroup)
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= AppendToGroup(G1,Element$)
                RecordFunctionOff
        END IF
        END IF
NEXT I
RecordFunctionOn
Stat= SetNoPathFlag(G1,WsFalse)
V1= FreeParameter("Fill pen",Fill_pen)
G2= FindClosedContours(G1)
G3= CreateBoundarys(G2)
Fill_group= FillBoundaryGrp(G3,V1)
Stat= AutoNameGroup(Fill_group,Name$)
Stat= AutoNameGroup(G2,Gname$,WsTrue)
Stat= AutoNameGroup(G3,Bgroup$)
Stat= DeleteEntityList(G1,G2,G3,Fill_group)
RecordFunctionOff
CloseVarRecord

Stat= PlotEntity(Fill_group)
Stat= PlotEntity(Group)
Stat= DeleteEntity(Group)
GOTO Group_loop

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

