
! script name: plot.d/autoplotscale
! topic: Sample - plot, center, and fit main view on a plotter to scale

!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 and fitted on the plotter.
!A
!B INSTRUCTIONS:
!B
!B      INPUT PAPER SIZE A B C D E: Enter the paper size to be used for
!B 	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
!C NOTE:
!C
!C 	This is an unsupported sample script.
!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]


! 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
PRINT Plotname$


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

CALL Setlinetypes(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)
stat= ScaleView(Plotter_view,Xmin,Xmax,Ymin,Ymax)
stat= TurnViewOff(Plotter_view)
stat= OutputString(Plotname$,"SP"&CHR$(10),WsAppend)
stat= OutputString(Plotname$,"SP"&CHR$(10),WsAppend)

IF Plot_to_device THEN
	stat= StartProcess("synplot.ksh "&Plotname$&" &")
END IF

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


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

SUB Setlinetypes(Answer$)
SELECT Answer$
CASE "A"
	stat= PlotLineTypeMap(2,"LT1,3")
	stat= PlotLineTypeMap(3,"LT1,5")
	stat= PlotLineTypeMap(4,"LT1,1.5")
	stat= PlotLineTypeMap(5,"LT2")
	stat= PlotLineTypeMap(6,"LT4")
	stat= PlotLineTypeMap(7,"LT5")
	stat= PlotLineTypeMap(8,"LT6")
	stat= PlotLineTypeMap(9,"LT")
	stat= PlotLineTypeMap(10,"LT5")
	stat= PlotLineTypeMap(11,"LT2")
	stat= PlotLineTypeMap(12,"LT6")
	stat= PlotLineTypeMap(13,"LT2")
	stat= PlotLineTypeMap(14,"LT6")
	stat= PlotLineTypeMap(15,"LT5")
CASE "B"
	stat= PlotLineTypeMap(2,"LT1,1")
	stat= PlotLineTypeMap(3,"LT1,2")
	stat= PlotLineTypeMap(4,"LT1,.5")
	stat= PlotLineTypeMap(5,"LT2,2")
	stat= PlotLineTypeMap(6,"LT4")
	stat= PlotLineTypeMap(7,"LT5")
	stat= PlotLineTypeMap(8,"LT6")
	stat= PlotLineTypeMap(9,"LT")
	stat= PlotLineTypeMap(10,"LT5")
	stat= PlotLineTypeMap(11,"LT2")
	stat= PlotLineTypeMap(12,"LT6")
	stat= PlotLineTypeMap(13,"LT2")
	stat= PlotLineTypeMap(14,"LT6")
	stat= PlotLineTypeMap(15,"LT5")
CASE "C"
	stat= PlotLineTypeMap(2,"LT1,.75")
	stat= PlotLineTypeMap(3,"LT1,1.5")
	stat= PlotLineTypeMap(4,"LT1,.375")
	stat= PlotLineTypeMap(5,"LT2,2")
	stat= PlotLineTypeMap(6,"LT4,1")
	stat= PlotLineTypeMap(7,"LT5,2")
	stat= PlotLineTypeMap(8,"LT6,2")
	stat= PlotLineTypeMap(9,"LT")
	stat= PlotLineTypeMap(10,"LT5,1")
	stat= PlotLineTypeMap(11,"LT2,2")
	stat= PlotLineTypeMap(12,"LT6,2")
	stat= PlotLineTypeMap(13,"LT2,2")
	stat= PlotLineTypeMap(14,"LT6,2")
	stat= PlotLineTypeMap(15,"LT5,2")
CASE "D"
	stat= PlotLineTypeMap(2,"LT1,.75")
	stat= PlotLineTypeMap(3,"LT1,1.5")
	stat= PlotLineTypeMap(4,"LT1,.375")
	stat= PlotLineTypeMap(5,"LT2,.5")
	stat= PlotLineTypeMap(6,"LT4,1")
	stat= PlotLineTypeMap(7,"LT5,2")
	stat= PlotLineTypeMap(8,"LT6,2")
	stat= PlotLineTypeMap(9,"LT")
	stat= PlotLineTypeMap(10,"LT5,1")
	stat= PlotLineTypeMap(11,"LT2,2")
	stat= PlotLineTypeMap(12,"LT6,2")
	stat= PlotLineTypeMap(13,"LT2,2")
	stat= PlotLineTypeMap(14,"LT6,2")
	stat= PlotLineTypeMap(15,"LT5,2")
CASE "E"
	stat= PlotLineTypeMap(2,"LT1,.5")
	stat= PlotLineTypeMap(3,"LT1,1")
	stat= PlotLineTypeMap(4,"LT1,.25")
	stat= PlotLineTypeMap(5,"LT2,.375")
	stat= PlotLineTypeMap(6,"LT4,1")
	stat= PlotLineTypeMap(7,"LT5,1.5")
	stat= PlotLineTypeMap(8,"LT6,1.5")
	stat= PlotLineTypeMap(9,"LT")
	stat= PlotLineTypeMap(10,"LT5,1")
	stat= PlotLineTypeMap(11,"LT2,2")
	stat= PlotLineTypeMap(12,"LT6,2")
	stat= PlotLineTypeMap(13,"LT2,2")
	stat= PlotLineTypeMap(14,"LT6,2")
	stat= PlotLineTypeMap(15,"LT5,2")
END SELECT
SUBEND
