Author Topic: Updating tag within BLOCKABC block depending on 3rd character in filename value  (Read 1713 times)

0 Members and 1 Guest are viewing this topic.

kameron1967

  • Guest
Hi,

If I have a block called 3042RD and it has an APPROV tag name that I want it to put the following as its default value depending on the 3rd character in the filename.

So for XXE - it would be AB

for XXH - it would be CD

For XXM - it would be EF, etc.

Thanks!

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Look to use ssget to create a SelectionSet of blocks with name 3042RD (DXF Group 2), iterate through this SelectionSet (plenty of examples of how to do this on the forum) and, for each block in the set, iterate through its attributes (again, examples on the forum or on my site here).

If an attribute with Tag String (DXF Group 2 of Attribute) equal to "APPROV" is found, update the attribute with the correct value (DXF Group 1 of Attribute).

To determine the value to use: look at the DWGNAME System Variable (use the getvar function for this) and use the substr function to get the correct character.
« Last Edit: February 02, 2011, 10:49:59 AM by Lee Mac »

kameron1967

  • Guest
Thanks, Lee.  I'll look around and see what I can come up.

kameron1967

  • Guest
Lee - this is something that someone sent me, but I'm getting an error.  Can you look into it and tell me what's missing?

Code: [Select]
(defun fun ()
(setq 3char (strcase (substr (getvar "DWGNAME") 3 1)))
(setq atts nil ss nil)
(setq ss (ssget "x" '((0 . "INSERT") (2 . "3042RD"))))
(setq i (sslength ss))
(while (not (minusp (setq i (1- i))))
(setq en (ssname ss i))
;; Convert entity to Object
(setq vlaObj (vlax-ename->vla-object en))
(setq newli (cond ((= 3char "E") (list (cons "APPROV" "AB")))
    ((= 3char "H") (list (cons "APPROV" "CD")))
    ((= 3char "M") (list (cons "APPROV" "EF")))))

(VxSetAtts vlaObj newli )
)
)

[\code]

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Well there's quite a bit wrong with it: on first glance I see no variable localisation, no checking for a valid selection set, you're missing a subfunction named 'VxSetAtts', there's no clean exit...

But seeing that you don't seem to actually want to learn to write the code yourself, as is clearly demonstrated here and also in your other thread; save me writing the whole code for you, I feel it would probably be a waste of my time to critique/comment on code that someone else has written for you.
« Last Edit: February 02, 2011, 05:44:55 PM by Lee Mac »

fixo

  • Guest
Agreed completely with Lee
Your code is working good on my end
Just a quick revision:
I've changed the function on command it's likes me more
I've added (vl-load-com) to load ActiveX library and added
the check on  validof selection set
And also you might want to use local variables in your routine
(follows after the slash within the brackets after defun)

Code: [Select]

(defun C:myfun (/ 3char atts en i newli ss vlaObj)
   (vl-load-com)
(setq 3char (strcase (substr (getvar "DWGNAME") 3 1)))
(setq atts nil ss nil)
  (if
(setq ss (ssget "x" '((0 . "INSERT") (2 . "3042RD"))))
    (progn
(setq i (sslength ss))
(while (not (minusp (setq i (1- i))))
(setq en (ssname ss i))
;; Convert entity to Object
(setq vlaObj (vlax-ename->vla-object en))
(setq newli (cond ((= 3char "E") (list (cons "APPROV" "AB")))
  ((= 3char "H") (list (cons "APPROV" "CD")))
  ((= 3char "M") (list (cons "APPROV" "EF")))))


(VxSetAtts vlaObj newli )
)
)
    (alert "No such blocks in the document")
)
  (princ)
  )

kameron1967

  • Guest
Thanks, Fixo.  I was able to get it to work after I added the subroutine that Lee was kind enough to point out.