how to check if object exists? the get entity name

how to check if object exists? the get entity name

Anonymous
Not applicable
4,983 Views
34 Replies
Message 1 of 35

how to check if object exists? the get entity name

Anonymous
Not applicable

Lets say i need to put entity name to the database with vlax-ldata-put . How i can check if that object exist and if exist get enitity name ? It need ssget function ?

0 Likes
4,984 Views
34 Replies
Replies (34)
Message 2 of 35

Anonymous
Not applicable

Here is a code i writed 

 

(defun c:bcount ( / p1 b a n obj name)
 
	(setq p1 (getstring "\Name of Block : "))
	;get the name of the block
 
	(setq b (cons 2 p1))
	;construct a dotted pair - code 2 is for blocks
 
	(setq a (ssget "x" (list b)))
	;filter for the block name
      (progn
     )
	(if (/= a nil)
	;check if there are any blocks of that name
 
	   (progn
	   ;if there is…
 
		(setq n (sslength a))
	    (setq ent (entlast a))
		;count the number of blocks

		(alert (strcat "\nThere are " (itoa n)  " in the DataBase. Enity: " (itoa ent) ))

	    
		;display the result
 
	   );progn
	   ;if there are no blocks
 
		(alert "\nThere are none in the DataBase")
		;inform the user
 
	);if
 
   (princ)
 
)

 But it dont get entity name in alertbox

0 Likes
Message 3 of 35

dbroad
Mentor
Mentor

Your code does not appear to have any relationship to your request.  Your posted code scans a drawing to find out how many block references you have with a given name.

 

This code is another way to do the same thing as your code body.  I am not sure it is any better.

(IF (VL-CATCH-ALL-ERROR-P
      (VL-CATCH-ALL-APPLY
	'(LAMBDA (name)
	   (ALERT
	     (STRCAT "There are "
		     (ITOA (SSLENGTH (SSGET "x" (LIST (CONS 2 name)))))
		     "block references with the name "
		     name
		     ".")))
	(LIST (SETQ name (GETSTRING "\nBlock name: ")))))
  (ALERT
    (STRCAT "There are no block references to the block name "
	    name
	    ".")))

 

To answer your OP, if you are storing block names, then the above (or something similar ) could be used to establish whether references exist to a block name.  If you want to store the names of entities that are not block references, then use handles.  Every object has a handle.  Assuming that ename is an entity name.

(cdr(assoc 5 (entget ename))) returns its handle.

 

If you store a handle, then it is easy to determine the entity still exists.

 

(handent <handle>) will return nil if the entity doesn't exist.  Otherwise it will return the entity's ename.

 

If you store handles, try storing them in a dictionary rather than as ldata.

Architect, Registered NC, VA, SC, & GA.
0 Likes
Message 4 of 35

Anonymous
Not applicable

is there possible to do that with "tblsearch" function ?

(defun c:function ()

(while

(if (tblsearch "block" "block_name")
;get the entity name

)
)
(princ)

)

0 Likes
Message 5 of 35

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

is there possible to do that with "tblsearch" function ?

