Size limit in dwg2bmp?

If you are having problems with QCAD, post here. Please report bugs through our Bug Tracker instead.

Always attach your original DXF or DWG file and mentions your QCAD version and the platform you are on.

Moderator: andrew

Forum rules

Always indicate your operating system and QCAD version.

Attach drawing files and screenshots.

Post one question per topic.

Post Reply
manmin
Newbie Member
Posts: 4
Joined: Tue Nov 17, 2015 11:28 pm

Size limit in dwg2bmp?

Post by manmin » Tue Nov 17, 2015 11:42 pm

Hi,

Is there a -r (or -x, -y) limit in dwg2bmp? A dwg file I tried to convert to png would work with -r 25, but fails with -r 26.

OS: Linux Fedora 20
QCAD: qcad-3.12.1-pro-linux-x86_64
DWG file: http://download.autodesk.com/us/samplef ... perial.dwg

$ ./dwg2bmp -a -b white -c -f -o architectural_example-imperial.png -r 26 architectural_example-imperial.dwg
...
Warning: libpng warning: Image width is zero in IHDR
Warning: libpng warning: Image height is zero in IHDR
libpng error: Invalid IHDR data
...

What would be the best way to work out the maximum -r (or -x, -y) value I can use for a given dwg file? The reason I want the max resolution possible is that I'm passing the generated png to an OCR program for text recognition.

Thanks for your help.

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

Re: Size limit in dwg2bmp?

Post by andrew » Wed Nov 18, 2015 9:33 am

manmin wrote:Is there a -r (or -x, -y) limit in dwg2bmp? A dwg file I tried to convert to png would work with -r 25, but fails with -r 26.
Yes, there are size limits for bitmaps. Please keep in mind that the bitmap has to be created in 24bit colors and uncompressed before it can be compressed into a much smaller PNG file.
manmin wrote:What would be the best way to work out the maximum -r (or -x, -y) value I can use for a given dwg file?
The -x and -y values can be used to indicate the desired size of the image and are the same for all drawing inputs. It's a matter of experimentation to find out which values still work on your system (probably somewhere between 20000 and 30000).
The reason I want the max resolution possible is that I'm passing the generated png to an OCR program for text recognition.
This seems like an unnecessary complicated and error prone process to me. Is there any reason why you cannot simply extract the plain text strings from the drawing file?

manmin
Newbie Member
Posts: 4
Joined: Tue Nov 17, 2015 11:28 pm

Re: Size limit in dwg2bmp?

Post by manmin » Wed Nov 18, 2015 10:02 am

The -x and -y values can be used to indicate the desired size of the image and are the same for all drawing inputs. It's a matter of experimentation to find out which values still work on your system (probably somewhere between 20000 and 30000).
What's the limit based on? Is it a memory limitation? Or some hardcoded limit? I'm just wondering if there's an equation I can use to determine the -x and -y values as I'll be using dwg2bmp in a script to process large numbers of dwg files (so trial and error method is not practical).
This seems like an unnecessary complicated and error prone process to me. Is there any reason why you cannot simply extract the plain text strings from the drawing file?
2 reasons:
1) I don't know how to retrieve the text from the dwg. Is there any tool in qcad that would allow me to do that?
2) The ocr process also retrieves the position of the text in the image, and this allows me to highlight the text when the image (from dwg2bmp) is presented to the user. I'm open to suggestions if there are any tools in qcad that would allow me to do that.

Thanks again for your help.

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

Re: Size limit in dwg2bmp?

Post by andrew » Wed Nov 18, 2015 10:53 am

What's the limit based on? Is it a memory limitation? Or some hardcoded limit? I'm just wondering if there's an equation I can use to determine the -x and -y values as I'll be using dwg2bmp in a script to process large numbers of dwg files (so trial and error method is not practical).
It's a limit imposed by the underlying libraries that are used to create the output file. Note that a 30000x30000 pixel bitmap takes up 30000x30000x4 Bytes of memory (~3.5GB).
1) I don't know how to retrieve the text from the dwg. Is there any tool in qcad that would allow me to do that?
It's certainly possible to extract this information using the script interface of QCAD, yes. It would require some script development (in JavaScript).
2) The ocr process also retrieves the position of the text in the image, and this allows me to highlight the text when the image (from dwg2bmp) is presented to the user. I'm open to suggestions if there are any tools in qcad that would allow me to do that.
Here's how I would likely approach this problem:
- Extract all texts in a DWG/DXF into a simple XML or JSON file containing all text strings and their bounding boxes (in pixels referring to the bitmap).
- Use the bitmap and XML file together to look up texts and their positions.

manmin
Newbie Member
Posts: 4
Joined: Tue Nov 17, 2015 11:28 pm

Re: Size limit in dwg2bmp?

Post by manmin » Wed Nov 18, 2015 11:07 am

