Help with LISP to Insert Block at Line Midpoint and Align to Line

Help with LISP to Insert Block at Line Midpoint and Align to Line

yu85.info
Collaborator Collaborator
578 Views
9 Replies
Message 1 of 10

Help with LISP to Insert Block at Line Midpoint and Align to Line

yu85.info
Collaborator
Collaborator

Hi everyone,

I need help with a LISP routine that takes a block and places it at the midpoint of a selected LINE while aligning the block to the line’s direction. The goal is for the block to be both centered and properly rotated along the line’s angle.

I have attached a DWG file with a block and a line as an example of the expected result.

If possible, I would like the LISP to prompt me to first select the block and then select the line.

Any assistance would be greatly appreciated!

Thanks in advance.

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

pendean
Community Legend
Community Legend

@yu85.info Are you aware you can create (and recreate your) blocks that do "... properly rotated along the line’s angle..." all the time? 

pendean_0-1739978341294.png

 

Message 3 of 10

cadffm
Consultant
Consultant

Hi,

 

we are in programming board, you ask for programming help - Where is your current (not working)  code?

 

We dson't know about your skills, so I start with a very basic way: Use command _-insert

(progn
(setvar "ATTDIA" 0)
(setvar "ATTREQ" 1)
(setq LINE (entsel))
(setq LDATA (entget(car LINE)))

; (command "_.-INSERT" "M4051_E" "_mid" (cadr LINE) 0.25 0.25  (cdr(assoc (if (< (cadr (assoc 10 LDATA)) (cadr (assoc 11 LDATA))) 11 10) LDATA)) "" "" "") ; <- Works in WCS ONLY
(command "_.-INSERT" "M4051_E" "_mid" (cadr LINE) 0.25 0.25 (trans (cdr(assoc (if (< (cadr (assoc 10 LDATA)) (cadr (assoc 11 LDATA))) 11 10) LDATA)) 0 1) "" "" "")
)

 

 

offtopic: Your current blockdefinition contains 3 Attributdefinitions - ALL set to invisible!

That doesn't match the old existing blockreference, where attributes are visible.

Sebastian

Message 4 of 10

yu85.info
Collaborator
Collaborator

Thanks sir but I need to use exciting blocks of someone else and not create ones of my own.

0 Likes
Message 5 of 10

yu85.info
Collaborator
Collaborator

Thanks sir but I got no skills unfortunately. If you can kindly expand your lisp it will help me. If not, I do appreciate your help.

In addition I did not understand your comment regarding the attributes.

Thanks 

0 Likes
Message 6 of 10

cadffm
Consultant
Consultant

Hi,

 

Your Block/Attributes

The best way to show you what i am talking is, follow my steps and check out the attributs of new insert.

 

Q: What value has Command: ATTDISP<enter> ?

 

Run command Classicinsert or Insert

and place a new blockreference of this block  "M4051_E"

Take a look to the attributes, while Attdisp is set to NORMAL.

Can you see the issue? No Attributes visible.

But they are visible on the original/previous existing blockreference.

 

 

"No skills"

Okay,  perhaps another one offer a full program.

 

But have you tried my code above?

Select all my code, from the first ( to the last )

CTRL+C

 

in your sample drawing: Commandline[F2] CTRL+V

 

Select a line..

 

and your block is placed rotated in the middle of the line.

 

Or?

 

Sebastian

Message 7 of 10

Kent1Cooper
Consultant
Consultant

Try the attached MarkMidPoints.lsp with its MMP command.  It lets you mark with various kinds of things, including Blocks, and includes an Align-with-the-object rotation option.  If your "lines" may not always be Line entities per se, but may include Polyline line segments, it can do it at the midpoints of each segment of a Polyline, or at its overall midpoint.  See the comments and watch the prompts.

Kent Cooper, AIA
Message 8 of 10

pendean
Community Legend
Community Legend

@yu85.info Why is your M4051_E block scaled x10 at the mid of the lines in the sample?

And is that also a requirement for your LISP? You forgot to mention that little dire need when inserting these attributed blocks.


And if I may ask, why are all the attributes hidden? Are you manually editing each block after placement too?

Message 9 of 10

yu85.info
Collaborator
Collaborator
Accepted solution

Thank you all for your help!

Here is what I got and worked:

(defun c:MoveAlignBlock ( / blkEnt blkObj lineEnt lineObj midPt lineAngle blkAngle)
(setq blkEnt (car (entsel "\nSelect a block: "))) ; Select block
(setq lineEnt (car (entsel "\nSelect a line: "))) ; Select line
(if (and blkEnt lineEnt) ; Check if valid entities are selected
(progn
(setq blkObj (vlax-ename->vla-object blkEnt)) ; Convert block entity to object
(setq lineObj (vlax-ename->vla-object lineEnt)) ; Convert line entity to object
(setq midPt (polar (vlax-get lineObj 'StartPoint)
(angle (vlax-get lineObj 'StartPoint)
(vlax-get lineObj 'EndPoint))
(/ (distance (vlax-get lineObj 'StartPoint)
(vlax-get lineObj 'EndPoint))
2))) ; Calculate midpoint of the line
(setq lineAngle (angle (vlax-get lineObj 'StartPoint)
(vlax-get lineObj 'EndPoint))) ; Get line angle
(setq blkAngle (vla-get-rotation blkObj)) ; Get block's current rotation

;; Move block to midpoint
(vla-move blkObj
(vlax-3d-point (vlax-get blkObj 'InsertionPoint))
(vlax-3d-point midPt))

;; Rotate block to match the line angle
(vla-put-rotation blkObj lineAngle)

;; Align block attributes
(vlax-for obj (vlax-invoke blkObj 'GetAttributes)
(vla-put-Rotation obj lineAngle)
)
)
)
(princ)
)

0 Likes
Message 10 of 10

Sea-Haven
Mentor
Mentor

My $0.05

DIVIDE B BlkName 2 Y

All done.

 

0 Likes