Jump to content

Too many layers!!! Need to merge them!


Recommended Posts

Posted

So here is the deal,

I have a list of bogus layers that exist in a bunch of drawings that need to be merged into the correct layer. I tried writing a script for this but not all the drawings have the bogus layers so the script gets stuck every time it reaches a layer that is not in the drawing. It is not a time saver to have to resume the script after every pause. Anyone know of a lisp or other type of program that can do this? I have attached the script I wrote up if maybe I can make some changes to it to have it work.

PurgeAll.scr

Posted

The script I wrote calls out the built in layer merge command. My problem is there are so many drawings that need this fix that I wanted to make this a batch file, however the script I wrote gets stuck to often and is not a time saver.

Posted

You could try something like this:

 

(defun MergeLayers (#OldLayers #NewLayers / #Layers)
 (setq #Layers (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))))
 (mapcar
   '(lambda (o n)
      (and (tblsearch "layer" o)
           (or (tblsearch "layer" n) (vla-add #Layers n))
           (vl-cmdf "_.-laymrg" "_n" o "" "_n" n "_y")
      ) ;_ and
    ) ;_ lambda
   #OldLayers
   #NewLayers
 ) ;_ mapcar
) ;_ defun

 

 

Example:

(MergeLayers '("A" "B" "C" "D") '("1" "2" "3" "4"))

Posted

A touch quicker:

 

(defun MergeLayers (#OldLayers #NewLayers)
 (setq #Layers (cond (#Layers) ((vla-get-layers
                                  (vla-get-activedocument (vlax-get-acad-object))))))
 (mapcar
   (function
     (lambda (o n)
        (and (tblsearch "layer" o)
             (or (tblsearch "layer" n) (vla-add #Layers n))
             (vl-cmdf "_.-laymrg" "_n" o "" "_n" n "_y")))) #OldLayers #NewLayers))

 

o:)

Posted

Is there any benefit to (function over '( ?

 

I thought about making the vla-get-layers global, but it seemed pointless since it would only be run once per drawing.

 

A touch quicker:

 

(defun MergeLayers (#OldLayers #NewLayers)
 (setq #Layers (cond (#Layers) ((vla-get-layers
                                  (vla-get-activedocument (vlax-get-acad-object))))))
 (mapcar
   (function
     (lambda (o n)
        (and (tblsearch "layer" o)
             (or (tblsearch "layer" n) (vla-add #Layers n))
             (vl-cmdf "_.-laymrg" "_n" o "" "_n" n "_y")))) #OldLayers #NewLayers))

o:)

Posted

Just because I was curious:

 

Command: (benchmark '( (tl) (ta) ))
Elapsed milliseconds / relative speed for 32768 iteration(s):

   (TL).....1578 / 1.01 <fastest>
   (TA).....1593 / 1 <slowest>

Command: (benchmark '( (tl) (ta) ))
Elapsed milliseconds / relative speed for 32768 iteration(s):

   (TL).....1594 / 1 <fastest>
   (TA).....1594 / 1 <slowest>

Command: (benchmark '( (tl) (ta) ))
Elapsed milliseconds / relative speed for 32768 iteration(s):

   (TL).....1593 / 1 <fastest>
   (TA).....1594 / 1 <slowest>

 

 

(defun tl ()
(setq #Layers (cond (#Layers) ((vla-get-layers
                                  (vla-get-activedocument (vlax-get-acad-object)))))))

(defun ta ()
(or #Layers (setq #Layers (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))))))

Posted

try to benchmark it alan. i think vovka on theswamp is the one who pointed

that it makes it faster.

Posted
try to benchmark it alan. i think vovka on theswamp is the one who pointed

that it makes it faster.

 

 

Command: (benchmark '( (test1) (test2) ))
Elapsed milliseconds / relative speed for 4096 iteration(s):

   (TEST1).....1625 / 1 <fastest>
   (TEST2).....1625 / 1 <slowest>

Command: (benchmark '( (test1) (test2) ))
Elapsed milliseconds / relative speed for 4096 iteration(s):

   (TEST1).....1625 / 1 <fastest>
   (TEST2).....1625 / 1 <slowest>

Command: (benchmark '( (test1) (test2) ))
Elapsed milliseconds / relative speed for 4096 iteration(s):

   (TEST1).....1609 / 1.02 <fastest>
   (TEST2).....1640 / 1 <slowest>

Command: (benchmark '( (test1) (test2) ))
Elapsed milliseconds / relative speed for 4096 iteration(s):

   (TEST1).....1609 / 1.01 <fastest>
   (TEST2).....1625 / 1 <slowest>

(defun test1 (/)
 (repeat 10 (mapcar '(lambda (a b) (strcat a " " b))
         '("A" "B" "C")
         '("1" "2" "3"))))
(defun test2 (/)
 (repeat 10 (mapcar (function (lambda (a b) (strcat a " " b)))
         '("A" "B" "C")
         '("1" "2" "3"))))

 

 

I need to run it on something a little more complex.

Posted

Alan,

 

The global #Layers will be much quicker when running the function through a mapcar statement :)

 

Your

 

(defun tl ()
(setq #Layers (cond (#Layers) ((vla-get-layers
                                  (vla-get-activedocument (vlax-get-acad-object)))))))

(defun ta ()
(or #Layers (setq #Layers (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))))))

 

Is not a valid comparison, as both have #Layers global.

Posted

Elapsed milliseconds / relative speed for 65536 iteration(s):

   (TL).....1872 / 3.23 <fastest>
   (TA).....6053 / 1.00 <slowest>

 

(defun ta (/ #Layers)
 (setq #Layers (vla-get-Layers
                 (vla-get-ActiveDocument
                   (vlax-get-acad-object)))))

(defun tl ( )
 (setq #Layers (cond (#Layers) ((vla-get-Layers
                                  (vla-get-ActiveDocument
                                    (vlax-get-acad-object)))))))

Posted
I thought about making the vla-get-layers global, but it seemed pointless since it would only be run once per drawing.

My apology lee but i have to agree with alan on this.

Posted
Alan,

 

The global #Layers will be much quicker when running the function through a mapcar statement :)

 

Your

 

(defun tl ()
(setq #Layers (cond (#Layers) ((vla-get-layers
                                  (vla-get-activedocument (vlax-get-acad-object)))))))

(defun ta ()
(or #Layers (setq #Layers (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))))))

Is not a valid comparison, as both have #Layers global.

 

 

That test was out of curiosity of use of (cond vs. (or.

 

I stated in my reason for not making it global. This would only be executed once per drawing, making the purpose of making it global pointless.

 

The mapcar statement will only be executed after vla-get-layers is defined.

Posted
My apology lee but i have to agree with alan on this.

 

Upon looking more closely, I see that the #Layers is not accessed in the mapcar statment... not quite sure what I was going on about.. :unsure:

 

My apologies AJ - you are quite right. :)

Posted
Upon looking more closely, I see that the #Layers is not accessed in the mapcar statment... not quite sure what I was going on about.. :unsure:

 

My apologies AJ - you are quite right. :)

LoL

No problem. :)

Posted
You could try something like this:

 

(defun MergeLayers (#OldLayers #NewLayers / #Layers)
 (setq #Layers (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))))
 (mapcar
   '(lambda (o n)
      (and (tblsearch "layer" o)
           (or (tblsearch "layer" n) (vla-add #Layers n))
           (vl-cmdf "_.-laymrg" "_n" o "" "_n" n "_y")
      ) ;_ and
    ) ;_ lambda
   #OldLayers
   #NewLayers
 ) ;_ mapcar
) ;_ defun

Example:

(MergeLayers '("A" "B" "C" "D") '("1" "2" "3" "4"))

 

 

I have been given a code in this form before however it was a while back and I am not sure how to initiate this code. Would it be something like this?

(DEFUN C:MRGLAYR ()
   (MergeLayers '("A" "B" "C" "D") '("1" "2" "3" "4"))
(PRINC)
)


(defun MergeLayers (#OldLayers #NewLayers / #Layers)
 (setq #Layers (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))))
 (mapcar
   '(lambda (o n)
      (and (tblsearch "layer" o)
           (or (tblsearch "layer" n) (vla-add #Layers n))
           (vl-cmdf "_.-laymrg" "_n" o "" "_n" n "_y")
      ) ;_ and
    ) ;_ lambda
   #OldLayers
   #NewLayers
 ) ;_ mapcar
) ;_ defun

Posted

Either that, or, if running a script, you can just put this:

 

(MergeLayers '("A" "B" "C" "D") '("1" "2" "3" "4"))

 

In your script, or, just at the command line.

Posted
Either that, or, if running a script, you can just put this:

 

(MergeLayers '("A" "B" "C" "D") '("1" "2" "3" "4"))

In your script, or, just at the command line.

 

I can put this line in a script and it will run this lisp program? I didn't know that. So if I wrote the script like this:

(MergeLayers '("A" "B" "C" "D") '("1" "2" "3" "4"))
Zoom
E
QSAVE

it would merge A-1, B-2, C-3, D-4, the Zoom to extents and save?

Posted

Very nice... Ill give that a go... Thanks

Posted
Very nice... Ill give that a go... Thanks

:) Glad you got what you needed.

Not that it's the best method, but you could even put everything into a .scr file and load it.

 

(defun MergeLayers (#OldLayers #NewLayers / #Layers)
 (setq #Layers (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))))
 (mapcar
   '(lambda (o n)
      (and (tblsearch "layer" o)
           (or (tblsearch "layer" n) (vla-add #Layers n))
           (vl-cmdf "_.-laymrg" "_n" o "" "_n" n "_y")
      ) ;_ and
    ) ;_ lambda
   #OldLayers
   #NewLayers
 ) ;_ mapcar
)
(MergeLayers '("A" "B" "C" "D") '("1" "2" "3" "4"))

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