It's certainly possible to extract this information using the script interface of QCAD, yes. It would require some script development (in JavaScript).
Could you please point me to any documentation where I can get started.
Here's how I would likely approach this problem:
- Extract all texts in a DWG/DXF into a simple XML or JSON file containing all text strings and their bounding boxes (in pixels referring to the bitmap).
- Use the bitmap and XML file together to look up texts and their positions.
That was the planned approach, but using an ocr tool. Doing it without the latter would be a much better approach. Thank you.

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

Re: Size limit in dwg2bmp?

Post by andrew » Wed Nov 18, 2015 11:36 am

Here's a simple, rough example you can use as starting point. This should solve the most crucial problems (creating a bitmap, extracting texts and bounding boxes mapped to pixels).
Save the file for example to scripts/Tools/ExtractTextInfo of your QCAD installation.

Then you can run it with:

Code: Select all

./qcad -autostart "scripts/Tools/ExtractTextInfo/ExtractTextInfo.js"
Note that this is by no means a complete solution. You might want to customize bitmap parameters, extract also text from blocks, etc.
include("scripts/library.js");
include("scripts/Tools/arguments.js");
include("scripts/File/BitmapExport/BitmapExportWorker.js");

function printHelp() {
    print("Usage: " + args[1] + " [OPTIONS]... <drawing file>");
    print();
    print("Extracts information from a drawing file (DWG, DXF, ...).");
    print();
    print("  -f, -force              Overwrite existing output file");
    print("  -o FILE, -outfile=FILE  Set output file to FILE");
    print("                          default is same path and base name as input file");
    print();
}

/**
 * \return RBox, mapped to view coordinates (pixels)
 */
function mapBoxToView(view, box) {
    return new RBox(view.mapToView(box.getMinimum()), view.mapToView(box.getMaximum()));
}

function main() {
    if (testArgument(args, "-h", "-help")) {
        printHelp();
        return;
    }
    
    if (args.length < 3) {
        print("No input file given. Try -h for help.");
        return;
    }

    var cadFile = args[args.length - 1];
    if (cadFile.indexOf("-") === 0) {
        print("No input file. Try -h for help.");
        return;
    }

    var fi = new QFileInfo(cadFile);
    if (!fi.isAbsolute()) {
        cadFile = RSettings.getLaunchPath() + QDir.separator + cadFile;
        fi = new QFileInfo(cadFile);
    }

    // get bitmap output name from command line:
    var bmpFileName = getArgument(args, "-o", "-outfile");
    if (bmpFileName!==undefined) {
        if (!new QFileInfo(bmpFileName).isAbsolute()) {
            bmpFileName = RSettings.getLaunchPath() + QDir.separator + bmpFileName;
        }
    }
    else {
        bmpFileName = fi.absolutePath() + QDir.separator + fi.completeBaseName() + ".bmp";
    }

    if (new QFileInfo(bmpFileName).exists() && !testArgument(args, "-f", "-force")) {
        print("Output file exists already, not overwriting: ", bmpFileName);
        print("Use -f to force overwrite");
        return;
    }

    // see BitmapExportWorker.js
    var properties = [];

    // create document:
    var storage = new RMemoryStorage();
    var spatialIndex = new RSpatialIndexNavel();
    var doc = new RDocument(storage, spatialIndex);
    var di = new RDocumentInterface(doc);
    var scene = new RGraphicsSceneQt(di);
    var view = new RGraphicsViewImage();
    view.setScene(scene, false);

    // load document:
    if (di.importFile(cadFile) != RDocumentInterface.IoErrorNoError) {
        di.destroy();
        print("Cannot import file:", cadFile);
        return;
    }

    QCoreApplication.processEvents();

    print("Converting");
    print("  from: " + cadFile);
    print("  to  : " + bmpFileName);

    var res = exportBitmap(doc, scene, bmpFileName, properties, view);

    var ids = doc.queryAllEntities();
    for (var i=0; i<ids.length; i++) {
        var e = doc.queryEntity(ids);
        if (isTextBasedEntity(e)) {
            qDebug("text:", e.getPlainText());
            qDebug("text bb in pixels:", mapBoxToView(view, e.getBoundingBox()));
        }
        if (isDimensionEntity(e)) {
            var td = e.getTextData();
            qDebug("dim label:", td.getPlainText());
            qDebug("dim label bb in pixels:", mapBoxToView(view, td.getBoundingBox()));
        }
    }

    print("Conversion finished.");
    di.destroy();
}

if (typeof(including)=='undefined' || including===false) {
    main();
}

manmin
Newbie Member
Posts: 4
Joined: Tue Nov 17, 2015 11:28 pm

Re: Size limit in dwg2bmp?

Post by manmin » Wed Nov 18, 2015 11:54 am

This is great - your sample code will give me a head start. Thank you very much for your help.

Post Reply

Return to “QCAD Troubleshooting and Problems”