Blocks Perpendicular to Polyline

Blocks Perpendicular to Polyline

adaptacad
Advocate Advocate
427 Views
4 Replies
Message 1 of 5

Blocks Perpendicular to Polyline

adaptacad
Advocate
Advocate

Good morning, everyone!

I would like to know if it’s possible to align multiple blocks so that they are perpendicular to polylines in a DWG file.

The goal is to select multiple texts and multiple polylines, ensuring that the texts align perpendicularly to the nearest polyline. I've searched on Google and ChatGPT but haven’t found a solution.

Can anyone help me? I’ve attached images and a DWG file as examples.

Thanks in advance!
adaptacad_0-1740486600033.png

0 Likes
Accepted solutions (2)
428 Views
4 Replies
Replies (4)
Message 2 of 5

ВeekeeCZ
Consultant
Consultant
Accepted solution

Not search, not ai, just learn. There is nothing complicated. Nothing.

 

(vl-load-com)

(defun c:BlocksPerpendiculartoPolyline ( / s d b l c q a)
  
  (and (setq s (ssget '((0 . "LWPOLYLINE,INSERT"))))
       (setq d (getdist "\nSpecify distance: " ))
       (setq s (vl-remove-if 'listp (mapcar 'cadr (ssnamex s))))
       (foreach e s
	 (if (= "INSERT" (cdr (assoc 0 (entget e))))
	   (setq b (cons e b))
	   (setq l (cons e l))))
       b
       l
       (foreach e b
	 (if (and (setq p (cdr (assoc 10 (entget e))))
		  (setq l (vl-sort l '(lambda (l1 l2) (< (distance p (vlax-curve-getclosestpointto l1 p))
							 (distance p (vlax-curve-getclosestpointto l2 p))))))
		  (setq c (car l))
		  )
	   (progn
	     (setq q (vlax-curve-getclosestpointto c p))
	     (setq a (angle q p))
	     (setpropertyvalue e "Rotation" a)
	     (setpropertyvalue e "Position" (polar q a d))))))
  (princ)
  )

 

Message 3 of 5

adaptacad
Advocate
Advocate

Thank you very much @ВeekeeCZ !!!! This worked very well! But is there a way to keep the original distance, without having to specify it?

0 Likes
Message 4 of 5

ec-cad
Collaborator
Collaborator
Accepted solution

If you want the original distance maintained, try the revised code:

 

 

(vl-load-com)

(defun c:BlocksPerpendiculartoPolyline ( / s d b l c q a)
  
  (and (setq s (ssget '((0 . "LWPOLYLINE,INSERT"))))
     ;; (setq d (getdist "\nSpecify distance: " )); we want to keep original distance
       (setq s (vl-remove-if 'listp (mapcar 'cadr (ssnamex s))))
       (foreach e s
	 (if (= "INSERT" (cdr (assoc 0 (entget e))))
	   (setq b (cons e b))
	   (setq l (cons e l))))
       b
       l
       (foreach e b
	 (if (and (setq p (cdr (assoc 10 (entget e))))
		  (setq l (vl-sort l '(lambda (l1 l2) (< (distance p (vlax-curve-getclosestpointto l1 p))
							 (distance p (vlax-curve-getclosestpointto l2 p))))))
		  (setq c (car l))
		  )
	   (progn
	     (setq q (vlax-curve-getclosestpointto c p))
	     (setq a (angle q p))
             (setq d (distance q p)); Set the Distance to what it was..
	     (setpropertyvalue e "Rotation" a)
	     (setpropertyvalue e "Position" (polar q a d))))))
  (princ)
  )
;; remove next 2 lines if desired for autoloading
 (c:BlocksPerpendiculartoPolyline)
 (princ)

 

ECCAD

0 Likes
Message 5 of 5

adaptacad
Advocate
Advocate

Thank you both very much! It worked great! @ВeekeeCZ  @ec-cad 

0 Likes