Jump to content

swap values of layer name and layer description


feargt

Recommended Posts

Hi,

I am working in an international firm, we have a layer structure where the layer name is in the local language and the layer description is the layer name in english.

 

Does anyone know of a routine that would allow a user to swap these values?

 

ie.

 

Layer Name Layer Description

Schraffur Hatch

 

run code

 

result

 

Layer Name Layer Description

Hatch Schraffur

 

I know the layer translator exists but this would probably suit our needs better.

 

Thanks.

Link to comment
Share on other sites

As posted on AUGI, for the benefit of those here at CADTutor:

 

([color=BLUE]defun[/color] c:test [color=BLUE]nil[/color] ([color=BLUE]vl-load-com[/color])
 ([color=BLUE]vlax-for[/color] layer ([color=BLUE]vla-get-layers[/color] ([color=BLUE]vla-get-activedocument[/color] ([color=BLUE]vlax-get-acad-object[/color])))
   ([color=BLUE]vl-catch-all-apply[/color]
     ([color=BLUE]function[/color]
       ([color=BLUE]lambda[/color] ( [color=BLUE]/[/color] ln ld )
         ([color=BLUE]setq[/color] ln ([color=BLUE]vla-get-name[/color] layer)
               ld ([color=BLUE]vla-get-description[/color] layer)
         )
         ([color=BLUE]vla-put-description[/color] layer ln)
         ([color=BLUE]vla-put-name[/color] layer ld)
       )
     )
   )
 )
 ([color=BLUE]princ[/color])
)

Edited by Lee Mac
Correct typo
Link to comment
Share on other sites

These should give you a jump start...

 

One layer at a time:

(defun c:LAYSWAP (/ layerName activeDoc layerItem oldDescription)
 (princ "\rLAYER SWAP ")
 (vl-load-com)
 (if (and (setq layerName (getstring T "\nEnter layer name to swap: "))
          (setq layerName (cdr (assoc 2 (tblsearch "layer" layerName)))))
   (progn
     (vla-startundomark
       (setq activeDoc (vla-get-activedocument (vlax-get-acad-object))))
     (setq layerItem (vla-item (vla-get-layers activeDoc) layerName))
     (setq oldDescription (vla-get-description layerItem))
     (vla-put-description layerItem layerName)
     (vla-put-name layerItem oldDescription)        
     (vla-endundomark activeDoc))
   (prompt "\n** Layer does not exist ** "))
 (princ))

 

This will handle all layer at once:

