Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

verify a (block by name) is or is not the last entity added to drawing.

11 REPLIES 11
SOLVED
Reply
Message 1 of 12
mid-awe
529 Views, 11 Replies

verify a (block by name) is or is not the last entity added to drawing.

Hi all,

 

I needed a sub-function that I could call with a blockname as an argument and get back a response of T or nil to verify if the blockname is or is not the last entity added to the drawing.

 

After texting different approaches I got it working like this:

 

(defun IsBlkLast (blkNm) (IF (= (CDR (ASSOC 2 (ENTGET (ENTLAST)))) blkNm) T NIL))

 I tried using:

 

(defun IsBlkLast (blkNm) (IF (= (VLA-GET-EFFECTIVENAME (VLAX-ENAME->VLA-OBJECT (ENTLAST))) blkNm) T NIL))

 But, for whatever reason beyond my understanding, it only worked while part of a longer function and only failed when called as a sub-function.

 

I'm just curious if anyone can shed some light on this for me. I suppose it wouldn't much matter, but I was hoping to use this universally for standard blocks as well as for dynamic blocks.

 

Thank you.

11 REPLIES 11
Message 2 of 12
BlackBox_
in reply to: mid-awe

 This seems to work for me:

 

(vl-load-com)

(defun IsBlkLast (blockName / e v)
;; Example: (IsBlkLast "SomeBlockName") (and (setq e (entlast)) (setq v (vlax-ename->vla-object e)) (= "AcDbBlockReference" (vla-get-objectname v)) (= blockName (vla-get-effectivename v)) ) )

 

 



"How we think determines what we do, and what we do determines what we get."

Message 3 of 12
mid-awe
in reply to: BlackBox_

Thank you. That is working well 🙂
Message 4 of 12
Kent1Cooper
in reply to: mid-awe


@mid-awe wrote:

....

I needed a sub-function that I could call with a blockname as an argument and get back a response of T or nil to verify if the blockname is or is not the last entity added to the drawing.

 

After texting different approaches I got it working like this:

 

(defun IsBlkLast (blkNm) (IF (= (CDR (ASSOC 2 (ENTGET (ENTLAST)))) blkNm) T NIL))

 I tried using:

 

(defun IsBlkLast (blkNm) (IF (= (VLA-GET-EFFECTIVENAME (VLAX-ENAME->VLA-OBJECT (ENTLAST))) blkNm) T NIL))

 But, for whatever reason beyond my understanding, it only worked while part of a longer function and only failed when called as a sub-function.

....


I would think you could omit the (if)'s with the T and nil at the ends, and just do:

 

(defun IsBlkLast (blkNm) (= (CDR (ASSOC 2 (ENTGET (ENTLAST)))) blkNm))

 

and similarly for the other one -- that should return T or nil without your specifying those as return values.

 

But as for the problem itself, could it be something like an input error, such as entering the Block name without the double-quotes it should have?  If you typed in, for instance:

 

(IsBlkLast Toilet)

 

instead of

 

(IsBlkLast "Toilet")

 

it would not work right.  But inside the larger routine, it might work right if the Block name is being derived or extracted in some way, rather than entered by the User.  It's hard to say without seeing the larger routine.

Kent Cooper, AIA
Message 5 of 12
mid-awe
in reply to: Kent1Cooper

That is very Likely! I say that because in the long function I sent the blockname with a variable, but one I moved it out side into a sub-function I needed to send the name out right.

I assume that I was trying to use the variable as a global when it wasn't.

Thank you for the insights.
Message 6 of 12
alanjt_
in reply to: mid-awe

