[solved] Importing DWG files

Discussion forum for C++ and script developers who are using the QCAD development platform or who are looking to contribute to QCAD (translations, documentation, etc).

Moderator: andrew

Forum rules

Always indicate your operating system and QCAD version.

Attach drawing files, scripts and screenshots.

Post one question per topic.

Post Reply
GnrlSchnavy
Newbie Member
Posts: 8
Joined: Fri Aug 18, 2017 2:58 pm

[solved] Importing DWG files

Post by GnrlSchnavy » Sat Aug 19, 2017 10:12 am

Eventually I need a script that opens multiple .DWG files, makes some changes to all of them and then exports them to .PDF.

Using some of the examples and the documentation I've gotten pretty far except there is one problem I keep running into to which is that I don't seem to be importing the .DWG correctly. When I use the Print class to print to a PDF it does print out the correct dimensions of the paper as I set it up. It just doesn't show the .DWG I imported.

To import the DWG I just use:

var doc = new RDocument(new RMemoryStorage(), new RSpatialIndexNavel());
var di = new RDocumentInterface(doc);
di.importFile("fileToImport.dwg");
var scene = new RGraphicsSceneQt(di);
var view = new RGraphicsViewImage();
view.setScene(scene,false);
doc.setUnit(RS.Millimeter);
scene.regenerate();

Any pointers to what the correct way for importing is?


Kind regards,

Yvan

GnrlSchnavy
Newbie Member
Posts: 8
Joined: Fri Aug 18, 2017 2:58 pm

Re: Importing DWG files

Post by GnrlSchnavy » Fri Aug 25, 2017 11:30 am

Almost 2k views and no one able to give me any hints? I'm wondering if it is needed, just like with the merge.js, to copy and paste every element one by one into a new seperate RDocument or if there is a way to skip all that and just import the .DWG file into the view or scene directly.

Again, if someone can give me any pointers in the right direction that would be awesome. We are using the PRO version.

Thanks in advance!

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

Re: Importing DWG files

Post by andrew » Fri Aug 25, 2017 12:14 pm

GnrlSchnavy wrote:Almost 2k views and no one able to give me any hints? I'm wondering if it is needed, just like with the merge.js, to copy and paste every element one by one into a new seperate RDocument or if there is a way to skip all that and just import the .DWG file into the view or scene directly.
Your code looks fine. You can verify some things though:

Code: Select all

// verify import:
if (di.importFile("fileToImport.dwg") != RDocumentInterface.IoErrorNoError) {
   // there was an error while importing the file..
}

// dump imported document to stdout:
doc.dump();

GnrlSchnavy
Newbie Member
Posts: 8
Joined: Fri Aug 18, 2017 2:58 pm

Re: Importing DWG files

Post by GnrlSchnavy » Fri Aug 25, 2017 12:53 pm

andrew wrote:
GnrlSchnavy wrote:Almost 2k views and no one able to give me any hints? I'm wondering if it is needed, just like with the merge.js, to copy and paste every element one by one into a new seperate RDocument or if there is a way to skip all that and just import the .DWG file into the view or scene directly.
Your code looks fine. You can verify some things though:

Code: Select all

// verify import:
if (di.importFile("fileToImport.dwg") != RDocumentInterface.IoErrorNoError) {
   // there was an error while importing the file..
}

// dump imported document to stdout:
doc.dump();
It dumps a lot of data, so I'm pretty sure the import has worked. That leaves me even more guessing to why my .pdf files are empty and only 1kb :roll: .

Also when I export it to a DFX file it works perfectly fine. I get the same file back, just not when doing a pdf export.

This is the full script I'm running now:

Code: Select all

function main(){ 

//Start imports
include("scripts/File/Print/Print.js");
include("scripts/ImportExport/SvgImporter/SvgImporterInit.js");
include("scripts/Tools/arguments.js");
//End imports

//Start document initiation
var doc = new RDocument(new RMemoryStorage(), new RSpatialIndexNavel());
var di = new RDocumentInterface(doc);
doc.setUnit(RS.Millimeter);

if(testArgument(args, "-f")){
	print("found file");
	inputfile = getArgument(args, "-f");
	di.importFile(inputfile);
	print("processing " + inputfile);
}

// verify import:
if (di.importFile(inputfile) != RDocumentInterface.IoErrorNoError) {
print("something went wrong with import");
}

// dump imported document to stdout:
// doc.dump();

var scene = new RGraphicsSceneQt(di);
var view = new RGraphicsViewImage();
view.setScene(scene,true);
scene.regenerate();
//End document initiation


//Start page variables
var pageWidth = 310;
var pageHeight = 97;
var bb = doc.getBoundingBox(true, true);
var scale = 1000.0;

doc.setVariable("PageSettings/PaperUnit", RS.Millimeter);
doc.setVariable("PageSettings/PaperWidth", pageWidth);
doc.setVariable("PageSettings/PaperHeight", pageHeight);
doc.setVariable("PageSettings/PageOrientation", "Portrait");
doc.setVariable("ColorSettings/ColorMode", "GreyScale");
doc.setVariable("PageSettings/Scale", "1000:1");
doc.setVariable("PageSettings/OffsetX", 0);
doc.setVariable("PageSettings/OffsetY", 0);
doc.setVariable("MultiPageSettings/Rows", 1);
doc.setVariable("MultiPageSettings/Columns", 1);
doc.setVariable("MultiPageSettings/PrintCropMarks", false);
doc.setVariable("PageTagSettings/EnablePageTags", false);
//End page variables

//Start export
var printer = new Print(undefined, doc, view);
filename = inputfile.substring(inputfile.lastIndexOf("/")+1,inputfile.length-4);
path = "/Users/yvan/Documents/pythonscripts/"+filename+".pdf";
printer.print(path);
print("fileexported to " + path);

//End export
}


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





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

Re: Importing DWG files

Post by andrew » Fri Aug 25, 2017 1:30 pm

Do you get the desired result when using dwg2pdf?
Can you attach the DXF file?

GnrlSchnavy
Newbie Member
Posts: 8
Joined: Fri Aug 18, 2017 2:58 pm

Re: Importing DWG files

Post by GnrlSchnavy » Fri Aug 25, 2017 2:27 pm

Already found a solution. I messed up the scale. :p. Thanks for your help Andrew. Now onto removing entities with text in batch :D

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”