Jump to content

Looking for a LISP to change selected objects to the color of their layer


Recommended Posts

Posted

I have been searching for a lisp that will prompt you to select objects, and change all selected objects color from "By Layer" to whatever color they currently are. Has anyone seen something that will do that?

Posted
(defun c:test (/ ss i d)
 (if (setq ss (ssget "_:L"))
   (repeat (setq i (sslength ss))
     (entmod (append (setq d (entget (ssname ss (setq i (1- i)))))
                     (list (assoc 62 (tblsearch "LAYER" (cdr (assoc 8 d)))))
             )
     )
   )
 )
 (princ)
)

Posted

Thank you so much! That is exactly what i was looking for :)

Posted
Thank you so much! That is exactly what i was looking for :)

You're welcome.

Posted (edited)

 

VVA, you might consider using VLAX-FOR when using VL* functions, and iterating a SelectionSet (much faster). :wink:

 

Small example (based on your code - hope you don't mind):

 

(defun c:CBL () (c:ColorByLayer))
(defun c:ColorByLayer  (/ *error*)
 (princ "\rCOLORBYLAYER ")
 (vl-load-com)

 (defun *error*  (msg)
   (cond ((not msg) (if acDoc (vla-endundomark acDoc)))           ; Normal exit
         ((member msg '("Function cancelled" "quit / exit abort")))    ; <esc> or (quit)
         ((princ (strcat "\n** Error: " msg " ** "))))                 ; Fatal error, display it
   (princ))

 ((lambda (acDoc / ss oLayer oLayers oLayersList)
    (if (setq ss (ssget "_:L"))
      (progn
        (vla-startundomark acDoc)
        (vlax-for x (setq ss (vla-get-activeselectionset acDoc))
          (if (= :vlax-true
                 (vla-get-lock
                   (setq oLayer (vla-item (cond (oLayers)
                                                ((setq oLayers
                                                        (vla-get-layers acDoc))))
                                          (vla-get-layer x)))))
            (progn
              (setq oLayersList (cons oLayer oLayersList))
              (vla-put-lock oLayer :vlax-false)))
          (vl-catch-all-apply
            'vla-put-color
            (list x (vla-get-color oLayer))))
        (vla-delete ss)
        (foreach oLayer  oLayersList
          (vla-put-lock oLayer :vlax-true))
        (*error* nil))
      (*error* "Nothing selected")))
   (vla-get-activedocument (vlax-get-acad-object))))

Edited by BlackBox
Posted

Isn't changing an object's color from 'By Layer' to "whatever" color just encouraging bad CAD practices? How many of us have received a drawing from an outside source and found that an object's color did not coincide with the color of the layer that it was on? Frustrating to say the least.

Posted
Isn't changing an object's color from 'By Layer' to "whatever" color just encouraging bad CAD practices? How many of us have received a drawing from an outside source and found that an object's color did not coincide with the color of the layer that it was on? Frustrating to say the least.

Yes. :oops:..............

Posted
That's a lot of code.

 

It is... I only meant to provide VVA a "working" VLAX-FOR example using his own code. o:)

 

Besides, if you think that's bad, don't bother learning (advanced) .NET - Sample: Adding speech recognition to AutoCAD via Kinect (continued)

 

... The simpler stuff (i.e., getting, setting system variables, etc.) is a lot shorter by comparison, and still *more code* than LISP! :rofl: LoL Poor ActiveX COM API. :cry:

 

Isn't changing an object's color from 'By Layer' to "whatever" color just encouraging bad CAD practices? How many of us have received a drawing from an outside source and found that an object's color did not coincide with the color of the layer that it was on? Frustrating to say the least.

 

Yes. :oops:..............

 

Yeah :oops:... *kicks dirt*

Posted
It is... I only meant to provide VVA a "working" VLAX-FOR example using his own code. o:)

 

Besides, if you think that's bad, don't bother learning (advanced) .NET

Oh, I know other languages are much more labor intensive. I just meant all that work when entmod could accomplish it a lot easier.

 

On the note of your stepping through the activeselectionset, I've actually abandoned using that method since I would randomly receive errors from routines that used that method. I know there's a slight time loss (I tested it myself long ago) with stepping through the selectionset with while/repeat and converting the ename to a vla-object, but I've stopped seeing the errors and the time loss is so minute you don't even notice.

Posted

Unfortunately, it appears that Autodesk is going to leave us behind so-to-speak, if we don't pick up .NET at some level. As it appears I'll be working with Civil 3D (at some point), and Microstation - .NET also suites a common thread for me personally. Even if I were fortunate to no longer need to work with Microstation, I'm still compelled to learn .NET for the simple purposes of gaining access to AECC* Objects that even Visual LISP fails (as we've discussed before).

 

As for the vlax-for iteration errors, I've not personally had any issues (and I use this method a lot), but would be interested in understanding the cause. I typically have two "shell" iteration code snippets... One for VLA Objects (vlax-for), and one for eNames (while + ssname). In any event, thanks for the heads up - I'll keep a lookout for those errors.

 

Cheers! :beer:

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...