Jump to content

moving text to the mid point of a line


MJLM

Recommended Posts

I m having issues with this routine. I made it to have two texts on each side (above & below) of a line move to the middle of the line if its length is changed. Lets say I want to make a line shorter or longer. By running this routine the text location should updated to the new mid point of the line.

 

The texts are offset by each side by half the distance of dimscale variable. I am taking advantage of the handles to select the texts as they have always +1 & +2 the handle name of the line respectively.

 

I am not sure what I m doing wrong but the two texts do not come on the right location. Either on the one third or two thirds of the line or sometimes it is correctly centered but in the perpendicular to the line direction it is not correct (comes a bit lower that it should).

 

Any help would be appreciated.

 

 

(defun c:ht (/)       

   (setq sslns (ssget '((0 . "LINE"))))

   (setq cnthdl (sslength sslns))
   (setq offset (/ (getvar "dimscale") 2))

   (while (>= (setq cnthdl (1- cnthdl)) 0)    

   (setq tpent (ssname sslns cnthdl))
     (setq lndt (entget tpent))

   (setq lnlng (distance (cdr (assoc 10 lndt)) (cdr (assoc 11 lndt))))

   (setq lndx (- (cadr (assoc 11 lndt)) (cadr (assoc 10 lndt))))
   (setq lndy (- (caddr (assoc 11 lndt)) (caddr (assoc 10 lndt))))
   (setq lndz (- (cadddr (assoc 11 lndt)) (cadddr (assoc 10 lndt))))

   (setq gpln (list (+ (cadr (assoc 10 lndt)) (/ lndx 2)) (+ (caddr (assoc 10 lndt)) (/ lndy 2)) (+ (cadddr (assoc 10 lndt)) (/ lndz 2))))    

   (if (/= lndy 0) 

       (setq txtendpt (list (- (+ (cadr (assoc 10 lndt)) (/ lndx 2)) offset) (+ (caddr (assoc 10 lndt)) (/ lndy 2)) (+ (cadddr (assoc 10 lndt)) (/ lndz 2))))

       (setq txtendpt (list (+ (cadr (assoc 10 lndt)) (/ lndx 2)) (+ (+ (caddr (assoc 10 lndt)) (/ lndy 2)) offset) (+ (cadddr (assoc 10 lndt)) (/ lndz 2))))
   )
   
   (setq txthdl (dec2hex (1+ (hex2dec (cdr (assoc 5 lndt))))))

   (setq txtprop (entget (handent txthdl)))

   (setq txtdimhdl (dec2hex (+ (hex2dec (cdr (assoc 5 lndt))) 2)))
   
   (setq txtdimprop (entget (handent txtdimhdl)))

   (setq 2txts (ssadd (handent txthdl) (ssadd (handent txtdimhdl))))

   (setq gptxt1 (cdr (assoc 11 txtdimprop)))

   (command "move" 2txts "" gptxt1 txtendpt)
   
   )

(princ)

)

Link to comment
Share on other sites

A different approach you are picking line so have start and endpt assoc 10&11 you get distance stpt endpt, then get ANGLE stpt endpt. Avoid all the assoc's

 

ok now use polar divide distance / 2.0 from stpt at angle then add to angle 90+ & 90 - use polar again to work out txtpt1 & 2

 

 (seqt stpt (cdr (assoc 10 lndt)))
(setq endpt (cdr (assoc 11 lndt)))
(setq lnlng (/ (distance stpt endpt )2.0))
(setq ang (angle stpt endpt))
(setq midpt (polar stpt lnlng ang))
(setq txt1 (polar midpt (getvar "dimscale") (+ (/ pi 2.0) ang)) ; note pi is a reserved variable name
(setq txt2 (polar midpt (getvar "dimscale") (- (/ pi 2.0) ang))

Link to comment
Share on other sites

Thank you. Your help was enlightening for me. I did adjust my code according to yours but it still behaves not as expected. The text flies to various locations. I really don't understand what s going wrong. Both the above codes should have worked fine. Do you believe it has to do with variables holding leftover values or what?

 

Below I ve attached the file with two lines and text. The lower is as it should be. The upper is after the execution of the code.

 

 

(defun c:ht (/)		

(setq txtsta nil txtend nil)

(setq sslns (ssget '((0 . "LINE"))))

(setq cnthdl (sslength sslns))
(setq offset (/ (getvar "dimscale") 2))

(while (>= (setq cnthdl (1- cnthdl)) 0)	

(setq tpent (ssname sslns cnthdl))
       (setq lndt (entget tpent))

(setq lnlng (/ (distance (cdr (assoc 10 lndt)) (cdr (assoc 11 lndt))) 2))
(setq ang (angle (cdr (assoc 10 lndt)) (cdr (assoc 11 lndt))))
(setq midpt (polar (cdr (assoc 10 lndt)) lnlng ang))

(setq txthdl (dec2hex (1+ (hex2dec (cdr (assoc 5 lndt))))))
(setq txtprop (entget (handent txthdl)))

(setq txtdimhdl (dec2hex (+ (hex2dec (cdr (assoc 5 lndt))) 2)))
(setq txtdimprop (entget (handent txtdimhdl)))

(setq 2txts (ssadd (handent txthdl) (ssadd (handent txtdimhdl))))

(setq txtsta (cdr (assoc 11 txtdimprop)))
(setq txtend (polar midpt offset (+ (/ pi 2.0) ang)))

(command "move" 2txts "" txtsta txtend)

)
(princ)

)

 

line_2txts.dwg

Link to comment
Share on other sites

While I don't quite understand all that you are trying to do, this is a simple line labeler using entmake :

 

[b][color=BLACK]([/color][/b]defun c:lnlabel [b][color=FUCHSIA]([/color][/b]/ tof ss i en ed p10 p11 la ta mpt tp bp[b][color=FUCHSIA])[/color][/b]

 [b][color=FUCHSIA]([/color][/b]setq tof [b][color=NAVY]([/color][/b]getvar [color=#2f4f4f]"DIMSCALE"[/color][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]

 [b][color=FUCHSIA]([/color][/b]while [b][color=NAVY]([/color][/b]setq ss [b][color=MAROON]([/color][/b]ssget '[b][color=GREEN]([/color][/b][b][color=BLUE]([/color][/b]0 . [color=#2f4f4f]"LINE"[/color][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]
        [b][color=NAVY]([/color][/b]setq i 0[b][color=NAVY])[/color][/b]
        [b][color=NAVY]([/color][/b]while [b][color=MAROON]([/color][/b]setq en [b][color=GREEN]([/color][/b]ssname ss i[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
               [b][color=MAROON]([/color][/b]setq ed [b][color=GREEN]([/color][/b]entget en[b][color=GREEN])[/color][/b]
                    p10 [b][color=GREEN]([/color][/b]cdr [b][color=BLUE]([/color][/b]assoc 10 ed[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]
                    p11 [b][color=GREEN]([/color][/b]cdr [b][color=BLUE]([/color][/b]assoc 11 ed[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]
                    la [b][color=GREEN]([/color][/b]angle p10 p11[b][color=GREEN])[/color][/b]
                    ta [b][color=GREEN]([/color][/b]if [b][color=BLUE]([/color][/b]< [b][color=RED]([/color][/b]* pi 0.5[b][color=RED])[/color][/b] la [b][color=RED]([/color][/b]* pi 1.5[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b] [b][color=BLUE]([/color][/b]+ la pi[b][color=BLUE])[/color][/b] la[b][color=GREEN])[/color][/b]
                    mpt [b][color=GREEN]([/color][/b]mapcar '[b][color=BLUE]([/color][/b]lambda [b][color=RED]([/color][/b]a b[b][color=RED])[/color][/b] [b][color=RED]([/color][/b]* [b][color=PURPLE]([/color][/b]+ a b[b][color=PURPLE])[/color][/b] 0.5[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b] p10 p11[b][color=GREEN])[/color][/b]
                    tp [b][color=GREEN]([/color][/b]polar mpt [b][color=BLUE]([/color][/b]+ ta [b][color=RED]([/color][/b]* pi 0.5[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b] tof[b][color=GREEN])[/color][/b]
                    bp [b][color=GREEN]([/color][/b]polar mpt [b][color=BLUE]([/color][/b]+ ta [b][color=RED]([/color][/b]* pi 1.5[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b] tof[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]

               [b][color=MAROON]([/color][/b]entmake [b][color=GREEN]([/color][/b]list [b][color=BLUE]([/color][/b]cons 0 [color=#2f4f4f]"TEXT"[/color][b][color=BLUE])[/color][/b]
                              [b][color=BLUE]([/color][/b]cons 1 [color=#2f4f4f]"ABOVE"[/color][b][color=BLUE])[/color][/b]
                              [b][color=BLUE]([/color][/b]cons 7 [b][color=RED]([/color][/b]getvar [color=#2f4f4f]"TEXTSTYLE"[/color][b][color=RED])[/color][/b][b][color=BLUE])[/color][/b]
                              [b][color=BLUE]([/color][/b]cons 8 [b][color=RED]([/color][/b]getvar [color=#2f4f4f]"CLAYER"[/color][b][color=RED])[/color][/b][b][color=BLUE])[/color][/b]
                              [b][color=BLUE]([/color][/b]cons 10 tp[b][color=BLUE])[/color][/b]
                              [b][color=BLUE]([/color][/b]cons 11 tp[b][color=BLUE])[/color][/b]
                              [b][color=BLUE]([/color][/b]cons 40 [b][color=RED]([/color][/b]getvar [color=#2f4f4f]"TEXTSIZE"[/color][b][color=RED])[/color][/b][b][color=BLUE])[/color][/b]
                              [b][color=BLUE]([/color][/b]cons 41 1.0[b][color=BLUE])[/color][/b]
                              [b][color=BLUE]([/color][/b]cons 50 ta[b][color=BLUE])[/color][/b]
                              [b][color=BLUE]([/color][/b]cons 72 4[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]

               [b][color=MAROON]([/color][/b]entmake [b][color=GREEN]([/color][/b]list [b][color=BLUE]([/color][/b]cons 0 [color=#2f4f4f]"TEXT"[/color][b][color=BLUE])[/color][/b]
                              [b][color=BLUE]([/color][/b]cons 1 [color=#2f4f4f]"BELOW"[/color][b][color=BLUE])[/color][/b]
                              [b][color=BLUE]([/color][/b]cons 7 [b][color=RED]([/color][/b]getvar [color=#2f4f4f]"TEXTSTYLE"[/color][b][color=RED])[/color][/b][b][color=BLUE])[/color][/b]
                              [b][color=BLUE]([/color][/b]cons 8 [b][color=RED]([/color][/b]getvar [color=#2f4f4f]"CLAYER"[/color][b][color=RED])[/color][/b][b][color=BLUE])[/color][/b]
                              [b][color=BLUE]([/color][/b]cons 10 bp[b][color=BLUE])[/color][/b]
                              [b][color=BLUE]([/color][/b]cons 11 bp[b][color=BLUE])[/color][/b]
                              [b][color=BLUE]([/color][/b]cons 40 [b][color=RED]([/color][/b]getvar [color=#2f4f4f]"TEXTSIZE"[/color][b][color=RED])[/color][/b][b][color=BLUE])[/color][/b]
                              [b][color=BLUE]([/color][/b]cons 41 1.0[b][color=BLUE])[/color][/b]
                              [b][color=BLUE]([/color][/b]cons 50 ta[b][color=BLUE])[/color][/b]
                              [b][color=BLUE]([/color][/b]cons 72 4[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
               [b][color=MAROON]([/color][/b]setq i [b][color=GREEN]([/color][/b]1+ i[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]
[b][color=FUCHSIA]([/color][/b]prin1[b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b]

 

 

You could the same basic engine for (entmod) editing groups 11 and 50 for midpoint and text angle.

 

-David

Link to comment
Share on other sites

An alternative is to use dims and turn off all the arrow stuff obiviously the length will update and the other text will move to centre also, just do a crossing window around end so it picks up the hidden dim locators. You will need to create a DIM style.

Link to comment
Share on other sites

I managed to resolve that with the help of David modifying his code a bit as I wanted to make it work also for lines rising in Z direction. Thanks again.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...