Blocks to layers

Blocks to layers

Anonymous
Not applicable
1,923 Views
7 Replies
Message 1 of 8

Blocks to layers

Anonymous
Not applicable

Guys and Gals,

 

Does anybody have a lisp that will send blocks to certain layers based on the name, preferably using wildcards?  So, block 

              "JM-1"            to     "Jamb" layer

              "JR-4"            to     "Jar" layer

              "HardStuff"  to      "Harware" layer

              "HJ-12"          to      "HeadJamb" layer.

The idea would be to run it at the end of a drawing to put those blocks that were inserted on the wrong layer onto something that the boss doesn't get ticked about.   I figure I'm not the only one in this situation and there are dozens of lisps doing this very thing already. 

 

Thanks in advance!

 

Don

 

 

 

0 Likes
Accepted solutions (1)
1,924 Views
7 Replies
Replies (7)
Message 2 of 8

dlanorh
Advisor
Advisor

 


@Anonymouswrote:

Guys and Gals,

 

Does anybody have a lisp that will send blocks to certain layers based on the name, preferably using wildcards?  So, block 

              "JM-1"            to     "Jamb" layer

              "JR-4"            to     "Jar" layer

              "HardStuff"  to      "Harware" layer

              "HJ-12"          to      "HeadJamb" layer.

The idea would be to run it at the end of a drawing to put those blocks that were inserted on the wrong layer onto something that the boss doesn't get ticked about.   I figure I'm not the only one in this situation and there are dozens of lisps doing this very thing already. 

 

Thanks in advance!

 

Don


The lisp is easy, controlling the lisp is difficult. A complete list of block names and required layers would help in forming the wildcard patterns.

I am not one of the robots you're looking for

0 Likes
Message 3 of 8

Anonymous
Not applicable

Yes, you're right!  

 

If it starts with "J*" it would go to JAMB  (the wildcard would be something like J* I think)

If it starts with "JA*"  it would go to "JAR"

                            "H*" would go to "HEM" layer  (I'm making these up)

                            "HA*"  would go to "HARDWARE" layer

 

The idea is to run this lisp on exit and take care of any anomalies.  If you could get me started I'll follow your pattern and get my own stuff in.

 

Thanks so much for the help.

 

Don

 

 

0 Likes
Message 4 of 8

dlanorh
Advisor
Advisor
Sorry a bit busy will get back in a couple of hours with starter code. Are you OK with Visual Lisp?

I am not one of the robots you're looking for

0 Likes
Message 5 of 8

Anonymous
Not applicable

I've done a bunch of lisp over the years, probably more of a joke to a real programmer, but no visual lisp.  In my current job I have NO time for learning or experimenting at all, that's why I'm asking you guys for help.  But if you can set me up with coding showing the pattern I can take it from there.

 

Thanks again,

 

Don

0 Likes
Message 6 of 8

Moshe-A
Mentor
Mentor

Don hi,

 

attached Block to Layer (b2l.lsp) that will do what you want

check the local function (get_block_layer)? that's where you specify your block name pattern

and right below specify the layer name for that block.

 

play around and tell us if it's enough simple for you to take it from here?

 

enjoy

Moshe

 

ps: it does not (still) support dynamic blocks

 

 

(defun c:B2L (/ get_block_layer ; local function 
	        ss i ename blkName lay)

 (defun get_block_layer ()
  (vl-some
    '(lambda (blkPatt blkLay)
      (if (wcmatch blkName (strcase blkPatt))
	blkLay ; return
      )
     )
  '("ja*"  "j*"  "ha*" "h*")	  ; specify block name pattern
  '("jamb" "jar" "hem" "hardware"); specify layer name
  )
 ); is_block_layer

 ; here start c:b2l
 (setvar "cmdecho" 0)
 (command "._undo" "_begin")
  
 (if (setq ss (ssget ":L" '((0 . "insert"))))
  (progn
   (setq i -1)
   (repeat (sslength ss)
    (setq i (1+ i) ename (ssname ss i))
    (setq blkName (strcase (cdr (assoc '2 (entget ename)))))
     
    (if (setq lay (get_block_layer))
     (progn
      ; if layer still not exist? create it
      (if (null (tblsearch "layer" lay))
       (command "._layer" "_new" lay "")
      )
      (command "._chprop" "_si" ename "_layer" lay "")
     ); progn
    ); if
   ); repeat
  ); progn
 ); if

 (command ".undo" "_end")
 (setvar "cmdecho" 1)
  
 (princ)
)

Message 7 of 8

dlanorh
Advisor
Advisor
Accepted solution

Not a problem. AFAICT all the code is there, you need to give the lisp a name "(defun c:???? )" replacing the ???? with whatever you want.

The real meat is in the " (cond ("  statement. I've set it up so that you can enter the pattern code for the block name and the layer name. This needs to be completed for each block pattern code and layer. I would set this out alphabetically as it makes the code easier to maintain. It is IMPORTANT to remember that pattern wise "JAM*" needs to be before "JA*" which needs to be before "J*" if they are going to different layers, otherwise "J*" will pick up all the "JAM*" and "JA*"

 

condition lines = ( (wcmatch (strcase b_name) "J*") (rh:lay_chk blk "???"))

 

This can be copied down so that they line up under each other.

 

(                                                           start of individual condition

 

(wcmatch (strcase b_name) "J*") the condition statement itself. You need to change the "J*" part to reflect the block name pattern. Uppercase ONLY

 

(rh:lay_chk blk "???") If true what it will do. Replace "????" with layer name. Text case as per layer name in dwg

 

)      close of individual condition

 

(t (alert (strcat "No condition set for block " b_name))) last line of cond. Must stay here as last line. This will pick up anything not accounted for above. Flashes up an alert with message and OK button

 

Attached is lisp file. Open in a text editor.

 

I am not one of the robots you're looking for

0 Likes
Message 8 of 8

Moshe-A
Mentor
Mentor

Don,

 

if you liked the solutions you got then please give some likes and close this theard.

 

thank you

moshe