Script to read block name, copy name and enter as text

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.

Rolinger
Junior Member
Posts: 18
Joined: Thu Aug 29, 2024 5:17 pm

Script to read block name, copy name and enter as text

Post by Rolinger » Wed Mar 05, 2025 12:19 pm

Hello QCAD users,

I need a script that will read the block name for the selected block and copy it to the clipboard (Referenced block name).
In the second step, this block name should be entered as simple text at the desired location in the drawing.
I have over 250 blocks whose names I need to write on the drawing.

Since I'm not a programmer, I tried to use artificial intelligence, but unfortunately, unsuccessfully :?

Anybody can help me with that?
Thanks in advance
_
Rolinger

Versions
QCAD Professional version: 3.32.2.0, Linux Mint
Last edited by Rolinger on Wed Mar 05, 2025 7:53 pm, edited 1 time in total.

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

Re: Script to read blocks name, copy name and enter as text

Post by CVH » Wed Mar 05, 2025 5:10 pm

Hi,

Blocks are listed in the Block List and have no specific place in your model.
A Block is a collection of entities. Model_Space is itself a Block.

We tend to use the term 'Block' for two rather different things: A Block definition and/or a Block Reference.

Block References are drawing entities and have a certain position in the model.
They display a copy of the referenced Block content at the insertion point.

Thus ... 250 Block References or 250 Blocks in the Block List ?


BlockRefExport.js in the Misc section already exports the occurrence of Block References to CSV.
As: 'Referenced Block name', X, Y
Menu Misc .. Block .. Export Block References List...

That is already a good overview of all Block Reference entities in your drawing.


Lets try to use QCAD methods to accomplish this.
Selection filter: Type: 'Block Reference' ; Property: 'Referenced Block' ; Comparison: 'is' ; Value: Enter Block name
Left action button: 'Replace the current selection'

This are Block References and these only have an insertion point beside some other properties.

Misc .. Information .. Store Positions (TT).
Draw .. Text (TE) and replace former text by the Block name .. OK
Misc .. Information .. Use Positions (TY).

The text will be created at every Block Reference insertion point that was selected.
But this is not really usable for 250 Block definitions in the Block List by name.


Yet another possible way ... Include the Block name as text in the Block definition.
By this you could ensure that the text is displayed at a well readable place for each Block.
Otherwise the 'desired location' is always related to the insertion point, no matter what the Block content is.


Otherwise define 'desired location' and provide a test file, explain what is required within that example file.
Define 'selected': Selected in Block List or selected Block Reference entities.
Give some examples, for example on a dedicated layer that can be hidden.

Regards,
CVH

Rolinger
Junior Member
Posts: 18
Joined: Thu Aug 29, 2024 5:17 pm

Re: Script to read blocks name, copy name and enter as text

Post by Rolinger » Wed Mar 05, 2025 7:44 pm

First of all, thank you for your answer CVH.

I'll try to describe what I want in the following image:
1_expl.jpg
1_expl.jpg (175.29 KiB) Viewed 51521 times
For me, success would be if I could select a block reference in the drawing and run a script with a shortcut that will copy the referenced block name. That would be more than 50% of the job. Now I wonder if that's even possible?
Thus ... 250 Block References or 250 Blocks in the Block List ?
I guess 3*250 Block References for 250 Blocks in the Block List. I have nothing but praise for QCAD's performance. I use this drawing with 250 blocks and a bunch of graphical elements on a 10-year-old laptop with 8GB of RAM and an i7 processor. I keep the entire drawing "clean" without unnecessary and redundant hatches for now. I use Edit block in place (BB) to edit block references, and I am extremely satisfied. The time "lag" needed to refresh the drawing is 2-3 seconds!

Best regards, Rolinger

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

Re: Script to read block name, copy name and enter as text

Post by CVH » Thu Mar 06, 2025 5:23 am

Rolinger wrote:
Wed Mar 05, 2025 7:44 pm
Now I wonder if that's even possible?
With scripting almost anything is possible, your imagination and scripting skills are the limit.
Remark that most QCAD tools are scripts.

Not as you described in the 1-4 sequence, we are not going to copy the block name to the OS clipboard.
The QCAD clipboard is intended for copying geometry and the Specific Property is not yet a Text entity.
Rolinger wrote:
Wed Mar 05, 2025 7:44 pm
and run a script with a shortcut
You can run a custom script with XC and indicating a script file from a file list.
'With a shortcut' ... Then it must be conceived as an Addon tool ... Remark that there are not many shortcut combinations left over.
Also, as Addon it has an action button ... For example listed under Misc .. Block
Already happy that your QCAD GUI is in English.

