!***********************************************************************
!  PublisgPDF / 2002-01-19 / Vertex Systems   
!                                                                      
!  This is an ADT program to create PDF file with multiple drawings.   
!  This program reads database Pdfpicts, converts drawings listed in   
!  it to PostScript and then uses Ghostscript (tm) to convert          
!  PostScript to PDF.                                                  
!***********************************************************************
! debug "./pdf_debug.txt"

ProjPath$ = get($PROJPATH)
PdfDir$ = ProjPath$
dircreate PdfDir$ 
! PdfDir$ = PdfDir$
Drive$ = "NOT_USED_HERE"
PdfProgramDrive$ = Drive$
CreateToc$ = get($CREATE_TOC)
PenColorSet$ = get($PEN_COLOR_SET)
UpdateTexts$ = get($TEXT_MACRO_UPD)
NewLimits$ = get($NEW_LIMITS)

ProjectDir$ = get($PROJPATH)
PdfParProgPath$ = get($PDFPROGPATH)          ;! Path for pdf adt programs

PdfProgram = 1                               ;! Needed in Ghostscript
! PdfProgramDrive$ = Drive$[1,1]&":"
PdfProgramPath$ = get($PROGPATH)             ;! Ghostscript folder

HomeDir$ = get($DIR_HOME)                    ;! Home folder
! Drive$ = HomeDir$[1,2]                       ;! Drive of home folder

Ps2PdfBat$ = PdfParProgPath$&"PDFps2pdf.bat" ;! ps -> pdf bat file
TmpPlotFile$ = HomeDir$&"plot.ps"            ;! Temporary ps file
GsLog$ = "gs.log"

set PDF_PRINT "1"

JobName$ = get($FILE)
JobHeader$ = get($ACTPROJNAME)

! Output files
FileName$ = ProjectDir$&JobName$
ControlPF$ = ".ctrl"            ;! PDF control file
MarkPF$ = ".mark"               ;! PDF pdfmark file
DrwgPF$ = ".drwg"               ;! All drawings in one PS -file
PdfPF$ = ".pdf"                 ;! PDF -file
PsPF$ = ".ps"                   ;! PS -file

