Get ObjectId of a non selected object

Get ObjectId of a non selected object

msarqui
Collaborator Collaborator
2,529 Views
4 Replies
Message 1 of 5

Get ObjectId of a non selected object

msarqui
Collaborator
Collaborator

Hello guys

 

 

I made this routine (with a lot of learning from this forum) to create a field to link two attributes.

 

 

(defun c:CreateField ( / nsel ent objid nsel2)
(setq nsel1 (nentsel "\nSelect the origin block"))
(setq ent (entget (car nsel1)))
(setq objid (vla-get-ObjectId (vlax-ename->vla-object (car nsel1))))
(setq nsel2 (car (nentsel "\nSelect the destination block")))
(vla-put-textstring (vlax-ename->vla-object nsel2)
(strcat "%<\\AcObjProp Object(%<\\_ObjId " (itoa objid) ">%).TextString>%")
)
(command "regen")
(princ)
)

 

Lets say I have a block named TITLE, with two attributes named HEAD and TAIL. Just for information, in the HEAD I put a letter and in the TAIL I put a number.

In a drawing, I have two or more insertions of this block. Using my routine above, I create a field to link HEAD from the first block to HEAD from the second block. After this, I repeat the operation in the same way to create a link between TAIL to TAIL.

Now I am wondering, and here it comes the tricky question...Is it possible to select only one attribute to create both links? I mean, how can I get the ObjectID of two objects, If I select only one? 

 

Thanks,

Marcelo

 

 

0 Likes
Accepted solutions (1)
2,530 Views
4 Replies
Replies (4)
Message 2 of 5

Ajilal.Vijayan
Advisor
Advisor

Hi,

Try with this code.

I have used the Attribute Functions from Lee Mac website.

And modified the function (LM:vl-getattributes) to (LM:vl-getobjectids) to return the objectid of attributes along with Tags.

 

Spoiler
(defun c:test ( / blk blk2 lst lst2)

(setq blk (entget (car (entsel "\nSelect the origin block:"))))
(setq blk2 (entget (car (entsel "\nSelect the destination block:"))))
(setq blk (vlax-ename->vla-object (cdr(car blk))))
(setq blk2 (vlax-ename->vla-object (cdr(car blk2))))

;get the list of Attributes with ObjectID and Tag (setq lst (LM:vl-getobjectids blk))
;Create a new list to change the objetid to add the field value (setq lst2 (mapcar '(lambda (b) (cons (car b) (strcat "%<\\AcObjProp Object(%<\\_ObjId " (itoa(cdr b)) ">%).TextString>%"))) lst))
;Apply the new list to Destination block (LM:vl-setattributevalues blk2 lst2)
(command "regen") );defun (defun LM:vl-setattributevalues ( blk lst / itm ) (foreach att (vlax-invoke blk 'getattributes) (if (setq itm (assoc (vla-get-tagstring att) lst)) (vla-put-textstring att (cdr itm)) ) ) ) (defun LM:vl-getobjectids ( blk ) (mapcar '(lambda ( att ) (cons (vla-get-tagstring att) (vla-get-ObjectId att))) (vlax-invoke blk 'getattributes) ) )
Message 3 of 5

msarqui
Collaborator
Collaborator

Hello Ajilal.Vijayan

 

Thanks for the answer. It works perfectly!
I would like to ask you one question. I revised my blocks library and I saw that I will also have a block named TITLE with 4 attributes (instead of 2 like I said before). So, the attributes will be HEAD, TAIL, UPINFO and DOWNINFO (In a rare case I will have more than 4 attributes, but HEAD and TAIL will be always there). Now, how can your routine be modified to make the link only between HEAD to HEAD and TAIL to TAIL, regardless any other attribute?

My first shot would be to retrieve (filter) this information in the lst from here:

 

(setq lst (LM:vl-getobjectids blk)) 

The problem is, I do not know how to to...

 

Last thing, If you do not mind... Could it be performed by nentsel instead of entsel? It is because the routine that I posted here was only a resume. My original routine has more things and they are all perform by having a nentsel selection. Sorry, I am still learning to code on lisp... Smiley Embarassed

 

Thanks again

Marcelo

0 Likes
Message 4 of 5

Ajilal.Vijayan
Advisor
Advisor
Accepted solution

Hi,

I have updated the main function as only HEAD and TAIL needs to update.

Also updated the code to use nentsel.

Spoiler
(defun c:test ( / nblk nblk2 blk blk2 lst lst2 head tail)

(setq nblk(nentsel "\nSelect the origin block:"))
(setq nblk2 (nentsel "\nSelect the destination block:"))

(setq blk (entget (car nblk)))
(setq blk2 (entget (car nblk2)))
(setq blk (vlax-ename->vla-object (cdr(assoc 330 blk))))
(setq blk2 (vlax-ename->vla-object (cdr(assoc 330 blk2))))

;get the list of Attributes with ObjectID and Tag
(setq lst (LM:vl-getobjectids blk))

;Create a new list to change the objetid to add the field value
;(setq lst2 (mapcar '(lambda (b) (cons (car b) 
;(strcat "%<\\AcObjProp Object(%<\\_ObjId " (itoa(cdr b)) ">%).TextString>%"))) lst))

(setq 
head "HEAD"
tail "TAIL")
(if (and (assoc head lst) (assoc tail lst))
	(progn
		(setq lst2
		(list 
		(cons head (strcat "%<\\AcObjProp Object(%<\\_ObjId " (itoa(cdr(assoc head lst))) ">%).TextString>%")) 
		(cons tail (strcat "%<\\AcObjProp Object(%<\\_ObjId " (itoa(cdr(assoc tail lst))) ">%).TextString>%")) 
		));list
		;Apply the new list to Destination block
		(LM:vl-setattributevalues blk2 lst2)
		(command "regen")
	);progn
	(princ "Head/Tail attributes not found")
);if
(princ)
);defun
0 Likes
Message 5 of 5

msarqui
Collaborator
Collaborator

Perfect!

Thank you very much.

 

0 Likes