auto lisp to insert a block, a distance from the middle point of all selected lines?

auto lisp to insert a block, a distance from the middle point of all selected lines?

mkroll9in5in
Enthusiast Enthusiast
3,096 Views
32 Replies
Message 1 of 33

auto lisp to insert a block, a distance from the middle point of all selected lines?

mkroll9in5in
Enthusiast
Enthusiast

I have a part with different edges, in different layers. I can write the macro to isolate the individual layers. All I need is a lisp to place a block by name, at a set distance, from all selected line segment midpoints. In the lisp I wish to be able to set the block name and offset distance. The trick is what side will it offset on. how will it know what is the inside of the part?  if need be, both sides.

0 Likes
3,097 Views
32 Replies
Replies (32)
Message 21 of 33

Sea-Haven
Mentor
Mentor

Explode text no need for a block is that ok ? New version. I have to tweak a bit about text offset from sides. adding a X or y value to mp. Next go.

 

 

(defun c:Labelbox ( / oldsnap )
(setq oldsnap (getvar 'osmode))
(setvar 'osmode 0)
(setvar 'angbase 0)
(setvar 'angdir 0)
(setvar 'aunits 3)
(setvar 'textstyle "Standard")
(command "bpoly" (getpoint "\nPick point inside ") "")
(setq co-ord (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 10)) (entget (entlast)))))
(setq co-ord (cons (last co-ord) co-ord))
(command "erase" (entlast) "")
(repeat (setq x (- (length co-ord) 1))
(setq pt1 (nth x co-ord) pt2 (nth (- x 1) co-ord))
(setq mp (mapcar '* (mapcar '+ pt1 pt2) '(0.5 0.5)))
(setq ang (angle pt1 pt2))
(alert (rtos ang 2 6))
(setq ent (entget (ssname (ssget mp) 0)))
(setq lay (cdr (assoc 8 ent)))
(cond
((= lay "3_POLISH")(setq str "P" add "Y"))
((= lay "1_MITER")(setq str "F" add "Y"))
((= lay "5_SEAM")(setq str "Seam" add "Y"))
((setq add "N"))
)
(if (= add "Y")
(progn
(cond 
((equal ang (/ pi 2) 1e-06)(setq j "BL"))
((equal ang (* 1.5 pi) 1e-06)(setq ang (+ pi ang) j "BR"))
((equal ang pi 1e-06)(setq ang (- pi (/ pi 2)) j "BL"))
)
(command "text" "J" j mp 2.5 (- ang (/ pi 2.)) str)
(command "txtexp" (entlast) "")
)
)
(setq x (1- x))
)
(setvar 'aunits 1)
(setvar 'angdir 1)
(setvar 'osmode oldsnap)
(princ)
)
(c:labelbox)

 

 

 

0 Likes
Message 22 of 33

komondormrex
Mentor
Mentor

@mkroll9in5in 

what is the shx font name you are using for marking segments etc?

0 Likes
Message 23 of 33

mkroll9in5in
Enthusiast
Enthusiast
I'm going to give this one a shot. I really want to thank you for helping me on this. I do by all means want to learn LISP because I can see its power. I'm already pretty good with macros. I'm just getting my hands on it now with AutoCAD 2024 LT.

Since you sent me the last code it got me thinking of another added possibility. So I rephrased the question and I just noticed you can add sample drawings, so I did that as well.
Please see the new question below. I really think this could become something a little more.

"macro or lisp to auto insert blocks at center of part and just inside the midpoint of surrounding line segments based on layer or pin color"

I'm thinking this should be to hard to add on, as I number the parts 1,2,3... it can auto label and dimension each part. This would eliminate 100's of clicks per job. Please see new question, sample drawing is there.
0 Likes
Message 24 of 33

Sea-Haven
Mentor
Mentor

Sample dwg ?

0 Likes
Message 25 of 33

komondormrex
Mentor
Mentor

instead of sample file)

MK.gif

0 Likes
Message 26 of 33

mkroll9in5in
Enthusiast
Enthusiast

Your first one was nice, how it prompted you to select the inner area of each part, that is what got me thinking. I had rephrased the question due to that first one you sent. please re-read below if you haven't already. I like that action of it prompting you to select the inner portion of each part and it label the inner side of each edge according to pin color or layer name, inserting a block just on the inside associated with that edge and explode it after placement. It doesn't seem too far fetched to have it also add dimensions just on the outside at the same time.

"I'm looking for a macro or LISP, now on LT 2024, to insert consecutive number blocks for each part selected at center of part and label the edges with blocks just on the inside at (4"or so for 2" block) from the midpoint of surrounding line segments based on layer or pin color of that segment. also auto dimension just outside of each segment at (3" to 4"). 
I made a macro to insert the numbers consecutively and explode them 1,2,3... as you place them.
^C^C_-layer;s;0;;_-insert;block1;\;;;_x;l;_-insert;block2;\;;;_x;l;...
I have a LISP that works very similar to what I am looking for, but I'm rephrasing my question with hopefully more details to get my point as clearly as I can. The reason for the blocks that look like text is for a laser projector on a CNC. I'm going to add the LISP I currently have, created by Sea-Haven, below the image.

(defun c:wow ( / oldsnap )
;The dtr function converts degrees to radians
;The rtd function converts radians to degrees
(defun dtr (a)
(* pi (/ a 180.0))
)
;
(defun rtd (a)
(/ (* a 180.0) pi)
)

(setq oldsnap (getvar 'osmode))
(setvar 'osmode 0)
(setvar 'angbase 0)
(setvar 'textstyle "Standard")

(command "bpoly" (getpoint "\nPick point inside ") "")

(setq co-ord (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 10)) (entget (entlast)))))
(setq co-ord (cons (last co-ord) co-ord))
(command "erase" (entlast) "")

(repeat (setq x (- (length co-ord) 1))
(setq pt1 (nth x co-ord) pt2 (nth (- x 1) co-ord))
(setq mp (mapcar '* (mapcar '+ pt1 pt2) '(0.5 0.5)))
(setq ang (angle pt1 pt2))
(setq ent (entget (ssname (ssget mp) 0)))
(setq lay (cdr (assoc 8 ent)))
(command "text" mp 2.5 (rtd (- ang (/ pi 2.))) lay)
(princ (strcat "\n" (rtos ang 2 4)))
(setq x (1- x))
)

(setvar 'osmode oldsnap)
(princ)
)
(c:wow)

 

0 Likes
Message 27 of 33

mkroll9in5in
Enthusiast
Enthusiast

that works pretty nice but seeing that you are having to select around the part is where that may not work. the parts with seams will inevitably be connected to another part. Sea-Haven's LISP asks you to select the inner portion of the part, utilizing the bpoly command in some fashion. I'm hoping for something to work as smooth as my numbering macro but added auto edge labeling & dimension as I single click number each part.
^C^C_-layer;s;0;;_-insert;Block1;\;;;_x;l;_-insert;Block2;\;;;_x;l;...

0 Likes
Message 28 of 33

komondormrex
Mentor
Mentor

i am truly do not think so)

MK_2.gif

Message 29 of 33

mkroll9in5in
Enthusiast
Enthusiast
To tell you the truth, that's pretty nice. I still feel the single click on the inner portion of each part can be done. Even if it doesn't number it, just label and dimension would remove so many clicks. I'm just starting my journey down the LISP rabbit hole. I have been dreaming of something that does this trick for at lest 15 years doing countertops. I have been stuck on LT the hole time.
0 Likes
Message 30 of 33

komondormrex
Mentor
Mentor

not a smallest doubt on that one. hate command methods.

0 Likes
Message 31 of 33

mkroll9in5in
Enthusiast
Enthusiast

hello Sea-Haven,
          It has been a while since you created this lisp for me that got me started down this rabbit hole. Between the one you started, Chat-GPT and I have gotten a lot closer to my goal while learning a lot about lisp asking the AI to break things down. I have this one running as a hybrid macro/lisp inserting a number series of blocks of segments in the shape of numbers 1-100, for a laser projector as text will not work.

Where I am still running into issue is having the rotation angle of each edge label blocks applied by the LISP (not the number blocks applied by the macro) match the angle of the line segment it is adjacent to. If the block were single letters this wouldn't be an issue, but seam is spelled out. so getting it to align to the edge in which it is associated would be nice.

Other than that, thank you for guiding me in the write direction with this one. It has been a button on my mind for a long time. Next is to try to tie in for it to apply dimensions to the outside of the same segments at their endpoints and the endpoints of a poly-line (excluding the individual segments of that poly-line). but its one step at a time.

^C^C_model;_z;e;_-layer;s;0;;_LBL;\_-insert;1A;\;;;_x;l;_LBL;\_-insert;2A;\;;;_x;l;....................

0 Likes
Message 32 of 33

Sea-Haven
Mentor
Mentor

Be careful using Chatgp it can provide code that is not very useful so would be confusing to you and when running end up with errors.

0 Likes
Message 33 of 33

Kent1Cooper
Consultant
Consultant

@mkroll9in5in wrote:

....
^C^C_model;_z;e;_-layer;s;0;;_LBL;\_-insert;1A;\;;;_x;l;_LBL;\_-insert;2A;\;;;_x;l;....................


Are you aware that there's a simpler way to get the pieces of a Block into a drawing than to Insert it and then Explode it?  You can Insert it "pre-Exploded."  Prefix the Block name with an asterisk, and you can leave out the Exploding:

...._LBL;\_-insert;*1A;\;;_LBL;\_-insert;*2A;\;;.....

 

And note only two semicolons after the positioning, because it will ask for only one scale factor.

 

It does have the potential drawback, if it is one for you, that you don't see it "drag" as you move the cursor to place it.  But if you're always placing it based on something like an Osnap location, maybe that won't bother you.

Kent Cooper, AIA
0 Likes