dwg2pdf - diffrent layouts and paper sizes

Use this forum to ask questions about how to do things in QCAD.

Moderator: andrew

Forum rules

Always indicate your operating system and QCAD version.

Attach drawing files and screenshots.

Post one question per topic.

Post Reply
bernhard
Newbie Member
Posts: 4
Joined: Mon Jul 08, 2019 12:58 pm

dwg2pdf - diffrent layouts and paper sizes

Post by bernhard » Tue Jul 09, 2019 7:53 am

Hi,

I wrote a small script (in python) that converts all dwg files in a folder into pdfs using the dwg2pdf bat file.
The problem is that these dwg files have different layouts and paper sizes so the output pdf is always cutting off some part of the dwgs.
In know about the auto fit option but that doesn't really work as it compress the font and lines to much so that you can't read anything in pdf.

So I thought I would need the information about the layout and paper size of the dwg before I process it with the dwg2pdf bat file.
Any idea how to access this information? Or other suggestions or ways of doing it?

I'm using the Windows QCAD Professional V3.22.1.0.

Thanks!

User avatar
andrew
Site Admin
Posts: 9052
Joined: Fri Mar 30, 2007 6:07 am

Re: dwg2pdf - diffrent layouts and paper sizes

Post by andrew » Tue Jul 09, 2019 11:01 am

Are these drawings set up to print with QCAD? I.e. when you load the drawing in QCAD and switch to print preview, is it all set up correctly for printing / PDF export?

bernhard
Newbie Member
Posts: 4
Joined: Mon Jul 08, 2019 12:58 pm

Re: dwg2pdf - diffrent layouts and paper sizes

Post by bernhard » Tue Jul 09, 2019 1:30 pm

No, the drawings werde made with Autosketch and no the drawings are not set up correctly for print.
It seems that the default settings for QCAD are the layout "portrait" and the paper size "ANSI A (Letter)" regardless of the dwg file... (see Attachment as example for print preview)
Attachments
QCAD_example.png
example
QCAD_example.png (11.57 KiB) Viewed 4614 times

User avatar
andrew
Site Admin
Posts: 9052
Joined: Fri Mar 30, 2007 6:07 am

Re: dwg2pdf - diffrent layouts and paper sizes

Post by andrew » Tue Jul 09, 2019 1:51 pm

OK. Perhaps you could come up with a heuristic based on the bounding box of a drawing (bbox command line tool).

bernhard
Newbie Member
Posts: 4
Joined: Mon Jul 08, 2019 12:58 pm

Re: dwg2pdf - diffrent layouts and paper sizes

Post by bernhard » Tue Jul 09, 2019 2:43 pm

I was thinking about that too but this would still means that I need that information before I exceute dwg2pdf.
And I don't know how to get these information from a dwg file...

What do mean with bbox command line tool?

bernhard
Newbie Member
Posts: 4
Joined: Mon Jul 08, 2019 12:58 pm

Re: dwg2pdf - diffrent layouts and paper sizes

Post by bernhard » Tue Aug 20, 2019 8:00 am

So the bbox command line tool is an undocumented command line tool that reports the bounding box sizes of the dwg file. It is located at the same path as the others and is named bbox.bat
Example:

Code: Select all

08:27:39: Debug:    calling odInitialize
Min: 9.832068327158936/8.033705542856726
Max: 201.95302034075638/285.3751060698974
Size: 192.12095201359745/277.34140052704066
Based on this I extracted the sizes of the x or y coordinate to determine if the file is in landscape or protrait mode.
Luckily I could ignore the different paper sizes as I had further discussions with my colleagues.

Here the complete script (in Python) for others with the same or similar questions:

Code: Select all

#Import
import os
import time
import datetime
import subprocess

#Listing of all files with complete path in source folder
for path, subdirs, documents in os.walk(source_graz_HI):
    for name in documents:
        if '.dwg' in name:
            #Appending dwg to list
            dwg_documents.append(os.path.join(path, name))

