Create code that can determine the length of polyline segments and fit into the drawing area above the segments.

Create code that can determine the length of polyline segments and fit into the drawing area above the segments.

blackcat1024
Observer Observer
143 Views
1 Reply
Message 1 of 2

Create code that can determine the length of polyline segments and fit into the drawing area above the segments.

blackcat1024
Observer
Observer

We need a code in AutoLisp for Autosad 2023 that can calculate the length of all polyline segments and write them above the segments in the drawing area. This is the code for the segment.

Here is the code for the segment.

(defun c:Lin_di ()
(while (setq obj (car (entsel "\nSelect a segment (or press Enter to exit): ")))
(setq startPoint (cdr (assoc 10 (entget obj))))
(setq endPoint (cdr (assoc 11 (entget obj))))
(setq length (distance startPoint endPoint))
(setq roundedLength (fix length)) ; Round the length to a whole number
(setq textPoint (polar startPoint (angle startPoint endPoint) (/ length 2.0)))
(setq textString (rtos roundedLength 2 0)) ; Rounded length as an integer

(command "_text" textPoint "8" "0" textString)
(princ "\nAdded an inscription with a rounded segment length.")
)
(princ "\nExiting the mode of measuring the length of segments.")
(princ)
)

0 Likes
144 Views
1 Reply
Reply (1)
Message 2 of 2

Kent1Cooper
Consultant
Consultant

[I see that works on a Line object only, not on a Polyline segment as implied in its prompt.  Also, it will always round down to the whole number below, not to the nearest whole number -- a value of 3.999 will become 3, not 4.]

 

Make a Dimension Style that has both dimension lines and both extension lines suppressed, and whose text is set to round to the nearest whole-number value, and use DimPoly.lsp, >here<.  The DPI command puts the Dimensions on the Inside of the Polyline perimeter(s), DPO on the Outside.  Read the comments at the top of the file, and within it.

 

The big advantage of using Dimensions instead of Text is that if you Stretch the shape of things, for line segments the contents and the positionings and the alignments of the texts will adjust themselves automatically.

 

I use it with the Dimension Style defined for the text orientation to be parallel with the dimension-line direction, but you can define it to always have the text horizontal.  And if you really want the Polyline segments to run through the middle of the text contents, as it looks like yours does, change this much:

                  (command ; complete Dimension: dimension line location
                    (polar
                      pt2
                      (apply ; angle
                        (if (or (and cw (= side "in")) (and (not cw) (= side "out"))) '- '+)
                        (list
                          (angle '(0 0 0) (vlax-curve-getFirstDeriv pl (- inc 0.5)))
                          (/ pi 2)
                        ); list
                      ); apply
                      (* styht 1.5); distance
                        ;; [If you don't use stacked fractions, consider using styht without multiplier]
                    ); polar
                  ); command

to just:

    (command pt1)

 

Kent Cooper, AIA