! script name: conv_parabolas
! topic: Custom script to convert parabolic interpolation to linear
!A	
!A	NAME:  Convert Parabolas
!A	
!A	DESCRIPTION:
!A	This script converts parabolic interpolation (G6) into linear moves.
!A	
!A	The source tape file can be in either decimal or trailing zero format.
!A	The converted tape is in decimal format.
!A	
!A	When you run this script, only the G6 Blocks are replaced. The other
!A	blocks are not modified, except to convert trailing to decimal.
!A	The X,Y,Z,I,J,K,R,F words are converted to decimal if necessary.
!A	
!A	The approximate chord height is specified by the tolerance.  This should
!A	be treated as an estimate. The tolerance specified should always be
!A	smaller than the tolerance needed for machining.  No matter what the
!A	tolerance, a parabola is replaced by at least two lines. The size of
!A	the converted tape file may be several times the size of the source
!A	file depending on the tolerance.
!A	
!A	This script can operate in plot or no-plot mode.  In plot mode the
!A	tape moves for G0,G1,G6 motion create line entities which remain after
!A	the conversion is finished. These entities may be treated like any
!A	other entities.
!A	
!A	In no-plot mode the source file is converted and the parabolas are
!A	interpolated but no entities are left behind. For extremely
!A	large files it may be necessary to use the no-plot mode, since
!A	there may not be enough memory in the computer to retain these entities.
!A	
!A	Additional modes can be accessed by changing the script or sending
!A	additional pass parameters. A debug mode exists which leaves behind the
!A	parabolic curves.  Another mode allows the original endpoints to be
!A	retained. 
!A	The format for running the script is:
!A	
!A	\E<Directory>/conv_parabolas:[Plot_on][Debug_on][Keep_points]
!A	
!A	Examples:
!A	To run the script with default modes from the misc.d directory, enter:
!A	\Emisc.d/conv_parabolas
!A	
!A	To run the script with all modes enabled from misc.d, enter:
!A	\Emisc.d/conv_parabolas:1,1,1
!A	
!A	To run the script with all modes enabled except Debug_on, enter:
!A	\Emisc.d/conv_parabolas:1,0,1
!A	
!A	The parabolic curves created from the G6 are constructed by the method
!A	outlined in the machine manual (diagram 4.2).  The parabolic curves are
!A	then interpolated by straight lines using the given tolerance.  The
!A	paraboliccurves are represented by a nurb curve of degree 2.  The
!A	construction creates the nurb curve identical to the parabola. In
!A	Synergy, we represent parabolic curves with nurb curves.
!A	All curves generated by polynomial interpolation can be represented
!A	exactly by nurbs.
!A	
!A	The sample files supplied all had some locations where the curve fitting
!A	parabolas were not blending smoothly with the surrounding elements.
!A	These parabolas are interpolated like any other.
!A	
!A	

stat=ChangeLabelStr("ScriptNameStatus","Function: convert parabolas ")

DIM Answer$[256]
DIM Destination$[256]
DIM Fileline$[2048]
DIM Fileline1$[2048]
DIM Filename$[256]
DIM Output_file$[256]
DIM Tempdata$[256]
DIM Tapeline$[512]
DIM Dir$[512]
DIM Dir1$[512]
DIM D_label$[256]
DIM Sub_label$[256]
DIM F_label$[256]
DIM Choice$[256]
DIM Mask$[32]
DIM A$[256],B$[256],C$[256]
DIM Comments$[512]
DEG

StdWorkplane

IF ARGC<2 THEN
        Plot_on= 1
ELSE
        Plot_on= VAL(ARGV(1))
END IF
IF ARGC<3 THEN
        Debug_on= 0
ELSE
        Debug_on= VAL(ARGV(2))
END IF
IF ARGC<4 THEN
        Keep_points= 0
ELSE
        Keep_points= VAL(ARGV(3))
END IF



Delta= 0.0001
Curve_color= 4
Line_color= 5
Point_color= 8

Tempdata$= "tempdata"
Stat= RemoveFile("tempdata")


