Author Topic: How to select Block with attribute?  (Read 1849 times)

0 Members and 1 Guest are viewing this topic.

HasanCAD

  • Swamp Rat
  • Posts: 1422
How to select Block with attribute?
« on: February 11, 2013, 05:42:07 AM »
When select a block with attribute with field for block reference using this code
Code: [Select]
(setq sset (ssget "_X" '((0 . "INSERT") (2 . "XY-loc") (66 . 1))))Some times work and some time not work.

HasanCAD

  • Swamp Rat
  • Posts: 1422
Re: How to select Block with attribute?
« Reply #1 on: February 11, 2013, 05:46:57 AM »
Using
Code: [Select]
EntMaker CAB 03- MakeEntmake.lsp
Gives
Code: [Select]
(2 . "*U2")

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: How to select Block with attribute?
« Reply #2 on: February 11, 2013, 07:15:29 AM »
That looks like a dynamic block (since the name is an anonymous name). Search around for something called Effective Block names. If you want to stick with using a selection filter, then an option can be as per my select similar routine here: http://forums.augi.com/showthread.php?96601-Select-all-occurences-of-a-Dynamic-Block-in-Modelspace&p=946511&viewfull=1#post946511
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: How to select Block with attribute?
« Reply #3 on: February 11, 2013, 07:24:13 AM »
It looks as though your attributed blocks are dynamic.

When the dynamic properties (e.g. Linear Parameters, Visibility State etc.) of a dynamic block reference are altered, the reference becomes anonymous, with a block name of the form *U##. This is necessary since the appearance or geometry of the dynamic block reference may have changed and so the block must reference a new block definition containing this altered geometry (with everything linked back to the main dynamic block definition via dictionaries & xdata). Otherwise, if every block reference referenced the same block definition, all references would have identical geometry, as per a static block reference.

With this in mind, you will need to collect a selection set of all anonymous attributed block references in addition to those blocks matching your given block name, and then iterate over the selection set testing the effective name of each block reference. The effective name is the name of the original dynamic block definition for an anonymous dynamic block reference.

For example:
Code: [Select]
(if (setq sel (ssget "_X" '((0 . "INSERT") (2 . "`*U*,XY-LOC") (66 . 1))))
    (repeat (setq cnt (sslength sel))
        (setq obj (vlax-ename->vla-object (ssname sel (setq cnt (1- cnt))))
              bnm (if (vlax-property-available-p obj 'effectivename)
                      (vla-get-effectivename obj)
                      (vla-get-name obj)
                  )
        )
        (if (= "XY-LOC" (strcase bnm))
            ... do your thing ...
        )
    )
)

Alternatively, you can collect the anonymous block names for the relevant dynamic block references, and include only these anonymous block names in the selection set filter; I describe this alternative technique here.

EDIT: Irneb beat me to it ;)
« Last Edit: February 11, 2013, 07:32:27 AM by Lee Mac »

HasanCAD

  • Swamp Rat
  • Posts: 1422
Re: How to select Block with attribute?
« Reply #4 on: February 12, 2013, 04:14:13 AM »
Thanx irneb - LLE
comes in mind 2 ways
- Changing DXF Value. But the attriburte does not move
Code: [Select]
(if (setq sel (ssget "_X" '((0 . "INSERT") (2 . "`*U*,GM-Limits") (66 . 1)))) ; change dxf
  (progn
    (defun round (number by) (if (zerop by) number (+ (* (fix (/ number (setq by (abs by)))) by) (if (< (* 0.5 by) (rem number by)) by  0 ))))
    (setq entity (ssname sel 0))
    (setq dxfdata (entget entity))
    (setq old-dxf (assoc 10 dxfdata))
    (setq new-dxf (cons 10 (list (Round (nth 1 old-dxf) 5) (Round (nth 2 old-dxf) 5) 0.0)))
    (setq dxfdata (subst new-dxf old-dxf dxfdata))
    (entmod dxfdata))
  (princ "\nBlock ( Limits ) not found.")
  )

- Move the the block
Code: [Select]
(if (setq sel (ssget "_X" '((0 . "INSERT") (2 . "`*U*,GM-Limits") (66 . 1)))) ; move
  (progn
    (defun round (number by) (if (zerop by) number (+ (* (fix (/ number (setq by (abs by)))) by) (if (< (* 0.5 by) (rem number by)) by  0 ))))
    (setq entity (ssname sel 0))
    (setq dxfdata (entget entity))
    (setq old-dxf (assoc 10 dxfdata))
    (setq pnt1 (list        (nth 1 old-dxf)           (nth 2 old-dxf)           (nth 3 old-dxf)   ))
    (setq pnt2 (list (Round (nth 1 old-dxf) 5) (Round (nth 2 old-dxf) 5) (Round (nth 3 old-dxf) 5)))   
    (vl-cmdf "_.move" sel "" pnt1 pnt2)
    )
  (princ "\nBlock ( GM-Limits ) not found.")
)

Which one is better?
Is there a better way than these 2?

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: How to select Block with attribute?
« Reply #5 on: February 12, 2013, 04:18:50 PM »
Not sure if this is relevant, just a hit & run.  :-)
http://www.theswamp.org/index.php?topic=23944
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.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: How to select Block with attribute?
« Reply #6 on: February 12, 2013, 04:25:17 PM »
Using
Code: [Select]
EntMaker CAB 03- MakeEntmake.lsp
Gives
Code: [Select]
(2 . "*U2")
EntMaker Version 1.5 http://www.theswamp.org/index.php?topic=31145
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.