Inserting Blocks

Inserting Blocks

kzD1219
Collaborator Collaborator
756 Views
11 Replies
Message 1 of 12

Inserting Blocks

kzD1219
Collaborator
Collaborator

Are there any routines out there that could place a particular block at the end of every line, polyline or arc?  Once my plan is ready to go to the client, we need to place this block at the end of every line and there is a lot of human error that could happen if we aren't careful and some spots may get missed.  I was hoping there may be a way to make things easier, so it is correct every time.  

kzD1219_0-1697621598761.png

 

0 Likes
Accepted solutions (1)
757 Views
11 Replies
Replies (11)
Message 2 of 12

ВeekeeCZ
Consultant
Consultant

Do you mean to insert the block at each end (no matter whether it's "end" or "start")... or vertex - even a middle vertex?

And always would be just a single block per node.

0 Likes
Message 3 of 12

kzD1219
Collaborator
Collaborator

It looks like a vertext, but no, just the end of every line, polyline or arc.  There are intersections, but I just insert at the end of every single node.  And yes it would be a single block at the end of every node (or what would be a node point if we used them) if you need to look at it like that .  The blocks name is 155.dwg and isn't annotative so I would need to scale it up afterwards which isn't a big deal.  We use many scales so can't just pick one.  And the layer it would need to on is SM_2BSET.  I was thinking I could isolate the particular layer we need to add blocks too and run a routine that could just insert that block.  I can read a little code and get by, but no idea how to approach this.  I have multiple other people that would hugely benefit from it and save a lot of time.  Early in the am and I am just not wanting to miss any endpoints so my mind is thinking there has got to be an easier way.  

0 Likes
Message 4 of 12

ВeekeeCZ
Consultant
Consultant

So, do you want to have a prompt for the scale?

0 Likes
Message 5 of 12

kzD1219
Collaborator
Collaborator

@ВeekeeCZ wrote:

So, do you want to have a prompt for the scale?


Yes that would be a benefit and complete the action.  And sorry for polylines it would be every vertex, which to me is an end point since that is what I osnap at, but I mispoke and had time to think...lol. No midpoints though.

0 Likes
Message 6 of 12

ВeekeeCZ
Consultant
Consultant
Accepted solution

The block needs to be placed in a searchable path.

 

(vl-load-com)

(defun c:Insert155 ( / bn ln s c i e x l)
  
  (setq bn "155"
	ln "SM_2BSET")
  
  (if (and (or (tblsearch "block" bn)
	       (and (findfile (strcat bn ".dwg"))
		    (progn (command "_.-insert" bn) (command)
		      t))
	       (prompt (strcat "\nError: Block '" bn "' not found.")))
	   (setq s (ssget '((0 . "LINE,ARC,LWPOLYLINE"))))
	   (setq c (getdist "\nSpecify scale factor: "))
	   )
    (repeat (setq i (sslength s))
      (setq e (ssname s (setq i (1- i)))
	    x (if (= "LWPOLYLINE" (cdr (assoc 0 (entget e))))
		(mapcar 'cdr (vl-remove-if '(lambda (x) (/= (car x) 10)) (entget e)))
		(mapcar '(lambda (r) (reverse (cdr (reverse r)))) (list (vlax-curve-getstartpoint e) (vlax-curve-getendpoint e)))))
      (foreach p x
	(if (not (vl-some '(lambda (r) (equal p r 1e-6)) l))
	  (setq l (cons p l))))))
  
  (foreach p l (entmake (list '(0 . "INSERT") (cons 2 bn) (cons 8 ln) (cons 10 p) (cons 41 c) (cons 42 c) (cons 43 c))))
  (princ)
  )

 

Message 7 of 12

kzD1219
Collaborator
Collaborator

So the block is super buried in some file folders.  Where would I need to change that to make the lisp find the block?  It is in every drawing, but I get it would need to find it outside of the drawing.

 

 

0 Likes
Message 8 of 12

komondormrex
Mentor
Mentor

another one

(defun c:put_block_every_point (/ curves_sset)
	(if (null block_name) (setq block_name "BLOCK DEFINED IN DRAWING"))
	(if (null block_scale) (setq block_scale 1))
	(setq block_name (getstring t (strcat "\nEnter block name (Unhighlight to accept) <" block_name ">: "))
		  block_scale_factor (getreal (strcat "\nEnter scal factor for block (Unhighlight to accept) <" (rtos block_scale 2 1) ">: "))
		  curves_sset (ssget '((0 . "lwpolyline,line,arc")))
	)
	(if curves_sset 
		(mapcar '(lambda (insertion_point) (vla-insertblock (vla-get-block (vla-get-activelayout (vla-get-activedocument (vlax-get-acad-object)))) 
															(vlax-3d-point insertion_point) 
															block_name
															block_scale_factor block_scale_factor block_scale_factor 0
										   ) 
				 )
				 (apply 'append (mapcar '(lambda (ename) (cond (
																	(= (cdr (assoc 0 (entget ename))) "LWPOLYLINE")  
																		(mapcar 'cdr (vl-remove-if-not '(lambda (group) (= 10 (car group))) (entget ename)))
										   					   )
										   					   (
																	t
																		(list (vlax-get (vlax-ename->vla-object ename) 'startpoint) 
																			  (vlax-get (vlax-ename->vla-object ename) 'endpoint)
																		)
										   					   )
									 					)
					 					 )
					 					 (vl-remove-if 'listp (mapcar 'cadr (ssnamex curves_sset)))
								)
				 )
		)
	)
	(princ)
)

c:put_block_every_point - autolisp custom command definition

Message 9 of 12

ВeekeeCZ
Consultant
Consultant

NO.

 

Preferably add your block path here... 

eekeeCZ_0-1697630581487.png

 

And see THIS to understand what that c: means.

0 Likes
Message 10 of 12

kzD1219
Collaborator
Collaborator

@ВeekeeCZ wrote:

NO.

 

Preferably add your block path here... 

eekeeCZ_0-1697630581487.png

 

And see THIS to understand what that c: means.


Ah yes, after looking at some of my other lisps, I see that now, thanks.  Going to test it out.

0 Likes
Message 11 of 12

kzD1219
Collaborator
Collaborator

Wow, you are both amazing and fast!  Thank you so very much for you time and effort.😀

0 Likes
Message 12 of 12

Sea-Haven
Mentor
Mentor

Where I used to work we had external software that looked for a block on end of lines, as you asked, but it was not necessary to have multiple blocks at the same point. In the image the 4 lines meet at a common point so this is a variation and will only put 1 block at any line ends that meet.

SeaHaven_0-1697695040711.png

It may be useful or not.

(defun c:block_every_point (/ curves_sset ename lst lst2 x remove_doubles)

; defun Code by Gile
(defun remove_doubles (lst)
  (if lst
    (cons (car lst) (remove_doubles (vl-remove (car lst) lst)))
  )
)

(if (or (null block_name)(= block_name "")) (setq block_name "BLOCK DEFINED IN DRAWING"))
(if (= block_scale nil) (setq block_scale 0.5))

(setq bname (getstring t (strcat "\nEnter block name (Unhighlight to accept) <" block_name ">: ")))
(if (= bname "")(princ)(setq block_name bname))

(setq bscale (getreal (strcat "\nEnter scale factor for block (Unhighlight to accept) <" (rtos block_scale 2 1) ">: ")))
(if (= bscale nil)(princ)(setq block_scale bscale))

(setq curves_sset (ssget '((0 . "LWPOLYLINE,LINE,ARC"))))

(setq lst '())
(repeat (setq x (sslength curves_sset))
  (setq ename (ssname curves_sset (setq x (1- x))))
  (if (= (cdr (assoc 0 (entget ename))) "LWPOLYLINE")
  (progn
    (setq co-ord (mapcar 'cdr (vl-remove-if-not '(lambda (group) (= 10 (car group))) (entget ename))))
    (setq lst (cons  (car co-ord) lst))
    (setq lst (cons (last co-ord) lst))
  )
  (progn
    (setq lst (cons (vlax-get (vlax-ename->vla-object ename) 'startpoint) lst))
    (setq lst (cons (vlax-get (vlax-ename->vla-object ename) 'endpoint) lst))
  )
  )
  
  (setq lst (vl-sort lst
	 '(lambda (a b)
	    (cond
	      ((< (car a) (car b)))
	      ((= (car a) (car b)) (< (cadr a) (cadr b)))
	    )
	  )
  ))
)

(setq lst2 (remove_doubles lst))

(setq curspace (vla-get-block (vla-get-activelayout (vla-get-activedocument (vlax-get-acad-object)))))

(foreach pt lst2
  (vla-insertblock curspace
  (vlax-3d-point pt) 
  block_name
  block_scale_factor block_scale_factor block_scale_factor 0
  )
)

(princ)
)
(c:block_every_point)

 

0 Likes