Tol=NamedVarVal("3D Cutting Tol",Ws3DSystemVars,0.001)
Stat= NamedVarStr("retrieve dir",WsSystemVars,"/usr/weber/ST/",Dir$)
IF (OSystemId<>1) THEN      ! NT
        Stat= ReplaceString(Dir$,"/","\\",Dir1$)
        Stat= DisplayMessage("CHOOSE SOURCE TAPE FILE FROM FILE BOX")
        LOOP
            Stat= StartProcess("/mksnt/filebox -a -d "&Dir1$&" >/tmp/filename")
            IF Stat THEN Leave
	    Stat= GetFileLine("/tmp/filename",1,Filename$)
            IF NOT Stat THEN 
            	Stat= DisplayMessage("ERROR ACCESSING FILE ")
            	Leave_loop= 0
	    ELSE
        	Stat= ReplaceString(Filename$,CHR$(13),"",Answer$)
            	Leave_loop= 1
	    END IF
            EXIT IF Leave_loop
        END LOOP
        Stat= ReplaceString(Dir1$,"\\","/",Dir$)
	Filename$= Answer$
ELSE                    !HP
        D_label$="DIRECTORY"
        Sub_label$="SUB DIRECTORIES"
        F_label$="FILES"
        Choice$="FILE TO OPEN"
        Mask$="[A-Za-z0-9]*"

   	Stat=UserFileBox(Dir$,Mask$,D_label$,Sub_label$,F_label$,Choice$,"Synergy")
        Stat=SetInputFormat("a99","")
        Stat=GetValue("SELECT SOURCE TAPE FILE",Answer$)
        IF (ExitScript(Destination$)) THEN Leave
        IF Answer$= "CANCEL" THEN Leave
        LowerFileBox
	Filename$= Answer$
END IF

