Lisp to find XREF insertion point and use it for a new xref

Lisp to find XREF insertion point and use it for a new xref

Anonymous
Not applicable
1,851 Views
6 Replies
Message 1 of 7

Lisp to find XREF insertion point and use it for a new xref

Anonymous
Not applicable

Hello,

 

Hopefully someone can help me.

 

With Lisp I want to find the insertion point of an xref called "layout" and want to use these coordinates to insert a block called "demolition".

 

Thank you in advance.

 

0 Likes
Accepted solutions (2)
1,852 Views
6 Replies
Replies (6)
Message 2 of 7

Kent1Cooper
Consultant
Consultant
Accepted solution

Try something like [untested]:

 

(defun C:DALI (/ ss); = Demolition At Layout Insertion

  (if (setq ss (ssget "_X" '((0 . "INSERT") (2 . "layout"))))

    (command "_.insert" "demolition" "_none" (cdr (assoc 10 (entget (ssname ss 0)))) "" "" "")

  )

)

 

That assumes you don't have an ordinary Block also called "layout" in the drawing, and that you want "demolition" inserted at a scale of 1 and rotation of 0, and that "demolition" is not defined for only uniform scaling.

Kent Cooper, AIA
Message 3 of 7

Anonymous
Not applicable

Thanks!

 

You're correct and this works fine.

 

 

grx.

0 Likes
Message 4 of 7

john.uhden
Mentor
Mentor
Accepted solution

I spiffed up @Kent1Cooper's contribution a little so you don't have to worry about an ordinary block named "Layout."

 

(defun C:DALI ( / blocks block ss p); = "Demolition" At "Layout" Insertion
   (and
     (setq Blocks (vlax-get (vla-get-activedocument (vlax-get-acad-object)) 'blocks))
     (setq block (vla-item blocks "Layout"))
(or (/= (vlax-get block 'isxref) 0)
(prompt "\n'Layout' is the name of an ordinary block")
) (setq ss (ssget "_X" '((0 . "INSERT") (2 . "Layout")))) (setq p (cdr (assoc 10 (entget (ssname ss 0))))) (vl-cmdf "_.insert" "demolition" "_none" p "" "" "") ) )

But I think I left out checking for Model vs. Paper space.

@Kent1Cooper:  Do we need to maybe (trans p)?

John F. Uhden

Message 5 of 7

bijan.yazdaniAR2WN
Explorer
Explorer
that's lovely John.
just wondering if we could have a lisp to inset a block or xref in ucs coordinates without changing it to wcs

thx
Jon
0 Likes
Message 6 of 7

Kent1Cooper
Consultant
Consultant

@bijan.yazdaniAR2WN wrote:
.... if we could have a lisp to inset a block or xref in ucs coordinates without changing it to wcs ....

Yes, as @john.uhden questioned, I think there a (trans) function would be appropriate, to convert the WCS coordinates of the insertion point to the current UCS.  In my shorter version:

 

(defun C:DALI (/ ss); = Demolition At Layout Insertion

  (if (setq ss (ssget "_X" '((0 . "INSERT") (2 . "layout"))))

    (command "_.insert" "demolition" "_none"

            (trans (cdr (assoc 10 (entget (ssname ss 0)))) 0 1) "" "" ""

    )

  )

)

Kent Cooper, AIA
0 Likes
Message 7 of 7

john.uhden
Mentor
Mentor

My apologies.  I included some faulty code...

(setq Blocks (vlax-get (vla-get-activedocument (vlax-get-acad-object)) 'blocks))
(setq block (vla-item blocks "Layout"))

The vla-item method will return an error if the block name does not exist.  Whereas the pre-VisualLisp is simpler and returns nil if the block name does not exist:

(tblobjname "block" "<name>") 

John F. Uhden

0 Likes