How to export block?

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
David_2021
Registered Member
Posts: 2
Joined: Mon Sep 20, 2021 4:44 am

How to export block?

Post by David_2021 » Mon Sep 20, 2021 5:21 am

Hi Andrew,

I have a DXF file contains lots of blocks. And I want to export blocks to separated DXF files one by one by Javascript. I tried as below but it doesn't work. Did I miss something in those codes? Thank you.

Code: Select all

MyAction.prototype.beginEvent = function() {
    MyTest.prototype.beginEvent.call(this);

    var appWin = EAction.getMainWindow();

    var di_src = EAction.getDocumentInterface();
    var doc_src = di_src.getDocument();

    var my_appWin = RMainWindowQt.getMainWindow();
    var my_tabBar = my_appWin.getTabBar();
    var idx = my_tabBar.currentIndex;

    var ids = doc_src.queryAllBlocks();
    if (ids.length !== 0) {
        for (var i = 0; i < ids.length; i++) {
            if (!doc_src.queryObject(ids[i]).isModelSpace() && !doc_src.isLayoutBlock(ids[i])) {

                var action1 = RGuiAction.getByScriptFile("scripts/File/NewFile/NewFile.js");
                if (!isNull(action1)) {
                    action2.slotTrigger();
                }
                var di_dest = EAction.getDocumentInterface();
                var doc_dest = di_dest.getDocument();

                var op_clipBoard = new RClipboardOperation();
                op_clipBoard.copyBlock(ids[i], doc_src, doc_dest);


                op_past = new RPasteOperation(RDocument.getClipboard());
                op_past.setOffset(new RVector(0, 0));
                op_past.setRotation(0.0);
                op_past.setScale(1.0);
                op_past.setFlipHorizontal(false);
                op_past.setFlipVertical(false);
                op_past.setToCurrentLayer(true);
                op_past.setOverwriteBlocks(true);


                di_dest.applyOperation(op_past);

                di_dest.exportFile(myPath + blockName + ".dxf", "DXF 2000");

                var action2 = RGuiAction.getByScriptFile("scripts/File/CloseFile/CloseFile.js");
                if (!isNull(action2)) {
                    action2.slotTrigger();
                }
            }

        }
        var my_mdiArea = EAction.getMdiArea();
        var windows = my_mdiArea.subWindowList();
        my_mdiArea.setActiveSubWindow(windows[idx]);
    } else {
        appWin.handleUserMessage("an empty file");
    }

    this.terminate();
};
Attachments
blocktest.dxf
(181.85 KiB) Downloaded 298 times
blockexport.js
(3.17 KiB) Downloaded 308 times

CVH
Premier Member
Posts: 3415
Joined: Wed Sep 27, 2017 4:17 pm

Re: How to export block?

Post by CVH » Mon Sep 20, 2021 5:49 am

Hi,
Not sure ... blocks are not my thing.

Code: Select all

var action1 = RGuiAction.getByScriptFile("scripts/File/NewFile/NewFile.js");
if (!isNull(action1)) {
    action2.slotTrigger();
}
I think that you mean if action1 exists then trigger action1 instead of action2 what is not yet declared at this point.

IMHO there is no new file created and di_dest is the same document interface as di_src.
The copy/paste then acts on the same document. :wink:

The same happens if action1 doesn't exist at all.
Then there is no use of even trying to copy/paste.

Maybe it is just a shot in the dark. :wink:

Regards,
CVH

CVH
Premier Member
Posts: 3415
Joined: Wed Sep 27, 2017 4:17 pm

Re: How to export block?

Post by CVH » Mon Sep 20, 2021 7:04 am

Further:

Code: Select all

op_clipBoard.copyBlock(ids[i], doc_src, doc_dest);
Will throw "Wrong number/types of arguments for RClipboardOperation.copyBlock()"

Documented here:
https://qcad.org/doc/qcad/3.0/developer ... 1365bf73ea
It doesn't seem that the 4 missing arguments are optional.

The operation op_clipBoard itself is not applied anywhere.

I even think that one can't copy a block definition to the clipboard.

Regards,
CVH

CVH
Premier Member
Posts: 3415
Joined: Wed Sep 27, 2017 4:17 pm

Re: How to export block?

Post by CVH » Mon Sep 20, 2021 7:20 am

Another issue:
For exporting the new file the path and block name are not declared:

Code: Select all

di_dest.exportFile(myPath + blockName + ".dxf", "DXF 2000");
You probably have to add a folder separator in between, a '/' or '\' or QDir.separator

CVH

David_2021
Registered Member
Posts: 2
Joined: Mon Sep 20, 2021 4:44 am

Re: How to export block?

Post by David_2021 » Tue Sep 21, 2021 1:32 am

HI CVH, thanks so much. So if I want to export block from a DXF file to another DXF with JS, the process I think should be : select the block-->copy -->create a new Dxf file-->paste--->export the new DXF file. Is it correct? and any suggestion that which functions are useable to finish the process?

CVH
Premier Member
Posts: 3415
Joined: Wed Sep 27, 2017 4:17 pm

Re: How to export block?

Post by CVH » Tue Sep 21, 2021 6:53 am

David,

Like I said, blocks are not my thing.
David_2021 wrote:
Tue Sep 21, 2021 1:32 am
select the block-->copy -->create a new Dxf file-->paste--->export
There is a major difference between an RBlock and an RBlockReferenceEntity.
The last one is an entity/object that we can copy, paste, duplicated, move, rotate and so on.
Copy is not available when you select a block in the Block List.

Yesterday, I managed coding the envelope with off-screen documents.
No need to switch drawing tabs at all and much faster there display updating and rendering is not required.

It exports 4 files from your example file ... 4 empty ones. :oops:

It simply failed casting the block in those files analog to lines 44-46 in ExAddBlock.js :shock:
https://github.com/qcad/qcad/blob/bc881 ... ddBlock.js
But that would only add a named empty block definition ... An empty container. :wink:

In the example script we see:
- creation of a block
- adding entities to the block
- adding a block reference to the drawing

And there is much more to it:
- nested blocks
- layers for block entities
- block attributes
Those can make things quite complicated.

I a way we could avoid all that with casting a block reference in the original and cut/paste that to another drawing.
Doing so within the GUI all the things related to the block are duplicated and moved.

I'll send you the so far working code that I mentioned above by PM.
Maybe I have another go at it. :wink:
It's a pitty that Duplicate Block is not an open source script ...

Regards,
CVH

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”