#for each dwg file in source folder do this
for dwg in dwg_documents:
    if '.dwg' in dwg:
        print "DWG: ", dwg
        #Since dwg file and linked pdf file are stored under the same name and path
        #I can use the path + name of each dwg file to generate the same for the pdf file
        extension = dwg.split('.')
        pdf = str(extension[0]) + ".pdf"
        print "PDF: ", pdf
        #if dwg file and pdf file exists do this
        if os.path.isfile(dwg) and os.path.isfile(pdf): 
            #Get the modify date (unix timestamp) of the dwg and pdf file
            mtime_dwg = os.path.getmtime(dwg)
            print "Mtime pur DWG: ", mtime_dwg
            #ctime converts unixtimestamp to human-readable time & date
            print "Mtime DWG: ", time.ctime(mtime_dwg) 

            mtime_pdf = os.path.getmtime(pdf)
            print "Mtime pur PDF: ", mtime_pdf
            print "Mtime PDF: ", time.ctime(mtime_pdf)
            
            #Caluclate the time difference betweend dwg and pdf and divide through one day seconds.
            #60*60*24 = number of seconds of one day
            #Difference var = Amount of days between modify date of dwg and pdf file
            difference  = (mtime_pdf-mtime_dwg)/(60*60*24)
            print "Time: ", difference
            
            #if the difference is smaller then 0 (days) then the pdf is older then the dwg file and we need to create a new pdf
            if difference <0:
                print "PDF is older then DWG!"
                export = "True"
            #if the difference is bigger or equal then 0 (days) then the pdf is younder then the dwg file and we do not need to create a new pdf
            elif difference >= 0:
                print "PDF is younger then DWG!"
                export = "False"
        #If dwg file exists and pdf file does not exisits for cases where a complete new dwg is generate 
        elif os.path.isfile(dwg) and not os.path.isfile(pdf):
            print "No PDF found!"
            export ="True"

        #if new pdf is needed do this                 
        if export == "True":
            #First I need the bounding box values to detect portrait or landscape format of dwg and pdf file
            #Path of bbox.bat file
            bbox_call = r'"C:\Program Files\QCAD\bbox.bat"'
            #Call text to call the bbox file on the current dwg file
            bbox_bat_call = bbox_call + ' "' + dwg + '"'
            
            #Usage of call text var and to store the results (bounding box sizes) of the dwg
            #Decode is necessary since check_output is a byte object and we need a string
            bounding_box = subprocess.check_output(bbox_bat_call).decode("utf-8")

            #Necessary splitting to get only the x and y sizes of the bounding box 
            value = bounding_box.split('\n')[3]
            x_corr = value.split('/')[0]
            y_corr = value.split('/')[1]

            #Take only numbers and point into new var
            x_corr = ''.join([c for c in x_corr if c in '1234567890.'])
            y_corr = ''.join([c for c in y_corr if c in '1234567890.'])

            #convert string to float
            x_corr = float(x_corr)
            y_corr = float(y_corr)

            #Path of dwg2pdf bat file
            dwg2pdf_call = r'"C:\Program Files\QCAD\dwg2pdf.bat"'
            #X corrdiante bigger then y coordinate = landscape
            if x_corr > y_corr: 
                print "Querformat!"
                count_quer = count_quer + 1 #Counter
                #Call text for dwg2pdf bat file with dwg and necessary commmands
                dwg2pdf_bat_call = dwg2pdf_call + " -d -a -f -l -p 'A4'" + ' "' + dwg + '"'

            #X corrdiante smaller then y coordinate = portrait
            elif x_corr < y_corr: 
                print "Hochformat!"
                count_hoch = count_hoch + 1 #counter
                #Call text for dwg2pdf bat file with dwg and necessary commmands
                dwg2pdf_bat_call = dwg2pdf_call + " -d -a -f -p 'A4'" + ' "' + dwg + '"'
                
            print "DWG2PDF_Bat_call", dwg2pdf_bat_call
            print "\n"
            #Call to call dwg2pdf bat file
            subprocess.call(dwg2pdf_bat_call)
            count_call = count_call + 1 #Counter

Post Reply

Return to “QCAD 'How Do I' Questions”