For a single Block Reference that could be conceived as:
- Select 1 Block Reference entity
- Initiate custom Addon tool
- Place text entity (With preview).

But that would still require 3x250 independent actions.

Automated can be as:
- Selection filtered for Block Reference entities
- Initiate custom Addon tool
- Place a text entity for each.

Or by selecting the Block name in the Block List and then for all Block Reference entities that refer to that Block.
Or for all Block Reference entities that exists in the drawing at once.

As said, almost anything is possible but you need to decide on a plan of attack.

For 'automated' you need to specify the relative placement of the Text in regards with the Block_Ref. insertion point.
Otherwise it is simply put at the Block_Ref. insertion point ... (The blue squared marker in your screen capture)
Automated there is no user interaction for arranging that individually.
But even that is possible ... Again 3x250 manual interactions for a single run ... You will get bored by indicating all these.
(Similar to 'Reorder' MO until they are all green)

Also: How this text is aligned, 3x4 options, from left high to right low.
Angled? Consider Block_Ref. angle?
Considering Block content? In the Middle? Or right low?
What font, font size and color?
A dedicated layer or not?
Selected in Block List ... Only for Model_space or process stacked Blocks?
...
..

In other words ... Before we can code something, we need to know the requirements.

Regards,
CVH

Rolinger
Junior Member
Posts: 18
Joined: Thu Aug 29, 2024 5:17 pm

Re: Script to read block name, copy name and enter as text

Post by Rolinger » Thu Mar 06, 2025 10:50 am

To make a plan of attack, I first need to describe the way the drawing is created and what I want to achieve.
EXMPL.dwg
(55.47 KiB) Downloaded 695 times

So let me explain:
Each block represents a part consisting of a steel profile, steel plates, holes, bolts, etc...
Each part can be represented in a 2D drawing in at least one or more views - plan, side plan, section, etc...
Each part (each block) has a unique name with a suffix added that indicates the view used (plan, section,...). The first part of the name is unique for each part. The second part of the name in the suffix is ​​somehow standardized and describes the view used (plan, section, side plan...).

Each block has its own position in the GAD drawing.
What do I want to achieve with the script?
I want to display the name of each part. Nothing more and nothing less than this. For this, I planned to create one layer, e.g. Part_name_layer.
The text font, text size and alignment can remain current, i.e. as they were last used when entering the text.
Once we have the block names in a common layer, it is easy to manipulate the suffix used in the name - change it, keep it or delete it.
I can also change fonts, text size etc...

The next step is to use the excellent QCAD script T2L, which I will use to link the block name to the desired block reference. (https://qcad.io/rsforum/viewtopic.php?t=8949)

I don't have any text entries in the draft yet.
Considering Block content? In the Middle? Or right low?
What font, font size and color?
A dedicated layer or not?
Your suggestion of automatically entering block names at the Block_Ref. insertion point or even better considering the content of the block in the middle might not be so bad (I didn't know this was even possible. :wink: )

Each block will be used further to create a detailed workshop draft. It will show dimensions, details, descriptions, etc...
I haven't figured out the fastest way to do this yet - manually copying each block into individual new drawings or something else....I need to think about the best way to do this with QCAD. I'm still a beginner in the QCAD world though :)
In any case, thank you VERY much for your time and advice!

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

Re: Script to read block name, copy name and enter as text

Post by CVH » Fri Mar 07, 2025 9:15 am

... Thinking out loud:

Automated was discussed because of the vast amount (~3x 250).
Automated, the text position is kinda a fixed offset in regards with some reference within the Block Reference.
Considering the displayed Block content or not.
(Please diversify between a 'Block' OR a 'Block Reference' in your posts)

Fully automated for all Block References in the current active Block (e.g. Model_Space) creates duplicates on a second run.

Automated for all selected Block References may create duplicates depending the selection.
At least there is the option to not process all Block References.

If the idea is to process each included Block name Text entity afterwards with T2L ... Again ~3x 250 items ...
Would it not be more appropriate to combine all that as a tool that includes a Text entity and a Leader:
- 1) Indicate a Block Reference
- Presented preview is the Text floating in association with a default Leader.
- 2) Indicate Text position
- Presented preview is the Text no longer floating with the arrow tip floating
- 3) Indicate Leader arrow tip position
- Over new until the tool is terminated.
(A WIpeOut under the text would be nice :) but this entity type is undocumented :( )
As interactive we can step one level back from state 2 or 3.

