Jump to content

Color changing in AutoLisp


Amalia

Recommended Posts

Hi! So I have this Lisp code that selects all circles found in the drawing and moves them to have the same center (specified by user). I want to also change the colors of the circles to red ( or anything else really ) but I have no idea how to do that. Can anyone help me?

 

(defun C:CIRCLES (/ ss n)
 
 (setq newCenter (getpoint "The new center: "))
 (setq circles (ssget "X" '((0 . "CIRCLE")) ))
 (setq n (sslength circles))
 (setq x 0)
  (repeat n
    (setq name (ssname circles x))
    (setq circle (entget name))
    (setq center (cdr (assoc 10 circle)))
    (setq circle (subst (cons 10 noulCentru) (assoc 10 circle) circle))
    (entmod circle)
    (setq x (+ 1 x))
    )
(princ)
) 

 

 

 

 

Edited by CADTutor
Code moved to code block
Link to comment
Share on other sites

I would suggest the following:

(defun c:circles ( / i s x )
    (cond
        (   (not (setq s (ssget "_X" (list '(0 . "CIRCLE") (if (= 1 (getvar 'cvport)) (cons 410 (getvar 'ctab)) '(410 . "Model"))))))
            (princ "\nNo circles were found in the active drawing layout or viewport.")
        )
        (   (setq c (getpoint "\nSpecify new centre: "))
            (repeat (setq i (sslength s))
                (setq i (1- i)
                      x (entget (ssname s i))
                      x (subst (cons 10 (trans c 1 (cdr (assoc 210 x)))) (assoc 10 x) x)
                      x (append x '((62 . 1)))
                )
                (entmod x)
            )
        )
    )
    (princ)
)

 

A few point to note on the above:

  • I use a cond statemtent to test whether the selection set exists prior to evaluating other selection set functions (such as sslength) - else these functions will error if supplied with a null selection set.
  • I use the second cond test expression to test the validity of the point supplied by the user, to account for the possibility of the user dismissing the prompt.
  • I filter the selection set to only consider circles residing in the current layout/viewport, rather than in all layouts.
  • Appending DXF group 62 is the easiest way to update the object colour: this accounts for the possibility of the object colour being set to ByLayer (in which case, no DXF group 62 entry will be present in the DXF data with which to use subst), and if an existing entry for DXF group 62 exists in the DXF data, this will be overridden by the last entry in the list.

 

  • Like 1
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...