乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 79|回复: 7

[编程交流] Replace block, but keeping old

[复制链接]

1

主题

4

帖子

3

银币

初来乍到

Rank: 1

铜币
5
发表于 2022-7-5 17:19:16 | 显示全部楼层 |阅读模式
Hello,
 
I'm looking for something like the following:
 
I have a drawing with a lot of similar or nearly equal blocks. Unfortunately each block has a unique name so it's not a block reference. But each block has the same attribute list, but with block specific values.
 
What I want to do is to change all blocks against one master block, so that each block has the same name (block reference). For this I found some scripts. Unfortunately the unique block attributes get lost or will be replaced by the master block.
 
http://www.cadtutor.net/forum/showthread.php?37765-Merge-Block
 
 
I also found some scripts for copying attributes from one to another block.
 
http://www.lee-mac.com/matchattribs.html
 
BUT what I'm locking is a script that does the following.
 
1. Select  the master block (A)
2. Select all other blocks (B,C,D,E, ..)
3. Starting the replacement of blocks (B,C,D,E, ..)
3.1 Copy attributes values of block B to a list
3.2 Replacing the block B against master block A
3.3 Copy back attribute values to the newly replaced block
3.4 Starting with Block C again ( steps 3.1 - 3.3)
 
Anybody who can help ?? I looked into the code of the scripts, but I'm "too" Newbee  to lisp to make this work.
 
Thanks in advance
Dandy
回复

使用道具 举报

106

主题

1万

帖子

101

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1299
发表于 2022-7-5 17:31:09 | 显示全部楼层
There are two ways to access info about block attributes either by their "tag name" or "creation order" in your case unless every block has the same tag names I would use the second method.
 
