lisp: select a text automatically

lisp: select a text automatically

Anonymous
Not applicable
5,888 Views
18 Replies
Message 1 of 19

lisp: select a text automatically

Anonymous
Not applicable

Hello, everyone,

 

I’m working on a lisp to select an existing text, and copy its content and paste it in an attribute.

(entsel) prompt people to select a text by mouse, but what I need is to select that text automatically since this will repeat in more than 100 plans. The texts in all plans have the same coordinate point.

Please let me know which Funcion is applicable.

0 Likes
Accepted solutions (4)
5,889 Views
18 Replies
Replies (18)
Message 2 of 19

Ranjit_Singh
Advisor
Advisor

Something like this for example

(ssname (ssget '(120 300)) 0) ; selecting entity at 120, 300

or this

(nentselp '(120 300))

 

 

Message 3 of 19

Ranjit_Singh
Advisor
Advisor
Accepted solution

I think this is what you mean.

;;Ranjit Singh
;;7/12/17
(defun c:somefunc  (/ jpgobj)
 (vlax-invoke (vlax-get (vlax-get (setq jpgobj (vlax-get-or-create-object "jpegfile")) 'parentwindow)
                        'clipboarddata)
              'setdata
              "text"
              (cdr (assoc 1 (entget (car (nentselp '(120 300))))))); select text at 120, 300
 (vlax-release-object jpgobj)
 (princ))
Message 4 of 19

Anonymous
Not applicable

Thanks, Ranjit, it works.

 

Can we make the question a step further? now the text is a Mtext which has two lines (refer to attached cad block); Can the code identify the first and second line and copy the content respectively? the corresponding attribure would be TITRE1 and TITRE2.

 

thanks,

0 Likes
Message 5 of 19

Ranjit_Singh
Advisor
Advisor
Accepted solution

I don't think you need the clipboard object. I was thrown off by your other post over here specifically mentioning clipboard.

If I understand correctly now, following works to select mtext and copy the first and second lines to the attributes. Just be aware it is tested only for the drawing you posted.

;;Ranjit Singh
;;7/17/17
(defun c:somefunc  (/ ent ent2 etdata)
 (setq ent  (car (entsel "\nSelect block: "))
       ent2 (eval
             (read
              (strcat "\'(\""
                      (vl-string-subst "\" \"" "\\P" (cdr (assoc 1 (entget (car (entsel "\nSelect mtext:"))))))
                      "\")"))))
 (while (/= "SEQEND" (cdr (assoc 0 (setq etdata (entget (setq ent (entnext ent)))))))
  (and (= "ATTRIB" (cdr (assoc 0 etdata)))
       (cond ((= "TITRE1" (cdr (assoc 2 etdata))) (entmod (subst (cons 1 (car ent2)) (assoc 1 etdata) etdata)))
             ((= "TITRE2" (cdr (assoc 2 etdata))) (entmod (subst (cons 1 (cadr ent2)) (assoc 1 etdata) etdata)))
             (t ())))))

Copy_to_attributes.gif

 

Message 6 of 19

Anonymous
Not applicable

It works, eventhough I havn't understood the code yet.

Thanks man!

0 Likes
Message 7 of 19

Anonymous
Not applicable

Hello, Ranjit,

I am trying to adjust your code in order to realise copying Mtext automatically and assign them to attributes, but it didn't work. the same dwg file attached originally.

Can you spare a bit time to take a look?

Thanks,

;;Ranjit Singh
;;7/17/17
(defun c:mtdbl (/ ent ent2 etdata mt blk)
  (setq	mt				; get mtext
	 (ssget	"_X"
		'((0 . "MTEXT")
		  (410 . "~Model")
		  (-4 . ">=,>=,*")
		  (10 1275.0 75.0 0.0)
		  (-4 . "<=,<=,*")
		  (10 1435.0 55.0 0.0)
		 )
	 )
  )

  (setq	blk				; find block
	 (ssget	"_X"
		'((0 . "INSERT")
		  (66 . 1)
		  (2 . "FR_ATTR-****")
		 )
	 )
  )

  (setq	ent  (car blk)
	ent2 (eval
	       (read
		 (strcat "\'(\""
			 (vl-string-subst
			   "\" \""
			   "\\P"
			   (cdr (assoc 1 (entget (car mt))))
			 )
			 "\")"
		 )
	       )
	     )
  )
  (while
    (/=	"SEQEND"
	(cdr
	  (assoc 0 (setq etdata (entget (setq ent (entnext ent)))))
	)
    )
     (and
       (= "ATTRIB" (cdr (assoc 0 etdata)))
       (cond
	 ((= "TITRE1" (cdr (assoc 2 etdata)))
	  (entmod (subst (cons 1 (car ent2)) (assoc 1 etdata) etdata))
	 )
	 ((= "TITRE2" (cdr (assoc 2 etdata)))
	  (entmod (subst (cons 1 (cadr ent2)) (assoc 1 etdata) etdata)
	  )
	 )
	 (t ())
       )
     )
  )
)
0 Likes
Message 8 of 19

Ranjit_Singh
Advisor
Advisor
Accepted solution

You cannot cycle through a selection set using car. You need to call ssname function. This applies to the blk and mt selection sets. Second problem is in your -4 filter. See the fix in below code

;;Ranjit Singh
;;7/17/17
(defun c:mtdbl (/ ent ent2 etdata mt blk)
  (setq	mt ; get mtext
	 (ssget	"_X"
		'((0 . "MTEXT")
		  (410 . "11175-03")
		  (-4 . ">=,>=,*")
		  (10 1275.0 55.0 0.0)
		  (-4 . "<=,<=,*")
		  (10 1435.0 75.0 0.0)
		 )
	 )
  )

  (setq	blk ; find block
	 (ssget	"_X"
		'((0 . "INSERT")
		  (66 . 1)
		  (2 . "FR_ATTR-****")
		 )
	 )
  )

  (setq	ent  (ssname blk 0)
	ent2 (eval
	       (read
		 (strcat "\'(\""
			 (vl-string-subst
			   "\" \""
			   "\\P"
			   (cdr (assoc 1 (entget (ssname mt 0))))
			 )
			 "\")"
		 )
	       )
	     )
  )
  (while
    (/=	"SEQEND"
	(cdr
	  (assoc 0 (setq etdata (entget (setq ent (entnext ent)))))
	)
    )
     (and
       (= "ATTRIB" (cdr (assoc 0 etdata)))
       (cond
	 ((= "TITRE1" (cdr (assoc 2 etdata)))
	  (entmod (subst (cons 1 (car ent2)) (assoc 1 etdata) etdata))
	 )
	 ((= "TITRE2" (cdr (assoc 2 etdata)))
	  (entmod (subst (cons 1 (cadr ent2)) (assoc 1 etdata) etdata)
	  )
	 )
	 (t ())
       )
     )
  )
)

dbl_attribute.gif

 

Message 9 of 19

pbejse
Mentor
Mentor

@Anonymous wrote:

 

 

Thanks,

.......
.... (setq mt ; get mtext (ssget "_X" '((0 . "MTEXT") (410 . "~Model") (-4 . ">=,>=,*") (10 1275.0 75.0 0.0) (-4 . "<=,<=,*") (10 1435.0 55.0 0.0) ) ) ) (setq blk ; find block (ssget "_X" '((0 . "INSERT") (66 . 1) (2 . "FR_ATTR-****") ) ) ) )
....

Really hifrank001 

 

Besides, you don't want specifying layout name for 410  DXF group code, that will be specific.to a layout name. such as (410 . "11175-03")

Anyways, its your call.

 

Cheers buddy

 

EDIT:

(ssget	"_X"
		(list
		  '(0 . "MTEXT") (cons 410 (getvar 'ctab))
		  '(-4 . ">=,>=,*")
		  '(10 1275.0 75.0 0.0)
		  '(-4 . "<=,<=,*")
		  '(10 1435.0 55.0 0.0))
		 )

i'm just saying 🙂

 

Message 10 of 19

pbejse
Mentor
Mentor

Looking thru most of your query, I believe you should be approaching the problems in a more logical way. Take this discussion for  instance, Is this a "repair" job? Upgrading existing title block to for future use and keeping up with company standards?  

 

What we have offered so far is quick "fix" for something too specific, tell us the whole story, maybe we can come up with a better solution to your problem.

 

 

pBe

Message 11 of 19

Anonymous
Not applicable

Hello, Ranjit,

As the animation shows, it works; pBe brought up  a good point: ctab variable, since plans have different layouts' names; I tested the way suggested, but it always came back with error message SSGET incorrect. Could you incorperate this in the routine?

Thanks,

0 Likes
Message 12 of 19

Ranjit_Singh
Advisor
Advisor

I did post the routine in post 8 of 11. Just change 

(ssget	"_X"
		'((0 . "MTEXT")
		  (410 . "11175-03")
		  (-4 . ">=,>=,*")
		  (10 1275.0 55.0 0.0)
		  (-4 . "<=,<=,*")
		  (10 1435.0 75.0 0.0)
		 )
	 )

to

(ssget	"_X"
		(list '(0 . "MTEXT")
		  (cons 410  (getvar 'ctab))
		  '(-4 . ">=,>=,*")
		  '(10 1275.0 55.0 0.0)
		  '(-4 . "<=,<=,*")
		  '(10 1435.0 75.0 0.0)
		 )
	 )
Message 13 of 19

Anonymous
Not applicable

Yes, it is a 'repair' work which includes the followings

1. dettach old xref (titleblock), erase old text and mtext

2 insert new xref, blocks which should have the information from old deledted text and mtext;  see this post and this one

3 new block will also incorperate the information from file name and/or layout name; see this post

 

All the points weren't listed in one single post because my lisp/script knowledge didn't allow me to ask the correct questions at the very beginning (even now).

 

Hope this can answer your concern.

 

 

0 Likes
Message 14 of 19

Anonymous
Not applicable

Hmm, it works now.

0 Likes
Message 15 of 19

Anonymous
Not applicable

I will combine all routines into one when everthing is solved; at that moment, I believe there must be a simplified way which you guys will come up with. It's a good idea that one post focus one single issue, isn't it? easier for people to search and refer to.

Thanks again.

0 Likes
Message 16 of 19

Anonymous
Not applicable

One more question:

If the Mtext has only one line, it should be copied to TITRE2, and TITRE1 should be empty.

Thanks always.

0 Likes
Message 17 of 19

Ranjit_Singh
Advisor
Advisor
Accepted solution

See the change in bold

;;Ranjit Singh
;;7/17/17
(defun c:somefunc (/ ent ent2 etdata) (setq ent (car (entsel "\nSelect block: ")) ent2 (eval (read (strcat "\'(\"" (vl-string-subst "\" \"" "\\P" (cdr (assoc 1 (entget (car (entsel "\nSelect mtext:")))))) "\")")))) (while (/= "SEQEND" (cdr (assoc 0 (setq etdata (entget (setq ent (entnext ent))))))) (and (= "ATTRIB" (cdr (assoc 0 etdata))) (cond ((= "TITRE1" (cdr (assoc 2 etdata))) (entmod (subst (cons 1 (if (cdr ent2) (car ent2) "")) (assoc 1 etdata) etdata))) ((= "TITRE2" (cdr (assoc 2 etdata))) (entmod (subst (cons 1 (if (cdr ent2) (cadr ent2) (car ent2))) (assoc 1 etdata) etdata))) (t ())))))

dbl_attribute_2.gif

 

Message 18 of 19

appuraja
Explorer
Explorer

Mr Ranjit, This is the awesome solution but i want to use this for bit different application. For example by block is having total 4 attributes ROOM1, ROOM2, ROOM3,ROOM4. Now depends on room name and number i want to distribute in this all 4 attributes. Let me share my testing file for your ready reference.  Thanks in advance. 

0 Likes
Message 19 of 19

appuraja
Explorer
Explorer
Also it will be good if routine will allow to select more than 1 block at a time and then take contain from mtext so that all block of same area will have data in single process. Please let me know if i need to explain more about requirement. Thanks in advance.
0 Likes