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

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 Oct 14, 2015 1:00 pm

OK, thanks!

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 Oct 14, 2015 1:16 pm

marzof wrote: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.
Thanks. I've fixed this in:
https://github.com/qcad/qcad/blob/maste ... kDialog.js
https://github.com/qcad/qcad/blob/maste ... ddBlock.js
marzof wrote: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!
This is expected behaviour for recursive block references (i.e. block A contains directly or indirectly a reference to block A). After 16 iterations, QCAD stops to prevent endless recursion and ultimately a crash.

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

Re: Replace block with same name

Post by marzof » Fri Oct 16, 2015 11:26 am

andrew wrote:This is expected behaviour for recursive block references (i.e. block A contains directly or indirectly a reference to block A). After 16 iterations, QCAD stops to prevent endless recursion and ultimately a crash.
I see.
Anyway I tried to make some little change to your code (here attached) to allow users to redefine block by adding something to one of its references and renaming it likewise.
I think it could be a comfortable way to redefine existing blocks in the drawing context. It also gives the opportunity just to reset block's origin relative to other elements in the drawing.
Don't you think it could be useful?
Attachments
CreateBlock.js
(4.34 KiB) Downloaded 845 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 » Sat Nov 14, 2015 12:13 am

Hi guys,

I really enjoy this new feature of overwriting an existing block - but one thing isn't nice working. I can't use a block in my drawing to overwrite the same block in my blocklist - if I do so it will delete the selection in the drawing and will also delete the content of the block. With other words - everything is gone without a warning.

If I work with blocks and assemble something and then see that a block needs to be scaled, rotated etc. by default or just like to use this block to overwrite the reference point of this block in the block list I have first to explode the block in my drawing - otherwise I delete everything in that moment when I click OK for overwriting the block. And I will notice that only accidental because there is no warning.

Would it be possible to change the code that a block which is used to overwrite the same block is first exploded by script?

Testdrawing: Try to save Block 1 (without exploding) again under the name "block 1".
test block overwrites same block.dxf
(103.67 KiB) Downloaded 828 times
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 » Sat Nov 14, 2015 11:10 am

Not sure I can follow.

If you have a block reference of block "A" in your drawing, you cannot use that block reference to overwrite the definition of block "A" since the block reference and the block definition are already identical (by definition).

If you create a new block "A" which contains a block reference to block "A" (itself), that will indeed lead to unpredictable behavior due to a direct block recursion (block "A" now contains block "A" which contains block "A", ...).

In short, this is expected behavior, QCAD does what you tell it to do: it creates a block "A" with a block reference to block "A" in it. Whether this make any sense or not is not really for QCAD to decide :wink:

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

Re: Replace block with same name

Post by marzof » Sat Nov 14, 2015 1:00 pm

Hi Husky,

I tried to test your file using my last edit of CreateBlock.js (posted here on October 16) and I didn't notice any problems: the block is updated and renamed properly without the need to explode it first.
Here is what I see:
Image

I think this is an easy way to update blocks in the drawing context (for example if you want to change the block reference point referring to other entities).

As Andrew says it could appear ambiguous to users: make a block containing itself logically cause a recursion.
But I think also that, in real cases, an action like this implies the intent to redefine the block itself.
What do you think about?

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 » Sat Nov 14, 2015 8:30 pm

Thanks guys for you help.
andrew wrote:In short, this is expected behavior, QCAD does what you tell it to do: it creates a block "A" with a block reference to block "A" in it. Whether this make any sense or not is not really for QCAD to decide :wink:
Ok, then it is just me - I didn't expect a behavior like this and I can't reproduce what is shown in the video.



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 » Sat Nov 14, 2015 8:42 pm

In both videos, a block recursion is created.

In the first video, the resulting structure is:
'block 1' contains only one entity: a reference to 'block 1' (i.e. itself).
There is nothing to show here, since block 1 only contains a reference to itself, there are no actual entities involved since the original block containing a rectangle and a number was overwritten.

In the second video, the resulting structure is:
'block 1' contains reference to 'block 1' and some other entities.
The original rectangle and number is lost here too (block is overwritten). The other entities are shown as well as the reference to 'block 1' (itself) with the same entities again and again. After about 16 iterations, QCAD stops to prevent a crash or running out of memory.

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 » Sat Nov 14, 2015 8:56 pm

Thanks Andrew. Question to the second video: I can see in the video from marzof that it is working like I would expect but I can't reproduce that. What I'm doing different?
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 » Mon Nov 16, 2015 10:13 am

I think he uses another version CreateBlock.js which was only posted here and is not included in QCAD.

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 » Mon Nov 16, 2015 9:13 pm

andrew wrote:I think he uses another version CreateBlock.js which was only posted here and is not included in QCAD.
Ok, that explains why the behavior is different. Thanks!

I replaced the last posted CreateBlock.js with the CreateBlock.js in the actual QCAD Version. I'm not a programmer and I'm sure I can't really imagine the whole picture of all consequences of such a script but it looks to me it works like expected. I think it could be the solution for my problem. Is there a reason why it isn't in QCAD?
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 » Mon Nov 16, 2015 9:56 pm

Husky wrote: Is there a reason why it isn't in QCAD?
I know that this function might be practical for some users. However, I think it's generally not a good idea to try to guess what a user is really trying to do and change the software in such a way that it no longer does what the user tells it to do but instead does what the software thinks the user probably intended to do :wink:
It gets very hairy to document and explain such behaviour for users: Yes I know you did 'A', but the software thinks that 'B' might be better for you, so it did 'B' instead e_confused

Perhaps there will be a way to edit blocks in place at one point which would offer a solution for adding but also removing or editing entities that are part of a block. Till then, users who are aware of this special feature can install the CreateBlock.js script from here if desired.

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 » Mon Nov 16, 2015 11:14 pm

andrew wrote:
Husky wrote: Is there a reason why it isn't in QCAD?
I know that this function might be practical for some users. However, I think it's generally not a good idea to try to guess what a user is really trying to do and change the software in such a way that it no longer does what the user tells it to do but instead does what the software thinks the user probably intended to do :wink:
It gets very hairy to document and explain such behaviour for users: Yes I know you did 'A', but the software thinks that 'B' might be better for you, so it did 'B' instead e_confused
Yes, I understand your point!
But on the other hand you will also understand thats for me - as a usual user and not as a programmer - it's much more difficult to understand the tool if I see that it will delete my block (in the drawing and in the block list) or will produce multiple copy's of my "to overwriting" block. That is not what I told the tool (simple user thoughts) - I just told it to overwrite an existing block what means for me: Replace what you have in the old block with that what I have selected for the new block. With doing this I'm not expecting some kind of alteration like deleting, copies or block in block nesting etc. etc..
And for me it is hard to understand why I can overwrite block 1 with content from block 2 (flawless) but not block 1 with content of block 1 (leads to deleting, multiple not or only difficult editable copies etc.) ...

Just to plot the thoughts of a usual user (me!) ... :wink:
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 Nov 17, 2015 9:19 am

Sure, fair point.

Post Reply

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