
! script name: trim.d/addcontours
! topic: add two closed contours together to form a simgle area.

!A
!A NAME:
!A
!A 	Add Contours 
!A
!A DESCRIPTION:
!A
!A 	This script adds two closed contours together.
!A
!A~ image location - images/addcontours.gif
!A
!B INSTRUCTIONS:
!B
!B 	PICK CONTOUR1: Pick the first contour.
!B
!B 	PICK CONTOUR2: Pick the second contour.
!B
!B
!B NOTE:
!B
!B	This script can be useful when it is necessary to take the 
!B	shapes defined by two 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.

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

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


ActivateUndo

Start:!

Pick_contour1:!
Stat=SetInputFormat("n4","")
E1=PickEntity("PICK CONTOUR #1")
IF (ExitScript(Destination$)) THEN Leave
Contour1= ParentGroup(E1)
IF (NOT ValidGeometry(Contour1)) THEN Pick_contour1
Closed=IsContClosed(Contour1)
IF NOT Closed THEN Pick_contour1
T1= LastPickPara


Pick_contour2:!
Stat=SetInputFormat("n4","")
E2=PickEntity("PICK CONTOUR #2")
IF (ExitScript(Destination$)) THEN Leave
Contour2= ParentGroup(E2)
IF (NOT ValidGeometry(Contour2)) THEN Pick_contour2
Closed=IsContClosed(Contour2)
IF NOT Closed THEN Pick_contour2
T2= LastPickPara

PlotOff
P1= ParameterPt(E1,T1)
P2= ParameterPt(E2,T2)
Inside1= InsideContour(Contour1,P2)
Inside2= InsideContour(Contour2,P1)
Inout1= WsInside*(Inside1=1)+ WsOutside*(Inside1=0)
Inout2= WsInside*(Inside2=1)+ WsOutside*(Inside2=0)
Stat= DeleteEntity(P1)
Stat= DeleteEntity(P2)
PlotOn

Cont1= CopyGroup(Contour1)
Cont2= CopyGroup(Contour2)
ActivateUndo
Stat= AdvanceUndoNumber("add two contours")
Stat= AddGroupToUndo(WsUBDeleteMode,Contour1,1,WsTrue)
Stat= AddGroupToUndo(WsUBDeleteMode,Contour2,1,WsTrue)
Stat= DeleteGroup(Contour1,WsTrue)
Stat= DeleteGroup(Contour2,WsTrue)

G1=AddContours(Cont1,Inout1,Cont2,Inout2)
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)

GOTO Pick_contour1

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

