
! script name: 3drender.d/blocksize
! 3dtopic: define starting block for syncut

!A
!A NAME:
!A
!A 	Syncut Block Size
!A
!A DESCRIPTION:
!A
!A 	This script allows the block size and resolution for syncut
!A 	simulation to be input.
!A
!B INSTRUCTIONS:
!B
!B	BLOCK THICKNESS: Input the block thickness.
!B
!B	START CORNER: Select a corner or the block.
!B
!B	PICK OTHER CORNER: Pick the other corner of the block.
!B
!B 	RESOLUTION (150 crude <-> 1000 fine ): Number of pixels along the side
!B 		of the block.
!B
!B 		For the large window 500 is a good quality image.
!B 		For a small window 300 is a good quality image.
!B
!C NOTES:
!C
!C 	The resolution determines the accuracy of the final image as well as
!C 	the calculation time.  Imaging time is proportional to the square of
!C 	the resolution and to the area of the final shaded view.  If the
!C 	resolution is doubled the imaging time is quadrupled.  Machining time
!C 	is proportional to the square of the resolution as well as the square
!C 	of the cutter diameter.  Simply put a .5 inch cutter will process four
!C 	times more slowly than a .25 inch cutter.
!C
!C 	Horizontal and vertical lines at a constant Z height will process much
!C 	faster than lines and curves with arbitrary orientation.
!C
!C 	The largest graphics window may only have 1250 pixels width maximum.
!C 	The largest normal synergy window has about 1000 pixels width.
!C 	A resolution of 1000 is the largest practical resolution for display
!C 	purposes. The largest possible resolution is 2000.
!C
!C 	On a 5 inch long block with a resolution of 500 the smallest
!C 	detail that can possibly be seen will be 5/500=0.01 of an inch.
!C 	Real variation may be +/- twice this quantity.
!C

Stat=ChangeLabelStr("ScriptNameStatus","Function: block size ")

DIM Destination$[256]
DIM Answer$[256]
DIM Load_script$[256]
REAL Params(0:10)

DeActivateUndo
IF ARGC>1 THEN
	Load_script$= ARGV(1)
ELSE
	Load_script$= "typecontrol"
END IF

Start:!

StdWorkplane

Units=CurrentUnits

Block_color=8
Top_color=2
Xedge_color=14
Yedge_color=14

IF Units=WsMetric THEN 
	Resolution=480
	Zthick=50
ELSE
	Resolution=480
	Zthick=2
END IF

Resolution=NamedVarVal("Syncut resolution",Ws3DSystemVars,Resolution)
Zthick=NamedVarVal("Syncut block thickness",Ws3DSystemVars,Zthick)


Block_thickness:!
Stat=SetInputFormat("n4a1=",",C,")
Stat=SetEqualsValue(Zthick)
Zthick=GetValue("BLOCK THICKNESS  Current block",Answer$)
IF ExitScript(Destination$) THEN Leave
IF Answer$= "C" THEN Leave
IF Zthick= 0.0 THEN Block_thickness

Pick_start:!
Stat=SetInputFormat("n4b","")
Ps=XyzInput("BLOCK CORNER",WsXyzInput)
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidGeometry(Ps) THEN Pick_start

Stat=SetLastPoint(Ps)

Depth= -Zthick

PlotOff
Length1= 4.0*(Radius+ ABS(Depth))
Width1=  Length1
Pe= PtIncrXyz(Ps,Length1,Width1,0.0)
Angle=0
Ruled1= TaperedBox(0.0,0.0,Length1,Width1,Depth,0.0,0.0,0.0)
IF NOT ValidGeometry(Ruled1) THEN
	Stat=DeleteEntity(Ps)
	Stat=DeleteEntity(Pe)
	GOTO Pick_start
END IF
Stat= MakeInvisible(Ruled1)
Stat= MakeInvisible(Pe)
PlotOn

Vnum= LastPickView
Params(0)= Depth
Num_params=1
Stat=SetInputFormat("n4b","")
Stat=DynamicEntity(Vnum,Ruled1,Ps,Pe,WsDyn3DBox,"PICK OTHER CORNER ",Params,Num_params)
IF (ExitScript(Destination$)) THEN
        Stat=DeleteEntity(Ruled1)
	Stat=DeleteEntity(Ps)
	Stat=DeleteEntity(Pe)
        IF Destination$= "BACKUP" THEN Pick_start
        GOTO Leave
END IF
Stat= PlotEntity(Ruled1)
PlotOff
Stat=DeleteEntity(Ps)
Stat=DeleteEntity(Pe)
Ps= SurfParameterPt(Ruled1,0,0)
Pe= SurfParameterPt(Ruled1,0.5,0)
P1= SurfParameterPt(Ruled1,0,1)
Z1= ZPointValue(Ps)
Z2= ZPointValue(P1)
Zthick= Z2-Z1
Stat=DeleteEntity(Ruled1)
PlotOn

Calculate_block:!
X1=XPointValue(Ps)
Y1=YPointValue(Ps)
Z1=ZPointValue(Ps)
X2=XPointValue(Pe)
Y2=YPointValue(Pe)
Xlength= ABS(X2-X1)
Ywidth= ABS(Y2-Y1)
Xc= (X1+ X2)/2
Yc= (Y1+ Y2)/2
Zc= Z1

PlotOff
Stat=DeleteEntity(Ruled1)
Stat=DeleteEntity(Ps)
Stat=DeleteEntity(Pe)
Stat=DeleteEntity(P1)
PlotOn

xmin=Xc-Xlength/2
xmax=Xc+Xlength/2
ymin=Yc-Ywidth/2
ymax=Yc+Ywidth/2
IF Zthick < 0 THEN
	zmin=Zc+Zthick
	zmax=Zc
ELSE
	zmin=Zc
	zmax=Zc+Zthick
END IF

IF Ask_resolution THEN
	Stat=SetInputFormat("n0=","")
	Stat=SetEqualsValue(Resolution)
	Resolution=GetValue("RESOLUTION  (150 crude <-> 1000 fine )",Answer$)
	IF ExitScript(Destination$) THEN Leave
END IF


Max_units=Resolution
u1=Max_units/Xlength
u2=Max_units/Ywidth
gunits=u1
IF u2< u1 THEN gunits=u2


Stat=Setup3dBlock(gunits,xmin,xmax,ymin,ymax,zmin,zmax,Block_color,Top_color,Xedge_color,Yedge_color)

Leave:!
VResolution=CreateVariable("Syncut resolution",Ws3DSystemVars,Resolution)
VThickness=CreateVariable("Syncut block thickness",Ws3DSystemVars,Zthick)
Stat=DeleteEntity(Ruled1)
Stat=DeleteEntity(Ps)
Stat=DeleteEntity(Pe)
IF NOT LEN(Destination$) THEN
	LOAD Load_script$
ELSE
	LOAD Destination$
END IF
END