! Open .ctrl file and plot drawings
open(9) FileName$&ControlPF$
write(9) "0 0 """&JobHeader$&""""

DbId=1
IdIn = get($DATABASE_ID)
dbopen(DbId) "ALREADY_OPEN",IdIn
if dbvalid(DbId) = 0 then
  exit
endif

DbList=1
dbfind(DbList) DbId, 0
dblsort(DbList) "PICNAME"

! Create pseudo plotter
PlotterDef$ = "plotter= PDF-TEMP 7 5 """&TmpPlotFile$
PlotterDef$ = PlotterDef$&""" """" 1 1 6 1 11 5.5 31 """" 32 ""PDF-SHEET"" 33 "
PlotterDef$ = PlotterDef$&PenColorSet$
set PLOTTER_DEF PlotterDef$
set PLOTTER_MODE "1"
function PLOTTER_SET

! Create pseudo sheet
set SHEET_MODE "1"
function SHEET_SET

I = 0
FirstPic = 1
Picts0 = dblistnum(DbList)
if Picts0 <= 0 then
  exit
endif
loop Picts0
  I = I+1 
  RowId= dblist(DbList,I)
  PicFile$ = dbgetf(DbId,RowId,"PICFILE")
  PicName$ = dbgetf(DbId,RowId,"PICNAME")
  Levels$ =  dbgetf(DbId,RowId,"LEVELGROUP")
  DrawingFile$ = PicFile$
  Ok = dwgopen("PICTURE",DrawingFile$, 10,10,35,100)
  if Ok = 1 then
    dwgactivate "PICTURE"

    ! New limits
    if cmp(NewLimits$,"1") then
      dwgsetlimitsby 0 0.05
    endif
	
    ! Calculate drawing size including margins
    Scale = get($SCALE)
    Width = (Xlimit_max-Xlimit_min)/Scale
    Height = (Ylimit_max-Ylimit_min)/Scale
    
    set "PDFVXP_PICW" Width
    set "PDFVXP_PICH" Height
    
    call pdf/pdf_sheet.prg
    ShFound = get("PDFVXP_SHTFOUND")
    Oversize = 0
    if (ShFound) then
      ShWidth = get("PDFVXP_SHTW")
      ShHeight = get("PDFVXP_SHTH")
    else
      MaxSize = inch_to_dwg(45.0)
      if Width>MaxSize then
        Width = MaxSize
        Oversize = 1
      endif
      if Height>MaxSize then
        Height = MaxSize
        Oversize = 1
      endif
      ShWidth = Width
      ShHeight = Height
    endif

    set TMPSHEET_XMIN "0"
    set TMPSHEET_XMAX ShWidth
    set TMPSHEET_YMIN "0"
    set TMPSHEET_YMAX ShHeight
    function SET_PDF_SHEET

    fileremove TmpPlotFile$

    Settings$ = "interaction=n,rotate=n,plotter=PDF-TEMP"
    if Oversize then
      Settings$ = Settings$&",free_scale=y"
    else
      Xo = (ShWidth-Width)/2
      Yo = (ShHeight-Height)/2
      Settings$ = Settings$&",xorg="&Xo
      Settings$ = Settings$&",yorg="&Yo
      Settings$ = Settings$&",free_scale=n"
    endif
    if (len(Levels$) > 0) then
      Levels$= "level_group="""&Levels$&""""
      Settings$ = Settings$&","&Levels$
    else
      Settings$ = Settings$&",level=0-255"
    endif
    
    ! Update text macros
    if cmp(UpdateTexts$,"1") then
      fill_dtext
    endif

    ! Plot
    dwgplot Settings$

    ! Write all ps files in one .drwg file
    if FirstPic then
      filecopy TmpPlotFile$ FileName$&DrwgPF$
      FirstPic = 0
    else
      Command$ ="type "&TmpPlotFile$&" >> "&FileName$&DrwgPF$
      Ok = system(Command$)
    endif

    dwgclose "PICTURE"
       
    ! Write to control file .ctrl
    Pic$ = I
    write(9) Pic$&" "&PicName$
  endif
endloop

! Delete pseudo plotter and sheet
set SHEET_MODE "0"
function SHEET_SET
set PLOTTER_MODE "0"
function PLOTTER_SET

dwgactivate "OLDDWG"
! Close .ctrl file
close(9)

if CreateToc$ = 1 then
  ! Create table of contents using control file
  file_roman8toiso FileName$&ControlPF$
  set PS_CONTROL_FILE FileName$&ControlPF$
  set PDF_MARK_FILE FileName$&MarkPF$
  MarkProg$ = PdfParProgPath$&"PDFcontrol2mark"
  call MarkProg$
  filecopy FileName$&MarkPF$ FileName$&PsPF$

endif

! Attach ps drawings
Command$ = "type """&FileName$&DrwgPF$&""" >> """&FileName$&PsPF$&""""
Ok = system(Command$)

! Convert PostScript to PDF
In$ = FileName$&PsPF$
Out$ = FileName$&PdfPF$
Command$ = Ps2PdfBat$&" """&FileName$&""" "&PdfProgram&" """&PdfProgramPath$&""" "&PdfProgramDrive$&" "&GsLog$
Ok = system(Command$)

! Remove temporary files
fileremove In$
fileremove PdfProgramPath$&GsLog$
fileremove FileName$&DrwgPF$
fileremove FileName$&MarkPF$
fileremove FileName$&ControlPF$
fileremove TmpPlotFile$

PdfFile$ = PdfDir$&JobName$&PdfPF$
set PDF_FILE PdfFile$