(defun c:function ()

(while

(if (tblsearch "block" "block_name")
;get the entity name
....


No.  That is about the definition of the Block, which can exist in the drawing even if there are no insertions of it.  An entity name is associated with an insertion of it.

Kent Cooper, AIA
0 Likes
Message 6 of 35

Anonymous
Not applicable

then what need to change in this code (below) that it automaticaly get count of specific block (in my case pickets) and entity name? When just pressing  function name in command line and all data saving to database with  "vlax-ldata-put" ? I searched all forums and didnt find out what more need

(IF (VL-CATCH-ALL-ERROR-P
      (VL-CATCH-ALL-APPLY
	'(LAMBDA (name)
	   (ALERT
	     (STRCAT "There are "
		     (ITOA (SSLENGTH (SSGET "x" (LIST (CONS 2 name)))))
		     "block references with the name "
		     name
		     ".")))
	(LIST (SETQ name (GETSTRING "\nBlock name: ")))))
  (ALERT
    (STRCAT "There are no block references to the block name "
	    name
	    ".")))

 

0 Likes
Message 7 of 35

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

then what need to change in this code (below) that it automaticaly get count of specific block (in my case pickets) and entity name? When just pressing  function name in command line and all data saving to database with  "vlax-ldata-put" ? I searched all forums and didnt find out what more need

 


I haven't used (vlax-ldata-put) and am minimally aware of the (...catch...) functions, so someone else may be better able to help with those parts.  But a question:  What do you mean by getting an entity name when the count of a specific Block can be more than one?  Do you want all the entity names [plural] of insertions of that Block?  Do you want any one entity name of an insertion of the Block?  Do you want a particular individual insertion, such as the first one, or the last one?  If you're looking for a single entity name, it is also possible to get the Block definition as an entity name with (tblobjname), but that is unrelated to a count of Block insertions, since it may be present with no insertions, so I don't think that can be what you want.

Kent Cooper, AIA
0 Likes
Message 8 of 35

Anonymous
Not applicable

Yes, all the entity names of insertions of that Block, (for example it should be block named "picket" in my situation)  i hope you understood me.

0 Likes
Message 9 of 35

hmsilva
Mentor
Mentor

@Anonymous wrote:

Yes, all the entity names of insertions of that Block, (for example it should be block named "picket" in my situation)  i hope you understood me.


Not the entity names, but the handles, if you need the enames, should be easy for you to modify

(vl-load-com)
(defun blk-p (DictName BlkName / ent i lst ss)
  (if (setq ss (ssget "_X" (list '(0 . "INSERT") (cons 2 BlkName))))
    (progn
      (setq lst "")
       (repeat (setq i (sslength ss))
	(setq ent (entget (ssname ss (setq i (1- i))))
	      lst (strcat lst (cdr (assoc 5 ent)) " ")
	)
         )
      (vlax-ldata-put DictName BlkName (vl-string-right-trim " " lst))
      (prompt (strcat "\n " (itoa (sslength ss)) " " BlkName " were added to " DictName " dictionary..."))
      )
    (prompt (strcat "\nNo " BlkName " was found in the dwg..."))
    )
  (princ)
  )

 

Usage

(blk-p "MyDict" "picket")

"MyDict" = your dictionary name

"picket" = your block name

 

To get the data

(vlax-ldata-get "MyDict" "piked")

 

I hope this helps

Henrique

EESignature

0 Likes
Message 10 of 35

Anonymous
Not applicable
Stupid question how to use this code ? (: just type in command line (blk-p "MyDict" "picket") ? or what ? And why need dictionary name ?
0 Likes
Message 11 of 35

hmsilva
Mentor
Mentor

@Anonymous wrote:
... how to use this code ? (: just type in command line (blk-p "MyDict" "picket") ? or what ? And why need dictionary name ?

As  I had previously said, load the code, enter at command line

(blk-p "MyDict" "picket")

if there are blocks named "picket" at your dwg, the blocks handles will be stored in a 'dictionary' named MyDict (or whatever name you prefer), in a 'key' named 'picket', only to make it easier to retrive the ldata with

(vlax-ldata-get "MyDict" "piked")

 

If you prefer to add ldata to an object, instead of a dictionary, it is also possible.

Read about vlax-data-put and vlax-ldata-get and should be easy for you to make necessary modifications.

 

I hope this helps

Henrique

 

EESignature

0 Likes
Message 12 of 35

Anonymous
Not applicable

Thank you very much it works (:

 

Some question about code

 

(vl-load-com)
(defun blk-p (DictName BlkName / ent i lst ss)
  (if (setq ss (ssget "_X" (list '(0 . "INSERT") (cons 2 BlkName)))) ;in this line it gets x coordinate? and block name?
    (progn
      (setq lst "")
       (repeat (setq i (sslength ss)) 
	(setq ent (entget (ssname ss (setq i (1- i)))) ;what happens here ?
	      lst (strcat lst (cdr (assoc 5 ent)) " ")
	)
         )
      (vlax-ldata-put DictName BlkName (vl-string-right-trim " " lst))
      (prompt (strcat "\n " (itoa (sslength ss)) " " BlkName " were added to " DictName " dictionary..."))
      )
    (prompt (strcat "\nNo " BlkName " was found in the dwg..."))
    )
  (princ)
  )
0 Likes
Message 13 of 35

pbejse
Mentor
Mentor

@Anonymous wrote:

Thank you very much it works (:

 

Some question about code

 

(vl-load-com)
(defun blk-p (DictName BlkName / ent i lst ss)
  (if (setq ss (ssget "_X" (list '(0 . "INSERT") (cons 2 BlkName)))) ;in this line it gets x coordinate? and block name?

In that line, it filters the selection set to select only the specified block name


@Anonymous wrote:

 

	(setq ent (entget (ssname ss (setq i (1- i)))) ;what happens here ?
	      lst (strcat lst (cdr (assoc 5 ent)) " ")
	)
         )
      

What happens there is, the function entget retrieves the entity data in a form of a list

 

((-1 . <Entity name: 7fffec243f0>)

(0 . "INSERT");<---- where this data are store, Entity type

(330 . <Entity name: 7ffff732a80>) (5 . "ECFFF") (100 . "AcDbEntity") (67 . 1) (410 . "C-111E")

(8 . "G-ANNO-SYMB");<--  Layer Name

(48 . 1.0) (100 . "AcDbBlockReference")

(2 . "narrow")<-- Block name

(10 213.877 475.369 0.0);<-- insertion point

and so on

 

HTH

Message 14 of 35

hmsilva
Mentor
Mentor

@Anonymous wrote:

Thank you very much it works (:

 


You're welcome, 2r33222
Glad I could help.


2r33222 wrote:

Some question about code

 


2r33222,

(setq ss (ssget "_X" (list '(0 . "INSERT") (cons 2 BlkName)))) ;in this line it gets x coordinate? and block name?

 ssget = function to create a selection set

"_X" = the selection set method, X is entire database

list = to create a list with to filter the desired object 

(0 . "INSERT") = associated with the dxf 0 is the object type, an  insert

(cons 2 BlkName) = dxf 2 is the insert name...

It will create a selectin set with all blocks inseted in the dwg, with the name we provided.

	(setq ent (entget (ssname ss (setq i (1- i)))) ;what happens here ?

In each loop it will set the variable i (index) with current value subtracted one (te initial value is the returned from 'sslength ss') , and get the entity name  from the selection set using the index and the ssname function, then sets the variable ent with the entity's definition data returned from the entget function...

 

EDIT: tooooo sloooowwww... Smiley Happy

         pBe already explained.

         Cheers pBe

 

I hope this helps

Henrique

 

EESignature

Message 15 of 35

pbejse
Mentor
Mentor

@hmsilva wrote:
EDIT: tooooo sloooowwww... Smiley Happy

         pBe already explained.

         Cheers pBe

 

I hope this helps

Henrique


I was wondering why the OP would assume the "X" coordinate have anythinbg to do with the selecton

 

Well for what its worth, you explained it better than i did Smiley Very Happy

 

Kudos to you my friend 🙂

 

 

0 Likes
Message 16 of 35

Anonymous
Not applicable

What happens there is, the function entget retrieves the entity data in a form of a list

 

((-1 . <Entity name: 7fffec243f0>)

(0 . "INSERT");<---- where this data are store, Entity type

(330 . <Entity name: 7ffff732a80>) (5 . "ECFFF") (100 . "AcDbEntity") (67 . 1) (410 . "C-111E")

(8 . "G-ANNO-SYMB");<--  Layer Name

(48 . 1.0) (100 . "AcDbBlockReference")

(2 . "narrow")<-- Block name

(10 213.877 475.369 0.0);<-- insertion point

and so on

 

HTH


 

So as i understood i can select which data i need ? 

by this line : lst (strcat lst (cdr (assoc 5 ent)) " ") i get ECFF, an entity name i will get with -1 number ? 

0 Likes
Message 17 of 35

pbejse
Mentor
Mentor

@Anonymous wrote:

 

So as i understood i can select which data i need ? 

by this line : lst (strcat lst (cdr (assoc 5 ent)) " ") i get ECFF, an entity name i will get with -1 number ? 


That is correct.

 

The correct syntax would be.  

(cdr (assoc 5 ent)) ;<--- it will give you "ECFF" a string value. or are you after somethng else with the strcat function?

 

Good for you 2r33222

0 Likes
Message 18 of 35

pbejse
Mentor
Mentor

Okay, i read thru the the whole thread and still dont understand exactly want you want to.

 

If we are to base our answers on your OP, IMO dbroad sums it all for you at post #3. 

 

You did ask for vlax-ldata-put, Hmsilva gave you a snippet for that to.

 

We're not really sure if you meant Block Name or Handle. What is the purpose of the routine you are wanting to write 2r33222?  Give us a good description and sample drawings if you may, that way we can give you better suggestions than what we have now.

 

I'm thinking want you need is a fairly simple code.  only thing is,  there's a disconnect between every other post you replied to.

 

Help us help you. 🙂

 

 

 

0 Likes
Message 19 of 35

hmsilva
Mentor
Mentor

@pbejse wrote:

...

 What is the purpose of the routine you are wanting to write 2r33222?  Give us a good description and sample drawings if you may, that way we can give you better suggestions than what we have now.

I'm thinking want you need is a fairly simple code.  only thing is,  there's a disconnect between every other post you replied to.

Help us help you. 🙂


1+

 

Henrique

EESignature

0 Likes
Message 20 of 35

Anonymous
Not applicable

@pbejse wrote:

Okay, i read thru the the whole thread and still dont understand exactly want you want to.

 

If we are to base our answers on your OP, IMO dbroad sums it all for you at post #3. 

 

You did ask for vlax-ldata-put, Hmsilva gave you a snippet for that to.

 

We're not really sure if you meant Block Name or Handle. What is the purpose of the routine you are wanting to write 2r33222?  Give us a good description and sample drawings if you may, that way we can give you better suggestions than what we have now.

 

I'm thinking want you need is a fairly simple code.  only thing is,  there's a disconnect between every other post you replied to.

 

Help us help you. 🙂

 

 

 


I dont understand exact diferences between handle and block name i quess 😕 If there get entity name its fine with that code. I just want to sure

Problem is i am beginner.. Sometimes difficult undertand me because i dont know how to ask.. Sorry for that its hard with me and thank you for your patience (:

0 Likes