Such a tool would be usable along in the design process (only) where required and has perhaps some purpose for more users.
True, for existent drawings with many Block References it would take some effort to include them all.
At least only for the intended Block References at a proper place with a proper Leader.
If there are a few missing or when Block References are added, the Block name Text + Leader can be added in just the same way.

Regards,
CVH

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

Re: Script to read block name, copy name and enter as text

Post by CVH » Fri Mar 07, 2025 5:14 pm

Rolinger wrote:
Thu Mar 06, 2025 10:50 am
To make a plan of attack
Please see later today for an initial attempt. :wink:
Send to you as Private Message.
See forum top right for that.

Regards,
CVH

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

Re: Script to read block name, copy name and enter as text

Post by CVH » Sat Mar 08, 2025 9:55 am

Rolinger wrote:
Thu Mar 06, 2025 10:50 am
Your suggestion of automatically entering block names at the Block_Ref. insertion point or even better considering the content of the block in the middle might not be so bad (I didn't know this was even possible. :wink: )
Piggybacked that on the current attempt.
It runs once in that mode if you force a hidden preference.
Just to prevent it from running multiple times by accident or unintended.

But it is a bit messy because the text and the leader are at a fixed offset to the Block Reference insertion point.
Most entities in QCAD are unrelated, there is also no simple AI method for a proper arrangement without masking others.

It creates those in an instant for every (selected) Block Reference.
Just the same for 1500 Block References mentioned per PM ... Adding 3000 entities ... :wink:

Nudge me to receive a preliminary copy. :wink:


I said 'unrelated' but that is partially not fully true.
Why should I not use things that are proven to be beneficial?
New entities receive a 'Selection Group' tag and you can select things together with: Entities Selection Groups
Activated the selection listener, move them as one (MV, by dragging or use the arrow keys), drag the arrow tip by its marker.

Regards,
CVH

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

Re: Script to read block name, copy name and enter as text

Post by CVH » Sun Mar 09, 2025 11:51 am

Now including a Wipeout with the same ease ... :wink:

See Q&A about the recently added RWipeoutEntity

Regards,
CVH

333onlyhalfevil
Full Member
Posts: 92
Joined: Fri Apr 28, 2023 12:39 pm

Re: Script to read block name, copy name and enter as text

Post by 333onlyhalfevil » Mon Mar 10, 2025 6:20 pm

Another option to consider would be to just add the text to each individual block instead of writing a custom script for it. If the purpose of this script is to add the text when you want to instead of to everything, you could always make two separate blocks (one with the text and one without the text) and use the appropriate one for the appropriate situation.

For a script like this, I could see a future issue being the positioning/rotation of the text. A simple version of this script would end up adding the text to the same point for every single separate block. After the script is made, you'll probably end up wanting it to insert the text at a specific place that is based on the geometry of each individual block. This will require creating and maintaining a database within your script of all possible blocks and the coordinates of the insertion point and rotation for each individual text entity.

Just a thought.

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

Re: Script to read block name, copy name and enter as text

Post by CVH » Tue Mar 11, 2025 7:19 am

333onlyhalfevil wrote:
Mon Mar 10, 2025 6:20 pm
Another option to consider would be to just add the text to each individual block instead of writing a custom script for it
I my initial reply:
CVH wrote:
Wed Mar 05, 2025 5:10 pm
Yet another possible way ... Include the Block name as text in the Block definition.
The issue is probably that this are not individual entities or 1 text block per Block Reference.
The next step was to add Leaders with T2L.
An additional Wipeout would be nice too.

Another issue is that we can't define a text to always have a 'readable' angle.
The Block content can be displayed at any angle.

333onlyhalfevil wrote:
Mon Mar 10, 2025 6:20 pm
For a script like this, I could see a future issue being the positioning/rotation of the text.
...
This will require creating and maintaining a database
Not really, most entities are fully unrelated under QCAD.
There is thus no intention to create the Block name text near a Block Reference and make it parametric.

I don't need a database ...
On creation near a Block Reference I can query the Block (definition) content at that instant.
Content is then translated, rotated and scaled according the Block Reference properties.
Of this we can take the bounding box center, a corner or whatever.
All done in the background.

Afterwards the added entities are again fully unrelated to other drawing content.
What can be done is ensuring that the Text, the Leader and the Wipeout can be selected together with Entities Selection Groups

333onlyhalfevil wrote:
Mon Mar 10, 2025 6:20 pm
A simple version of this script would end up adding the text to the same point for every single separate block.
First: Please diversify between a Block (definition) and a Block Reference.
A Block Reference is itself an entity that lives on a layer and has an insertion point in the drawing.
A Block is a collection of entities ... A Block is not a part of your drawing and has no position in for example the Model_Space Block.
Blocks are only listed in the Block List.

It is thus practically impossible to define a Block position reference in the drawing.
But each Block Reference is basically nothing more than a positional reference.

Your assumption is true when two Block References are inserted at the same position.
I don't see duplicate Referenced Block names at the same position as an error of the tool.
Duplicates of Referenced Blocks at the same position is by design and at user discretion.


BTW: The script is already fully functional for:
- Each indicated Block Reference in a 3 step process.
- Automated for a selection.
- Automated for all Block References in a drawing (Model_Space).
(Automated only runs once it you force it to because this can lead to duplicates)

Regards,
CVH

333onlyhalfevil
Full Member
Posts: 92
Joined: Fri Apr 28, 2023 12:39 pm

Re: Script to read block name, copy name and enter as text

Post by 333onlyhalfevil » Wed Mar 12, 2025 6:08 am

CVH wrote:
Wed Mar 05, 2025 5:10 pm
I my initial reply:
Fair enough. I forgot by the time I got through reading it all.
CVH wrote:
Wed Mar 05, 2025 5:10 pm
Not really, most entities are fully unrelated under QCAD.
There is thus no intention to create the Block name text near a Block Reference and make it parametric.

I don't need a database ...
On creation near a Block Reference I can query the Block (definition) content at that instant.
Content is then translated, rotated and scaled according the Block Reference properties.
Of this we can take the bounding box center, a corner or whatever.
This is an interesting idea that could help. I didn't originally think of doing your text positioning/rotation/scaling based on the content of the block. However, whether you need to maintain a database or not would depend on the blocks (definitions) and what you're trying to do. He said there were ~250 different blocks (definitions). I'll admit I don't know anything about the 250 blocks so maybe not maintaining a database would work, but it is also possible that the blocks have different requirements for where the text should be positioned/rotated/scaled. A script without a maintained database would place the text in the same exact way for all 250 blocks (definitions) which may or may not be sufficient. It would probably be ok to not worry about maintaining a database based on the example file since everything is relatively square, but what if some of those blocks have goofy shapes? For example, a script that places the text based on a square bounding box might put the text too far away if the bounding box has some emptiness on the side where the text gets placed. That's all I was getting at.
CVH wrote:
Wed Mar 05, 2025 5:10 pm
Please diversify between a Block (definition) and a Block Reference.
By "Block", I was referring to a block definition.
CVH wrote:
Wed Mar 05, 2025 5:10 pm
Your assumption is true when two Block References are inserted at the same position.
I don't see duplicate Referenced Block names at the same position as an error of the tool.
Duplicates of Referenced Blocks at the same position is by design and at user discretion.

Your assumption is true when two Block References are inserted at the same position.
I don't see duplicate Referenced Block names at the same position as an error of the tool.
Duplicates of Referenced Blocks at the same position is by design and at user discretion.
What I was getting at was that each block definition has its own origin point and its own gross geometry of the entities that make up the block definition. So when you're inserting the text, the script would insert the text in the same position relative to the block reference entity for all blocks. So, say you have a block definition that the entities within it have a bounding box that is 10x10 and the origin is at the center of the 10x10 bounding box, you might have your script place the text at 5,6 plus the insertion point of the block reference entity. But then if you have a different block that has a bounding box that is 20x20, your 5,6 plus the insertion point would place the text overlapping the block reference entity rather than above it like with the previous example. If you then improved your script to look at the bounding box of the block definition and insert the text at a position that has a y value that is 1 more than the largest y value within the block definition, you might run into a problem if the entities within the block definition are arranged in an L shape since the text would be positioned near the empty section of the bounding box.
CVH wrote:
Wed Mar 05, 2025 5:10 pm
BTW: The script is already fully functional for:
- Each indicated Block Reference in a 3 step process.
- Automated for a selection.
- Automated for all Block References in a drawing (Model_Space).
(Automated only runs once it you force it to because this can lead to duplicates)
This reminds me that I was also going to request to leave this stuff public. I'd be interested to read through the code, even if it is something that I won't ever use. Insights can be gained from looking at the code of other developers.

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

Re: Script to read block name, copy name and enter as text

Post by CVH » Wed Mar 12, 2025 6:59 am

CVH wrote:
Fri Mar 07, 2025 9:15 am
... Thinking out loud:

Automated was discussed because of the vast amount (~3x 250).
Automated, the text position is kinda a fixed offset in regards with some reference within the Block Reference.
Considering the displayed Block content or not.

Fully automated for all Block References in the current active Block (e.g. Model_Space) creates duplicates on a second run.

Automated for all selected Block References may create duplicates depending the selection.
At least there is the option to not process all Block References.

If the idea is to process each included Block name Text entity afterwards with T2L ... Again ~3x 250 items ...
Would it not be more appropriate to combine all that as a tool that includes a Text entity and a Leader:
Meanwhile
CVH wrote:
Sat Mar 08, 2025 9:55 am
Just the same for 1500 Block References mentioned per PM ... Adding 3000 entities ...
Meanwhile
CVH wrote:
Sun Mar 09, 2025 11:51 am
Now including a Wipeout with the same ease ...
But there are issues when saved as DWG (FS#2651)

No automatic created database would fix the 'void' issue.
There are plenty of online examples of code to add labels in so said proper places ...
... Typically exploiting a convex hull ... But all will fail in some specific situations.

Automated may get messy but it adds a label to all (selected) Block References in one action.
It is piggy backed on the 3 step interactive script.
Simply to address the vast amount of Block References.

Proper placement can then be done individually or as group if required.
(There was already the intention to process each label with T2L)

Depending on the follow up I might release it in public.
But the follow up has more to do with XREF capabilities ... :roll:

Regards,
CVH

333onlyhalfevil
Full Member
Posts: 92
Joined: Fri Apr 28, 2023 12:39 pm

Re: Script to read block name, copy name and enter as text

Post by 333onlyhalfevil » Wed Mar 12, 2025 5:48 pm

CVH wrote:
Wed Mar 12, 2025 6:59 am
No automatic created database would fix the 'void' issue.
There are plenty of online examples of code to add labels in so said proper places ...
... Typically exploiting a convex hull ... But all will fail in some specific situations.
Automated may get messy but it adds a label to all (selected) Block References in one action.
I never said anything about creating the database automatically. I agree it would be messy which is why you'd need to create/maintain a database to fix the mess. Well, maybe a smaller one that only contains the block definition names of the specific situations which fail. Something simple like this:

Code: Select all

var blockNames = [];
blockNames.push("exampleBlock1");
blockNames.push("exampleBlock2");

var blockPositions = [];
blockPositions.push(new RVector(5, 6));
blockPositions.push(new RVector(2, 15));
Then, just include a simple check to see if the block name is in the blockNames database and, if it is, use the positioning information from the blockPositions database, and if not, run the main add labels in proper places script. Also note that when I say "database" I'm not referring to a big official database, just something small and simple to store the information within the larger labeling script.
CVH wrote:
Wed Mar 12, 2025 6:59 am
Depending on the follow up I might release it in public.
But the follow up has more to do with XREF capabilities ... :roll:
Did you have this script made before the OP started the thread? If so, then nevermind my request to make it public. I read it like it was taken off the public portions of the forum and was worked on in private after the fact.

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

Re: Script to read block name, copy name and enter as text

Post by CVH » Thu Mar 13, 2025 5:12 pm

333onlyhalfevil wrote:
Wed Mar 12, 2025 5:48 pm
Then, just include a simple check to see if the block name is in the blockNames database ...
And how long thus this 'database' lives?
Variables have block scope.
The lists are created out of the blue or are they hard coded?
Where are they stored within the document for later reference?

'Blocks in the sense of Block definitions' ...
... What if I use a Block Reference at a scale, at an angle?
Is that unique Block definitions label position still valid when turned upside down? When mirrored?

As said, automated is kept simple, one Block label (set) for each (selected) Block Reference.
Simply pointing to the insertion point of the Block Reference but that can be anything.
Simply at a fixed offset to the insertion point or other reference point.

The 3 step interactive mode lets you add the referenced Block name where you want it with the Leader pointing where you set it.
That is: 'Script to read block name, copy name and enter as text' + T2L in one action.


Regards,
CVH

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”