Lisp to Sort Line by Length & Change Color

Lisp to Sort Line by Length & Change Color

Anonymous
Not applicable
2,956 Views
13 Replies
Message 1 of 14

Lisp to Sort Line by Length & Change Color

Anonymous
Not applicable

Need a lisp routine to sort a small number of lines according to differing lengths and change their color by their respective lengths. Have searched the Internet and cannot seem to find what seems to be quite simple routine to me (i.e. - one without great programming skills). All help is greatfully appreciated!

0 Likes
Accepted solutions (1)
2,957 Views
13 Replies
Replies (13)
Message 2 of 14

hmsilva
Mentor
Mentor

As a 'demo'

 

(vl-load-com)
(defun c:demo (/ i len obj ss)
 ; is select lines not in locked layers
   (if (setq ss (ssget "_:L" '((0 . "LINE"))))
 ; repeat and set i with the selection set length
      (repeat (setq i (sslength ss))
 ; decreasing one to i, get ssname and set obj as a vlaObject
         (setq obj (vlax-ename->vla-object (ssname ss (setq i (1- i))))
 ; set len with the length property from obj
               len (vla-get-length obj)
         )
         (cond ( ; if len is in between 0.0 and 10.0
                (<= 0.0 len 10.0)
 ; put color 2 to obj
                (vla-put-color obj 2)
               )
               ( ; if len is in between 10.0 and 20.0
                (<= 10.0 len 20.0)
 ; put color 3 to obj
                (vla-put-color obj 3)
               )
               ( ; if len is in between 20.0 and 30.0
                (<= 20.0 len 30.0)
 ; put color 4 to obj
                (vla-put-color obj 4)
               )
 ; if len is over 30.0
               (T
 ; put color 5 to obj
                (vla-put-color obj 5)
               )
         )
      )
   )
   (princ)
)

 

 

 

Hope this helps,
Henrique

EESignature

0 Likes
Message 3 of 14

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

Need a lisp routine to sort a small number of lines according to differing lengths and change their color by their respective lengths. ....


Would PolylineClassify.lsp provide a starting point for you?  It differentiates by Polyline areas, and creates Layers for each classification, but the same principles could be applied to Line lengths and assign colors instead.

Kent Cooper, AIA
0 Likes
Message 4 of 14

Anonymous
Not applicable

Henrique,

 

Thanks for the routine! When I tried it though, it only changed one of four selected lines of differing lengths to a differnt color (i.e. - Cyan). Is there something I am missing?

 

0 Likes
Message 5 of 14

phanaem
Collaborator
Collaborator
Accepted solution

You might already have your answer, but here is my solution:

 

(defun C:TEST ( / *error* a b ss l d)
;;;  (setq *error* (err)) ; <- add your error function here
  (if
    (setq a 1e308 b 0 ss (ssget "_:L" '((0 . "LINE"))))
    (progn
      (repeat (setq i (sslength ss))
        (setq l
          (cons
            (list
              (setq e (ssname ss (setq i (1- i))))
              (setq d
                (vlax-curve-getdistatpoint e
                  (vlax-curve-getendpoint e)
                )
              )
            )
            l
          )
        )
        (setq
          a (min a d)
          b (max b d)
        )
      )
      (if
        (= a b)
        (princ (strcat "\nSelected lines are equal: " (rtos a)))
        (foreach x l
          (entmod
            (list
              (cons -1 (car x))
              (cons 62 (1+ (fix (+ 0.5 (/ (* 254.0 (- (cadr x) a)) (- b a))))))
            )
          )
        )
      )
    )
  )
;;;  (*error* nil)
  (princ)
)
Message 6 of 14

hmsilva
Mentor
Mentor

@Anonymous wrote:

Henrique,

 

Thanks for the routine! When I tried it though, it only changed one of four selected lines of differing lengths to a differnt color (i.e. - Cyan). Is there something I am missing?

 


rayf-man1967,

the demo is just a starting point for you.

The code test for lengths from 0.0 to 30.0, and change to color 2, 3 and 4... You'll have to change/add new conditions to test for other lengths...

 

Henrique

EESignature

0 Likes
Message 7 of 14

Anonymous
Not applicable

Great! Just what I was looking for, though Henrique was on the right track. I would like to have something that would work on lines regardless of line length restraints written into the code that I would have to change. I realize that I might still be limited by color considering that AutoCAD has but 256 standard colors, but that should not be an issue for what I need. Thanks again everyone!

0 Likes
Message 8 of 14

Anonymous
Not applicable

After some use, noticed that this lisp seems to have a limitation of around 13 decimals places. Past that it starts identifying lines of differing lengths as having the same length. What, if any, aspect of the code can be modified to get accuracy levels past 13 decimals places?

0 Likes
Message 9 of 14

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

.... this lisp seems to have a limitation of around 13 decimals places. Past that it starts identifying lines of differing lengths as having the same length. ....


Wow....  Are we talking angstroms here, or what?

 

If I read it correctly, the routine divides the range of length between the shortest and longest Lines into 254 equal pieces, and gives a color to Lines within each sub-range representing where that sub-range lies within the overall range.  If two Lines are given the same color, it isn't identifying them as having the same length, but only as falling within the same 1/254th sub-range of the overall length range.  If the overall range between shortest and longest is 254 units, each sub-range will be 1 unit long, and a Line that's, say, 54.0000000253 units longer than the shortest Line will fall in the same sub-range, and therefore be given the same color, as something 54.99999999418 units longer.  Greater precision is not going to make any difference to that determination, except perhaps with Lines whose lengths fall very close to the break points between sub-ranges.

 

If you have Lines that differ in length only down at the 13th decimal place or beyond, and you want them given different colors, maybe what you want is a different method of categorization.  Should it sort the Lines by length, and regardless of how much difference there is in the lengths of adjacent Lines in the sorted list, give them colors based on their position in the list, rather than their actual length relative to the overall range?

 

AutoCAD can't handle anything beyond 16 significant figues, and the number of decimal places that limits it to depends on what there is before the decimal point, and if only a 0, how many more 0's there are after the decimal point before it gets to non-0 numerals.  So whether it can think about things beyond 13 decimal places depends on the kinds of sizes you're talking about.

 

 

 

Kent Cooper, AIA
0 Likes
Message 10 of 14

Anonymous
Not applicable

Swooooosh - You about cleaned the rest of the hair off my head with that reply, lol. In a nut shell (and this re-visits a different post I've made on line length issues with AutoCAD), we use a custom made LISP program to process steel joist to a spreadsheet bill of material, which separates joists of differing lengths. The issue is that, certain joists are being separated at times when their length does not show to be different in AutoCAD. That is why I am trying to segregate them based on their exact length, or as precisely as I can.

0 Likes
Message 11 of 14

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

... we use a custom made LISP program to process steel joist to a spreadsheet bill of material, which separates joists of differing lengths. ... certain joists are being separated at times when their length does not show to be different in AutoCAD. ... I am trying to segregate them based on their exact length, or as precisely as I can.


Hmmm....  Knowing what I do about steel joists [and that's considerable -- I used to work for a supplier of structural steel, and drew joist layouts and erection plans and details and so on], it's really hard to imagine how 13 decimal places could make any difference to anything.  An quarter or maybe an eighth of an inch was about as tight a tolerance as we would ever try to use.

 

If phanaem's routine is doing that, there must be Lines that are right on the cusp between sub-ranges of length.  Maybe you need it to round the Line lengths to the closest, say, 0.2 of a drawing unit, and round the sub-range division points to the nearest 0.1 multiple that is not a 0.2 multiple [0.1, 0.3, 0.5 etc., like FM radio frequencies in MHz, in the US at least], so that all Line length determinations will clearly be inside a sub-range, and nothing will fall on the cusp.  It would take some additional code, since it's more complicated than the plain division currently used, but it's certainly doable.

Kent Cooper, AIA
0 Likes
Message 12 of 14

Anonymous
Not applicable

You are correct in your summation about the importance of the overall length when built, but I believe this to be an issue with the custom lisp determining the length too precisely (if that's possible) during output to Excel, which still shows the joists are the same length but still palces under a different mark number for fabrication. I still have not determined why certain lines joists of the same type are being seen in different lengths anyway when those who have the issue "say" they simply created the first one and copied it, but, from tests I've done, something is minutely differnt about the lines and I would like to rule out the length issue first since some have tld me that has been a porblem with similar programs in times past. I also thought about a program that could "true up" lines of the intended same length, which would be fix too. Any ideas?

0 Likes
Message 13 of 14

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

.... I also thought about a program that could "true up" lines of the intended same length, which would be fix too. Any ideas?


There have been several threads here in the past about routines to force things like Line endpoints to the nearest multiple in all directions of some User-specified increment [i.e. onto a grid, at whatever grid value you like, and independent of AutoCAD's GRID functionality].  You can Search for one and use it to make sure all such Line endpoints lie exactly on the closest, for example, 1/8"- or 1/4"- or 1"-multiple [or whatever] position.

Kent Cooper, AIA
0 Likes
Message 14 of 14

vladimir_michl
Advisor
Advisor

You can also use the classification utility AutoClass - see:

http://www.cadstudio.cz/autoclass

 

AutoClass5.png

 

Vladimir Michl, www.cadstudio.cz  www.cadforum.cz