Lee Mac Posted May 30, 2009 Posted May 30, 2009 OK, I have this - It creates the attributed block, but the attributes come in as just definitions, and the block is not inserted as a block, but as all of its sub-entities. [i][color=#990099];; Block ReName by Lee McDonnell [07.05.09][/color][/i] [b][color=RED]([/color][/b][b][color=BLUE]defun[/color][/b] BlkReName [b][color=RED]([/color][/b]Blk Nme pt [b][color=BLUE]/[/color][/b] Blk[b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]if[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]and[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]tblsearch[/color][/b] [b][color=#ff00ff]"BLOCK"[/color][/b] Blk[b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]setq[/color][/b] Blk [b][color=RED]([/color][/b][b][color=BLUE]tblobjname[/color][/b] [b][color=#ff00ff]"BLOCK"[/color][/b] Blk[b][color=RED])[/color][/b][b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]snvalid[/color][/b] Nme[b][color=RED])[/color][/b][b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]progn[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]entmake[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]list[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]cons[/color][/b] [b][color=#009900]0[/color][/b] [b][color=#ff00ff]"BLOCK"[/color][/b][b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]cons[/color][/b] [b][color=#009900]2[/color][/b] Nme[b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]assoc[/color][/b] [b][color=#009900]10[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]entget[/color][/b] Blk[b][color=RED])[/color][/b][b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]assoc[/color][/b] [b][color=#009900]70[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]entget[/color][/b] Blk[b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]mapcar[/color][/b] [b][color=DARKRED]'[/color][/b][b][color=BLUE]entmake[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]mapcar[/color][/b] [b][color=DARKRED]'[/color][/b][b][color=BLUE]entget[/color][/b] [b][color=RED]([/color][/b]GetObj Blk[b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]entmake[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]list[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]cons[/color][/b] [b][color=#009900]0[/color][/b] [b][color=#ff00ff]"ENDBLK"[/color][/b][b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]cons[/color][/b] [b][color=#009900]8[/color][/b] [b][color=#ff00ff]"0"[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]entmake[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]list[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]cons[/color][/b] [b][color=#009900]0[/color][/b] [b][color=#ff00ff]"INSERT"[/color][/b][b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]cons[/color][/b] [b][color=#009900]2[/color][/b] Nme[b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]cons[/color][/b] [b][color=#009900]66[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]if[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]eq[/color][/b] [b][color=#009900]2[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]logand[/color][/b] [b][color=#009900]2[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]cdr[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]assoc[/color][/b] [b][color=#009900]70[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]entget[/color][/b] Blk[b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b] [b][color=#009900]1[/color][/b] [b][color=#009900]0[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]cons[/color][/b] [b][color=#009900]10[/color][/b] pt[b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b] [b][color=BLUE]nil[/color][/b][b][color=RED])[/color][/b] Nme[b][color=RED])[/color][/b] [i][color=#990099]; Get Sub-Entities from Table Def[/color][/i] [b][color=RED]([/color][/b][b][color=BLUE]defun[/color][/b] GetObj [b][color=RED]([/color][/b]bObj[b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]if[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]setq[/color][/b] bObj [b][color=RED]([/color][/b][b][color=BLUE]entnext[/color][/b] bObj[b][color=RED])[/color][/b][b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]cons[/color][/b] bObj [b][color=RED]([/color][/b]GetObj bObj[b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b] [i][color=#990099]; Test Function[/color][/i] [b][color=RED]([/color][/b][b][color=BLUE]defun[/color][/b] c:test [b][color=RED]([/color][/b][b][color=BLUE]/[/color][/b] old new pt[b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]setq[/color][/b] old [b][color=RED]([/color][/b][b][color=BLUE]getstring[/color][/b] [b][color=BLUE]t[/color][/b] [b][color=#ff00ff]"\nOld Block Name: "[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]setq[/color][/b] new [b][color=RED]([/color][/b][b][color=BLUE]getstring[/color][/b] [b][color=BLUE]t[/color][/b] [b][color=#ff00ff]"\nNew Block Name: "[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]setq[/color][/b] pt [b][color=RED]([/color][/b][b][color=BLUE]getpoint[/color][/b] [b][color=#ff00ff]"\nSelect Point for Block: "[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]if[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]not[/color][/b] [b][color=RED]([/color][/b]BlkReName old new pt[b][color=RED])[/color][/b][b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]princ[/color][/b] [b][color=#ff00ff]"\n<< Block Rename Failed >>"[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]princ[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b] Quote
David Bethel Posted May 31, 2009 Posted May 31, 2009 The first nested entity in a BLOCK table definition is stored in group -2 I'd use something like this: [b][color=BLACK]([/color][/b]defun leeblk [b][color=FUCHSIA]([/color][/b]ob nb ip / tdef fe at fd[b][color=FUCHSIA])[/color][/b] [b][color=FUCHSIA]([/color][/b]and [b][color=NAVY]([/color][/b]setq tdef [b][color=MAROON]([/color][/b]tblsearch [color=#2f4f4f]"BLOCK"[/color] ob[b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b] [b][color=NAVY]([/color][/b]setq fe [b][color=MAROON]([/color][/b]cdr [b][color=GREEN]([/color][/b]assoc -2 tdef[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b] at [b][color=MAROON]([/color][/b]if [b][color=GREEN]([/color][/b]= [b][color=BLUE]([/color][/b]logand [b][color=RED]([/color][/b]cdr [b][color=PURPLE]([/color][/b]assoc 70 tdef[b][color=PURPLE])[/color][/b][b][color=RED])[/color][/b] 2[b][color=BLUE])[/color][/b] 2[b][color=GREEN])[/color][/b] t nil[b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b] [b][color=NAVY]([/color][/b]entmake [b][color=MAROON]([/color][/b]subst [b][color=GREEN]([/color][/b]cons 2 nb[b][color=GREEN])[/color][/b] [b][color=GREEN]([/color][/b]assoc 2 tdef[b][color=GREEN])[/color][/b] tdef[b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b] [b][color=NAVY]([/color][/b]while fe [b][color=MAROON]([/color][/b]setq fd [b][color=GREEN]([/color][/b]entget fe[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b] [b][color=MAROON]([/color][/b]entmake fd[b][color=MAROON])[/color][/b] [b][color=MAROON]([/color][/b]setq fe [b][color=GREEN]([/color][/b]entnext fe[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b] [b][color=NAVY]([/color][/b]entmake [b][color=MAROON]([/color][/b]list [b][color=GREEN]([/color][/b]cons 0 [color=#2f4f4f]"ENDBLK"[/color][b][color=GREEN])[/color][/b][b][color=GREEN]([/color][/b]cons 8 [color=#2f4f4f]"0"[/color][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b] [b][color=NAVY]([/color][/b]entmake [b][color=MAROON]([/color][/b]list [b][color=GREEN]([/color][/b]cons 0 [color=#2f4f4f]"INSERT"[/color][b][color=GREEN])[/color][/b][b][color=GREEN]([/color][/b]cons 2 nb[b][color=GREEN])[/color][/b][b][color=GREEN]([/color][/b]cons 10 ip[b][color=GREEN])[/color][/b] [b][color=GREEN]([/color][/b]cons 66 [b][color=BLUE]([/color][/b]if at 1 0[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b] at [color=#8b4513];Now here Is the fun part, do you want the default ATTDEF values[/color] [color=#8b4513];or replace an exisiting blocks values[/color] [b][color=NAVY]([/color][/b]entmake [b][color=MAROON]([/color][/b]list [b][color=GREEN]([/color][/b]cons 0 [color=#2f4f4f]"ATTRIB"[/color][b][color=GREEN])[/color][/b] ,,,,,,[b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b] [b][color=NAVY]([/color][/b]entmake [b][color=MAROON]([/color][/b]list [b][color=GREEN]([/color][/b]cons 0 [color=#2f4f4f]"ATTRIB"[/color][b][color=GREEN])[/color][/b] ,,,,,,[b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b] [b][color=NAVY]([/color][/b]entmake [b][color=MAROON]([/color][/b]list [b][color=GREEN]([/color][/b]cons 0 [color=#2f4f4f]"ATTRIB"[/color][b][color=GREEN])[/color][/b] ,,,,,,[b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b] [b][color=NAVY]([/color][/b]entmake [b][color=MAROON]([/color][/b]list [b][color=GREEN]([/color][/b]cons 0 [color=#2f4f4f]"SEQEND"[/color][b][color=GREEN])[/color][/b][b][color=GREEN]([/color][/b]cons 8 [color=#2f4f4f]"0"[/color][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b] [b][color=FUCHSIA])[/color][/b][color=#8b4513];and[/color] nb[b][color=BLACK])[/color][/b] -David Quote
David Bethel Posted May 31, 2009 Posted May 31, 2009 Also , you can entmod group 2 of an existing INSERT if the BLOCK table definition exists. So it would be: 1) make the new BLOCK 2) Select and change an existing INSERTs name All of the ATTRIB values remain intact. ( a lot less testing and stuff ) -David Quote
Lee Mac Posted May 31, 2009 Posted May 31, 2009 Hi David, Many thanks for offering your expertise in this - it is much appreciated. Using your ideas and advice - this is the final result: [i][color=#990099];; Block ReName by Lee McDonnell [31.05.09][/color][/i] [b][color=RED]([/color][/b][b][color=BLUE]defun[/color][/b] BlkReName [b][color=RED]([/color][/b]oBlk nBlk [b][color=BLUE]/[/color][/b] tdef[b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]if[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]and[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]setq[/color][/b] tdef [b][color=RED]([/color][/b][b][color=BLUE]tblsearch[/color][/b] [b][color=#ff00ff]"BLOCK"[/color][/b] oBlk[b][color=RED])[/color][/b][b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]snvalid[/color][/b] nBlk[b][color=RED])[/color][/b][b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]progn[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]entmake[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]subst[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]cons[/color][/b] [b][color=#009900]2[/color][/b] nBlk[b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]assoc[/color][/b] [b][color=#009900]2[/color][/b] tdef[b][color=RED])[/color][/b] tdef[b][color=RED])[/color][/b][b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]mapcar[/color][/b] [b][color=DARKRED]'[/color][/b][b][color=BLUE]entmake[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]mapcar[/color][/b] [b][color=DARKRED]'[/color][/b][b][color=BLUE]entget[/color][/b] [b][color=RED]([/color][/b]GetObj [b][color=RED]([/color][/b][b][color=BLUE]tblobjname[/color][/b] [b][color=#ff00ff]"BLOCK"[/color][/b] oBlk[b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]entmake[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]list[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]cons[/color][/b] [b][color=#009900]0[/color][/b] [b][color=#ff00ff]"ENDBLK"[/color][/b][b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]cons[/color][/b] [b][color=#009900]8[/color][/b] [b][color=#ff00ff]"0"[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b] [b][color=BLUE]nil[/color][/b][b][color=RED])[/color][/b] Nme[b][color=RED])[/color][/b] [i][color=#990099]; Get Sub-Entities from Table Def[/color][/i] [b][color=RED]([/color][/b][b][color=BLUE]defun[/color][/b] GetObj [b][color=RED]([/color][/b]bObj[b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]if[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]setq[/color][/b] bObj [b][color=RED]([/color][/b][b][color=BLUE]entnext[/color][/b] bObj[b][color=RED])[/color][/b][b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]cons[/color][/b] bObj [b][color=RED]([/color][/b]GetObj bObj[b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b] [i][color=#990099]; Test Function[/color][/i] [b][color=RED]([/color][/b][b][color=BLUE]defun[/color][/b] c:test [b][color=RED]([/color][/b][b][color=BLUE]/[/color][/b] Blk Nme[b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]if[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]and[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]setq[/color][/b] Blk [b][color=RED]([/color][/b][b][color=BLUE]car[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]entsel[/color][/b] [b][color=#ff00ff]"\nSelect Block to Change: "[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]eq[/color][/b] [b][color=#ff00ff]"INSERT"[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]cdr[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]assoc[/color][/b] [b][color=#009900]0[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]setq[/color][/b] Blk [b][color=RED]([/color][/b][b][color=BLUE]entget[/color][/b] Blk[b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]setq[/color][/b] Nme [b][color=RED]([/color][/b][b][color=BLUE]getstring[/color][/b] [b][color=BLUE]t[/color][/b] [b][color=#ff00ff]"\nSpecify New Block Name: "[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]if[/color][/b] [b][color=RED]([/color][/b]BlkReName [b][color=RED]([/color][/b][b][color=BLUE]cdr[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]assoc[/color][/b] [b][color=#009900]2[/color][/b] Blk[b][color=RED])[/color][/b][b][color=RED])[/color][/b] Nme[b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]entmod[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]subst[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]cons[/color][/b] [b][color=#009900]2[/color][/b] Nme[b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]assoc[/color][/b] [b][color=#009900]2[/color][/b] Blk[b][color=RED])[/color][/b] Blk[b][color=RED])[/color][/b][b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]princ[/color][/b] [b][color=#ff00ff]"\n<< Block ReName Failed >>"[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]princ[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b] Thanks once again. I do have one question, however. What reference do you use (if any) when working with DXF codes? I currently use this from AutoDesk, but I struggle to locate some codes, so I wondered if there was a better one? Thanks Lee Quote
David Bethel Posted May 31, 2009 Posted May 31, 2009 Lee, I'm a hard copy book kind of person. Using on screen help is doesn't cut it for me. I use Release 13 AutoCAD Customization Guide as my main reference for coding. I am going to have to change soon 'cause the covers are worn out and missing and half of the rear index is missing from years of over use. I do keep a link to A2K VLisp help on the desktop of 1 machine and use it for most general help stuff. Just not for coding help. The (if (and statement you used can be consensed just to an (and ) call. I like to use this scenario as it will stop evaluating upon the first nil return. For example the 'at' test that I used would stop everything if the ATTDEF flag wasn't set. You are getting better at making things simpler in your code. Congrats. I make it a challenge to see how compact I can make code and still be complete. Regards. -David Quote
Lee Mac Posted May 31, 2009 Posted May 31, 2009 I'm a hard copy book kind of person. Using on screen help is doesn't cut it for me. I use Release 13 AutoCAD Customization Guide as my main reference for coding. I am going to have to change soon 'cause the covers are worn out and missing and half of the rear index is missing from years of over use. Yes, I admit, I prefer using a hard copy reference - as I first learnt using an old AutoLISP Programmer's Reference from Release 11 (published October 1990)... but I stopped using this when I discovered the magnitude of functions not covered in it - and I reverted to using a combination of the VLIDE help in '04 and a PDF of an AutoLISP reference from 2000 (which surprisingly is sometimes more useful than the newer '04 reference...). The (if (and statement you used can be consensed just to an (and ) call. I like to use this scenario as it will stop evaluating upon the first nil return. For example the 'at' test that I used would stop everything if the ATTDEF flag wasn't set. I understand where you are coming from with the use of AND, instead of (IF (AND..., making the code more compact and possibly faster - but in your case of the ATTDEF flag test, I assume this would not work for blocks with no attributes? I just feel the IF makes things easier to test for - as you don't have to worry about statements that inadvertently return nil. You are getting better at making things simpler in your code. Congrats. I make it a challenge to see how compact I can make code and still be complete. Thanks David, I try to use as few variables as I can (within reason), and make the code as short and compact as I can and still achieve the same functionality. It makes it look nicer too Thanks for your advice, you have been very helpful as always Cheers, Lee Quote
Lee Mac Posted May 31, 2009 Posted May 31, 2009 also, -rename can be used to rename blocks. Trust us to miss the simple way Nice one Wizman Quote
David Bethel Posted May 31, 2009 Posted May 31, 2009 I was under the impression from the OP that they wanted to make a copy of an existing INSERT with a new name and not change any other remaining INSERTs of the same name. I could see a use for that. -David Quote
wizman Posted June 1, 2009 Posted June 1, 2009 Trust us to miss the simple way Nice one Wizman Thanks Lee, i know you guys know it already, just thrown it for adding input. I was under the impression from the OP that they wanted to make a copy of an existing INSERT with a new name and not change any other remaining INSERTs of the same name. I could see a use for that. -David Useful indeed david but in the wrong hands dangerous to see two blocks that looks similar with different names. Quote
Freerefill Posted June 1, 2009 Author Posted June 1, 2009 Useful indeed david but in the wrong hands dangerous to see two blocks that looks similar with different names. Dangerous indeed, but this function is actually intended to be an addition to a global Edit function I've written which allows the user to edit text, mtext, attribute text and nested block text with a single command and a single click. Whilst not perfect, it has so far done the job admirably, with the exception of being able to edit a single instance of a single block. I mean, heck, I'm not even sure if it's -useful-, but then, many things I create are not. That, however, never has and never will stop me from trying~ Quote
David Bethel Posted June 1, 2009 Posted June 1, 2009 A possible use would be if you were doing a piping drawing with lots of say valves. Same basic symbol, different names for different sizes. After adding more fixtures, you realize that you need increase a valve size upstream. Update the INSERT with the proper name. Here's a little more robust routine with a few more options: [b][color=BLACK]([/color][/b]defun c:updblk [b][color=FUCHSIA]([/color][/b]/ ss ob st nb i en ed[b][color=FUCHSIA])[/color][/b] [b][color=FUCHSIA]([/color][/b]while [b][color=NAVY]([/color][/b]or [b][color=MAROON]([/color][/b]not ss[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=NAVY])[/color][/b] [b][color=NAVY]([/color][/b]princ [color=#2f4f4f]"\nSelect An INSERT To Update: "[/color][b][color=NAVY])[/color][/b] [b][color=NAVY]([/color][/b]setq ss [b][color=MAROON]([/color][/b]ssget [b][color=GREEN]([/color][/b]list [b][color=BLUE]([/color][/b]cons 0 [color=#2f4f4f]"INSERT"[/color][b][color=BLUE])[/color][/b] [b][color=BLUE]([/color][/b]if [b][color=RED]([/color][/b]getvar [color=#2f4f4f]"CTAB"[/color][b][color=RED])[/color][/b] [b][color=RED]([/color][/b]cons 410 [b][color=PURPLE]([/color][/b]getvar [color=#2f4f4f]"CTAB"[/color][b][color=PURPLE])[/color][/b][b][color=RED])[/color][/b] [b][color=RED]([/color][/b]cons 67 [b][color=PURPLE]([/color][/b]- 1 [b][color=TEAL]([/color][/b]getvar [color=#2f4f4f]"TILEMODE"[/color][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]setq ob [b][color=NAVY]([/color][/b]cdr [b][color=MAROON]([/color][/b]assoc 2 [b][color=GREEN]([/color][/b]entget [b][color=BLUE]([/color][/b]ssname ss 0[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b] st [b][color=NAVY]([/color][/b]ssget [color=#2f4f4f]"X"[/color] [b][color=MAROON]([/color][/b]list [b][color=GREEN]([/color][/b]cons 0 [color=#2f4f4f]"INSERT"[/color][b][color=GREEN])[/color][/b] [b][color=GREEN]([/color][/b]cons 2 ob[b][color=GREEN])[/color][/b] [b][color=GREEN]([/color][/b]if [b][color=BLUE]([/color][/b]getvar [color=#2f4f4f]"CTAB"[/color][b][color=BLUE])[/color][/b] [b][color=BLUE]([/color][/b]cons 410 [b][color=RED]([/color][/b]getvar [color=#2f4f4f]"CTAB"[/color][b][color=RED])[/color][/b][b][color=BLUE])[/color][/b] [b][color=BLUE]([/color][/b]cons 67 [b][color=RED]([/color][/b]- 1 [b][color=PURPLE]([/color][/b]getvar [color=#2f4f4f]"TILEMODE"[/color][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]while [b][color=NAVY]([/color][/b]or [b][color=MAROON]([/color][/b]not nb[b][color=MAROON])[/color][/b] [b][color=MAROON]([/color][/b]not [b][color=GREEN]([/color][/b]snvalid nb[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b] [b][color=NAVY]([/color][/b]setq nb [b][color=MAROON]([/color][/b]strcase [b][color=GREEN]([/color][/b]getstring [color=#2f4f4f]"\nNew BLOCK Name: "[/color][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b] [b][color=NAVY]([/color][/b]cond [b][color=MAROON]([/color][/b][b][color=GREEN]([/color][/b]= ob nb[b][color=GREEN])[/color][/b] [b][color=GREEN]([/color][/b]command [color=#2f4f4f]"_.INSERT"[/color] [b][color=BLUE]([/color][/b]strcat ob [color=#2f4f4f]"="[/color] nb[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b] [b][color=GREEN]([/color][/b]command[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b] [b][color=MAROON]([/color][/b][b][color=GREEN]([/color][/b]tblsearch [color=#2f4f4f]"BLOCK"[/color] nb[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b] [b][color=MAROON]([/color][/b][b][color=GREEN]([/color][/b]findfile [b][color=BLUE]([/color][/b]strcat nb [color=#2f4f4f]".DWG"[/color][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b] [b][color=GREEN]([/color][/b]command [color=#2f4f4f]"_.INSERT"[/color] nb[b][color=GREEN])[/color][/b] [b][color=GREEN]([/color][/b]command[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b] [b][color=MAROON]([/color][/b]T [b][color=GREEN]([/color][/b]setq nb nil[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]if [b][color=NAVY]([/color][/b]and [b][color=MAROON]([/color][/b]> [b][color=GREEN]([/color][/b]sslength st[b][color=GREEN])[/color][/b] 1[b][color=MAROON])[/color][/b] [b][color=MAROON]([/color][/b]/= ob nb[b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b] [b][color=NAVY]([/color][/b]progn [b][color=MAROON]([/color][/b]initget 1 [color=#2f4f4f]"All Single"[/color][b][color=MAROON])[/color][/b] [b][color=MAROON]([/color][/b]if [b][color=GREEN]([/color][/b]= [color=#2f4f4f]"All"[/color] [b][color=BLUE]([/color][/b]getkword [b][color=RED]([/color][/b]strcat [color=#2f4f4f]"\n"[/color] ob [color=#2f4f4f]" Replacement - All/Single: "[/color][b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b] [b][color=GREEN]([/color][/b]setq ss st[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] ob [color=#2f4f4f]" -> "[/color] nb [color=#2f4f4f]" Replacement..."[/color][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b] [b][color=FUCHSIA]([/color][/b]setq i [b][color=NAVY]([/color][/b]sslength ss[b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b] [b][color=FUCHSIA]([/color][/b]while [b][color=NAVY]([/color][/b]not [b][color=MAROON]([/color][/b]minusp [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=NAVY]([/color][/b]setq en [b][color=MAROON]([/color][/b]ssname ss i[b][color=MAROON])[/color][/b] ed [b][color=MAROON]([/color][/b]entget en[b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b] [b][color=NAVY]([/color][/b]entmod [b][color=MAROON]([/color][/b]subst [b][color=GREEN]([/color][/b]cons 2 nb[b][color=GREEN])[/color][/b] [b][color=GREEN]([/color][/b]cons 2 ob[b][color=GREEN])[/color][/b] ed[b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b] [b][color=FUCHSIA]([/color][/b]command [color=#2f4f4f]"_.REGENALL"[/color][b][color=FUCHSIA])[/color][/b] [b][color=FUCHSIA]([/color][/b]prin1[b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b] -David Quote
Freerefill Posted June 1, 2009 Author Posted June 1, 2009 Lee, I didn't get a chance to really look over your code over the weekend but I did now. Once again, quite mind-blowing. I didn't know it was possible to use (entmake) and then another, quite separately, (entmake) and have them both be working on the same object. I don't know much about code and how it works on the "lower level" (I think that's what it's called) but that's what this seems to be doing: filling "memory" with information, then, finally, telling it to stop (much like an array of characters in C). Is this what it's doing with all those entmakes? Also, I had a question on how you named the block. You seemed to be naming it in two locations; the first (entmake) of your BlkReName function, and then again in your test function after you call it. Just a guess, is that because you need to edit the table definition? I'm also wondering how you return your variable Nme in the BlkReName, when it doesn't get defined in there. Forgive me if I ask a lot of questions, but this stuff is so far beyond me.. after I make this post, I'm going to look up tblsearch again Quote
Lee Mac Posted June 1, 2009 Posted June 1, 2009 I'm also wondering how you return your variable Nme in the BlkReName, when it doesn't get defined in there. Firstly - I apologise for this, a mistake on my part - carried over erroneously from previous versions. But as for the rest of the code, I shall try my best to explain it below: [i][color=#990099]; Test Function[/color][/i] [b][color=RED]([/color][/b][b][color=BLUE]defun[/color][/b] c:test [b][color=RED]([/color][/b][b][color=BLUE]/[/color][/b] Blk Nme[b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]if[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]and[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]setq[/color][/b] Blk [b][color=RED]([/color][/b][b][color=BLUE]car[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]entsel[/color][/b] [b][color=#ff00ff]"\nSelect Block to Change: "[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]eq[/color][/b] [b][color=#ff00ff]"INSERT"[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]cdr[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]assoc[/color][/b] [b][color=#009900]0[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]setq[/color][/b] Blk [b][color=RED]([/color][/b][b][color=BLUE]entget[/color][/b] Blk[b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]setq[/color][/b] Nme [b][color=RED]([/color][/b][b][color=BLUE]getstring[/color][/b] [b][color=BLUE]t[/color][/b] [b][color=#ff00ff]"\nSpecify New Block Name: "[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]if[/color][/b] [b][color=RED]([/color][/b]BlkReName [b][color=RED]([/color][/b][b][color=BLUE]cdr[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]assoc[/color][/b] [b][color=#009900]2[/color][/b] Blk[b][color=RED])[/color][/b][b][color=RED])[/color][/b] Nme[b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]entmod[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]subst[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]cons[/color][/b] [b][color=#009900]2[/color][/b] Nme[b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]assoc[/color][/b] [b][color=#009900]2[/color][/b] Blk[b][color=RED])[/color][/b] Blk[b][color=RED])[/color][/b][b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]princ[/color][/b] [b][color=#ff00ff]"\n<< Block ReName Failed >>"[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]princ[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b] Lets start with the Test function, and step through it: [b][color=RED]([/color][/b][b][color=BLUE]if[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]and[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]setq[/color][/b] Blk [b][color=RED]([/color][/b][b][color=BLUE]car[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]entsel[/color][/b] [b][color=#ff00ff]"\nSelect Block to Change: "[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]eq[/color][/b] [b][color=#ff00ff]"INSERT"[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]cdr[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]assoc[/color][/b] [b][color=#009900]0[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]setq[/color][/b] Blk [b][color=RED]([/color][/b][b][color=BLUE]entget[/color][/b] Blk[b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]setq[/color][/b] Nme [b][color=RED]([/color][/b][b][color=BLUE]getstring[/color][/b] [b][color=BLUE]t[/color][/b] [b][color=#ff00ff]"\nSpecify New Block Name: "[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED]) [/color][/b] Obviously, this is the user input - Checking that a block has indeed been selected, and prompting for a new block name. [b][color=RED]([/color][/b][b][color=BLUE]if[/color][/b] [b][color=RED]([/color][/b]BlkReName [b][color=RED]([/color][/b][b][color=BLUE]cdr[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]assoc[/color][/b] [b][color=#009900]2[/color][/b] Blk[b][color=RED])[/color][/b][b][color=RED])[/color][/b] Nme[b][color=RED])[/color][/b] Now that we have all the necessary user inputs, I invoke the sub-function to create the block table definition - so lets move onto the sub-function: [b][color=RED]([/color][/b][b][color=BLUE]defun[/color][/b] BlkReName [b][color=RED]([/color][/b]oBlk nBlk [b][color=BLUE]/[/color][/b] tdef[b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]if[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]and[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]setq[/color][/b] tdef [b][color=RED]([/color][/b][b][color=BLUE]tblsearch[/color][/b] [b][color=#ff00ff]"BLOCK"[/color][/b] oBlk[b][color=RED])[/color][/b][b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]snvalid[/color][/b] nBlk[b][color=RED])[/color][/b][b][color=RED])[/color][/b] Ok, so firstly, we need to check that a valid string has been supplied, and also pull the current table definition from the block table. [b][color=RED]([/color][/b][b][color=BLUE]progn[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]entmake[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]subst[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]cons[/color][/b] [b][color=#009900]2[/color][/b] nBlk[b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]assoc[/color][/b] [b][color=#009900]2[/color][/b] tdef[b][color=RED])[/color][/b] tdef[b][color=RED])[/color][/b][b][color=RED])[/color][/b] We can now start to "re-build" a new table definition to assign to our block. So I use the old table definition and just substitute the new name for the old. [b][color=RED]([/color][/b][b][color=BLUE]mapcar[/color][/b] [b][color=DARKRED]'[/color][/b][b][color=BLUE]entmake[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]mapcar[/color][/b] [b][color=DARKRED]'[/color][/b][b][color=BLUE]entget[/color][/b] [b][color=RED]([/color][/b]GetObj [b][color=RED]([/color][/b][b][color=BLUE]tblobjname[/color][/b] [b][color=#ff00ff]"BLOCK"[/color][/b] oBlk[b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b] I then grab all the sub-entities from the block using the other sub-function "GetObj" which I think we have discussed before. [b][color=RED]([/color][/b][b][color=BLUE]entmake[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]list[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]cons[/color][/b] [b][color=#009900]0[/color][/b] [b][color=#ff00ff]"ENDBLK"[/color][/b][b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]cons[/color][/b] [b][color=#009900]8[/color][/b] [b][color=#ff00ff]"0"[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b] I then finish the block definition using "ENDBLK". [b][color=BLUE]nil[/color][/b][b][color=RED])[/color][/b] nBlk[b][color=RED])[/color][/b] If the string is not valid, nil is returned, else the new block name is returned. Now that we have "re-built" the table definition, we can substitute this new name into our block (as we discussed previously in the thread, only a name that resides in the table can be used): [b][color=RED]([/color][/b][b][color=BLUE]entmod[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]subst[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]cons[/color][/b] [b][color=#009900]2[/color][/b] Nme[b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]assoc[/color][/b] [b][color=#009900]2[/color][/b] Blk[b][color=RED])[/color][/b] Blk[b][color=RED])[/color][/b][b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]princ[/color][/b] [b][color=#ff00ff]"\n<< Block ReName Failed >>"[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]princ[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b] I hope this clarifies things better for you FreeRefill. If you still have questions, just shout dude. Lee Quote
Freerefill Posted June 1, 2009 Author Posted June 1, 2009 Great explanation, I think I understand now. In my own words.. what it seems is that you're creating a new table definition, but -not- a new block (that is to say, you aren't putting any entities into the drawing, which is what I was doing). Once the new definition exists, it's possible to rename the block when it was not before. Man, just when you think you have an idea of what you're doing.... I think what confuses me is that when I used entmake, it physically created the object, but it doesn't seem to be doing that with yours. Or does that last (entmake) with the "ENDBLK" create the block from the created entities? Quote
Lee Mac Posted June 1, 2009 Posted June 1, 2009 Great explanation, I think I understand now. In my own words.. what it seems is that you're creating a new table definition, but -not- a new block (that is to say, you aren't putting any entities into the drawing, which is what I was doing). Once the new definition exists, it's possible to rename the block when it was not before. Man, just when you think you have an idea of what you're doing.... I think what confuses me is that when I used entmake, it physically created the object, but it doesn't seem to be doing that with yours. Or does that last (entmake) with the "ENDBLK" create the block from the created entities? I believe you are somewhat correct in your understanding - the entmake is creating the table definition, but no entites are created in the drawing. To entmake this new block into the drawing, one would have to use: (entmake (list [color=Red][b] (cons 0 "INSERT")[/b][/color] etc etc... Whereas, in creating the table definition, I am using (entmake (list [color=Red][b](cons 0 "BLOCK")[/b][/color] etc etc... ;; entmake some entities... (entmake (list [color=Red][b] (cons 0 "ENDBLK")[/b][/color] etc etc... Where all the entities "entmade" between the BLOCK and ENDBLK flags are part of the block table definition. Hope this makes things clearer, Lee Quote
Freerefill Posted June 1, 2009 Author Posted June 1, 2009 Right.. and since code progresses from start to finish, each piece needs to be evaluated, you're adding a section to the code of the drawing that AutoCAD is looking at.. it sees that a new block definition (but -not- something to be drawn, just a definition) is being created, it continues on the code, adding all the entities until it sees the "ENDBLK", it calls all of that jazz a "new table definition", then continues on to the next thing. If you hadn't put the "ENDBLK" in there, would you get some kind of crazy error where AutoCAD tries to keep adding objects to the block? Or would it simply not allow you to do that? Going for the win.. is the GetObj function linearly recursive..? Quote
Lee Mac Posted June 1, 2009 Posted June 1, 2009 If you hadn't put the "ENDBLK" in there, would you get some kind of crazy error where AutoCAD tries to keep adding objects to the block? Or would it simply not allow you to do that? It wouldn't keep adding the objects, as there would be no more Entmake experessions to be evaluated, but the table definition would not be created properly. Going for the win.. is the GetObj function linearly recursive..? Haha, you've been talking to Se7en... but in answer to your question, yes, I believe it is, as the "cons" function is yet to be evaluated during the recursive loop. Lee Quote
DVDM Posted July 21, 2009 Posted July 21, 2009 Fantastic! I needed something like this. Very useful. Just when I thought I was the only one who would find something like this useful, after a fruitless google search... Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.