Jump to content

Select objects of specific color and assign new layer


Fjedsen

Recommended Posts

Hey!

 

This is my first post, so here is some background to me and my problem. A friend of mine asked me if I could help her with a coding problem and *tada* here I am. He has several files that he loads into AutoCAD, selects objects of a specific color and assign them to a new (specific) layer. Since there are several hundred files, he asked if it is possible to write a LISP routine for that. I have a coding background (Python and R) and usually work with these languages but I have absolutely no knowledge of LISP routines or AutoCAD. But of course I said I will have a look at it, and at least try to find out if this is possible.


Due to the internet and specifically this great forum, I have come up with some erroneous code:

 

 (defun c:movehatches ()
  (setq targetColor '(144 237 144))
  (setq targetLayer "layer_of_interest")

  (setq docCount (vla-get-activedocument (vlax-get-acad-object)))
  (setq i 0)
  (while (< i docCount)
    (setq doc (vla-get-document (vla-get-activedocument (vlax-get-acad-object)) i))
    (vla-activate doc)
    (setq modelSpace (vla-get-modelspace doc))
    
    (setq ss (ssget "_X" (list (cons 420 (apply '+ targetColor)))))
    
    (if ss
      (progn
        (command "._SELECTIONSET" "P" ss "")
        (command "._CHPROP" "LA" targetLayer "")
    
    (setq i (1+ i))
  )
  (princ)
)

 

My intention: I want to select all hatches of a specific color (RGB 144, 237, 144) and assign them to the layer 'layer_of_interest'.

 

Am I completely on the wrong track and I have to start from scratch, or can the code still be salvaged?

Link to comment
Share on other sites

if your ssget worked properly it would select anything that matched the RGB color and move it to "layer_of_interest" but since it needs to be converted with Lee Mac's function to work properly. it probably isn't doing anything.

 

This should work better. limiting to hatches that match the color.

 

;;Select any hatch that are color x and move to layer_of_interest
(defun c:movehatches (\ ss)
  (if (setq ss (ssget "_X" '((0 . "HATCH") (420 . 9420608)))
    (foreach hatch (mapcar 'vlax-ename->vla-object (mapcar 'cadr (ssnamex SS)))
      (vlax-put-property hatch 'LAYER "layer_of_interest")
    )
  )
)

 

Edited by mhupp
Link to comment
Share on other sites

Part 2 is write a script to process the dwgs, it would be a simple open dwg (load "yourhatchedit") (c:movehatches) save and close.

 

Have a look at scriptpro.

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