Automatic Centerline LISP

Automatic Centerline LISP

Anonymous
Not applicable
2,539 Views
3 Replies
Message 1 of 4

Automatic Centerline LISP

Anonymous
Not applicable

Hi all !

Recently I got several drawings and need to modify them, so I created a rough but working lisp for changing lines, but I want to make an lisp that would change the lines inside the circles from 0 to Center and linetype to ByLayer. Here's my code, but it doesen't make the assoc function.

 

(defun C:caxis ()
  (setq axis (ssget "X" '((8 . "0") (0 . "Circle"))))
  (setq wtf (car axis))
  (setq coord (cdr (assoc 10 (entget wtf))))
  (Setq lngth (sslength axis))
  (repeat lngth
    (setq s1 (car coord))
    (setq s2 (cdr coord))
    (setq x1 (- 1 s1))
    (setq x2 (- 1 s2))
    (setq x3 (+ 1 s1))
    (setq x4 (+ 1 s2))
    (setq x5 (ssget "_C" '(x1 x2) '(x3 x4) ))
    (command ".chprop" x5 "" "LA" "Center" "LT" "ByLayer" "")
     )
)

 

I can't really understand the assoc function, maybe it's impossible to make using it because it's has many enteties to analyse ?

Thanks in advance

Eugene.

0 Likes
Accepted solutions (1)
2,540 Views
3 Replies
Replies (3)
Message 2 of 4

Kent1Cooper
Consultant
Consultant
Accepted solution

You appear to be confusing the nature of what is returned by (ssget) with what is returned by (entsel).  From (entsel), you can get the entity name of the selected object with (car) [the first element in a list of an entity and the selection point], but from (ssget), you need to use (ssname) to get an entity out of it.

 

Try something like this kind-of minimal adjustment [untested]:

 

(defun C:caxis (/ axis n cir coord s1 s2 x1 x2 x3 x4 x5); localize variables
  (setq axis (ssget "X" '((8 . "0") (0 . "Circle")))); all Circles on Layer 0
  (repeat (setq n (sslength axis)); number of Circles found

    (setq

      cir (ssname axis (setq n (1- n))); start with last one, count forward one each time through

      coord (cdr (assoc 10 (entget cir)))

      s1 (car coord)
      s2 (cdr coord)
      x1 (- 1 s1)
      x2 (- 1 s2)
      x3 (+ 1 s1)
      x4 (+ 1 s2)
      x5 (ssget "_C" '(x1 x2) '(x3 x4))

    ); setq
    (command ".chprop" x5 "" "LA" "Center" "LT" "ByLayer" "")
  ); repeat
); defun

 

But that can be shortcutted considerably, with far fewer variables:

 

(defun C:caxis (/ cirss n cir ctr ctrents); localize variables
  (setq cirss (ssget "X" '((8 . "0") (0 . "Circle")))); all Circles on Layer 0
  (repeat (setq n (sslength cirss)); number of Circles found

    (setq

      cir (ssname cirss (setq n (1- n))); start with last one, count forward one each time through

      ctr (cdr (assoc 10 (entget cir)))

      ctrents (ssget "_C" (mapcar '- ctr '(1 1 0)) (mapcar '+ ctr '(1 1 0)))

    ); setq
    (command ".chprop" ctrents "" "LA" "Center" "LT" "ByLayer" "")
  ); repeat
); defun

 

Or if what you're after are always things such as center-Lines that do pass precisely through the center of the Circle, you can make a zero-size crossing window:

....

      ctrents (ssget "_C" ctr ctr)

....

That has the advantage that it's not limited to working as expected with only Circles with a radius larger than the square root of 2.  In the case of Circles that size or smaller, the original approach will also change the properties of the Circle itself, and possibly other things you don't want, although that can be mitigated at least somewhat by object-type filtering in (ssget).

 

You might also consider putting some (if) tests in there about whether it found any Circles, or anything going through the middle of them, before proceeding, because it will run into problems if it tries to work with nil as a selection set.

Kent Cooper, AIA
0 Likes
Message 3 of 4

Anonymous
Not applicable

I'm new to the LISP and knew that one of (entget) or (assoc) aren't used properly, but didn't knew the alternatives.

Thanks for help, Kent !

0 Likes
Message 4 of 4

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

I'm new to the LISP and knew that one of (entget) or (assoc) aren't used properly, but didn't knew the alternatives.

Thanks for help, Kent !


You're welcome.  You really weren't "improper" in the use of those two functions in particular, as far as I could see, but rather the problem was with the getting of entities out of a selection set.  It's always informative to read up about Lisp functions in Help, where you may well learn other things about them that you'll have a use for another time.

Kent Cooper, AIA
0 Likes