Jump to content

A lisp to change blocks in a layer to a layer with a prefix of that layer


YZ

Recommended Posts

I currently do this a clumsy way using a macro and 2 filters, copying to clipboard, erasing what I don't need, renaming layers with RENAME command, then pasting back from the clipboard all the geometry that needs to keep the original layer name!

 

But all I actually need is that any block in any layer in a drawing be moved into a layer that has a prefix of "SYM_"layer.

 

Can this be done in AutoLISP? Any hints at where I should start looking?

Link to comment
Share on other sites

Hi Lee Mac, thanks for your response.

 

Yes, my profile still says LT. I am currently in a full version trial, and trying to solve 3 things to justify the upgrade.

 

I have confirmed 2, and now this is the last one.

 

Can I change my question? I have learned more and am now comfortable with "Command", so I just need a method to loop through all layers in a drawing and add a suffix to each.

Link to comment
Share on other sites

BlackBox's code from this thread: http://www.cadtutor.net/forum/showthread.php?51619-renaming-layers-with-a-prefix

 

Seems to be what I need, but I am not able to debug it (even after 2 hours of attempts). It looks like the exact logic I am after. Ill bet there is simply some syntax that I cannot recognise as a small error yet.

 

(defun SUFFIX  (arg / oldLayerName)   [color=seagreen];; Example: (SUFFIX "-TEST")[/color]   (vl-load-com)   (cond     (*activeDoc*)     ((setq *activeDoc* (vla-get-activedocument (vlax-get-acad-object)))))   (vlax-for x  (vla-get-layers *activeDoc*)     (if (and       (not (vl-position        (setq oldLayerName (strcase (vla-get-name x)))        '("0" "DEFPOINTS")))       (not (vl-string-search "|" oldLayerName)))    (vla-put-name x (strcat oldLayerName arg))))   (princ)) ;_end defun

Link to comment
Share on other sites

(defun SUFFIX  (arg / oldLayerName)
 ;; Example: (SUFFIX "-TEST")
 (vl-load-com)
 (cond
   (*activeDoc*)
   ((setq *activeDoc* (vla-get-activedocument (vlax-get-acad-object)))))
 (vlax-for x  (vla-get-layers *activeDoc*)
   (if (and
     (not (vl-position
      (setq oldLayerName (strcase (vla-get-name x)))
      '("0" "DEFPOINTS")))
     (not (vl-string-search "|" oldLayerName)))
  (vla-put-name x (strcat oldLayerName arg))))
 (princ)) ;_end defun

Edited by YZ
Link to comment
Share on other sites

I see where you're struggling to paste copied code... Use notepad as helper tool (copy+paste code from cadtutor to notepad firstly)... Then post code by (copy+paste from notepad into code tags [noparse]

 Your code here ... 

[/noparse])...

Link to comment
Share on other sites

Try this and see if it needs debugging...

 

(defun c:blkslayprefix ( / PREFIX ss i e )

 (defun PREFIX ( arg oldLayerName )
   (if (and
         (not (vl-position oldLayerName '("0" "DEFPOINTS")))
         (not (vl-string-search "|" oldLayerName))
       )
     (strcat arg oldLayerName)
     oldLayerName
   )
 )

 (prompt "\nSelect blocks to rename their layer names to names with specified prefix...")
 (setq ss (ssget "_:L" '((0 . "INSERT"))))
 (while (not ss)
   (prompt "\nEmpty sel.set... Please select blocks to rename their layer names to names with specified prefix again...")
   (setq ss (ssget "_:L" '((0 . "INSERT"))))
 )
 (initget 1)
 (setq pref (getstring t "\nSpecify prefix : "))
 (repeat (setq i (sslength ss))
   (setq e (ssname ss (setq i (1- i))))
   (entupd (cdr (assoc -1 (entmod (subst (cons 8 (PREFIX pref (cdr (assoc 8  (entget e))))) (cdr (assoc 8 (entget e))) (entget e))))))
 )
 (princ)
)

Edited by marko_ribar
Link to comment
Share on other sites

That looks promising!

 

This is the error I get when I run it:

Empty sel.set... Please select blocks to rename their layer names to names with specified prefix again...

Link to comment
Share on other sites

I just need a method to loop through all layers in a drawing and add a suffix to each.

 

Here's a very simple form of such a program:

(defun c:laysuf ( / pat suf )
   (setq suf "-test"
         pat (strcase (strcat "0,defpoints,*|*,*" suf))
   )
   (vlax-for lay (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
       (or (wcmatch (strcase (vla-get-name lay)) pat)
           (vla-put-name lay (strcat (vla-get-name lay) suf))
       )
   )
   (princ)
)
(vl-load-com) (princ)

Note that this does not account for cases in which appending the suffix will result in a duplicate layer.

Link to comment
Share on other sites

  • 1 month later...

Thanks Lee, long time coming but I got back to this only today.

 

This works like a dream. Except I wrote suffix where I should have written prefix.

 

I tried wrangling your routine into a prefix equivalent, but I think I still need some coaching on what this line is doing:

 (vla-put-name lay (strcat (vla-get-name lay) suf))

 

Here's what I came up with:

(defun c:laypref ( / pat suf )
   (setq pre "SYM_"
         pat (strcase (strcat "0,defpoints,*|*,*" pre))
   )
   (vlax-for lay (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
       (or (wcmatch (strcase (vla-get-name lay)) pat)
           (vla-put-name lay (strcat pre (vla-get-name lay)))
       )
   )
   (princ)
)
(vl-load-com) (princ)

Link to comment
Share on other sites

I'm glad the code is working for you.

 

You will also need to change:

pat (strcase (strcat "0,defpoints,*|*,*" pre))

to:

pat (strcase (strcat "0,defpoints,*|*," pre "*"))

And change this:

( / pat suf )

to:

( / pat pre )

Link to comment
Share on other sites

Gets and Puts are programming terms I am getting old used in Basic 30 years ago.

 

Get go and retrieve something you would use Assoc if using dxf codes

 

Put change something you would use ENTMOD if using dxf codes

Link to comment
Share on other sites

Thanks BIGAL!

 

Lee Mac, yep, that's all it was.

 

For the benefit of readers, to add a nominated prefix to layers:

(defun c:laypref ( / pat pre )
   (setq pre "SYM_"
         pat (strcase (strcat "0,defpoints,*|*," pre "*"))
   )
   (vlax-for lay (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
       (or (wcmatch (strcase (vla-get-name lay)) pat)
           (vla-put-name lay (strcat pre (vla-get-name lay)))
       )
   )
   (princ)
)
(vl-load-com) (princ)

 

I am trying, but still I read sample lisps and I get lost as to what is happening. To stop asking silly questions, is there a basics course or resource I can read? Everything I read so far is so different from the next that I feel like I haven't learnt anything that helps me interpret the next lisp!

 

But, I have dreamed of prefixing layers for YEARS!

Link to comment
Share on other sites

I bought 4 Visual lisp books for Kindle/pc from Amazon and they were ony a few dollars each I think they were like $8 dollars each they have lisp books as well.

 

Being here in AUS I have a convert "Civilcad" changes all the layer, names, Linetype and colour like you adds EX_ to survey code/layer name, does lots more happy to share. What is your survey software ? Have you gone to CIV3D ?

Link to comment
Share on other sites

Hey Bigal, using Magnet (formerly CivilCAD) and AutoCAD. Not Civil3D at this stage.

 

I'd gladly look at your transfer lisp if you were willing. Was it that obvious that it was a land surveying lisp I was making?! Haha.

Link to comment
Share on other sites

What the lisp does is a number of things converting a Civilcad export to dwg.

 

1 removes the cross point block

2 changes line types to only Autocad & supported custom.lin is ok, this removes link to SHX created for each dwg.

3 converts all line work to 2d maintains 3dfaces as 3d for contouring.

4 keeps a Autocad Point of each surveyed point as a 3d value.

5 converts layers re linetype and colour based on a external txt file that has all survey codes, adds EX as its Field survey. This is part of our construction dwg requirement.

 

Can you post a export dwg from Magnet for testing.

Link to comment
Share on other sites

  • 1 month later...

Here is the same after running the conversion, all the point blocks are gone and replaced with an Autocad POINT type Ptype still 3d, your layer linetypes have been modified to match true AutoCAD line types so eliminating the annoying message about missing shx. See Solid and Dash now gone. You would need to tell me what 12,14 & 84 linetypes are then they to can be unlinked from the shx also. The renaming of the layers could be removed its part of our standards to identify existing v's design.

 

 

Please note we had a dot linetype and opening a dwg was horrendous time wait changed the line type in civilcad to stop the problem, looked at 84 not sure of the pattern.

07-154SEW-2.dwg

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