Happy Hobbit Posted November 27, 2015 Share Posted November 27, 2015 I'm trying to create a lisp that modifies the height of a picked individual attribute in all blocks of the same name. A prolonged Google search didn't come up with much. There's BATTMAN of course, but in a multi-attributed block it's not always easy to discern which attribute is which, especialy if they've been given a ambiguous name. There's also Lee Macs excellent Redefine_Block_Text but it's redefines all items within a block. My need is for a lisp which picks an attribute, returns the old height & prompts for a new height, then attsyncs all. The code I have created works until the block is 'attsynced'. It modifies the height of the attribute, but it returns to the previous size on attsync. I would guess that it's modifying the height of the value of the attribute rather than the tag. My code so far: (defun c:Atthght ( / a ent ent entdxf newh oldh ) (vl-load-com) (while (setq ent (car(nentsel "\nSelect Attribute <exit>:"))) (if (wcmatch (cdr (assoc 0 (setq entdxf (entget ent)))) "ATTRIB") (progn (setq oldh (rtos (cdr(assoc 40 (entget ent))))) (setq newh (getstring (strcat "\nSpecify New Height: <" oldh ">"))) (if (entmod (subst (cons 40 (atof newh)) (assoc 40 entdxf) entdxf)) (entupd ent) ) (while (setq a (tblnext "BLOCK" (null a))) ;; (if (= (cdr (Assoc 70 a)) 2) ;; attsync (vl-cmdf "_.AttSync" "Name" (cdr (assoc 2 a))) ;; bit ) ;; ) ;; );; progn (princ "\nSelected object is not an Attribute.") ) ) (princ) ) Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted November 27, 2015 Share Posted November 27, 2015 Since you are using ATTSYNC, you can change the height of the attribute definition within the block definition and such changes will be automatically reflected across all references of the block (your current code is changing the height of the attribute reference within a single block reference). Quote Link to comment Share on other sites More sharing options...
Happy Hobbit Posted November 27, 2015 Author Share Posted November 27, 2015 Hello again Lee I realised that it could be done using REFEDIT or BEDIT, I was trying to find a shortcut. Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted November 27, 2015 Share Posted November 27, 2015 I'm not suggesting using REFEDIT or BEDIT. Quote Link to comment Share on other sites More sharing options...
Happy Hobbit Posted November 27, 2015 Author Share Posted November 27, 2015 Well, what are you suggesting? Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted November 27, 2015 Share Posted November 27, 2015 Well, what are you suggesting? As per my above post, that you: change the height of the attribute definition within the block definition <...> (your current code is changing the height of the attribute reference within a single block reference). Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted November 27, 2015 Share Posted November 27, 2015 To give some more direction: retrieve the BLOCK_RECORD entity using the tblobjname function, and step through the block component entities using the entnext function; when you encounter the target ATTDEF entity, modify the height in the same way as you have with the ATTRIB entity in your code. Then invoke the ATTSYNC command to apply the change across all references of the block. To provide more information about the structure of a block definition, see my post here from 2012. Quote Link to comment Share on other sites More sharing options...
Happy Hobbit Posted November 27, 2015 Author Share Posted November 27, 2015 I've never even heard of tblobynamethingy.... I will search for en existing lisp to see how it works Ta Lee Quote Link to comment Share on other sites More sharing options...
Happy Hobbit Posted November 27, 2015 Author Share Posted November 27, 2015 I got as far as: (setq entdxf (entget(car(entsel)))) (setq bn (cdr(assoc 2 entdxf))) (tblobjname "block" bn) Then I got a bit lost Quote Link to comment Share on other sites More sharing options...
Tharwat Posted November 27, 2015 Share Posted November 27, 2015 As Lee described earlier that you need to work on the Block definition and not on the Block reference , so I am just giving you an example to show you the way you should go. (defun c:Test (/ s e h dc bn tg) ;; Tharwat 27.11.2015 ;; (if (and (setq s (nentsel "\nSelect Attribute in a Block :")) (= (cdr (assoc 0 (setq e (entget (car s))))) "ATTRIB") (setq h (getdist (strcat "\nSpecify new height < current height [ " (rtos (cdr (assoc 40 e)) 2) " ] :" ))) ) (progn (setq dc (vla-get-activedocument (vlax-get-acad-object)) bn (vla-get-effectivename (vlax-ename->vla-object (cdr (assoc 330 e)))) tg (strcase (cdr (assoc 2 e))) ) (vlax-for x (vla-item (vla-get-blocks dc) bn) (if (and (= (vla-get-objectname x) "AcDbAttributeDefinition") (= (strcase (vla-get-tagstring x)) tg) ) (vla-put-height x h) ) ) (vl-cmdf "_attsync" "_name" bn) ) ) (princ) )(vl-load-com) Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted November 27, 2015 Share Posted November 27, 2015 I got as far as: (setq entdxf (entget(car(entsel)))) (setq bn (cdr(assoc 2 entdxf))) (tblobjname "block" bn) Then I got a bit lost Here is a full annotated Vanilla AutoLISP example for you to study: [color=GREEN];; Define function & declare local variables[/color] ([color=BLUE]defun[/color] c:atthgt ( [color=BLUE]/[/color] att atx blk bln ent enx hgt tag ) [color=GREEN];; If the following expression returns a non-nil value[/color] ([color=BLUE]if[/color] [color=GREEN];; If all of the following expressions return a non-nil value[/color] ([color=BLUE]and[/color] [color=GREEN];; Prompt the user to select an attribute[/color] ([color=BLUE]setq[/color] att ([color=BLUE]car[/color] ([color=BLUE]nentsel[/color] [color=MAROON]"\nSelect attribute: "[/color]))) [color=GREEN];; Retrieve the DXF data for the selected object[/color] ([color=BLUE]setq[/color] atx ([color=BLUE]entget[/color] att)) [color=GREEN];; Check that the selected object is indeed an attribute[/color] ([color=BLUE]=[/color] [color=MAROON]"ATTRIB"[/color] ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 0 atx))) [color=GREEN];; Evaluate the following expressions and return the value[/color] [color=GREEN];; returned by the last evaluated expression[/color] ([color=BLUE]progn[/color] [color=GREEN];; Restrict the following prompt to positive non-zero values[/color] ([color=BLUE]initget[/color] 6) [color=GREEN];; Prompt the user for a new text height[/color] ([color=BLUE]setq[/color] hgt ([color=BLUE]getdist[/color] [color=MAROON]"\nSpecify new attribute height: "[/color])) ) [color=GREEN];; end progn[/color] ) [color=GREEN];; end and[/color] [color=GREEN];; Then:[/color] [color=GREEN];; Evaluate the following expressions within a single 'progn'[/color] [color=GREEN];; expression constituting the THEN argument for the 'if' function[/color] ([color=BLUE]progn[/color] [color=GREEN];; Retrieve the attribute tag of the selected attribute[/color] ([color=BLUE]setq[/color] tag ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 002 atx)) [color=GREEN];; Retrieve the parent entity of the attribute reference (i.e. the block reference)[/color] blk ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 330 atx)) [color=GREEN];; Retrieve the block name (non-dynamic blocks only!)[/color] bln ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 002 ([color=BLUE]entget[/color] blk))) [color=GREEN];; Retrieve the corresponding block record entity for the block[/color] ent ([color=BLUE]tblobjname[/color] [color=MAROON]"block"[/color] bln) ) [color=GREEN];; end setq[/color] [color=GREEN];; While the following expression returns a non-nil value[/color] ([color=BLUE]while[/color] [color=GREEN];; Step through the entities following the block record entity in the database[/color] ([color=BLUE]setq[/color] ent ([color=BLUE]entnext[/color] ent)) [color=GREEN];; If the following expression returns a non-nil value[/color] ([color=BLUE]if[/color] [color=GREEN];; If all of the following expressions return a non-nil value[/color] ([color=BLUE]and[/color] [color=GREEN];; Retrieve the DXF data for the block component[/color] ([color=BLUE]setq[/color] enx ([color=BLUE]entget[/color] ent)) [color=GREEN];; Check that it's an ATTDEF[/color] ([color=BLUE]=[/color] [color=MAROON]"ATTDEF"[/color] ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 0 enx))) [color=GREEN];; Check that is has the correct tag[/color] ([color=BLUE]=[/color] tag ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 2 enx))) ) [color=GREEN];; end and[/color] [color=GREEN];; Modify the following DXF data in the drawing database[/color] ([color=BLUE]entmod[/color] [color=GREEN];; Substitute the following group[/color] ([color=BLUE]subst[/color] [color=GREEN];; Construct a dotted pair to form DXF group 40[/color] ([color=BLUE]cons[/color] 40 hgt) [color=GREEN];; For the existing DXF group 40 entry[/color] ([color=BLUE]assoc[/color] 40 enx) [color=GREEN];; Within the ATTDEF dxf data[/color] enx ) [color=GREEN];; end subst[/color] ) [color=GREEN];; end entmod[/color] ) [color=GREEN];; end if[/color] ) [color=GREEN];; end while[/color] [color=GREEN];; Use the ATTSYNC command to apply the changes to all references of the block[/color] ([color=BLUE]command[/color] [color=MAROON]"_.attsync"[/color] [color=MAROON]"_n"[/color] bln) ) [color=GREEN];; end progn[/color] [color=GREEN];; Else the initial conditions were not met[/color] [color=GREEN];; No real need to inform the user - they will know the error of their ways...[/color] ) [color=GREEN];; end if[/color] [color=GREEN];; Suppress the value returned by the last evaluated expression[/color] ([color=BLUE]princ[/color]) ) [color=GREEN];; end defun[/color] Given that I have donated my time to writing this code and commenting every line with an explanation, please take the time to carefully read the comments and study the code. Lee Quote Link to comment Share on other sites More sharing options...
Happy Hobbit Posted November 27, 2015 Author Share Posted November 27, 2015 Jeepers Lee! Christmas has come early for me, loads of comments added! Thank you very much indeed. I'd actually got slightly further than in my last post, realised that it's the block that needs the entmod & to use tblobjname to search within it for the relevant attribute., but that was as far as my thinking got really. It's far more than I was expecting & far more complex that I thought the code would be. I created a lisp to change text/mtext heights & it was a doddle, then I thought to add attribute capability.... Quote Link to comment Share on other sites More sharing options...
BIGAL Posted November 27, 2015 Share Posted November 27, 2015 it's not always easy to discern which attribute is which Just a bit more as an alternative you can change a attribute by its creation order rather than say nentsel & Tagname. Change 3rd attribute. You should be able to apply this to the block definition. ; Change attribute value by created position ; By Alan H (vl-load-com) (setq y 1) (setq ss1 (car (entsel))) (setq bname (vla-get-name(vlax-ename->vla-object SS1))) (setq x (getint "\nEnter line no to pick")) ; change this line in block (SETQ newstrblank ".") (foreach att (vlax-invoke (vlax-ename->vla-object SS1) 'getattributes) (if (= y x) (progn (setq newstr (vla-get-textstring att )) (vla-put-textstring att newstrblank) ) ) (setq y (+ Y 1)) ) (setq y 1) (setq x (getint "\nEnter line no to move to")) (foreach att (vlax-invoke (vlax-ename->vla-object SS1) 'getattributes) (if (= y x) (vla-put-textstring att newstr) ) (setq y (+ Y 1)) ) (princ) Quote Link to comment Share on other sites More sharing options...
Happy Hobbit Posted November 28, 2015 Author Share Posted November 28, 2015 Further to the earlier reply it occurred to me that if there were duplicate tags within the block (quite common, the numbties) then the wrong attribute may get changed. Therefore I thought that if the lisp were using the entity name, which is to the best of my knowlege unique, then it would not amend the wrong attribute. But I'm having trouble making the entity names match. I'll change the name of the symbol 'tag' to something more suitable later. This: (setq tag (cdr (assoc 002 atx)) To this: (setq tag (cdr (assoc -1 atx)) And this: (= tag (cdr (assoc 2 enx))) To this: (= tag (cdr (assoc -1 enx))) As you can see from the log watch, the values don't match (not equal) for: (= tag (cdr (assoc -1 enx))) ............... LOG Watch ............... ENT = <Entity name: 7ef047b8> ENX = ((-1 . <Entity name: 7ef047b8>) (0 . "ATTDEF") ~ ~ ~ TAG = <Entity name: 7ef04870> ............... But I'm working on it PS Thanks again for all the commented code, comments are priceless to me when trying something new. Quote Link to comment Share on other sites More sharing options...
Tharwat Posted November 28, 2015 Share Posted November 28, 2015 Did you see my reply ? Quote Link to comment Share on other sites More sharing options...
Happy Hobbit Posted November 28, 2015 Author Share Posted November 28, 2015 Yes I did & I apologise for not replying sooner Tharwat. I admit that I got a bit frightened off by all those VL functions, ordinary lisp I'm fairly comfortable with when it comes to tweaking it, but I don't yet know enough about visual lisp to feel confident enough to attempt a tweak. I've just tried your code & it works very well indeed. Please may I ask what criteria the lisp uses to pin-point which attribute is to have it's text height changed (Lee's uses the tag name). Quote Link to comment Share on other sites More sharing options...
Tharwat Posted November 28, 2015 Share Posted November 28, 2015 Yes I did & I apologise for not replying sooner Tharwat. No problem, and its okay. I admit that I got a bit frightened off by all those VL functions, ordinary lisp I'm fairly comfortable with when it comes to tweaking it, but I don't yet know enough about visual lisp to feel confident enough to attempt a tweak. It may is at the first glance but it would become easy to you by time if you keep on coding and readying about functions you need to use. Just ask if you have any question about my codes. I've just tried your code & it works very well indeed. Please may I ask what criteria the lisp uses to pin-point which attribute is to have it's text height changed (Lee's uses the tag name). I am also used the tag string / name to pick the needed tag string's height to be changed. Regards. Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted November 28, 2015 Share Posted November 28, 2015 ...it occurred to me that if there were duplicate tags within the block (quite common, the numbties) then the wrong attribute may get changed. Therefore I thought that if the lisp were using the entity name, which is to the best of my knowlege unique, then it would not amend the wrong attribute. Entity names & Object IDs are indeed unique within the active drawing session (but not persistent between drawing sessions - only handles are persistent). However, as noted earlier, the attribute selected by the user is the attribute reference (ATTRIB), whereas, the modifications are being performed on the attribute definition (ATTDEF), which is not the same entity. PS Thanks again for all the commented code, comments are priceless to me when trying something new. You're welcome - I'm glad it helps. Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted November 28, 2015 Share Posted November 28, 2015 Here is one possible method to account for duplicate tags: [color=GREEN];; Define function & declare local variables[/color] ([color=BLUE]defun[/color] c:atthgt ( [color=BLUE]/[/color] att atx blk bln cnt ent enx flg han hgt tag ) [color=GREEN];; If the following expression returns a non-nil value[/color] ([color=BLUE]if[/color] [color=GREEN];; If all of the following expressions return a non-nil value[/color] ([color=BLUE]and[/color] [color=GREEN];; Prompt the user to select an attribute[/color] ([color=BLUE]setq[/color] att ([color=BLUE]car[/color] ([color=BLUE]nentsel[/color] [color=MAROON]"\nSelect attribute: "[/color]))) [color=GREEN];; Retrieve the DXF data for the selected object[/color] ([color=BLUE]setq[/color] atx ([color=BLUE]entget[/color] att)) [color=GREEN];; Check that the selected object is indeed an attribute[/color] ([color=BLUE]=[/color] [color=MAROON]"ATTRIB"[/color] ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 0 atx))) [color=GREEN];; Evaluate the following expressions and return the value[/color] [color=GREEN];; returned by the last evaluated expression[/color] ([color=BLUE]progn[/color] [color=GREEN];; Restrict the following prompt to positive non-zero values[/color] ([color=BLUE]initget[/color] 6) [color=GREEN];; Prompt the user for a new text height[/color] ([color=BLUE]setq[/color] hgt ([color=BLUE]getdist[/color] [color=MAROON]"\nSpecify new attribute height: "[/color])) ) [color=GREEN];; end progn[/color] ) [color=GREEN];; end and[/color] [color=GREEN];; Then:[/color] [color=GREEN];; Evaluate the following expressions within a single 'progn'[/color] [color=GREEN];; expression constituting the THEN argument for the 'if' function[/color] ([color=BLUE]progn[/color] [color=GREEN];; Retrieve the attribute tag of the selected attribute[/color] ([color=BLUE]setq[/color] tag ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 002 atx)) [color=GREEN];; Retrieve the handle of the selected attribute (in case there are duplicate tags)[/color] han ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 005 atx)) [color=GREEN];; Retrieve the parent entity of the attribute reference (i.e. the block reference)[/color] blk ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 330 atx)) [color=GREEN];; Retrieve the block name (non-dynamic blocks only!)[/color] bln ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 002 ([color=BLUE]entget[/color] blk))) [color=GREEN];; Retrieve the corresponding block record entity for the block[/color] ent ([color=BLUE]tblobjname[/color] [color=MAROON]"block"[/color] bln) [color=GREEN];; Retrieve the first attribute reference entity[/color] att ([color=BLUE]entnext[/color] blk) [color=GREEN];; Retrieve the DXF data for the first attribute reference entity[/color] atx ([color=BLUE]entget[/color] att) [color=GREEN];; Initialise a counter variable[/color] cnt 0 ) [color=GREEN];; end setq[/color] [color=GREEN];; While the following expression returns a non-nil value[/color] ([color=BLUE]while[/color] [color=GREEN];; If all of the following expressions return a non-nil value[/color] ([color=BLUE]and[/color] [color=GREEN];; We haven't found the selected attribute reference[/color] ([color=BLUE]not[/color] flg) [color=GREEN];; While the entity is an attribute reference[/color] ([color=BLUE]=[/color] [color=MAROON]"ATTRIB"[/color] ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 0 atx))) ) [color=GREEN];; end and[/color] [color=GREEN];; If the attribute tag matches that of the selected attribute[/color] ([color=BLUE]if[/color] ([color=BLUE]=[/color] tag ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 2 atx))) [color=GREEN];; Then increment the counter variable[/color] ([color=BLUE]setq[/color] cnt ([color=BLUE]1+[/color] cnt) [color=GREEN];; Test whether the attribute reference handle matches that of the selected attribute[/color] flg ([color=BLUE]=[/color] han ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 5 atx))) ) [color=GREEN];; end setq[/color] ) [color=GREEN];; end if[/color] [color=GREEN];; Retrieve the next entity in the database[/color] ([color=BLUE]setq[/color] att ([color=BLUE]entnext[/color] att) [color=GREEN];; Retrieve the DXF data for the next entity in the database[/color] atx ([color=BLUE]entget[/color] att) ) [color=GREEN];; end setq[/color] ) [color=GREEN];; end while[/color] [color=GREEN];; While the following expression returns a non-nil value[/color] ([color=BLUE]while[/color] [color=GREEN];; Step through the entities following the block record entity in the database[/color] ([color=BLUE]setq[/color] ent ([color=BLUE]entnext[/color] ent)) [color=GREEN];; If the following expression returns a non-nil value[/color] ([color=BLUE]if[/color] [color=GREEN];; If all of the following expressions return a non-nil value[/color] ([color=BLUE]and[/color] [color=GREEN];; Retrieve the DXF data for the block component[/color] ([color=BLUE]setq[/color] enx ([color=BLUE]entget[/color] ent)) [color=GREEN];; Check that it's an ATTDEF[/color] ([color=BLUE]=[/color] [color=MAROON]"ATTDEF"[/color] ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 0 enx))) [color=GREEN];; Check that is has the correct tag[/color] ([color=BLUE]=[/color] tag ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 2 enx))) [color=GREEN];; And that it is the correct attribute (if duplicate tags exist)[/color] ([color=BLUE]zerop[/color] ([color=BLUE]setq[/color] cnt ([color=BLUE]1-[/color] cnt))) ) [color=GREEN];; end and[/color] [color=GREEN];; Modify the following DXF data in the drawing database[/color] ([color=BLUE]entmod[/color] [color=GREEN];; Substitute the following group[/color] ([color=BLUE]subst[/color] [color=GREEN];; Construct a dotted pair to form DXF group 40[/color] ([color=BLUE]cons[/color] 40 hgt) [color=GREEN];; For the existing DXF group 40 entry[/color] ([color=BLUE]assoc[/color] 40 enx) [color=GREEN];; Within the ATTDEF dxf data[/color] enx ) [color=GREEN];; end subst[/color] ) [color=GREEN];; end entmod[/color] ) [color=GREEN];; end if[/color] ) [color=GREEN];; end while[/color] [color=GREEN];; Use the ATTSYNC command to apply the changes to all references of the block[/color] ([color=BLUE]command[/color] [color=MAROON]"_.attsync"[/color] [color=MAROON]"_n"[/color] bln) ) [color=GREEN];; end progn[/color] [color=GREEN];; Else the initial conditions were not met[/color] [color=GREEN];; No real need to inform the user - they will know the error of their ways...[/color] ) [color=GREEN];; end if[/color] [color=GREEN];; Suppress the value returned by the last evaluated expression[/color] ([color=BLUE]princ[/color]) ) [color=GREEN];; end defun[/color] Quote Link to comment Share on other sites More sharing options...
Happy Hobbit Posted November 28, 2015 Author Share Posted November 28, 2015 Thanks for that Lee, I'll give it a try. When I started this post I expected someone to point out a simple error in my code. It's kinda snowballed! I think I've broken my vlide playing with bits of code today - it no longer reports global variables on a 'Check Text in Editor' :-( Quote Link to comment Share on other sites More sharing options...
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.