
! script name: vcontours.d/joincontourcuts
! var_topic: join two contour with a retract between cuts.

!A
!A NAME:
!A
!A 	Join Contour Cuts	VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script joins two contours.  A line will join the start point
!A 	of the first contour to the end point of the other contour.
!A 	The original contour is deleted.
!A
!B INSTRUCTIONS:
!B
!B 	PICK FIRST CONTOUR: Pick the first
!B 		contour to be joined, the start point of this
!B 		contour will be used for the joining.
!B
!B 	PICK NEXT CONTOUR: Pick the other
!B 		contour to be joined, the end point of this
!B 		contour will be used for the joining.
!B

Stat=ChangeLabelStr("ScriptNameStatus","Function: join contours ")

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

Var_object=CheckVarObject
IF ExitScript(Destination$) THEN Leave

ActivateUndo
Join_loop: !

Pick_contour1: !
Contour1=PickGroupEnt("PICK FIRST CONTOUR ")
IF ExitScript(Destination$) THEN Leave
IF NOT ValidDependency(Contour1) THEN Pick_contour1
Stat=EntityNameCheck(Contour1,Contour1$)
IF NOT IsEntVariational(Contour1) THEN
   Stat=SetInputFormat("a99","")
   Stat= GetValue("NON-VDM ENTITY. CANNOT USE ON NON VDM ENTITY ",Answer$)
   Stat=SetInputFormat("n4","")
   GOTO Leave
END IF


Pick_contour2: !
Contour2=PickGroupEnt("PICK NEXT CONTOUR ")
IF ExitScript(Destination$) THEN Leave
IF NOT ValidDependency(Contour2) THEN Pick_contour2
Stat=EntityNameCheck(Contour2,Contour2$)
IF NOT IsEntVariational(Contour2) THEN
   Stat=SetInputFormat("a99","")
   Stat= GetValue("NON-VDM ENTITY. CANNOT USE ON NON VDM ENTITY ",Answer$)
   Stat=SetInputFormat("n4","")
   GOTO Leave
END IF


stat= SetInputFormat("n4","")
Stat=SetPickReturn(WsGeometryZ)
Zlevel=GetValue("Z RETRACT LEVEL",Answer$)
Stat=SetPickReturn(WsGeometryEnd)
IF (ExitScript(Destination$)) THEN Leave
No_z_var= LastVariableInput(Z_var$)


Tool= GetAssocTool(Contour1$)
Has_tool= ValidGeometry(Tool)

Stat=GenerateVarName(WsGroupEnt,Name$)
RecordFunctionOn
Stat=RecordDescr("CONTOUR: Join contour cuts")
IF No_z_var THEN
	V1= FreeParameter("Z",Zlevel)
ELSE
	V1= FreeParameter("Z",Z_var$)
END IF
V2= FreeParameter("Dia comp dir",GetDCompDirection(Contour1$))
C1=CopyGroup(Contour1$,WsTrue)
C2=CopyGroup(Contour2$,WsTrue)
Pe= ContEndPt(C1)
Ps= ContStartPt(C2)
Pz1= PointXYZ(XpointValue(Pe),YPointValue(Pe),V1)
Pz2= PointXYZ(XpointValue(Ps),YPointValue(Ps),V1)
L1= LineTwoPoints(Pe,Pz1)
Stat= SetRapidFlag(L1,1)
Stat= SetLineType(L1,8)
Stat= AddToGroup(C1,L1)
L2= LineTwoPoints(Pz1,Pz2)
Stat= SetRapidFlag(L2,1)
Stat= SetLineType(L2,8)
Stat= AddToGroup(C1,L2)
L3= LineTwoPoints(Pz2,Ps)
Stat= SetRapidFlag(L3,1)
Stat= SetLineType(L3,8)
Stat= AddToGroup(C1,L3)
C3=JoinContours(C1,C2)
Stat=DeleteEntity(C1)
Stat=DeleteEntity(C2)
Stat=DeleteEntityList(C1,C2,Pz1,Pz2,Ps,Pe)
RecordFunctionOff
IF ValidGeometry(C3) THEN
	RecordFunctionOn
	Stat=AutoNameGroup(C3,Name$)
	Stat=SetDCompDirection(C3,V2)
	IF Has_tool THEN
		Tool= GetAssocTool(Contour1$)
		Stat= AssociateTool(C3,Tool)
	END IF
	ActivateUndo
	Stat= AdvanceUndoNumber("join contour cuts")
	Stat= AddGroupToUndo(WsUBDeleteMode,Contour1$,1,WsTrue)
	Stat= AddGroupToUndo(WsUBDeleteMode,Contour2$,1,WsTrue)

	Stat=DeleteGroup(Contour1$,WsTrue)
	Stat=DeleteGroup(Contour2$,WsTrue)
	RecordFunctionOff
	Stat=PlotEntity(C3)
END IF
RecordFunctionOn
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
IF NOT ValidGeometry(C3) THEN
	DelLastVarRecord
ELSE
	Stat= AddGroupToUndo(WsUBCreateMode,C3,1,WsTrue)
	Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
END IF

GOTO Join_loop

Leave:!
IF NOT LEN(Destination$) THEN
	LOAD "typecontrol"
ELSE
	LOAD Destination$
END IF
END

