Insert Block At Selected endline

Insert Block At Selected endline

a.tawkXHKN6
Contributor Contributor
1,946 Views
9 Replies
Message 1 of 10

Insert Block At Selected endline

a.tawkXHKN6
Contributor
Contributor

Is there a lisp that allow us to pick a block and insert it at selected line endpoint? see attached. 

0 Likes
Accepted solutions (1)
1,947 Views
9 Replies
Replies (9)
Message 2 of 10

Moshe-A
Mentor
Mentor

@a.tawkXHKN6 

 

check this, replace "YourBlockName" with your block name, ESC to stop the command

 

(defun c:mins ()
 (while t
  (command "insert" "YourBlockName" "_endp" pause 1 1 0)
 )
)

 

enjoy

Moshe

 

0 Likes
Message 3 of 10

a.tawkXHKN6
Contributor
Contributor

Thanks for your reply, but what I need is that the routine asks me to select the block in order to get its name so that the lisp would me more general, second I want the lisp to ask me to select the lines and then to insert the block automatically at each line endpoint. 

0 Likes
Message 4 of 10

Moshe-A
Mentor
Mentor

@a.tawkXHKN6 wrote:

Thanks for your reply, but what I need is that the routine asks me to select the block in order to get its name so that the lisp would me more general

 

put the word pause instead of "YourBlockName"

 

, second I want the lisp to ask me to select the lines and then to insert the block automatically at each line endpoint. 


what's the difference if you select the line or pick it's endpoint?

 

 

 

 

0 Likes
Message 5 of 10

Kent1Cooper
Consultant
Consultant

Which endpoint -- the start or the end?  If not always the same  end, how would the routine know which end to use unless they are selected individually?  If that's the case, the ENDpoint pick approach should be as good as selecting the Lines.  If always the same end, then you could select multiple Lines, such as in a window, but how will you know where it's going to put the Blocks?  You can't tell by looking on-screen which end of a Line is which.

Kent Cooper, AIA
0 Likes
Message 6 of 10

pbejse
Mentor
Mentor

@a.tawkXHKN6 wrote:

Thanks for your reply, but what I need is that the routine asks me to select the block in order to get its name so that the lisp would me more general, second I want the lisp to ask me to select the lines and then to insert the block automatically at each line endpoint. 


