Replace block with same name

This forum is for 'Work-in-Progress' QCAD user developed script Add Ons and Plug-ins.

Please use this forum to request/submit your script Add-Ons & Plug-in idea's.

Moderators: andrew, Husky, J-J

marzof
Junior Member
Posts: 23
Joined: Tue Mar 18, 2014 5:47 pm

Replace block with same name

Post by marzof » Wed Sep 30, 2015 11:54 am

Hi,

I'm working on a function that allow to replace an existing block and its instances when a user creates a new block with the same name.
In order to do this, I made a script that, when users choose an existing block name, follows this steps:

1. creates a new temporary block marked by a prefix
2. redirects all the instances of the block to substitute to this new temporary block
3. deletes the old block
4. renames the temporary block with the same name of the deleted block

I don't know if this is the more convenient way to achieve the goal: maybe it would be easier to take advantage of the "overwrite block" option which is in Paste command or in Insert from Library command. What do you think about?
I attach the modified CreateBlock.js and BlockDialog.js (difference are marked in comments) and the new script ReplaceBlock.js which has to be in CreateBlock folder.

Obviously feel free to use, modify, redistribute or ignore these files.
Bye
Attachments
ReplaceBlock.js
It has to be placed in scripts/Block/CreateBlock folder
(2.18 KiB) Downloaded 966 times
CreateBlock.js
(3.7 KiB) Downloaded 955 times
BlockDialog.js
(4.47 KiB) Downloaded 960 times

User avatar
Husky
Moderator/Drawing Help/Testing
Posts: 4931
Joined: Wed May 11, 2011 9:25 am
Location: USA

Re: Replace block with same name

Post by Husky » Wed Sep 30, 2015 7:07 pm

Hi marzof,

I'm sure I'm doing something wrong but it doesn't works as expected. :oops:

The correct location for this files are?

Block/
BlockDialog.js

CreateBlock/
CreateBlock.js, ReplaceBlock.js
Work smart, not hard: QCad Pro
Win10/64, QcadPro, QcadCam version: Current.
If a thread is considered as "solved" please change the title of the first post to "[solved] Title..."

marzof
Junior Member
Posts: 23
Joined: Tue Mar 18, 2014 5:47 pm

Re: Replace block with same name

Post by marzof » Wed Sep 30, 2015 8:02 pm

Hi Husky,
no, you are totally right.
Those are the correct files locations.

It's weird: I don't know why but sometimes it seems that ReplaceBlock.js is not read.
In that case I open CreateBlock.js, make some irrelevant change, save it again and restart QCAD... and it works! And it keeps working for a while even if I close and restart the application.

Maybe the new file is not recognized during the script initialization. Any advice?

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

Re: Replace block with same name

Post by andrew » Wed Sep 30, 2015 9:34 pm

marzof wrote:Maybe the new file is not recognized during the script initialization. Any advice?
Can you try to change the include from
include("ReplaceBlock.js");
to
include("scripts/Block/CreateBlock/ReplaceBlock.js");
Also the include of InsertBlockItem.js:
include("scripts/Block/InsertBlockItem/InsertBlockItem.js");
I didn't have a chance to try this yet, but I can imagine that the local context of those includes might cause problems with relative paths (once the .js file has been evaluated, the context in which it has been evaluated is lost, so when coordinateEvent is called, QCAD does not know where to look up ReplaceBlock.js).

marzof
Junior Member
Posts: 23
Joined: Tue Mar 18, 2014 5:47 pm

Re: Replace block with same name

Post by marzof » Thu Oct 01, 2015 8:15 am

Hi,