(defun c:LAYSWAP (/ *error* _layswap activeDoc layerName)
 (princ "\rLAYER SWAP ")
 (vl-load-com)

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

 (defun _layswap  (layerName / oLayers layerDesc)
   (if
     (and
       (setq
         layerItem (vla-item
                     (setq oLayers (vla-get-layers activeDoc))
                     layerName))
       (setq layerDesc (vla-get-description layerItem))
       (setq layerName (vla-get-name layerItem)))
      (progn
        (vla-put-name layerItem layerDesc)
        (vla-put-description layerItem layerName))))
 
 (vla-startundomark
       (setq activeDoc (vla-get-activedocument (vlax-get-acad-object))))
 (vlax-for x (vla-get-layers activeDoc)
   (if (not (vl-position (strcase (setq layerName (vla-get-name x))) '("0" "DEFPOINTS")))
     (_layswap layerName)))
 (vla-endundomark activeDoc)
 (princ))

 

Note that this function will fail if a duplicate layer description already exists as a layer name. Give fixing this yourself a try, and I'll help mitigate where necessary.

 

Hope this helps!

Link to comment
Share on other sites

It appears that Lee beat me to the punch (again). :( :sigh:

 

Guys be glad the both of you are not in an "outdraw cowboy game".

Someone could get hurt...

 

cowboy.gif

Link to comment
Share on other sites

But yours is a more complete answer :thumbsup:

 

Thanks Lee, that is kind of you to say. :oops:

 

I was going to ask your opinion on that; am I going overboard with including the start, and end undo marks, etc.? Brevity is the soul of wit, and well, when compared to your code my own is certainly less witty. :lol: LoL

 

It just seems like a lot of overhead when you see them (your code, and mine) side-by-side.

Link to comment
Share on other sites

Guys be glad the both of you are not in an "outdraw cowboy game".

Someone could get hurt...

 

cowboy.gif

 

This is my rifle, this is my [code], this is for fighting, this is for [blokes].

 

(^^ I'm an expert marksman, says the US Army 8) ^^)

Video_Detail.gif

 

That's cool Marco. :lol:

 

1+

Link to comment
Share on other sites

Thanks Lee Mac and Renderman for taking an interest in this one....

 

I've tested the two lisps provided here and a further one over on AUGI on the same template drawing containing a 150 layers where no 2 layer descriptions are the same.

 

Renderman,

 

While I really like the undo function, your lisp returned

"** Error: Automatisierungsfehler Doppelter Datensatzname ** which translates to (roughly)

"**Error: Atuomation error double data entry name**

 

Lee Mac,

 

your code worked like a charm (without the undo function and error trappings)

 

I tried to combine the best of both worlds but so far without success.

 

Thanks for your help on this, very much appreciated.

Link to comment
Share on other sites

I was going to ask your opinion on that; am I going overboard with including the start, and end undo marks, etc.?

 

I think it depends on the purpose of the code. If I were to utilise this code in a full-blown application, or were writing it specifically for a client, then I would be inclined to include an error handler and undo marks. However, since this is just a throw-away snippet for the forums, I wasn't going to spend too much time to build a full program, but rather just demonstrate the method/intention.

 

That said, there are a few things I would say about your code. I tend to only use an error handler if it is crucial that items need resetting (such as System Variables); or objects need unloading or releasing (such as when using DCL, or interfacing with objects outside of the scope of the AutoCAD Object Model). Or perhaps when the user has the opportunity to disrupt the flow of the program (at a prompt for example). However, in this code, everything is performed by the program, and furthermore, there isn't anything that crucially needs to be reset/released - so, for clarity I would omit the error handler in this instance. Of course, there are definitely exceptions to everything I've just stated.

 

With regard to the UndoMarks, its a difficult one. For small snippets of code like this, I would tend to omit them for clarity. I think its about finding a balance between functionality and clarity of code/intention for those learning.

 

However, there is one point I would raise with your code - I think the sub is unnecessary:

 

 (defun _layswap  (layerName / oLayers layerDesc)
   (if
     (and
       (setq
         [color=darkorange]layerItem [/color](vla-item
                     (setq oLayers [color=purple](vla-get-layers activeDoc)[/color])
                     layerName))
       (setq layerDesc (vla-get-description layerItem))
       (setq layerName (vla-get-name layerItem)))
      (progn
        (vla-put-name layerItem layerDesc)
        (vla-put-description layerItem layerName))))
 
 (vla-startundomark
       (setq activeDoc (vla-get-activedocument (vlax-get-acad-object))))
 [color=red](vlax-for x (vla-get-layers activeDoc)[/color]
   (if (not (vl-position (strcase (setq [color=green]layerName[/color] (vla-get-name x))) '("0" "DEFPOINTS")))
     (_layswap [color=green]layerName[/color])))

You are iterating through the LayersCollection, so you already have the VLA Layer Objects (bound to variable 'x'). Then, you get the name of each Layer Object, pass it to the sub, which proceeds to get the Layers Collection (again), and retrieves the Layer Object (again) which you already had in the first place when iterating through the vlax-for loop.

 

 

your code worked like a charm (without the undo function and error trappings)

 

Thanks feargt :)

 

Note that I have just corrected a typo in my above code (missed one of the 'layer' variables).

Link to comment
Share on other sites

Lee thanks for your comments.

 

However, there is one point I would raise with your code - I think the sub is unnecessary:

 

You are iterating through the LayersCollection, so you already have the VLA Layer Objects (bound to variable 'x'). Then, you get the name of each Layer Object, pass it to the sub, which proceeds to get the Layers Collection (again), and retrieves the Layer Object (again) which you already had in the first place when iterating through the vlax-for loop.

 

... As for this slip up, there's not much I can say. It's embarrasing, as I pasted the wrong code from my unnamed LISP document, and never even noticed. LoL

Link to comment
Share on other sites

... As for this slip up, there's not much I can say. It's embarrasing, as I pasted the wrong code from my unnamed LISP document, and never even noticed. LoL

 

No worries :)

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