(defun c:InTheEnd ( / e blkName i el)
  (if (and
	(princ "\nSelect BLock")
	(setq e (ssget "_+.:S:E" '((0 . "INSERT"))))
	(setq blkName (getpropertyvalue
			(setq e (ssname e 0))
			"BlockTableRecord/Name"
		      )
	)
	(setq Lines (ssget '((0 . "LINE"))))
      )
    (progn
      (repeat (setq i (sslength Lines))
	(setq el (ssname Lines (setq i (1- i))))
	(entmakex (list	(cons 0 "INSERT")
			(cons 2 blkName)
			(cons 10 (vlax-curve-getEndPoint el))
		  )
	)
      )
    )
  )
  (princ)  
)

 

Depending on how the lines were created , the "Endpoint" might not be what you expected.

We can add an option to see where the block will be inserted and prompt the user to relocated the block.

 

HTH

 

Message 7 of 10

a.tawkXHKN6
Contributor
Contributor

👍

I just need the block to be insert at the opposite lower side of the line not the upper.

Thank you. 

0 Likes
Message 8 of 10

pbejse
Mentor
Mentor

@a.tawkXHKN6 wrote:

👍

I just need the block to be insert at the opposite lower side of the line not the upper.

Thank you. 


(defun c:InTheEnd ( / e blkName i el)
  (if (and
	(princ "\nSelect BLock")
	(setq e (ssget "_+.:S:E" '((0 . "INSERT"))))
	(setq blkName (getpropertyvalue
			(setq e (ssname e 0))
			"BlockTableRecord/Name"
		      )
	)
	(setq Lines (ssget '((0 . "LINE"))))
      )
    (progn
      (repeat (setq i (sslength Lines))
	(setq el (ssname Lines (setq i (1- i))))
	(setq data (list (vlax-curve-getEndPoint el) (vlax-curve-getStartPoint el)))
	(entmakex (list	(cons 0 "INSERT")
			(cons 2 blkName)
			(cons 10
			      (if (> (cadar data)(cadadr data))
				     (cadr data)(Car data))
			      )
		  )
	)
      )
    )
  )
  (princ)  
)

HTH

 

0 Likes
Message 9 of 10

Moshe-A
Mentor
Mentor

@a.tawkXHKN6 ,

 

here is your ultimate solution  😀

 

IBCE command,  starts with request for block name and than Select objects: ... use the "fence" method to select the lines (you also can select plines even splines) the block would be insert at closest endpoint of the line crossing the fence line.

 

enjoy

Moshe

 

 

 

 

(vl-load-com) ; load activex support
; IBCE - Insert Block at Close End
(defun c:ibce (/ get_block_name retrieve_fence_data ; local functions
                 bname ss ename dX dS dE pS pE)
 (defun get_block_name (/ bn)
  (while (not
           (and
             (/= (setq bn (getstring "\nBlock name: ")) "")
              (or  
                (tblsearch "block" bn)
                (findfile (strcat bn ".dwg"))
              )
           )
         )
    (vlr-beep-reaction)
    (prompt (strcat "\nBlock " bn " is not exist."))
  ); while
  bn          
 ); get_block_name
    
 (defun retrieve_fence_data (/ fence-data)
  (defun fence-data () 
   (vl-remove-if
    'not
    (mapcar
     '(lambda (x)
       (if (= (car x) 4) ; fence selection
        (cons (cadr x) (list (cadr (last x))))
       )
      ); lambda
     (ssnamex ss)
    ); mapcra
   ); vl-remove-if
  );fence-data

  (cond
   ((fence-data))
   ( t
    (vlr-beep-reaction)
    (prompt "\nRequires Fence method selection.")
   )
  ); cond
 ); retrieve_fence_selection
  
 ; here start C:IBCE
 (setvar "cmdecho" 0)
 (command "._undo" "_begin")
  
 (if (and
       (setq bname (get_block_name))
       (setq ss (ssget '((0 . "*line")))) ; select by "fence" only
     )
  (foreach item (retrieve_fence_data)
   (setq ename (car item)) 
   (setq dX (vlax-curve-getDistAtPoint ename (vlax-curve-getClosestPointTo ename (cadr item))))
   (setq dS (vlax-curve-getDistAtPoint ename (setq pS (vlax-curve-getStartPoint ename))))
   (setq dE (vlax-curve-getDistAtPoint ename (setq pE (vlax-curve-getEndPoint   ename))))

  (command "._insert" bname (if (< (- dX dS) (- dE dX)) pS pE) 1 1 0)
 ); foreach
 ); if

 (command "._undo" "_end")
 (setvar "cmdecho" 1)
 (princ)
); c:ibce

    

 

 

 

0 Likes
Message 10 of 10

pbejse
Mentor
Mentor
Accepted solution
(defun c:InTheEnd ( / Thisblocks spc e blkName i ent el fnSTR StrDIA)
(defun Thisblocks ( / a LocalB)
  (while (setq a (tblnext "Block" (null a)))
    	(if (and
	      (/= 4 (logand 4 (cdr (assoc 70 a))))
	      (/= (substr (setq bn (cdr (assoc 2 a))) 1 1) "*"))
	  (setq LocalB (cons (cdr (assoc 2 a)) LocalB)))
    )
    LocalB
    )
(setq spc (vlax-get (vla-get-ActiveLayout
                  (vla-get-activedocument(vlax-get-acad-object))) 'Block))  
  (if (and
	(progn
		(initget "N")
	  	(setq ent (entsel "\nSelect block/ N for name: "))
	  	(setq blkName 
		  	(cond
			  ( (and
			      (eq ent "N")
			      (setq lst (Thisblocks)))
			        (setq StrDiaFnme (vl-filename-mktemp "tmp.DCL"))
			      	(setq fnSTR (open StrDiaFnme "w"))
			            (write-line
			                  "dcl_settings : default_dcl_settings { audit_level = 3; }
					  ListOfTags : dialog 
					  { label = \"Select Tag(s)\"; 
					  : list_box { key = \"TagList\"; 
					  width = 8.0; height = 10.0; } spacer ;
					  ok_cancel;  }"              fnSTR)
			            	(close fnSTR)
				(setq StrDIA (load_dialog StrDiaFnme))
				(if (not (new_dialog "ListOfTags" StrDIA))
				  (exit)
				  )
				(start_list "TagList")
				(mapcar 'add_list Lst)
				(end_list)
				(action_tile "TagList" "(setq dd (get_tile \"TagList\"))")
				(action_tile "accept" "(done_dialog 1)")
				(action_tile "cancel" "(done_dialog 0)")
				(start_dialog)
				(unload_dialog StrDIA)
			        (vl-file-delete StrDiaFnme)
			   	(nth (atoi dd) lst)
			     )
			  ( (and (setq e (car ent))
			      (eq (cdr (Assoc 0 (entget e))) "INSERT"))
				(getpropertyvalue e "BlockTableRecord/Name")
				      )
			   )
			)
		  )
	(print blkName)
	(princ "\nSelect object(s) for block insertion")
	(setq Lines (ssget '((0 . "ARC,*LINE"))))
      )
    (progn
      (repeat (setq i (sslength Lines))
	(setq el (ssname Lines (setq i (1- i))))
	(setq data (list (vlax-curve-getEndPoint el) (vlax-curve-getStartPoint el)))
	(vlax-invoke spc 'InsertBlock (if (> (cadar data)(cadadr data))
				     (cadr data)(Car data))
  		blkName 1 1 1 0)
      )
    )
    (princ "\nInvalid or null selection"))	   
  (princ)  
)
0 Likes