Jump to content

Select text and place the insertion point of Block


Recommended Posts

Posted

Hi,

can anybody help me actually i have multiple text which i want to move at block insertion point as i am doing it through move command one by one but it take lots of time is any lisp available to move all text to block insertion point 

Before

 

 

image.thumb.png.8873753a92a91d4b245de1b84333d053.png

 

After

image.thumb.png.11929b21143d689805b6816015f73c8e.png

Drawing2.dwg

Posted

Its a easy move select the text then look for the block using (ssget "CP" pts a small search window of points. Yes select all correct text. You have 347 posts so you should be starting to have a go yourself, I am sure if you google will find code for finding objects close by.

Posted (edited)

This should work

 

;; mttb for Move Text To Block
(defun c:mttb ( / i j ss texts blocks ent ip1 ip2 dst ind)
	
	(setq ss (ssget (list (cons 0 "TEXT,INSERT"))))
	;; lets make 2 selections.  Texts and blocks
	(setq texts (ssadd))
	(setq blocks (ssadd))
	
	(setq i 0)
	(repeat (sslength ss)
		(setq ent (entget (ssname ss i)))
		(if (= "INSERT" (cdr (assoc 0 ent)))
			(ssadd (ssname ss i) blocks)
			(ssadd (ssname ss i) texts)
		)
		(setq i (+ i 1))
	)
	;; now we loop over the blocks, for each block we fine a text that's closest to that block.
	;; (we could have chosen the the other way around)
	(setq i 0)
	(repeat (sslength blocks)
		
		(setq ip1 (cdr (assoc 10 (entget (ssname blocks i)))))
		(setq dst nil)	;; this holds the closest distance block-text
		(setq ind nil)	;; this holds the index j for the closest distance 
		(setq j 0)
		(repeat (sslength texts)
			(setq ip2 (cdr (assoc 10 (entget (ssname texts j)))))
			
			(if (= nil dst)
				(progn 	;; first try
					(setq dst (distance ip1 ip2))
					(setq ind j)
				)
				(progn	;; next we see if we find anything better
					(if (< (distance ip1 ip2) dst) 
						(progn
							(setq dst (distance ip1 ip2))
							(setq ind j)
						)
					)
				)
			)
			
			(setq j (+ j 1))
		)
		;; change insert point of TEXT
		(setq ent (ssname texts ind))
		(entmod (subst
			(cons 10 ip1)				;; insert point of the block
			(assoc 10 (entget ent))
			(entget ent)
		))
		(setq i (+ i 1))
	)
	;; this grips the text objects.  Notr really needed, but why not
	(sssetfirst nil texts)
	(princ)
)

 

EDIT

Oh yes, you can add this line of code.  (ssdel ...) will remove the text from the list.

In that case the variable texts should be empty at the end, then nothing gets gripped at the end

 

		;; change insert point of TEXT
		(setq ent (ssname texts ind))
		(entmod (subst
			(cons 10 ip1)				;; insert point of the block
			(assoc 10 (entget ent))
			(entget ent)
		))
		;; we can remove the TEXT from the selection.  No need to keep using it after we found a match
		(ssdel (ssname texts ind) texts)

 

Edited by Emmanuel Delay
  • Like 1
Posted
On 8/21/2023 at 1:55 PM, Emmanuel Delay said:

This should work

 

;; mttb for Move Text To Block
(defun c:mttb ( / i j ss texts blocks ent ip1 ip2 dst ind)
	
	(setq ss (ssget (list (cons 0 "TEXT,INSERT"))))
	;; lets make 2 selections.  Texts and blocks
	(setq texts (ssadd))
	(setq blocks (ssadd))
	
	(setq i 0)
	(repeat (sslength ss)
		(setq ent (entget (ssname ss i)))
		(if (= "INSERT" (cdr (assoc 0 ent)))
			(ssadd (ssname ss i) blocks)
			(ssadd (ssname ss i) texts)
		)
		(setq i (+ i 1))
	)
	;; now we loop over the blocks, for each block we fine a text that's closest to that block.
	;; (we could have chosen the the other way around)
	(setq i 0)
	(repeat (sslength blocks)
		
		(setq ip1 (cdr (assoc 10 (entget (ssname blocks i)))))
		(setq dst nil)	;; this holds the closest distance block-text
		(setq ind nil)	;; this holds the index j for the closest distance 
		(setq j 0)
		(repeat (sslength texts)
			(setq ip2 (cdr (assoc 10 (entget (ssname texts j)))))
			
			(if (= nil dst)
				(progn 	;; first try
					(setq dst (distance ip1 ip2))
					(setq ind j)
				)
				(progn	;; next we see if we find anything better
					(if (< (distance ip1 ip2) dst) 
						(progn
							(setq dst (distance ip1 ip2))
							(setq ind j)
						)
					)
				)
			)
			
			(setq j (+ j 1))
		)
		;; change insert point of TEXT
		(setq ent (ssname texts ind))
		(entmod (subst
			(cons 10 ip1)				;; insert point of the block
			(assoc 10 (entget ent))
			(entget ent)
		))
		(setq i (+ i 1))
	)
	;; this grips the text objects.  Notr really needed, but why not
	(sssetfirst nil texts)
	(princ)
)

 

EDIT

Oh yes, you can add this line of code.  (ssdel ...) will remove the text from the list.

In that case the variable texts should be empty at the end, then nothing gets gripped at the end

 

		;; change insert point of TEXT
		(setq ent (ssname texts ind))
		(entmod (subst
			(cons 10 ip1)				;; insert point of the block
			(assoc 10 (entget ent))
			(entget ent)
		))
		;; we can remove the TEXT from the selection.  No need to keep using it after we found a match
		(ssdel (ssname texts ind) texts)

 

 

message flash
Unknown command "MTTB".  Press F1 for help

Posted
On 8/20/2023 at 4:33 AM, BIGAL said:

Its a easy move select the text then look for the block using (ssget "CP" pts a small search window of points. Yes select all correct text. You have 347 posts so you should be starting to have a go yourself, I am sure if you google will find code for finding objects close by.

actually i used firstly google search but i didn't found correct answer then i came here

Posted

Copy paste my code to a empty text file.  Save it as (for example) "place-the-insertion-point-of-block.lsp".

Make sure the extenson is .lsp, and not .lsp.txt

 

Then slide that file in your drawing, in the Autocad window.

 

Or... what did you do with the code?

 

Posted (edited)

"Then slide that file in your drawing, in the Autocad window."  In Windows often referred to as  "Drag and drop".

 

Open explorer, find the lisp file "place-the-insertion-point-of-block.lsp" click once, then hold left mouse button down and DRAG the file over to your CAD screen than let go, ie DROP.

 

You can do the same with a dwg it will open the file.

 

 

Edited by BIGAL
Posted
18 hours ago, Emmanuel Delay said:

Copy paste my code to a empty text file.  Save it as (for example) "place-the-insertion-point-of-block.lsp".

Make sure the extenson is .lsp, and not .lsp.txt

 

Then slide that file in your drawing, in the Autocad window.

 

Or... what did you do with the code?

 

yes i do same, after that message flash

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...