
! script name: trim.d/addgrpofconts
! topic: add group of closed contours together to form a simgle area.

!A
!A NAME:
!A
!A 	Add Group Of Contours 
!A
!A DESCRIPTION:
!A
!A 	This script adds a group of closed contours together.
!A
!A~ image location - images/addcontours.gif
!A
!B INSTRUCTIONS:
!B
!B 	PICK CONTOUR  Backup, Proceed : Pick the contours to be added.
!B					Proceed when finished picking.
!B
!B NOTE:
!B
!B	This script can be useful when it is necessary to take the 
!B	shapes defined by several intersecting contours and find the 
!B	common periphery to the contours. The resulting shape will
!B	depend on the pick points.
!B
!B	The original contours are deleted.
!B

stat=ChangeLabelStr("ScriptNameStatus","Function: add group of contours ")

DIM Destination$[256]
DIM Answer$[256]
DIM Extras$[256]

Highlight_color= 3
Stat= SetDefaults(WsSelectedEntPen,Highlight_color)

ActivateUndo
Start:!
Group=CreateGroup("TEMP")
Extras$= ""
LOOP
        stat= SetInputFormat("a1",",P,B,")
        C1=PickGroupEnt("PICK CONTOUR"&Extras$,0,Answer$)
        IF ExitScript(Destination$) THEN Leave
        EXIT IF Answer$= "P"
        IF Answer$="B" THEN
                Num1=GroupCount(Group)
                C1=GroupElement(Group,Num1)
                IF Num1 THEN Stat= RemoveFromGrp(Group,Num1)
                Stat= SetSelectedFlag(C1,WsFalse)
                Stat= PlotEntity(C1)
                IF Num1=1 THEN Extras$= ""
        ELSE
                IF ValidGeometry(C1) THEN
			Closed=IsContClosed(C1)
			if Closed THEN
                               	Stat=AppendToGroup(Group,C1)
                               	Stat= SetSelectedFlag(C1,WsTrue)
                               	Stat= PlotEntity(C1)
                        	Extras$=", Backup , Proceed "
			END IF
                END IF
        END IF

END LOOP
stat= SetInputFormat("n4","")
IF NOT ValidGeometry(Group) THEN
        GOTO Start
ELSE
        IF NOT GroupCount(Group) THEN Start
END IF
Stat= SetSelectedFlag(Group,WsFalse)

Group1= CopyGroup(Group)
ActivateUndo
Stat= AdvanceUndoNumber("add multiple contours")
Stat= AddGroupToUndo(WsUBDeleteMode,Group,0,WsTrue)
Stat= DeleteGroup(Group,WsTrue)

G1=AddContours(Group1)
Stat= PlotEntity(G1)
FOR I=1 TO GroupCount(G1)
	Contour= GroupElement(G1,I)
        Stat=AutoNumberCont(Contour)
	Stat= AddGroupToUndo(WsUBCreateMode,Contour,1,WsTrue)
NEXT I
Stat= DeleteEntity(G1)
Stat= DeleteEntity(Group1)

GOTO Start

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