Please post a dwg with a few blocks in it.
 
  1. ; example of attributes by creation order; an example pick one attributed block(setq ss1 (ssget (list (cons 0 "INSERT"))))(foreach att (vlax-invoke (vlax-ename->vla-object (ssname SS1 0 )) 'getattributes)(alert (strcase (vla-get-textstring att))) ;displays attribute value;(alert (strcase (vla-get-tagstring att))) ;displays tag name)
回复

使用道具 举报

1

主题

4

帖子

3

银币

初来乍到

Rank: 1

铜币
5
发表于 2022-7-5 17:40:10 | 显示全部楼层
Hello Bigal,
 
thanks for replaying.
I have attached a sample.dwg
 
In this dwg are 3 blue blocks (they are irrelevant) and 4 other similar block references (white).
 
3 of them (with the base point in the blue ones) have an cryptic ID as a name "{.........}". The forth block (top right) has a name "Raumstempel".
 
What I want to achive is to replace the block with the cryptic names by a block reference of the block "Raumstempel", but before replacing the attribute values of each block (attributes of all 4 relevant blocks are the same, only values are unique) has to be stored and written back after the replacement.
 
I found a script for replacing and a  script for copying attributes form Mac Lee (see my first post). But because of I'm not familar with AutoCAD programming I'm not able to combine them
 
So that was the reason to ask for some support.
 
If you are someone else can help me this would be great
 
Thanks Dandy
 
 
sample.dwg
回复

使用道具 举报

106

主题

1万

帖子

101

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1299
发表于 2022-7-5 17:48:43 | 显示全部楼层
Ok you can find all the blocks via this as long as they always start with {
  1. (setq ss (ssget "x" (list( cons 0 "Insert")(cons 2 "{*"))))
 
I would read all the attributes using above swap the existing block onto a frozen layer, then insert the correct block same spot with the attribute values, when happy its all ok you could delete the old block rather than freeze it.
 
Need to replace in code above in a repeat (ssname SS1 x) where is x is a counter id from how many blocks have been found.
 
Bit busy right now some one else may jump in with a solution.
回复

使用道具 举报

26

主题

1495

帖子

20

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
118
发表于 2022-7-5 17:56:55 | 显示全部楼层
Actually it can be a fairly simple routine :
 
You can (subst) group 2 BLOCK names as long as the replacment BLOCK definition exists in the current drawing.
 
  1. [b][color=BLACK]([/color][/b]defun c:blk-mast [b][color=FUCHSIA]([/color][/b]/ ss i en ed[b][color=FUCHSIA])[/color][/b] [b][color=FUCHSIA]([/color][/b]if [b][color=NAVY]([/color][/b]not [b][color=MAROON]([/color][/b]tblsearch [color=#2f4f4f]"BLOCK"[/color] [color=#2f4f4f]"MASTER"[/color][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]     [b][color=NAVY]([/color][/b]alert [color=#2f4f4f]"MASTER Block Not Found"[/color][b][color=NAVY])[/color][/b]     [b][color=NAVY]([/color][/b]and [b][color=MAROON]([/color][/b]setq ss [b][color=GREEN]([/color][/b]ssget [color=#2f4f4f]"X"[/color] [b][color=BLUE]([/color][/b]list [b][color=RED]([/color][/b]cons 0 [color=#2f4f4f]"INSERT"[/color][b][color=RED])[/color][/b]                                    [b][color=RED]([/color][/b]cons 2 [color=#2f4f4f]"A,B,C"[/color][b][color=RED])[/color][/b]                                    [b][color=RED]([/color][/b]cons 66 1[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]          [b][color=MAROON]([/color][/b]setq i 0[b][color=MAROON])[/color][/b]          [b][color=MAROON]([/color][/b]while [b][color=GREEN]([/color][/b]setq en [b][color=BLUE]([/color][/b]ssname ss i[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]                 [b][color=GREEN]([/color][/b]setq ed [b][color=BLUE]([/color][/b]entget en[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]                 [b][color=GREEN]([/color][/b]entmod [b][color=BLUE]([/color][/b]subst [b][color=RED]([/color][/b]cons 2 [color=#2f4f4f]"MASTER"[/color][b][color=RED])[/color][/b] [b][color=RED]([/color][/b]assoc 2 ed[b][color=RED])[/color][/b] ed[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]                 [b][color=GREEN]([/color][/b]setq i [b][color=BLUE]([/color][/b]1+ i[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b] [b][color=FUCHSIA]([/color][/b]prin1[b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b]
 
A gotcha here could be any synchronize routine that goes back to the BLOCK table definitions.
 
-David
-BLKR.DWG
回复

使用道具 举报

1

主题

4

帖子

3

银币

初来乍到

Rank: 1

铜币
5
发表于 2022-7-5 18:06:49 | 显示全部楼层
Hello David,
thanks for replaying. I just checked your script and your sample dwg, it looked good and the script makes mainly what I'm looking for :-). But is it possible to provide the arguments by input selection (mouse courser) instead of a fixed list ?? Unfortunately my block names are not as simple as your sample A,B,C and picking would be much more easier than taking out the name of each block in my drawing and to placing it in a list. Thanks in advance ;-) Daniel
回复

使用道具 举报

26

主题

1495

帖子

20

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
118
发表于 2022-7-5 18:15:10 | 显示全部楼层
Maybe :
 
  1. [b][color=BLACK]([/color][/b]defun c:blk-mmst [b][color=FUCHSIA]([/color][/b]/ master rl ss i en ed bn[b][color=FUCHSIA])[/color][/b] [b][color=FUCHSIA]([/color][/b]while [b][color=NAVY]([/color][/b]not master[b][color=NAVY])[/color][/b]        [b][color=NAVY]([/color][/b]and [b][color=MAROON]([/color][/b]princ [color=#2f4f4f]"\nSelect MASTER Replacement BLOCK:   "[/color][b][color=MAROON])[/color][/b]             [b][color=MAROON]([/color][/b]setq ss [b][color=GREEN]([/color][/b]ssget [b][color=BLUE]([/color][/b]list [b][color=RED]([/color][/b]cons 0 [color=#2f4f4f]"INSERT"[/color][b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]             [b][color=MAROON]([/color][/b]= [b][color=GREEN]([/color][/b]sslength ss[b][color=GREEN])[/color][/b] 1[b][color=MAROON])[/color][/b]             [b][color=MAROON]([/color][/b]setq master [b][color=GREEN]([/color][/b]strcase [b][color=BLUE]([/color][/b]cdr [b][color=RED]([/color][/b]assoc 2 [b][color=PURPLE]([/color][/b]entget [b][color=TEAL]([/color][/b]ssname ss 0[b][color=TEAL])[/color][/b][b][color=PURPLE])[/color][/b][b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b] [b][color=FUCHSIA]([/color][/b]princ [b][color=NAVY]([/color][/b]strcat [color=#2f4f4f]"\n"[/color] master[b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b] [b][color=FUCHSIA]([/color][/b]while   [b][color=NAVY]([/color][/b]and [b][color=MAROON]([/color][/b]princ [color=#2f4f4f]"\nSelect BLOCKs To Replace:   "[/color][b][color=MAROON])[/color][/b]        [b][color=MAROON]([/color][/b]setq ss [b][color=GREEN]([/color][/b]ssget [b][color=BLUE]([/color][/b]list [b][color=RED]([/color][/b]cons 0 [color=#2f4f4f]"INSERT"[/color][b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]   [b][color=NAVY]([/color][/b]setq i 0[b][color=NAVY])[/color][/b]   [b][color=NAVY]([/color][/b]while [b][color=MAROON]([/color][/b]setq en [b][color=GREEN]([/color][/b]ssname ss i[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]          [b][color=MAROON]([/color][/b]setq ed [b][color=GREEN]([/color][/b]entget en[b][color=GREEN])[/color][/b]                bn [b][color=GREEN]([/color][/b]strcase [b][color=BLUE]([/color][/b]cdr [b][color=RED]([/color][/b]assoc 2 ed[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]          [b][color=MAROON]([/color][/b]cond [b][color=GREEN]([/color][/b][b][color=BLUE]([/color][/b]= bn master[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]                [b][color=GREEN]([/color][/b][b][color=BLUE]([/color][/b]member bn rl[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]                [b][color=GREEN]([/color][/b][b][color=BLUE]([/color][/b]setq rl [b][color=RED]([/color][/b]cons bn rl[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]          [b][color=MAROON]([/color][/b]setq i [b][color=GREEN]([/color][/b]1+ i[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b] [b][color=FUCHSIA]([/color][/b]foreach e rl   [b][color=NAVY]([/color][/b]and [b][color=MAROON]([/color][/b]setq ss [b][color=GREEN]([/color][/b]ssget [color=#2f4f4f]"X"[/color] [b][color=BLUE]([/color][/b]list [b][color=RED]([/color][/b]cons 0 [color=#2f4f4f]"INSERT"[/color][b][color=RED])[/color][/b]                                  [b][color=RED]([/color][/b]cons 2 e[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]        [b][color=MAROON]([/color][/b]princ [b][color=GREEN]([/color][/b]strcat [color=#2f4f4f]"\n"[/color] e[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]        [b][color=MAROON]([/color][/b]setq i 0[b][color=MAROON])[/color][/b]        [b][color=MAROON]([/color][/b]while [b][color=GREEN]([/color][/b]setq en [b][color=BLUE]([/color][/b]ssname ss i[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]               [b][color=GREEN]([/color][/b]setq ed [b][color=BLUE]([/color][/b]entget en[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]               [b][color=GREEN]([/color][/b]entmod [b][color=BLUE]([/color][/b]subst [b][color=RED]([/color][/b]cons 2 master[b][color=RED])[/color][/b] [b][color=RED]([/color][/b]assoc 2 ed[b][color=RED])[/color][/b] ed[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]               [b][color=GREEN]([/color][/b]setq i [b][color=BLUE]([/color][/b]1+ i[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b] [b][color=FUCHSIA]([/color][/b]command [color=#2f4f4f]"_.REGEN"[/color][b][color=FUCHSIA])[/color][/b] [b][color=FUCHSIA]([/color][/b]prin1[b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b]
 
 
This would not deal nested BLOCKs
 
HTH  -David
回复

使用道具 举报

1

主题

4

帖子

3

银币

初来乍到

Rank: 1

铜币
5
发表于 2022-7-5 18:25:13 | 显示全部楼层
Hello David, thanks for supporting .. I will try it out ;-). Best Dandy
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-13 08:14 , Processed in 0.704663 second(s), 68 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表