
! script name: lines.d/dynamicchain
! topic: links a series of points by line segments

!A
!A NAME:
!A
!A 	Line Chain
!A
!A DESCRIPTION:
!A
!A 	This script links together a series of points by line segments.
!A 	The first line is defined by two points.  The next line and all
!A 	subsequent lines are connected to the end point of the last line
!A 	defined in this script.
!A
!A~ image location - images/linechain.jpg
!B INSTRUCTIONS:
!B
!B 	STARTING ABS X,Y,Z: Enter the X,Y,Z coordinates of the start
!B 		point for the line.  The coordinates for the starting point
!B 		are only asked when the script is entered.  The next line
!B 		assumes the start point of the line is the end point of the
!B 		last line described in this script.
!B
!B	PICK END POINT: Pick the next endpoint.
!B 		NOTE: To enter the xyz coordinates change the Input Method
!B		Combo Box to Rectangular, or type R.
!B 		See DYNAMIC INPUT OPTIONS.
!B
!B
!# see also - points.d/dyninput.hlp

stat=ChangeLabelStr("ScriptNameStatus","Function: line chain ")

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

Graphical_mode=NamedVarVal("Graphical mode",WsSystemVars,1.0)
IF NOT Graphical_mode THEN LOAD "lines.d/linechain"

ActivateUndo
Start:!
Units= CurrentUnits
IF Units= WsMetric THEN
	Length1= 25
ELSE
	Length1= 1
END IF

Contour= CreateGroup("LINE CHAIN",WsContourGroup)

Pick_first:!
Stat=SetInputFormat("n4b"," ")
Ps=XyzInput("STARTING",WsXyzInput)
IF (ExitScript(Destination$)) THEN Leave
Stat=SetLastPoint(Ps)
Pstart= ParameterPt(Ps,0.0)

Line_loop:!

PlotOff
Pe= PtIncrXyz(Ps,Length1,0.0,0.0)
L1= LineTwoPoints(Ps,Pe)
Stat= MakeInvisible(Ps)
Stat= MakeInvisible(Pe)
Stat= MakeInvisible(L1)
PlotOn

Vnum= LastPickView
Stat=DynamicEntity(Vnum,L1,Ps,Pe,WsDynamicEnd,"PICK END POINT")
IF (ExitScript(Destination$)) THEN 
	Stat=DeleteEntity(L1,WsTrue)
	PlotOff
	Stat=DeleteEntity(Pe)
	Stat=DeleteEntity(Ps)
	PlotOn
        IF Destination$= "BACKUP" THEN 
		N1=GroupCount(Contour)
		IF N1=0  THEN Pick_first
		L1= GroupElement(Contour,N1)
		PlotOff
			Ps= ParameterPt(L1,0.0)
			Stat= MakeInvisible(Ps)
		PlotOn
		Stat= RemoveFromGrp(Contour,N1)
		Stat= DeleteEntity(L1)
		Stat= PlotEntity(Contour)
		GOTO Line_loop
	END IF
	ActivateUndo
	Stat= AdvanceUndoNumber("line chain")
	Stat= AddGroupToUndo(WsUBCreateMode,Contour,WsTrue,WsFalse)
	GOTO Leave
END IF
Stat= MakeVisible(L1)
Stat= PlotEntity(L1)
Stat= AddToGroup(Contour,L1)

PlotOff
Stat=DeleteEntity(Pe)
Pe= ParameterPt(L1,1.0)
Stat=SetLastPoint(Pe)
Stat=DeleteEntity(Ps)
Ps=Pe
PlotOn
GOTO Line_loop

Leave:!
Stat=DeleteEntity(Pstart)
Stat=DeleteEntity(Ps)
Stat=DeleteEntity(Pe)
N1=GroupCount(Contour)
IF (N1=0) THEN
	Stat=DeleteEntity(Contour)
ELSE
	Stat=AutoNumberCont(Contour)
	Stat= PlotEntity(Contour)
END IF
IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 IF Destination$= "SCRIPTSTART" THEN Start
         IF Destination$= "BACKUP" THEN Start
	 LOAD Destination$
END IF
END