Stat=SetInputFormat("a1",",T,D,")
Stat=GetValue("SOURCE FORMAT: Trailing, Decimal",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF Answer$= "D" THEN
        Source_type= 0
ELSE
        Source_type= 1
END IF

Output_name:!
Stat=SetInputFormat("a99","")
Stat=GetValue("DESTINATION FILE NAME (Full name path)",Answer$)
IF (ExitScript(Destination$)) THEN Leave
Output_file$= Answer$
IF Output_file$= Filename$ THEN Output_name

F_type= DoesFileExist(Output_file$)
SELECT F_type
CASE 1
        Stat=SetInputFormat("a1",",Y,N,")
        Stat=GetValue("FILE EXISTS: OVERWRITE  Yes or No",Answer$)
        IF (ExitScript(Destination$)) THEN Leave
        IF Answer$= "Y" THEN
                GOTO Ask_tolerance
        END IF
	GOTO Output_name
CASE 2
        Stat=SetInputFormat("a1",",P,")
        Stat=GetValue("FILE NAME IS A DIRECTORY: Proceed ",Answer$)
        IF (ExitScript(Destination$)) THEN Leave
	GOTO Output_name
END SELECT


Ask_tolerance:!
Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(Tol)
Tol=GetValue("INTERPOLATION TOLERANCE",Answer$)
IF (ExitScript(Destination$)) THEN Leave

Tol= 0.5*Tol


!      The source file must be decimal or trailing zero format 
!      The output file must be decimal format 

SELECT Source_type
CASE 0 		! decimal
	gbefore=2
	mbefore=2
	xyzbefore=4
	fbefore= 4
	sbefore= 4

	gafter=0
	mafter=0
	xyzafter=4
	fafter= 1
	safter= 0
CASE 1 		! trailing
	gbefore=2
	mbefore=2
	xyzbefore=4
	fbefore= 4
	sbefore= 4

	gafter=0
	mafter=0
	xyzafter=4
	fafter= 1
	safter= 0
END SELECT


Read_file_loop:!
stat= OutputString(Tempdata$,CHR$(10),WsNewFile)
Gr=0
Xr=0
Yr=0
Zr=0
Ir=0
Jr=0
Kr=0
Fr=0

N_Lines= FileLineCount(Filename$)
!  use basic file reading 
ON ERROR GOTO End_read
ASSIGN @File1 TO Filename$
FOR F_line=1 TO N_lines
	IF ((F_line MOD 100)= 1) THEN
	   Stat= DisplayMessage("WORKING ON "&VAL$(F_line)&" OF "&VAL$(N_lines))
	END IF
	ENTER @File1; Fileline$
	IF LEN(Fileline$) THEN
		GOSUB Read_line
	END IF
NEXT F_line
End_read:!
OFF ERROR
ASSIGN @File1 TO *

Stat= CopyFile(Tempdata$,Output_file$)
GOTO Leave

Read_line:!
N= POS(Fileline$,"(")
IF N>=1 THEN
	Fileline1$= Fileline$[1,N-1]
ELSE
	Fileline1$= Fileline$
END IF
IF Debug_on THEN PRINT F_line,  Fileline1$

CALL ReadWord(G,Gr,Fileline1$,"G",source_type,gbefore,gafter)
CALL ReadWord(X,Xr,Fileline1$,"X",source_type,xyzbefore,xyzafter)
CALL ReadWord(Y,Yr,Fileline1$,"Y",source_type,xyzbefore,xyzafter)
CALL ReadWord(Z,Zr,Fileline1$,"Z",source_type,xyzbefore,xyzafter)
CALL ReadWord(F,Fr,Fileline1$,"F",source_type,fbefore,fafter)

SELECT G
CASE 0,1
	IF Plot_on THEN
		PlotOff
		P1= PointXYZ(Xr,Yr,Zr)
		P2= PointXYZ(X,Y,Z)
		L1= LineTwoPoints(P1,P2)

		IF G=0 THEN Stat= SetLineType(L1,Line_color)

		Stat= DeleteEntity(P1)
		Stat= DeleteEntity(P2)
		PlotOn
		Stat= PlotEntity(L1)
	END IF

	CALL ReadWord(I,Ir,Fileline1$,"I",source_type,xyzbefore,xyzafter)
	CALL ReadWord(J,Jr,Fileline1$,"J",source_type,xyzbefore,xyzafter)
	CALL ReadWord(K,Kr,Fileline1$,"K",source_type,xyzbefore,xyzafter)

	GOSUB Convert_block
	Stat= OutputString(Tempdata$,Fileline$&CHR$(10),WsAppend)
CASE 2,3
	CALL ReadWord(I,Ir,Fileline1$,"I",source_type,xyzbefore,xyzafter)
	CALL ReadWord(J,Jr,Fileline1$,"J",source_type,xyzbefore,xyzafter)
	CALL ReadWord(K,Kr,Fileline1$,"K",source_type,xyzbefore,xyzafter)
	CALL ReadWord(R,Rr,Fileline1$,"R",source_type,xyzbefore,xyzafter)
	GOSUB Convert_block
	Stat= OutputString(Tempdata$,Fileline$&CHR$(10),WsAppend)
CASE 6
	CALL ReadWord(I,Ir,Fileline1$,"I",source_type,xyzbefore,xyzafter)
	CALL ReadWord(J,Jr,Fileline1$,"J",source_type,xyzbefore,xyzafter)
	CALL ReadWord(K,Kr,Fileline1$,"K",source_type,xyzbefore,xyzafter)

	PlotOff
	P1= PointXYZ(Xr,Yr,Zr)
	P2= PointXYZ(I,J,K)
	P3= PointXYZ(X,Y,Z)
	Stat= SetColor(P2,Point_color)
	Stat= SetColor(P3,Point_color)
	IF Keep_points THEN
		PlotOn
		IF Plot_on THEN
			Stat= PlotEntity(P2)
			Stat= PlotEntity(P3)
		END IF
	END IF

	! construct control point
	PlotOff
	L3= LineTwoPoints(P1,P3)
	IF ValidGeometry(L3) THEN
		P4= ParameterPt(L3,0.5)
		L4= LineTwoPoints(P4,P2)
		IF ValidGeometry(L4) THEN
			Dist= LineLength(L4)
			P5= PtIncrVector(P2,L4,Dist)

			Group=CreateGroup("TEMPGROUP")
			Stat=AddToGroup(Group,P1,P5,P3)
			Curve=BSplinePtGrp(Group,0,3)
			Stat= SetColor(Curve,Curve_color)
			Stat= DeleteEntity(Group)
		ELSE
			! no parabola possible */
			Curve=LineTwoPoints(P1,P3)
			Stat= SetColor(Curve,Curve_color)
		END IF

		PlotOn
		IF Plot_on THEN
			PlotOn
			IF Debug_on THEN Stat= PlotEntity(Curve)
		END IF
		PlotOff
		Group= ConvToLines(Curve,Tol)
		FOR Np= 1 TO GroupCount(Group)
		    L6= GroupElement(Group,Np)
		    Stat= SetColor(L6,Curve_color)
		    P6= ParameterPt(L6,1.0)
		    X= XPointValue(P6)
		    Y= YPointValue(P6)
		    Z= ZPointValue(P6)
		    GOSUB G01_block
		    IF LEN(Fileline$) THEN
		      Stat= OutputString(Tempdata$,Fileline$&CHR$(10),WsAppend)
		    END IF
		    Stat= DeleteEntity(P6)
		    IF Plot_on THEN
		        PlotOn
			Stat= PlotEntity(L6)
		        PlotOff
		    ELSE
		    	Stat= DeleteEntity(L6)
		    END IF
		NEXT Np
		G= 6
		IF NOT Debug_on THEN Stat= DeleteEntity(Curve)
		IF Plot_on THEN
			Stat= DeleteEntity(Group)
		ELSE
			Stat= DeleteEntity(Group)
		END IF
	END IF

	PlotOff
	Stat= DeleteEntity(P1)
	IF (NOT Keep_points) OR (NOT Plot_on) THEN
		Stat= DeleteEntity(P2)
		Stat= DeleteEntity(P3)
	END IF
	Stat= DeleteEntity(P4)
	Stat= DeleteEntity(P5)
	Stat= DeleteEntity(L3)
	Stat= DeleteEntity(L4)
	PlotOn

CASE 80,81,82,83,84,85,86,87,88,89
	CALL ReadWord(R,Rr,Fileline1$,"R",source_type,xyzbefore,xyzafter)
	GOSUB Convert_block
	Stat= OutputString(Tempdata$,Fileline$&CHR$(10),WsAppend)
CASE ELSE
	G= Gr
	GOSUB Convert_block
	Stat= OutputString(Tempdata$,Fileline$&CHR$(10),WsAppend)
END SELECT


Gr=G
Xr=X
Yr=Y
Zr=Z
Fr=F
Ir=I
Jr=J
Kr=K
Rr=R
Sr=S
RETURN


Convert_block:!
N= POS(Fileline$,"(")
IF N>=1 THEN
        Tapeline$= Fileline$[1,N-1]
	Comments$= Fileline$[N]
ELSE
        Tapeline$= Fileline$
	Comments$=""
END IF
IF Source_type=0 THEN
   IF G=1 THEN
   ! NEED TO GET RID OF IJK IN G01 BLOCK
   IF POS(Tapeline$,"I") THEN
        A$= ""
        CALL Word_string(B$,Tapeline$,"I")
        Stat= ReplaceString(Tapeline$,B$,A$,C$)
        Tapeline$= C$
   END IF
   IF POS(Tapeline$,"J") THEN
        A$= ""
        CALL Word_string(B$,Tapeline$,"J")
        Stat= ReplaceString(Tapeline$,B$,A$,C$)
        Tapeline$= C$
   END IF
   IF POS(Tapeline$,"K") THEN
        A$= ""
        CALL Word_string(B$,Tapeline$,"K")
        Stat= ReplaceString(Tapeline$,B$,A$,C$)
        Tapeline$= C$
   END IF
   Fileline$= Tapeline$
   RETURN
   END IF
ELSE
IF POS(Tapeline$,"X") THEN
	A$= "X"&VAL$(PROUND(X,-xyzafter))
        IF NOT POS(A$,".") THEN A$= A$&".0"
	CALL Word_string(B$,Tapeline$,"X")
	Stat= ReplaceString(Tapeline$,B$,A$,C$)
	Tapeline$= C$
END IF
IF POS(Tapeline$,"Y") THEN
	A$= "Y"&VAL$(PROUND(Y,-xyzafter))
        IF NOT POS(A$,".") THEN A$= A$&".0"
	CALL Word_string(B$,Tapeline$,"Y")
	Stat= ReplaceString(Tapeline$,B$,A$,C$)
	Tapeline$= C$
END IF
IF POS(Tapeline$,"Z") THEN
	A$= "Z"&VAL$(PROUND(Z,-xyzafter))
        IF NOT POS(A$,".") THEN A$= A$&".0"
	CALL Word_string(B$,Tapeline$,"Z")
	Stat= ReplaceString(Tapeline$,B$,A$,C$)
	Tapeline$= C$
END IF

IF G=1 THEN
   ! NEED TO GET RID OF IJK IN G01 BLOCK
   IF POS(Tapeline$,"I") THEN
        A$= ""
        CALL Word_string(B$,Tapeline$,"I")
        Stat= ReplaceString(Tapeline$,B$,A$,C$)
        Tapeline$= C$
   END IF
   IF POS(Tapeline$,"J") THEN
        A$= ""
        CALL Word_string(B$,Tapeline$,"J")
        Stat= ReplaceString(Tapeline$,B$,A$,C$)
        Tapeline$= C$
   END IF
   IF POS(Tapeline$,"K") THEN
        A$= ""
        CALL Word_string(B$,Tapeline$,"K")
        Stat= ReplaceString(Tapeline$,B$,A$,C$)
        Tapeline$= C$
   END IF
ELSE
   IF POS(Tapeline$,"I") THEN
	A$= "I"&VAL$(PROUND(I,-xyzafter))
        IF NOT POS(A$,".") THEN A$= A$&".0"
	CALL Word_string(B$,Tapeline$,"I")
	Stat= ReplaceString(Tapeline$,B$,A$,C$)
	Tapeline$= C$
   END IF
   IF POS(Tapeline$,"J") THEN
	A$= "J"&VAL$(PROUND(J,-xyzafter))
        IF NOT POS(A$,".") THEN A$= A$&".0"
	CALL Word_string(B$,Tapeline$,"J")
	Stat= ReplaceString(Tapeline$,B$,A$,C$)
	Tapeline$= C$
   END IF
   IF POS(Tapeline$,"K") THEN
	A$= "K"&VAL$(PROUND(K,-xyzafter))
        IF NOT POS(A$,".") THEN A$= A$&".0"
	CALL Word_string(B$,Tapeline$,"K")
	Stat= ReplaceString(Tapeline$,B$,A$,C$)
	Tapeline$= C$
   END IF
END IF

IF POS(Tapeline$,"R") THEN
	A$= "R"&VAL$(PROUND(R,-xyzafter))
        IF NOT POS(A$,".") THEN A$= A$&".0"
	CALL Word_string(B$,Tapeline$,"R")
	Stat= ReplaceString(Tapeline$,B$,A$,C$)
	Tapeline$= C$
END IF
IF POS(Tapeline$,"F") THEN
	A$= "F"&VAL$(PROUND(F,-fafter))
        IF NOT POS(A$,".") THEN A$= A$&".0"
	CALL Word_string(B$,Tapeline$,"F")
	Stat= ReplaceString(Tapeline$,B$,A$,C$)
	Tapeline$= C$
END IF
END IF

Fileline$= Tapeline$&Comments$
RETURN

G01_block:!   makes the g01 block for Interpolation

Tapeline$=""
G= 1
D1= ABS(G-Gr)
IF D1>= delta THEN
	Tapeline$=Tapeline$&"G"&VAL$(G)
	Gr= G
END IF

X= PROUND(X,-xyzafter)
D1= ABS(X-Xr)
IF D1>= delta THEN
	A$= "X"&VAL$(X)
	IF NOT POS(A$,".") THEN A$= A$&".0"
	Tapeline$=Tapeline$&A$
	Xr= X
END IF

Y= PROUND(Y,-xyzafter)
D1= ABS(Y-Yr)
IF D1>= delta THEN
	A$= "Y"&VAL$(Y)
	IF NOT POS(A$,".") THEN A$= A$&".0"
	Tapeline$=Tapeline$&A$
	Yr= Y
END IF

Z= PROUND(Z,-xyzafter)
D1= ABS(Z-Zr)
IF D1>= delta THEN
	A$= "Z"&VAL$(Z)
	IF NOT POS(A$,".") THEN A$= A$&".0"
	Tapeline$=Tapeline$&A$
	Zr= Z
END IF

F= PROUND(F,-fafter)
D1= ABS(F-Fr)
IF D1>= delta THEN
	A$= "F"&VAL$(F)
	IF NOT POS(A$,".") THEN A$= A$&".0"
	Tapeline$=Tapeline$&A$
	Fr= F
END IF

Fileline$= Tapeline$
RETURN


Leave:!

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

Word_string:SUB Word_string(W$,L$,A$)
DIM B$[512]

N= POS(L$,A$)
IF N= 0 THEN
        W$=""
ELSE
        B$= L$[N]

	W$=A$
	FOR I= 2 TO LEN(B$)
		SELECT B$[I,I]
		CASE " ","+","-",".","0","1","2","3","4","5","6","7","8","9"
			W$= W$&B$[I,I] 
		CASE ELSE
			SUBEXIT
		END SELECT
	NEXT I
END IF
SUBEND

Read_word:SUB ReadWord(Value,Valuer,L$,A$,stype,before,after)
DIM W$[512]

	CALL Word_string(W$,L$,A$)
	IF LEN(W$)>1 THEN

		SELECT stype
		CASE 0
			factor=1
		CASE 1  
			factor= 1.0/10.0^after
		END SELECT

		Value= VAL(W$[2])*factor
	ELSE
		Value= Valuer
	END IF

SUBEND

