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

!A
!A NAME:
!A
!A 	Add Contours  VARIATIONAL
!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.
!B	This function should only be used on contours which are
!B	created or embeded variationally. If used on a non variational
!B	contour the function will work the first time but future
!B	execution of the variational object will fail.
!B	If the contours are defined in a different object than the object
!B	that this function is in, it may be necessary to do an execute all
!B	when editing information.
!B

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

DIM Destination$[256]
DIM Answer$[256]
DIM Trim_contour$[256]
DIM Contour1$[256]
DIM Contour2$[256]
DIM Name$[256]


Var_object= CheckVarObject
IF ExitScript(Destination$) THEN Leave

ActivateUndo
Start:!

Pick_contour1:!
Stat=SetInputFormat("n4","")
E1=PickEntity("PICK CONTOUR #1")
IF (ExitScript(Destination$)) THEN Leave
Contour1= ParentGroup(E1)
IF (NOT ValidDependency(Contour1)) THEN Pick_contour1
Stat= EntityNameCheck(Contour1,Contour1$)
IF NOT LEN(Contour1$) THEN Pick_contour1
Closed=IsContClosed(Contour1)
IF NOT Closed THEN Pick_contour1
T1= LastPickPara
IF NOT IsEntVariational(Contour1) THEN
   Stat=SetInputFormat("a1",",Y,N,")
   Stat= GetValue("NON-VDM ENTITY. LOAD NON-VDM? Yes or No ",Answer$)
   IF Answer$="N" THEN Pick_contour1
   Destination$= "trim.d/addcontours"
   Stat=SetInputFormat("n4","")
   GOTO Leave
END IF




Pick_contour2:!
Stat=SetInputFormat("n4","")
E2=PickEntity("PICK CONTOUR #2")
IF (ExitScript(Destination$)) THEN Leave
Contour2= ParentGroup(E2)
IF (NOT ValidDependency(Contour2)) THEN Pick_contour2
Stat= EntityNameCheck(Contour2,Contour2$)
IF NOT LEN(Contour2$) THEN Pick_contour2
Closed=IsContClosed(Contour2)
IF NOT Closed THEN Pick_contour2
T2= LastPickPara
IF NOT IsEntVariational(Contour2) THEN
   Stat=SetInputFormat("a1",",Y,N,")
   Stat= GetValue("NON-VDM ENTITY. LOAD NON-VDM? Yes or No ",Answer$)
   IF Answer$="N" THEN Pick_contour1
   Destination$= "trim.d/addcontours"
   Stat=SetInputFormat("n4","")
   GOTO Leave
END IF


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

Stat= GenerateVarName(WsGroupEnt,Name$)
RecordFunctionOn
Stat= RecordDescr("TRIM: Add contours")
C1=CopyGroup(Contour1$)
C2=CopyGroup(Contour2$)
V1= FreeParameter("Side1",Inout1)
V2= FreeParameter("Side2",Inout2)
G1=AddContours(C1,V1,C2,V2)
RecordFunctionOff
IF ValidGeometry(G1) THEN
        RecordFunctionOn
        Stat=AutoNameGroup(G1,Name$)

        ActivateUndo
        Stat= AdvanceUndoNumber("add contours")
        Stat= AddGroupToUndo(WsUBDeleteMode,Contour1$,1,WsTrue)
        Stat= AddGroupToUndo(WsUBDeleteMode,Contour2$,1,WsTrue)
        Stat= AddGroupToUndo(WsUBCreateMode,G1,0,WsTrue)

        Stat=DeleteGroup(Contour1$,WsTrue)
        Stat=DeleteGroup(Contour2$,WsTrue)
	Stat=PlotEntity(G1)
	Stat= DeleteEntity(G1)
        RecordFunctionOff
END IF
RecordFunctionOn
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord

Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
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