I changed the path as you suggest (just for ReplaceBlock.js... InsertBlockItems.js it's not used here: it was a previous attempt and I forgot to delete it from the code) and it seems working.

I attach the file CreateBlock.js (the others are the same).
Here is an animation to show how it works in my computer:
Image
Attachments
CreateBlock.js
(3.67 KiB) Downloaded 954 times

User avatar
Husky
Moderator/Drawing Help/Testing
Posts: 4931
Joined: Wed May 11, 2011 9:25 am
Location: USA

Re: Replace block with same name

Post by Husky » Thu Oct 01, 2015 9:36 am

Hi marzof - now it works. :D

That is a very nice tool idea - I was hoping to get someday a block feature like this one in QCAD.


However - after testing a bit - here are my thoughts: :shock:

1. Creating a new block - works as expected,
2. Creating a new block which has to replace an already existing block - flawless,

3. QCAD has an automatic name generator for a new created block. It starts with "block 1" and counts automatically further to "block 2", "block 3" etc etc. Very useful if you need to create a couple blocks without a "deeper meaning".
Your tool will stick to the first generated name (block 1) and if I already used this name I get always the warning "Block already exists". Now I have always to create a new name ...
Would be nice to have the name generator back in order.

4. If I rename an existing Block directly in the block list (BN) your script will warn me about an identical block name - that's nice!
But if I change the name regardless the warning it will produce a invalid block name like "??block 1". Could that be solved?
Work smart, not hard: QCad Pro
Win10/64, QcadPro, QcadCam version: Current.
If a thread is considered as "solved" please change the title of the first post to "[solved] Title..."

marzof
Junior Member
Posts: 23
Joined: Tue Mar 18, 2014 5:47 pm

Re: Replace block with same name

Post by marzof » Thu Oct 01, 2015 10:23 am

I'm glad you like it.

The things you point out have to be fixed, no doubt.

I had noticed the name generator and I think I can fix it.
Instead, I didn't think about replacing through the block list: I'll try to understand how to solve it.
(Before getting deep in coding I wanted to know if it was of some interest)

There is another thing it could be better but, because of my lack of expertise, I wasn't be able to fix: in fixNames function, inside ReplaceBlock.js, I needed to do a useless action (hide all blocks and undo it) to refresh the block list. I think it's not the best way to do it but I don't know how to get it differently (furthermore this adds an unintended operation in the undo list).

I'll continue to work on it...
I'll update you as I can

User avatar
Husky
Moderator/Drawing Help/Testing
Posts: 4931
Joined: Wed May 11, 2011 9:25 am
Location: USA

Re: Replace block with same name

Post by Husky » Thu Oct 01, 2015 7:00 pm

marzof wrote:I'll continue to work on it...
I'll update you as I can
Nice! Thanks for doing that!
Work smart, not hard: QCad Pro
Win10/64, QcadPro, QcadCam version: Current.
If a thread is considered as "solved" please change the title of the first post to "[solved] Title..."

marzof
Junior Member
Posts: 23
Joined: Tue Mar 18, 2014 5:47 pm

Re: Replace block with same name

Post by marzof » Tue Oct 06, 2015 8:07 am

Hi,
I updated the scripts to fix what Husky pointed out.

The automatic name generator seems ok now.

Also blocks renaming has been corrected: now, if you give an existing name to a block, all its instances are redirected to the existing one and the renamed block is removed.
To tell the truth I don't know if this renaming function could confuse some user: which of the two blocks has to persist after renaming? Is it more right to disable same name renaming (like I did for AddBlock.js)?
Please, tell me what you think about so I can correct it accordingly.

In CreateBlock.js, I couldn't resolve the issue of list block refreshing yet: it seems to me that is not possible to rename a block with an existing name during the same operation, even after removing the original block. So I needed to:
- 1. applying the operation,
- 2. rename the block
- 3. do/undo something (like hiding all blocks) to refresh the block list.
I don't think it's an elegant way to achieve the goal. What do you suggest?

The updated files are attached. ReplaceBlock.js is no more needed because its functions are now integrated in the existing scripts.
Bye
Attachments
sameNameBlock.zip
(5.97 KiB) Downloaded 876 times

User avatar
Husky
Moderator/Drawing Help/Testing
Posts: 4931
Joined: Wed May 11, 2011 9:25 am
Location: USA

Re: Replace block with same name

Post by Husky » Wed Oct 07, 2015 8:16 am

Hi marzof - you are really busy! :wink:

I did just a brief testing but here are my first thoughts.
marzof wrote:The automatic name generator seems ok now.
Yes, works nicely.
marzof wrote:To tell the truth I don't know if this renaming function could confuse some user: which of the two blocks has to persist after renaming? Is it more right to disable same name renaming (like I did for AddBlock.js)?
Please, tell me what you think about so I can correct it accordingly.
First of all - it doesn't works on my system. It doesn't produce anymore invalid Names but that is all what is noticeable for me.
However, I agree that it will confuse user and I think now the "Rename Block" function in the "Block List" should stay as it is. What means: Renaming a block into an already used Name in the Block List should warn and stop the user with the message "Block already exits". On this way you are forced to use a different name.

The possibility to create a new block in the drawing which can be used to overwrite an existing block is all what we need. No need to make it more complicated than necessary.
I'm right?
Work smart, not hard: QCad Pro
Win10/64, QcadPro, QcadCam version: Current.
If a thread is considered as "solved" please change the title of the first post to "[solved] Title..."

marzof
Junior Member
Posts: 23
Joined: Tue Mar 18, 2014 5:47 pm

Re: Replace block with same name

Post by marzof » Wed Oct 07, 2015 10:41 am

Husky wrote:Hi marzof - you are really busy! :wink:
Not really. :)
I like doing it and it's a very useful exercise for me: in doing so, I'm improving my programming skills and, at the same time, trying to enhance an important instrument for my job.
I hope that my work doesn't bother you. :oops: In this case, please just leave my post unreplied (or don't rush to answer me): I'll fully comprehend.
Husky wrote:The possibility to create a new block in the drawing which can be used to overwrite an existing block is all what we need. No need to make it more complicated than necessary.
I'm right?
Totally right.
I removed all the changes from RenameBlock.js and AddBlock.js and reset them to the original state.
Now you can overwrite a block just creating a new one with the same name.
The correct files are attached.
Bye
Attachments
BlockDialog.js
(5.17 KiB) Downloaded 867 times
CreateBlock.js
(4.2 KiB) Downloaded 910 times

