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

!A
!A NAME:
!A
!A 	Add Group Of Contours  VARIATIONAL 
!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	SAVE  Inside or Outside:  I saves other geometry inside contour.
!B				  O saves other geometry outside contour.
!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
!B	Remember if two contours do not intersect it is not possible to
!B	be inside of both contours at the same time.
!B

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

DIM Destination$[256]
DIM Answer$[256]
DIM A$[256]
DIM Extras$[256]
DIM Contour$[256]
DIM Name$[256]
INTEGER Interiors(512)

Var_object= CheckVarObject
IF ExitScript(Destination$) THEN Leave

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)
				GOSUB InsideOutside
                               	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)

ActivateUndo
Stat= AdvanceUndoNumber("add contours")

Num_contours= GroupCount(Group)
Stat= GenerateVarName(WsGroupEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("TRIM: Add multiple contours")
G1=CreateGroup("")
RecordFunctionOff
FOR I= 1 TO Num_contours
	Contour= GroupElement(Group,I)
	Stat= EntityNameCheck(Contour,Contour$)
	In_out= Interiors(I)
	A$= "Save inside #"&VAL$(I)
	RecordFunctionOn
	C1= CopyGroup(Contour$)
	V1= FreeParameter(A$,In_out)
	Stat= SaveInteriorFlag(C1,V1)
	Stat= AppendToGroup(G1,C1)
	RecordFunctionOff
NEXT I
RecordFunctionOn
G2=AddContours(G1)
Stat= AutoNameGroup(G2,Name$)
RecordFunctionOff
FOR I= 1 TO Num_contours
	Contour= GroupElement(Group,I)
	Stat= EntityNameCheck(Contour,Contour$)
	Stat= AddGroupToUndo(WsUBDeleteMode,Contour,1,WsTrue)
	RecordFunctionOn
	Stat= DeleteGroup(Contour$,WsTrue)
	RecordFunctionOff
NEXT I
Stat= AddGroupToUndo(WsUBCreateMode,G2,0,WsTrue)
RecordFunctionOn
RecordFunctionOff
Stat= PlotEntity(G2)
RecordFunctionOn
Stat= DeleteEntity(G2)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord

Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
Stat= DeleteEntity(Group)
GOTO Start

InsideOutside:!
Stat=SetEqualsValue(0,"O")
Stat=SetInputFormat("a1=",",I,O,")
Stat=GetValue("SAVE Inside or Outside ",Answer$)
IF (ExitScript(Destination$)) THEN Leave
N1= GroupCount(Group)
IF Answer$= "I" THEN
	Interiors(N1)= 1
ELSE
	Interiors(N1)= 0
END IF
RETURN




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

