
! script name: plot.d/plottoscale
! topic: plot a view to scale on a plotter

!A
!A NAME:
!A
!A 	Plot to Scale
!A
!A DESCRIPTION:
!A
!A 	This script is for plotting a view on a plotter to scale.  The
!A 	perspective (e.g. isometric, top, front, or side) of the main
!A 	view is centered on the plotter.
!A
!B INSTRUCTIONS:
!B
!B 	INPUT PAPER SIZE A B C D E: Enter the paper size
!B 		to be used for plotting.
!B
!C NOTES:
!C
!C 	The A size is the default paper.  The data files for the different
!C 	size paper are in:
!C
!C 		"/usr/weber/util/tooldata/plotconfig"
!C
!C 	The files may need to be modified depending on the type of plotter
!C 	being used.  New files may be added by following the format of the
!C 	other files.  If a new file is added, make sure the option is added
!C 	to the question and answer selections.
!C

stat=ChangeLabelStr("ScriptNameStatus","Function: plot to scale ")

OPTION BASE 1
DIM Destination$[256]
DIM Answer$[256]
DIM Filename$[256]
DIM Namelist$[32000]
DIM Plotname$[256]
DIM Papersize$[16]


! 0 sends plot to a file plotfile1.hpgl in local directory
! 1 sends plot to device through synplot.ksh
Plot_to_device=1 


! Generate plotfile name
Namelist$= DirectoryNamesStr("./","*",WsAscending,"plotfile",".hpgl")
IF NOT POS(Namelist$,"plotfile") THEN
        Plotname$="plotfile1.hpgl"
ELSE
        A$= NextNumberedName(Namelist$,"plotfile",",")
        Plotname$=A$&".hpgl"
END IF

P1x= -4500
P1y= -2790
P2x= 4500
P2y= 2790

Filename$="/usr/weber/util/tooldata/plotconfig"

Stat=SetInputFormat("a1",",A,B,C,D,E,")
Stat= GetValue("INPUT PAPER SIZE A B C D E ",Answer$)
IF (ExitScript(Destination$)) THEN Leave
Papersize$=Answer$

Filename$=FileNameString(Filename$,Answer$&"size")
Plot_config=ReadUserData(2013,"PLOT CONFIG",Filename$)
IF (NOT (ValidGeometry(Plot_config))) THEN Start
P1x=GetUserValue(Plot_config,"P1x:")
P1y=GetUserValue(Plot_config,"P1y:")
P2x=GetUserValue(Plot_config,"P2x:")
P2y=GetUserValue(Plot_config,"P2y:")

PRINT "P1 & P2",P1x,P1y,P2x,P2y

Start:Plotter_view= FindView("PLOTTER")
IF NOT Plotter_view THEN
	xmin= -10
	xmax= 10
	ymin= -10
	ymax= 10
	stat= MakePlotterView("PLOTTER",Plotname$,xmin,xmax,ymin,ymax,p1x,p1y,p2x,p2y)
	GOTO Start
ELSE
	Stat= ChPlotterViewFile(Plotter_view,Plotname$)
END IF

Source_view= 1


Ask_scale:Stat=SetInputFormat("n4=","")
Stat=SetEqualsValue(1.0)
Plot_scale= GetValue("PLOT SCALE ",Answer$)
IF (ExitScript(Destination$)) THEN Leave
if(NOT plot_scale) THEN Ask_scale

Units= CurrentUnits

Plotter_units=1016
IF Units= WsMetric THEN plotter_units= 40

Scale_factor= 1/Plot_scale
Xlength=  Scale_factor*ABS(p2x- p1x)/plotter_units
Ywidth=  Scale_factor*ABS(p2y- p1y)/plotter_units

stat= CopyViewTrans(Plotter_view,Source_view)

! /* must resize source view for center point */

stat= ResizeView(Source_view,WsUpdatePlotFile,0)
Pc= ViewCornerPoint(Source_view,WsCenter)

Xc= XPointValue(Pc)
Yc= YPointValue(Pc)
Xmin= Xc- Xlength/2
Ymin= Yc- Ywidth/2
Xmax= Xc+ Xlength/2
Ymax= Yc+ Ywidth/2
stat= DeleteEntity(Pc)

PRINT "MINMAX",Xmin,Ymin,Xmax,Ymax
PRINT "Scale factor", Scale_factor

stat= TurnViewOn(Plotter_view)
stat= OutputString(Plotname$,"SP1"&CHR$(10),WsNewFile)
stat= ChangeP1_P2(Plotter_view,p1x,p1y,p2x,p2y)
Drawing= ActiveDrawing
IF ValidGeometry(Drawing) THEN
        Stat= PlotDrawing(Drawing,Plotter_view,WsTrue)
ELSE
        stat= ScaleView(Plotter_view,Xmin,Xmax,Ymin,Ymax)
END IF
stat= TurnViewOff(Plotter_view)
stat= OutputString(Plotname$,"SP"&CHR$(10),WsAppend)
stat= OutputString(Plotname$,"SP"&CHR$(10),WsAppend)

IF Plot_to_device THEN
	stat=ChangeLabelStr("ScriptNameStatus","SYNPLOT,"&Papersize$&","&Plotname$)
! 	stat= StartProcess("synplot.ksh "&Plotname$&" "&Papersize$&" &")
END IF

Leave:!
Stat= DeleteEntity(Plot_config)
Stat=SetInputFormat("n4","")

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