User avatar
Husky
Moderator/Drawing Help/Testing
Posts: 4931
Joined: Wed May 11, 2011 9:25 am
Location: USA

Re: Replace block with same name

Post by Husky » Fri Oct 09, 2015 9:06 am

I tested the new scrips a little bit more and I think that is a great solution. I like it! Thank you very much. :D

It would be nice to have this permanently in QCAD. @Andrew: What do you think?
Work smart, not hard: QCad Pro
Win10/64, QcadPro, QcadCam version: Current.
If a thread is considered as "solved" please change the title of the first post to "[solved] Title..."

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

Re: Replace block with same name

Post by andrew » Tue Oct 13, 2015 11:16 am

I've applied these changes (somewhat refactored) to the current master branch:
https://github.com/qcad/qcad/blob/maste ... kDialog.js
https://github.com/qcad/qcad/blob/maste ... teBlock.js

I'd appreciate if you guys could test if this still behaves as expected. I made some changes to avoid the 'redirect' function and access to function callers. Thanks.

User avatar
Husky
Moderator/Drawing Help/Testing
Posts: 4931
Joined: Wed May 11, 2011 9:25 am
Location: USA

Re: Replace block with same name

Post by Husky » Tue Oct 13, 2015 9:10 pm

andrew wrote:I'd appreciate if you guys could test if this still behaves as expected.
Yep, works still as expected. :D
Work smart, not hard: QCad Pro
Win10/64, QcadPro, QcadCam version: Current.
If a thread is considered as "solved" please change the title of the first post to "[solved] Title..."

marzof
Junior Member
Posts: 23
Joined: Tue Mar 18, 2014 5:47 pm

Re: Replace block with same name

Post by marzof » Wed Oct 14, 2015 12:58 pm

Hi Andrew,

now the code is much cleaner! Thanks.

I did some test and I noticed some strange behavior:

1. When I create a new empty block and name it like some other existing block the OK button in the dialog allows overwriting. But if I click it the dialog closes and nothing happens. I think that creating a new empty block with the same name of another should be prevented.

2. If I create a new block and name it like a block contained in it, the new instance appears as an array of 17 copies. If I check the new block in the block editor it seems correct but, when I come back in the drawing all its instances are now disappearing if I zoom in. Very strange!
An animation about it is attached.

I hadn't thought about this case when I had written the code so I tested it now and it seems to work except that the replaced block is missing in the new block: I think it could be solved exploding it before populating the new block in order to be included in it.

Image

Post Reply

Return to “QCAD 'Script Add-On & Plug-in challenge' - Work in Progress”