[Solved] How to Automatically Purge Unused Blocks and Layers

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
yamyam
Junior Member
Posts: 22
Joined: Fri Apr 19, 2024 2:31 am

[Solved] How to Automatically Purge Unused Blocks and Layers

Post by yamyam » Mon Sep 08, 2025 4:41 am

Hello everyone,

I am using QCAD Pro version 3.30.1.0 on Windows 10.

To reduce file size, I often use the "Purge Unused Layers" (YP) command from the Layer menu and "Purge Unused Blocks" (BP) from the Block menu.

I'm wondering if it's possible to automate this purging process through batch scripting. Are there any existing batch files—similar to dwginfo.bat and others—that can perform these operations automatically?

If not, could you please provide guidance on how to create such a batch file myself?

Thank you in advance for your help!
Last edited by yamyam on Tue Sep 09, 2025 1:36 am, edited 1 time in total.

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

Re: How to Automatically Purge Unused Blocks and Layers

Post by CVH » Mon Sep 08, 2025 6:02 am

Hi,

Included batch files typically mount QCAD (in the background) and execute a script file.

A tutorial to write your own custom OS command line tools can be found here: Command Line Tool Scripts

The part of interest is line 66-69 what simply adds a line in the example script.
Here one would include code to purge layers and blocks.

YP and BP or Misc .. Modify .. Purge unused Layers and Blocks are PRO features.
Not open source and there is little to no information to be found concerning PROprietary scripts.

Typically we include the required script(s) and call the function that performs the PRO method.
For many tools a bunch of parameters must already be set or passed along with the function call.
The sole parameter for purging that might be expected would be the RDocument or the RDocumentInterface.
The tools might also act on the current.

It could be as simple as defining an action and trigger that:

Code: Select all

    var action = RGuiAction.getByScriptFile("scripts/Pro/Misc/MiscModify/PurgeLayersAndBlocks/PurgeLayersAndBlocks.js");
    if (!isNull(action)) {
        action.slotTrigger();
    }
    else {
        print("Purge failed");
    }
:arrow: But that is a shot in the dark with no reference at all.
There would be no GUI to start with :!:


Regards,
CVH

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

Re: How to Automatically Purge Unused Blocks and Layers

Post by CVH » Mon Sep 08, 2025 7:09 am

After some digging ... A textual search on the term 'purge' ...
Including at the top:

Code: Select all

//include("scripts/simple.js");  // Simple API not required
include("scripts/Pro/Misc/MiscModify/PurgeLayersAndBlocks/PurgeLayersAndBlocks.js");  // For purging
include("scripts/Tools/arguments.js");    // To handle arguments of the Command Line tool
Custom code of interest:

Code: Select all

    // Purge all unused Layers and Blocks:
    PurgeLayersAndBlocks.purgeLayersAndBlocks(di);
Might do the trick :wink:
di is known because a file was imported a few lines higher.

There is no parameter p involved, remove line 12 of the example script in the tutorial.
The other arguments have their default function.

Regards,
CVH

yamyam
Junior Member
Posts: 22
Joined: Fri Apr 19, 2024 2:31 am

Re: How to Automatically Purge Unused Blocks and Layers

Post by yamyam » Mon Sep 08, 2025 9:53 am

Sorry, but I'm a complete newbie in this area. I'm trying to understand what you mean:
The YP and BP features are Pro features and cannot be operated through existing batch scripts. I need to write a batch script myself based on RDocument, RDocumentInterface, and Command Line Tool Scripts.
Is that correct?

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

Re: How to Automatically Purge Unused Blocks and Layers

Post by CVH » Mon Sep 08, 2025 10:33 am

Hi,

Included Command Line tools are typically Pro features.
Sure, they can access Pro-resources but there is no to little information to be found on Pro-resources.
These parts of the QCAD Professional code are proprietary, you use them under your Pro licence as they are.

Yes, you can write your own Command Line Tool Scripts.
A very basic script is included in the referenced tutorial.
The issue here is that there is almost no documentation on how to address Pro-resources.

Script files are textual, the code is human readable.
Function names are mostly self-explanatory.
Comments starting with // or from /* to */ make it even more readable. :wink:

Start a plain text editor of your choice, for example Notepad++
Copy/paste the raw content of the example code from here. (See top right)
'Raw' as with no line numbers.

'As is' it adds a line with: addLine(0,0, 100,100); but you are interested in purging.
Alter some of the code.
Save that as a script file "abcde.js" with a meaningful name, for example "MyPurge.js"
Anywhere you like, .../qcad/scripts/Tools/ExTool/ is the default path for the examples in tutorials.
Where the 3 dots stand for your installation path.

How to call that from the OS Command Line depends on your preferences/system.
Examples of Command Line instructions are given in the tutorial.

The instruction line also depends on the current location in the folder tree and that of referenced files.
You may need to expand the qcad(.exe) path matching your QCAD installation path and/or OS.
Include the full source and destination path if these are not located in your application path.

Regards,
CVH

yamyam
Junior Member
Posts: 22
Joined: Fri Apr 19, 2024 2:31 am

Re: How to Automatically Purge Unused Blocks and Layers

Post by yamyam » Tue Sep 09, 2025 1:35 am

Thank you very much for your detailed and helpful answer. I really appreciate you taking the time to explain the process and the considerations regarding Pro resources.

Your guidance makes it much clearer. I will follow your suggestions and start by experimenting with altering the basic example script.

Thanks again for your support.

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

Re: [Solved] How to Automatically Purge Unused Blocks and Layers

Post by CVH » Tue Sep 09, 2025 2:43 am

Hi,

I presumed that the code would do the trick, untested at this side.
Using Command Line tools is a small nightmare on my system as access may be blocked in several levels.

You added [Solved].
Does that means that the method did work out?

Regards,
CVH

yamyam
Junior Member
Posts: 22
Joined: Fri Apr 19, 2024 2:31 am

Re: [Solved] How to Automatically Purge Unused Blocks and Layers

Post by yamyam » Wed Sep 10, 2025 10:53 am

Hi CVH,

Yes, the command-line method worked perfectly. I was able to batch purge the files successfully using the command prompt.

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

Re: [Solved] How to Automatically Purge Unused Blocks and Layers

Post by CVH » Wed Sep 10, 2025 2:16 pm

Thanks for the feedback.

Never to old to learn a new trick. :wink:

Regards,
CVH

Post Reply

Return to “QCAD 'How Do I' Questions”