Is there a lisp that will align Blocks to the end of a PLine perpindicular in bulk

Is there a lisp that will align Blocks to the end of a PLine perpindicular in bulk

timLNMHN
Contributor Contributor
998 Views
16 Replies
Message 1 of 17

Is there a lisp that will align Blocks to the end of a PLine perpindicular in bulk

timLNMHN
Contributor
Contributor

autocad align blocks.jpgso as I asked in the title is there a possibility of a lisp that can do this? image added for clarification. they always need to be straight as shown in the picture. the orange thing is a (block reference) we use here in the company. we have a lisp that sometimes does work and alot of times puts it wrong like in the picture.

 

here is that lisp, if someone could help me that would be greatly appreciated! the lisp may be changed or if you have a all new lisp that already does this, feel fre e to share it with me! cheers! it is always for 2D drawings

 

(defun C:ABP (/ picksize bss n pss ins bdata box pl); = Align Blocks to Polylines
(setq picksize (* (getvar 'viewsize) (/ (getvar 'pickbox) (car (getvar 'screensize)))))
(if (setq bss (ssget "_:L" '((0 . "INSERT"))))
(repeat (setq n (sslength bss))
(if
(setq pss ; is insertion point on a Polyline?
(ssget "_C"
(mapcar '-
(setq ins (cdr (assoc 10
(setq bdata (entget (ssname bss (setq n (1- n))))))))
(setq box (list picksize picksize))
); mapcar
(mapcar '+ ins box)
'((0 . "*POLYLINE"))
); ssget
); setq
(entmod ; then
(subst
(cons 50
(angle
'(0 0 0)
(vlax-curve-getFirstDeriv (setq pl (ssname pss 0))
(vlax-curve-getParamAtPoint pl (vlax-curve-getClosestPointTo pl ins))
); ...FirstDeriv
); angle
); cons
(assoc 50 bdata)
bdata
); subst
); entmod
); if [Polyline there or not]
); repeat
); if [selected Block(s) or not]
(princ)
); defun
(vl-load-com)

0 Likes
999 Views
16 Replies
Replies (16)
Message 2 of 17

Kent1Cooper
Consultant
Consultant

What is this bit?

Kent1Cooper_0-1683213106888.png

If that's a Polyline, isn't the Block perpendicular to it?  If it's not, but the longer greenish thing is and that's what you want it to find, does it depend on your Zoom level?  Should you increase the selection crossing-box size?

Kent Cooper, AIA
Message 3 of 17

timLNMHN
Contributor
Contributor
The little line is the line it needs to get straight put on as the one in the photo who is correct. it is a Pline, I am fairly new to Autocad so sorry if I don't know some terms 🙂

the little line is the correct line for perpendicular placement!
0 Likes
Message 4 of 17

-didier-
Advisor
Advisor

Bonjour @timLNMHN 

 

Is the question:
A block must be inserted perpendicular to a polyline at given spaces?
If yes, use the "measure" command, click the polyline, give the name of the block and choose ALIGN

it's a native command, and it works very well.

 

Amicalement

Éternel débutant.. my site for learning : Programmer dans AutoCAD

DA

EESignature

Message 5 of 17

timLNMHN
Contributor
Contributor

thank you for your reply!

 

No the blocks are already inserted and connected to the PLine, it just now needs to rotate to be straight on top like in the picture 🙂 it usually is for like a few hundred to a few thousand of the same of them. When i insert them they are always on 0 degrees!

 

hope this clarifies something?

0 Likes
Message 6 of 17

Kent1Cooper
Consultant
Consultant

@timLNMHN wrote:
.... the little line is the correct line for perpendicular placement!

And if, at zero rotation, the Block looks like a question mark [or maybe an upside-down question mark?], then I suspect the routine is finding the longer Polyline, instead of the short one, in those cases where it "gets it wrong."  It's probably affected by Zoom level.  If you build in a Zoom to the Object of each Block, it should be at a Zoom level where your selection box won't catch the long Polyline.  Or it might work to simply reduce the size of the selection window by something like this:

 

(setq box (list (/ picksize 2) (/ picksize 2)))

Kent Cooper, AIA
Message 7 of 17

-didier-
Advisor
Advisor

Bonjour @timLNMHN 

 

Thank you to deliver a drawing for understanding the purpose.

 

Amicalement

Éternel débutant.. my site for learning : Programmer dans AutoCAD

DA

EESignature

Message 8 of 17

timLNMHN
Contributor
Contributor

And how is it possible to change the zoom level? In the block editor? and if so does this apply for all future drawn objects of the same type? thanks for all the replies 🙂

0 Likes
Message 9 of 17

komondormrex
Mentor
Mentor

and what is the angle of that linear segment encircled by Kent in block definition? does a block have one?

0 Likes
Message 10 of 17

Kent1Cooper
Consultant
Consultant

@timLNMHN wrote:

And how is it possible to change the zoom level? ....


Put this part:

(setq blk (ssname bss (setq n (1- n))))

as the first thing in the (repeat) function, followed by:

(command "_.zoom" "_object" blk "")

and then go on to check whether its insertion point is on a Polyline, in which test you can then replace this:

(setq bdata (entget (ssname bss (setq n (1- n))))))))

with just this, having the Block's entity name already saved:

(setq bdata (entget blk))

 

It will then do that for each Block individually.  You can build in a (command "_.zoom" "_previous") within that (repeat) if you want to end up at whatever view you started in.

 

Kent Cooper, AIA
0 Likes
Message 11 of 17

komondormrex
Mentor
Mentor

vlax-curve* functions do not operate with enames, which may be the cause of malfunctioning of the code. so i have made some improvement to source code.  check it below.

 

(defun C:ABP (/ insert_sset insert_object insertion_point pline_sset pline_object)
	(if (setq insert_sset (ssget "_:l" '((0 . "insert"))))
			(foreach insert_object  (mapcar 'vlax-ename->vla-object (vl-remove-if 'listp (mapcar 'cadr (ssnamex insert_sset))))
				(setq insertion_point (trans (vlax-get insert_object 'insertionpoint) 0 1))
				(if (setq pline_sset (ssget "_cp"
				  				(mapcar '(lambda (list_1 list_2) (mapcar '+ list_1 list_2)) 
										 (list insertion_point insertion_point insertion_point)
		  		  						 (list '(-3 -3) '(-3 3) '(3 3))
				  				)
								'((0 . "lwpolyline"))
							  )
					)
					(vla-put-rotation
						insert_object
;						(+ (* 0.5 pi)
							(angle (trans '(0 0 0) 1 0)
					  			  	(vlax-curve-getFirstDeriv
					  			  			(setq pline_object (vlax-ename->vla-object (ssname pline_sset 0)))
					  			  			(vlax-curve-getParamAtPoint
					  			  				pline_object
					  			  				(vlax-curve-getClosestPointTo
					  			  					pline_object
					  			  					(trans insertion_point 1 0)
					  			  				)
					  			  			)
					  			  	)
					  		)
;						)
			   		)
				)
			)
	)
	(princ)
)

 

Message 12 of 17

timLNMHN
Contributor
Contributor

The angle always varies so that's the tricky part I guess. Also the lengths of the smaller lines are not always the same!

0 Likes
Message 13 of 17

komondormrex
Mentor
Mentor

then the things must direct vertically?

0 Likes
Message 14 of 17

Kent1Cooper
Consultant
Consultant

@komondormrex wrote:

vlax-curve* functions do not operate with enames....


Yes, they do.  There's no need to convert things to VLA objects to use those functions on them.

 

Command: L  LINE

Specify first point: ;; picked a point
Specify next point or [Undo]: ;; picked a point
Specify next point or [Undo]: ;; Enter to end Line command
Command: (vlax-curve-getStartPoint (entlast)) ;; entity name not converted to VLA object
(-15.0188 0.0474828 0.0) ;; correct coodinates of start point

Kent Cooper, AIA
0 Likes
Message 15 of 17

-didier-
Advisor
Advisor

Bonjour @komondormrex 

 

Is it necessary that the blocks are already inserted approximately or precisely on the polyline ?

And they should be straightened perpendicular to the polyline.


Or should there be a program that inserts the blocks at the intersection of the small line on the large polyline?

Before I start working on it, I’m waiting for the answer.

 

Please give us DWG to know the blocks and some details

 

Amicalement

Éternel débutant.. my site for learning : Programmer dans AutoCAD

DA

EESignature

0 Likes
Message 16 of 17

Kent1Cooper
Consultant
Consultant

@timLNMHN wrote:
The little line is the line it needs to get straight put on as the one in the photo who is correct. it is a Pline, .... the little line is the correct line for perpendicular placement!

Another question:  Is the little-line Polyline always drawn in the same direction, either toward or away from the longer path that it touches at its other end?  It seems essential that they be that way, or some Blocks will spin around 180° from the intended orientation.  Am I correct that zero rotation of the Block puts it in the orientation that looks like a question mark?

Kent Cooper, AIA
0 Likes
Message 17 of 17

timLNMHN
Contributor
Contributor

To everyone, My problem is solved. someone in the company had a lisp that already does this. I just found out!

 

everyone thanks very much for the help 🙂 cheers~!

0 Likes