Author Topic: How to identify blocks starting with "Window_"?  (Read 2140 times)

0 Members and 1 Guest are viewing this topic.

MeasureUp

  • Bull Frog
  • Posts: 462
How to identify blocks starting with "Window_"?
« on: June 20, 2016, 09:59:05 PM »
In my drawing there are blocks named as "Window_North", "Window_East", "Window_West" and "Window_South". With "ssget" method, how to give a conditional function and make the following selection works?
Thanks for your help.

Code: [Select]
(while (not (ssget ":S:E")) ; <= user has to select any blocks which name starting with "Window"
(princ "\nYou need to select a window block.")
)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: How to identify blocks starting with "Window_"?
« Reply #1 on: June 20, 2016, 10:35:23 PM »
Experiment with this:
Code: [Select]
(while (not (ssget ":S:E" '((2 . "Window_*")))) ; <= user has to select any blocks which name starting with "Window"
   (princ "\nYou need to select a window block.")
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

MeasureUp

  • Bull Frog
  • Posts: 462
Re: How to identify blocks starting with "Window_"?
« Reply #2 on: June 20, 2016, 11:25:52 PM »
Thanks CAB.
The "*" does the trick.

MeasureUp

  • Bull Frog
  • Posts: 462
Re: How to identify blocks starting with "Window_"?
« Reply #3 on: June 21, 2016, 03:10:19 AM »
I am going to get an attribute value from a block and paste it in to an attribute in another block.
Here is an example:
In the target block the attribute value is to be replaced to 1000 from 500, or what ever it was.
But I cannot figure it out.
The details are shown below. Thanks for your help.

Source block name: OldWindow
Source attribute name: OldWindow_FrameHeight
The list:
'((OldWindow_FrameHeight . 1000) ...)

Target block name: Window
Target attribute name: Window_FrameHeight
The list:
'((Window_FrameHeight . 500) ...)

MeasureUp

  • Bull Frog
  • Posts: 462
Re: How to identify blocks starting with "Window_"?
« Reply #4 on: June 21, 2016, 08:28:42 PM »
I use a function "get_att" to get the elist of the target block:
Code: [Select]
(defun get_att (enm / att obj)
(if (or (and (eq (type enm) 'ENAME)
(setq obj (vlax-ename->vla-object enm))
(eq (vla-get-hasattributes obj) :vlax-true)
); end of and
(and (eq (type enm) 'VLA-OBJECT)
(setq obj enm)
(eq (vla-get-hasattributes obj) :vlax-true)
); end of and
); end of or
(mapcar '(lambda (att)
(cons (vla-get-TagString att) (vla-get-TextString att))
); end of lambda
(vlax-invoke obj "GetAttributes")
); end of mapcar
); end of if
)

Then following the HELP for "entomb" I have these lines (I use "ssget" to select the block):
Code: [Select]
(setq Window (vlax-ename->vla-object (ssname Window 0)))
(setq ed (get_att Window))
(setq ed (subst (cons "Window_FrameHeight" 1000) (assoc "Window_FrameHeight" ed) ed ))
(entmod ed)

It returns an error message:
error: bad DXF group: ("Window_FrameHeight" . 1000)

Can you help please? Thanks again.

danallen

  • Guest
Re: How to identify blocks starting with "Window_"?
« Reply #5 on: June 22, 2016, 12:13:48 AM »
I don't know the answer but Lee's routine copies between atrributes
http://www.lee-mac.com/lisp/html/CopySwapTextV1-6.html

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: How to identify blocks starting with "Window_"?
« Reply #6 on: June 22, 2016, 01:34:43 AM »
Hi,

You should have a look at the getpropertyvalue and setpropertyvalue functions (requires A2012+).
These functions see an attribute tag (string) as a property of the block reference:
Code - Auto/Visual Lisp: [Select]
  1. (setpropertyvalue
  2.   targetBlockEname
  3.   "Window_FrameHeight"
  4.   (getpropertyvalue sourceBlockEname "Window_FrameHeight")
  5. )
Speaking English as a French Frog

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: How to identify blocks starting with "Window_"?
« Reply #7 on: June 22, 2016, 04:42:07 AM »
If you can't (AutoCAD prior 2012) or don't want to use the upper functions, the following ones work the same:

[EDIT: corrected a typo, see reply #8]

Code - Auto/Visual Lisp: [Select]
  1. ;; gc:GetAttValue
  2. ;; Returns the attribute value or nil if not found.
  3. ;;
  4. ;; Arguments
  5. ;; blk: the block reference (ENAME)
  6. ;; tag: the attribute tag (STR)
  7. (defun gc:GetAttValue (blk tag / elst val)
  8.   (setq tag  (strcase tag)
  9.         elst (entget (entnext blk))
  10.   )
  11.   (while (and (= (cdr (assoc 0 elst)) "ATTRIB") (not val))
  12.     (if (= (cdr (assoc 2 elst)) tag)
  13.       (setq val (cdr (assoc 1 elst)))
  14.       (setq elst (entget (entnext (cdr (assoc -1 elst)))))
  15.     )
  16.   )
  17.   val
  18. )
  19.  
  20. ;; gc:SetAttValue
  21. ;; Sets the attribute value
  22. ;; Returns the value or nil if not found.
  23. ;;
  24. ;; Arguments
  25. ;; blk: the block reference (ENAME)
  26. ;; tag: the attribute tag (STR)
  27. ;; val: the new attribute value (STR)
  28. (defun gc:SetAttValue (blk tag val / elst done)
  29.   (setq tag  (strcase tag)
  30.         elst (entget (entnext blk))
  31.   )
  32.   (while (and (= (cdr (assoc 0 elst)) "ATTRIB") (not done))
  33.     (if (= (cdr (assoc 2 elst)) tag)
  34.       (entmod (subst (cons 1 (setq done val)) (assoc 1 elst) elst))
  35.       (setq elst (entget (entnext (cdr (assoc -1 elst)))))
  36.     )
  37.   )
  38.   done
  39. )

PS: The get_att function does not return the "elist of the target block". It returns an association list of the attributes as dotted pairs of type (tag . value). Maybe the author of this function also wrote a set_att one...
Anyway block attributes aren't part of the block reference DXF list (only a (66 . 1) group indicates attributes following). Attribute references are separated entities you can get with entnext as shown upper.
« Last Edit: June 22, 2016, 06:10:03 AM by gile »
Speaking English as a French Frog

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: How to identify blocks starting with "Window_"?
« Reply #8 on: June 22, 2016, 06:01:19 AM »
If you can't (AutoCAD prior 2012) or don't want to use the upper functions, the following ones work the same:
Code: [Select]
... (setq tag  (strcat tag) ...
Maybe (strcase tag)?

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: How to identify blocks starting with "Window_"?
« Reply #9 on: June 22, 2016, 06:08:10 AM »
If you can't (AutoCAD prior 2012) or don't want to use the upper functions, the following ones work the same:
Code: [Select]
... (setq tag  (strcat tag) ...
Maybe (strcase tag)?
Oopss!...
Thanks Marc.
Speaking English as a French Frog

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: How to identify blocks starting with "Window_"?
« Reply #10 on: June 22, 2016, 07:54:14 AM »
If you can't (AutoCAD prior 2012) or don't want to use the upper functions, the following ones work the same:
Code: [Select]
... (setq tag  (strcat tag) ...
Maybe (strcase tag)?
Oopss!...
Thanks Marc.
Prego.  :-)

MeasureUp

  • Bull Frog
  • Posts: 462
Re: How to identify blocks starting with "Window_"?
« Reply #11 on: June 29, 2016, 03:09:32 AM »
I now have a chance to fix my code after busying at work.
Thanks to gile, Marc & Dan.
And yes gile is right. It needs a "set attribute value" function.