Jump to content

How to get block name By Autolisp?


fathihvac

Recommended Posts

  • Replies 24
  • Created
  • Last Reply

Top Posters In This Topic

  • fathihvac

    9

  • pBe

    6

  • dbroada

    2

  • troggarf

    2

Top Posters In This Topic

Posted Images

 (defun c:GetBlkName  (/ ent)
[b][color=blue](vl-load-com)[/color][/b]
     (cond
           ((and
                  (setq ent (car (entsel "\nSelect Block Entity: ")))
                  (eq (cdr (assoc 0 (entget ent))) "INSERT")
                  (alert (strcat "Block Name:"
                                 (vla-get-effectivename
                                       (vlax-ename->vla-object ent)))))
            )))

 

(cdr (assoc 2 (entget ent)));;<---[i]ent[/i] as ename
(vla-get-effectivename (vlax-ename->vla-object ent));<---[i]ent [/i]as vla object

Edited by pBe
add (vl-load-com)
Link to comment
Share on other sites

THIS

 (defun c:GetBlkName  (/ ent)
     (cond
           ((and
                  (setq ent (car (entsel "\nSelect Block Entity: ")))
                  (eq (cdr (assoc 0 (entget ent))) "INSERT")
                  (alert (strcat "Block Name:"
                                 (vla-get-effectivename
                                       (vlax-ename->vla-object ent)))))
            )))

works for me.

Link to comment
Share on other sites

I still got the error

Select Block Entity: ; error: no function definition: VLAX-ENAME->VLA-OBJECT

 

Are you sure? :)

 

See first Edited post

Link to comment
Share on other sites

Are you sure? :)

 

See first Edited post

 

The code is missing

(vl-load-com)

Now that's ok Many Thanks.

Can modify it to insert the block name as text string in the drawing area?

Link to comment
Share on other sites

 
(defun c:GetBlkName  (/ ent)
(vl-load-com)
[color=blue](defun _MText ( point text)[/color]
[color=blue] (entmakex[/color]
[color=blue]   (list[/color]
[color=blue]     (cons 0 "MTEXT")[/color]
[color=blue]     (cons 100 "AcDbEntity")[/color]
[color=blue]     (cons 100 "AcDbMText")[/color]
[color=blue]     (cons 7 (getvar 'Textstyle))[/color]
[color=blue]     (cons 10 point)[/color]
[color=blue]     (cons 11 (getvar 'UCSXDIR))[/color]
[color=blue]     (cons 1  text)[/color]
[color=blue]           )[/color]
[color=blue] )[/color]
 )      
     (cond
           ((and
                  (setq ent (car (entsel "\nSelect Block Entity: ")))
                  (eq (cdr (assoc 0 (entget ent))) "INSERT")
                 [color=red](setq STR (vla-get-effectivename
                                       (vlax-ename->vla-object ent)))
[/color]                  [color=blue] (Setq pt (getpoint "\nPick Point to place Text: "))[/color]
[color=blue]                (_Mtext pt str )[/color]
            )))
     )

The Text created by this routine will depend on your current Text Style.

Other factors to consider are:

-Text Justification

-Layer

-Rotation

-Annotative or not

 

Hope this helps

Edited by pBe
Remove Alert
Link to comment
Share on other sites

Here's a routine I found that lets you select a block and then place the name of the block as text.

Click HERE to see it in action

 

;; BN.LSP Control Systems & Services, Inc.
;; Visit me at: http://www.bobscadshop.com

(DEFUN C:BN ()
; comment out next 2 lines if you don't want a point at the insertion point

(setq bn "" chk nil); set block name nil
(while (= chk nil)
(setq chk (entsel "\nPick a Block: "))
); end while

(setq pp (cadr chk))
(setq chk (entget (car chk)))
(setq bn (cdr (assoc 2 chk))); get block name
(setq ip (cdr (assoc 10 chk))); get insertion point

(setq A (car (getvar "LIMMAX")))
(if (> A 40)
(setq TSIZ "0.5")
(setq TSIZ "0.1875")
); end if

(setq ipt (getpoint "\nPlace Block Name Where
: "))
(if (/= bn "")
(command "_text" "_c" ipt TSIZ "0" bn); apply block name
); end if
(if (= bn "")
(prompt "\nCannot find the Blockname ?? : ")
); end if

; comment out next line if you don't want a point at the insertion point

(setq ip nil bn nil chk nil pp nil A nil ipt nil); cleanup
(princ)
); end function

Link to comment
Share on other sites

Oops,

Posted the wrong one.

Here is the one that is featured on my blog.

; places name of block as text via pick

(defun C:blockn()

(setvar "cmdecho" 0)

(setq pt(cadr(entsel"\nSelect Block:")))

(setq e1(ssget pt))

(setq e2 (entget (ssname e1 0)))

(setq blname (cdr(assoc 2 e2)))

(setq pt1 (getpoint"\nSelect point for block title:"))

(command "text" pt1 "" 0 blname)

)

Link to comment
Share on other sites

That's ok Thanks

Could you make it so it insert the block name as text string to the drawing area?

 

Try this, but it gives an error if selected entity is not a block. I don't know where to put the "while & not" functions.

 

 

(defun c:test  (/ ent point)

 (cond
   ((and
      (setq ent (car (entsel "\nSelect Block Entity: ")))
      (eq (cdr (assoc 0 (entget ent))) "INSERT")
      (setq point (getpoint "\nSelect insertion point  :"))			
   ))
 )

(entmakex
  (list
    (cons 0 "TEXT")
    (cons 7  (getvar 'TEXTSTYLE))
    (cons 40 
    (cons 8 "0") ;LAYER
    (cons 10 point)
    (cons 1 (cdr (assoc 2 (entget ent))))
    (cons 41 1)
    (cons 50 0)                 
  )
)

(princ)
)

Link to comment
Share on other sites

Try this, but it gives an error if selected entity is not a block. I don't know where to put the "while & not" functions.

 

...(cons 1 (cdr (assoc 2 (entget ent)))).... ;<--- if the selected entity is not a block this will error as variable [i]ent [/i]doesnt have the DXF group code 2

 

You can wrap entmakex inside the (cond ((and ...))). that way if (eq (cdr (assoc 0 (entget ent))) "INSERT") is nil there is no reason for the program to continue then it will stop evaluating the next condition.

 

now using IF outside the cond wrap, you can use this (if point (entmakex.....)) , only if variable point is not nil that this expression willl evaluate, since the previous condition only allows the evaluation of point variable after the first two conditions are met.

 

Hope this helps

Edited by pBe
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...