Replace block with same name
Moderators: andrew, Husky, J-J
Replace block with same name
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
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 974 times
-
- CreateBlock.js
- (3.7 KiB) Downloaded 967 times
-
- BlockDialog.js
- (4.47 KiB) Downloaded 972 times
Re: Replace block with same name
Hi marzof,
I'm sure I'm doing something wrong but it doesn't works as expected.
The correct location for this files are?
Block/
BlockDialog.js
CreateBlock/
CreateBlock.js, ReplaceBlock.js
I'm sure I'm doing something wrong but it doesn't works as expected.
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..."
Win10/64, QcadPro, QcadCam version: Current.
If a thread is considered as "solved" please change the title of the first post to "[solved] Title..."
Re: Replace block with same name
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?
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?
Re: Replace block with same name
Can you try to change the include frommarzof wrote:Maybe the new file is not recognized during the script initialization. Any advice?
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).
Re: Replace block with same name
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:
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:
- Attachments
-
- CreateBlock.js
- (3.67 KiB) Downloaded 963 times
Re: Replace block with same name
Hi marzof - now it works.
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:
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?
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:
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..."
Win10/64, QcadPro, QcadCam version: Current.
If a thread is considered as "solved" please change the title of the first post to "[solved] Title..."
Re: Replace block with same name
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
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
Re: Replace block with same name
Nice! Thanks for doing that!marzof wrote:I'll continue to work on it...
I'll update you as I can
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..."
Win10/64, QcadPro, QcadCam version: Current.
If a thread is considered as "solved" please change the title of the first post to "[solved] Title..."
Re: Replace block with same name
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
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 886 times
Re: Replace block with same name
Hi marzof - you are really busy!
I did just a brief testing but here are my first thoughts.
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?
I did just a brief testing but here are my first thoughts.
Yes, works nicely.marzof wrote:The automatic name generator seems ok now.
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.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.
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..."
Win10/64, QcadPro, QcadCam version: Current.
If a thread is considered as "solved" please change the title of the first post to "[solved] Title..."
Re: Replace block with same name
Not really.Husky wrote:Hi marzof - you are really busy!
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. In this case, please just leave my post unreplied (or don't rush to answer me): I'll fully comprehend.
Totally right.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?
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 877 times
-
- CreateBlock.js
- (4.2 KiB) Downloaded 919 times
Re: Replace block with same name
I tested the new scrips a little bit more and I think that is a great solution. I like it! Thank you very much.
It would be nice to have this permanently in QCAD. @Andrew: What do you think?
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..."
Win10/64, QcadPro, QcadCam version: Current.
If a thread is considered as "solved" please change the title of the first post to "[solved] Title..."
Re: Replace block with same name
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.
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.
Re: Replace block with same name
Yep, works still as expected.andrew wrote:I'd appreciate if you guys could test if this still behaves as expected.
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..."
Win10/64, QcadPro, QcadCam version: Current.
If a thread is considered as "solved" please change the title of the first post to "[solved] Title..."
Re: Replace block with same name
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.
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.