TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Coder on December 19, 2013, 02:55:56 AM

Title: Xref block
Post by: Coder on December 19, 2013, 02:55:56 AM
Hello guys .

Is there a way to not select Xref blocks with the ssget function ?

Code: [Select]
(setq bks (ssget '((0 . "INSERT")))) ;
Thanks in advance  :-)
Title: Re: Xref block
Post by: kruuger on December 19, 2013, 04:01:18 AM
Hello guys .

Is there a way to not select Xref blocks with the ssget function ?

Code: [Select]
(setq bks (ssget '((0 . "INSERT")))) ;
Thanks in advance  :)
yes. you need to create BlockNameFilter function:
Code: [Select]
(ssget (list (cons 0 "INSERT") (cd:009_BlockNameFilter)))
Code: [Select]
(defun cd:009_BlockNameFilter ()
  (cons 2 (strcat "`*U*," (cd:STR_ReParse (cd:SYS_CollList "BLOCK" (+ 2 4) ",")))
)
it is a part of this program:
http://cad.pl/ftp/Tools/009_RedefineBlockProperties/RedefineBlockProperties.lsp (http://cad.pl/ftp/Tools/009_RedefineBlockProperties/RedefineBlockProperties.lsp)


library functions are here (CadPack.lsp):
http://forum.cad.pl/cadpl-pack-v1-lsp-t78161.html (http://forum.cad.pl/cadpl-pack-v1-lsp-t78161.html)
kruuger
Title: Re: Xref block
Post by: Coder on December 19, 2013, 07:57:18 AM
Thank you kruuger for your help .

I lost with all these options that you posted  :embarrassed:

Can you please clarify ?
Title: Re: Xref block
Post by: ronjonp on December 19, 2013, 08:43:34 AM
Try this:
Code: [Select]
(defun _foo (/ b fltr)
  (setq fltr "")
  (while (setq b (tblnext "block" (not b)))
    (and (assoc 1 b) (setq fltr (strcat "~" (cdr (assoc 2 b)) "," fltr)))
  )
  (if (zerop (strlen fltr))
    "*"
    fltr
  )
)
(setq bks (ssget "_X" (list '(0 . "INSERT") (cons 2 (_foo)))))
Title: Re: Xref block
Post by: ribarm on December 19, 2013, 09:08:13 AM
Maybe this :

Code: [Select]
(defun c:ss~xref ( / ss adoc blks l )
  (vl-load-com)
  (setq adoc (vla-get-activedocument (vlax-get-acad-object)))
  (setq blks (vla-get-blocks adoc))
  (vlax-for item blks
    (if (eq (vla-get-isxref item) :vlax-true)
      (progn
        (setq l (cons (list item (vl-catch-all-apply 'vla-get-xrefdatabase (list item))) l))
        (vla-unload item)
      )
    )
  )
  (setq ss (ssget '((0 . "INSERT"))))
  (foreach item l
    (if (eq (type (cadr item)) 'vla-object)
      (vla-reload (car item))
    )
  )
  (sssetfirst nil ss)
  (princ)
)
Title: Re: Xref block
Post by: kruuger on December 19, 2013, 09:20:00 AM
Thank you kruuger for your help .

I lost with all these options that you posted  :oops:

Can you please clarify ?

if BL1, BL2, BL3 (xref), BL4, BL5 (xref) are dwg blocks
you need to go thru all all of them - omit xref's - and create string:
Code: [Select]
"BL1,BL2,BL4"and this one pass to ssget
Code: [Select]
(ssget (list (cons 0 "INSERT") (cons 2 "BL1,BL2,BL4")))k.
Title: Re: Xref block
Post by: Coder on December 20, 2013, 04:06:53 AM
Hello guys .

ron your codes didn't work .  :-(
ribarm your codes unloaded one xref only to allow me to select blocks but the other xrefblocks didn't unloaded and they included with the selection .

kruuger , your guide is ok and I hope I can make it .

Many thanks
Title: Re: Xref block
Post by: ronjonp on December 20, 2013, 08:29:21 AM
This seems to work:
Code: [Select]
(defun _foo (/ b fltr)
  (setq fltr "")
  (while (setq b (tblnext "block" (not b)))
    (and (assoc 1 b) (setq fltr (strcat (cdr (assoc 2 b)) "," fltr)))
  )
    fltr
)
(setq bks (ssget "_X" (list '(0 . "INSERT") '(-4 . "<NOT") (cons 2 (_foo)) '(-4 . "NOT>"))))
Title: Re: Xref block
Post by: ribarm on December 20, 2013, 09:02:19 AM
ribarm your codes unloaded one xref only to allow me to select blocks but the other xrefblocks didn't unloaded and they included with the selection .

I didn't notice that CAD won't unload all attached Xrefs, only what I thought was the issue was how to reload them back as previous state was... Here is my slight improvement ab that... On my CAD it unloads all Xrefs... But if it doesn't try my second code from this post...

Code: [Select]
(defun c:ss~xref ( / ss adoc blks )
  (vl-load-com)
  (setq adoc (vla-get-activedocument (vlax-get-acad-object)))
  (vla-startundomark adoc)
  (setq blks (vla-get-blocks adoc))
  (vlax-for item blks
    (if (eq (vla-get-isxref item) :vlax-true)
      (vla-unload item)
    )
  )
  (setq ss (ssget '((0 . "INSERT"))))
  (command "_.undo" "")
  (sssetfirst nil ss)
  (princ)
)

Code: [Select]
(defun c:ss~xref ( / ss adoc )
  (vl-load-com)
  (setq adoc (vla-get-activedocument (vlax-get-acad-object)))
  (vla-startundomark adoc)
  (command "_.-xref" "u" "*")
  (setq ss (ssget '((0 . "INSERT"))))
  (command "_.undo" "")
  (sssetfirst nil ss)
  (princ)
)
Title: Re: Xref block
Post by: Lee Mac on December 20, 2013, 07:03:02 PM
I would follow a similar route to Ron, but would probably write it like this:

Code - Auto/Visual Lisp: [Select]
  1. (
  2.     (lambda ( / a b )
  3.         (while (setq a (tblnext "block" (null a)))
  4.             (if (= 4 (logand 4 (cdr (assoc 70 a))))
  5.                 (setq b (vl-list* "," (cdr (assoc 2 a)) b))
  6.             )
  7.         )
  8.         (ssget "_X"
  9.             (cons '(0 . "INSERT")
  10.                 (if b
  11.                     (list
  12.                        '(-4 . "<NOT")
  13.                         (cons 2 (apply 'strcat (cdr b)))
  14.                        '(-4 . "NOT>")
  15.                     )
  16.                 )
  17.             )
  18.         )
  19.     )
  20. )

Ron, note that your code will not select anything if there are no xrefs present in the drawing  :wink:
Title: Re: Xref block
Post by: Coder on December 21, 2013, 08:02:56 AM
the last three posts work as i wanted , thank you all for your great help .  :-)
Title: Re: Xref block
Post by: ronjonp on December 21, 2013, 08:26:38 AM
I would follow a similar route to Ron, but would probably write it like this:

Code - Auto/Visual Lisp: [Select]
  1. (
  2.     (lambda ( / a b )
  3.         (while (setq a (tblnext "block" (null a)))
  4.             (if (= 4 (logand 4 (cdr (assoc 70 a))))
  5.                 (setq b (vl-list* "," (cdr (assoc 2 a)) b))
  6.             )
  7.         )
  8.         (ssget "_X"
  9.             (cons '(0 . "INSERT")
  10.                 (if b
  11.                     (list
  12.                        '(-4 . "<NOT")
  13.                         (cons 2 (apply 'strcat (cdr b)))
  14.                        '(-4 . "NOT>")
  15.                     )
  16.                 )
  17.             )
  18.         )
  19.     )
  20. )

Ron, note that your code will not select anything if there are no xrefs present in the drawing  ;)


I see that now  :oops:   **Fixed  :D


..  I wonder why the ~name1,~name2 filter would not work?
Title: Re: Xref block
Post by: MP on December 21, 2013, 11:50:08 AM
I wonder why the ~name1,~name2 filter would not work?

The pattern argument for wcmatching (I believe the underpinning for ssget filtering) functions akin to an 'OR', not an 'AND'.

For example, if you had inserts by the names of "A", "B" and "C" (ssget "x" '((0 . "insert")(2 . "~A,~B,~C"))) would actually select them all (because "A" matches "~B" etc).

In order to achieve what you intended you would have to do something like:

Code - Auto/Visual Lisp: [Select]
  1. (ssget "x"
  2.    '(   (0 . "insert")
  3.         (-4 . "<and")
  4.         (2 . "~A")
  5.         (2 . "~B")
  6.         (2 . "~C")
  7.         (-4 . "and>")
  8.     )
  9. )

Thus, given a variable xref_names that's self describing:

(interactive)
Code - Auto/Visual Lisp: [Select]
  1.     (append
  2.        '((0 . "insert")(-4 . "<and"))
  3.         (mapcar (function (lambda (x) (cons 2 (strcat "~" x)))) xref_names)
  4.        '((-4 . "and>"))
  5.     )
  6. )

(non-interactive)
Code - Auto/Visual Lisp: [Select]
  1. (ssget "x"
  2.     (append
  3.        '((0 . "insert")(-4 . "<and"))
  4.         (mapcar (function (lambda (x) (cons 2 (strcat "~" x)))) xref_names)
  5.        '((-4 . "and>"))
  6.     )
  7. )

Of course, only applicable if xrefs are actually present in the drawing, in the interests of brevity <something I'm not usually known for> that (pre)determination not detailed here.

All that said, the (-4 . "<not") ... (-4 . "not>") alternative Lee detailed would be the route I'd suggest -- just thought I'd respond to your "wonder why ..." musing.

Cheers. :)