(defun _isEntlastBlockName (blockName / o)
  (and (setq o (entlast))
       (eq (vla-get-objectname (setq o (vlax-ename->vla-object o))) "AcDbBlockReference")
       (eq (strcase (if (vlax-property-available-p o 'EffectiveName)
                      (vla-get-effectivename o)
                      (vla-get-name o)
                    )
           )
           (strcase blockName)
       )
  )
)

 

Message 7 of 12
Kent1Cooper
in reply to: Kent1Cooper


@Kent1Cooper wrote:
....
.... could it be something like an input error, such as entering the Block name without the double-quotes it should have?  .....

And another possibility occurred to me:  not having the upper- and lower-case combination correct, since (=) is case-sensitive.  That can be prevented by applying (strcase) to both strings.

Kent Cooper, AIA
Message 8 of 12
BlackBox_
in reply to: Kent1Cooper


@Kent1Cooper wrote:

@mid-awe wrote:

....

I needed a sub-function that I could call with a blockname as an argument and get back a response of T or nil to verify if the blockname is or is not the last entity added to the drawing.

 

After texting different approaches I got it working like this:

 

(defun IsBlkLast (blkNm) (IF (= (CDR (ASSOC 2 (ENTGET (ENTLAST)))) blkNm) T NIL))

 I tried using:

 

(defun IsBlkLast (blkNm) (IF (= (VLA-GET-EFFECTIVENAME (VLAX-ENAME->VLA-OBJECT (ENTLAST))) blkNm) T NIL))

 But, for whatever reason beyond my understanding, it only worked while part of a longer function and only failed when called as a sub-function.

....


I would think you could omit the (if)'s with the T and nil at the ends, and just do:

 

(defun IsBlkLast (blkNm) (= (CDR (ASSOC 2 (ENTGET (ENTLAST)))) blkNm))


Neat idea, Kent... Normally I avoid Entget calls that are dependent on a user selected eName, such as:

 

(cdr (assoc 2 (entget (car (entsel)))))

 ... For the obvious reason of mitigating potential error, but as Entlast will always return a valid eName, this works. Nicely done, my friend..

 


@alanjt_ wrote:
(defun _isEntlastBlockName (blockName / o)
  (and (setq o (entlast))
       (eq (vla-get-objectname (setq o (vlax-ename->vla-object o))) "AcDbBlockReference")
       (eq (strcase (if (vlax-property-available-p o 'EffectiveName)
                      (vla-get-effectivename o)
                      (vla-get-name o)
                    )
           )
           (strcase blockName)
       )
  )
)

 


Interesting AT... I've never seen an AcDbBlockReference without an EffectiveName before. I'd be interested to see a sample drawing if it's something commonplace that I've just been oblivious to.

 

Cheers



"How we think determines what we do, and what we do determines what we get."

Message 9 of 12
BlackBox_
in reply to: Kent1Cooper


@Kent1Cooper wrote:

@Kent1Cooper wrote:
....
.... could it be something like an input error, such as entering the Block name without the double-quotes it should have?  .....

And another possibility occurred to me:  not having the upper- and lower-case combination correct, since (=) is case-sensitive.  That can be prevented by applying (strcase) to both strings.


 

I really dislike Autodesk's forums inability to correct mistakes, update code, etc. of our own posts (beyond the initial timeframe), as (all?) other CAD forums do.

 

/OffTopic



"How we think determines what we do, and what we do determines what we get."

Message 10 of 12
alanjt_
in reply to: BlackBox_


@BlackBoxCAD wrote:

Interesting AT... I've never seen an AcDbBlockReference without an EffectiveName before. I'd be interested to see a sample drawing if it's something commonplace that I've just been oblivious to.

 

Cheers


As far as I can remember, it dates back to versions before annotative blocks. 2006 and back, maybe?

Message 11 of 12
BlackBox_
in reply to: alanjt_

Well that makes sense then; I was politely educated yesterday that there are many users still using +/-2004 for daily production, and I didn't realize that was an unavailable Property back then (thereabouts).

 

Always appreciate the clarification, AT... Cheers



"How we think determines what we do, and what we do determines what we get."

Message 12 of 12
alanjt_
in reply to: BlackBox_

Any time. 🙂

Yeah, not every one has a need to upgrade. I have a few surveyor friends that are still running 2002 because for what they do, there's no need to upgrade. Every thing runs fine, their clients are happy and they don't have to kill themselves buying newer machines.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost