CLS Posted December 19, 2008 Posted December 19, 2008 Hi all, First of all, I really like this forum and the helpful people on it. It really helped me a lot in the autocad learning process. I think my problem is very clear. I would like that my blocks (which are in an image menu) will be assigned automatically to a layer. Most blocks always get placed on the same layer manually, it would be nice if autocad (a lisp file) recognices a block and switched it onto the correct layer. I found a nice reactor though on the afralisp site, but when replacing a standard acad command for a block it will not work. It would be nice if the code is easy to adjust, since I just got into lisp a bit and even started learning acad a few months back. I hope my problem is clear. I apologize in advance for addressing a problem for which is already a topic for. I really spend quite some time reading forums and faqs for solving it on my own. Help to create my code would be much appreciated. Thanks for your time reading and thanks in advance for any help given. Chris Quote
Lee Mac Posted December 19, 2008 Posted December 19, 2008 Firstly, welcome to CADTutor Chris, its always good to see new members. I have a LISP that will perform a "Drawing Cleanup" which sounds like what you are after. It can be easily modified to suit blocks, let me know if its something you would be interested in and I'll make the modifications. (defun c:cleanup (/ *error* varLst oldVars txt_list typ ss1 ss2 ss3) ; --- Error Trap --- (defun *error* (msg) (mapcar 'setvar varLst oldVars) (if (= msg "") (princ "\nFunction Complete.") (princ "\nError or Esc pressed... ") ) ;_ end if (princ) ) ; end of *error* (setq varLst (list "CMDECHO" "CLAYER") oldVars (mapcar 'getvar varLst) ) ; end setq ; --- Error Trap --- (defun makelay (x) (if (not (tblsearch "Layer" x)) (command "-layer" "m" x "") ) ;_ end if ) ;_ end defun (setvar "cmdecho" 0) (mapcar 'makelay '("TEXT" "DIM" "DEFPOINTS")) (setq txt_list (list "TEXT" "MTEXT")) (foreach typ txt_list (setq ss1 (ssget "X" (list (cons 0 typ)))) (if (/= (sslength ss1) nil) (vl-cmdf "_chprop" ss1 "" "LA" "TEXT" "") ) ;_ end if ) ;_ end foreach (setq ss2 (ssget "X" (list (cons 0 "DIMENSION")))) (if (/= (sslength ss2) nil) (vl-cmdf "_chprop" ss2 "" "LA" "DIM" "") ) ;_ end if (setq ss3 (ssget "X" (list (cons 0 "VIEWPORT")))) (if (/= (sslength ss3) nil) (vl-cmdf "_chprop" ss3 "" "LA" "DEFPOINTS" "") ) ;_ end if (*error* "") (princ) ) ;_ end defun (c:cleanup) The above code will scoop up all Text, dimensions and viewports and assign them to the correct layers. And if the layers do not exist, it will create them. Quote
GhostRider Posted December 19, 2008 Posted December 19, 2008 I think I understand what you would like if I'm off base sorry... there's probably a few ways to get what you want depending on what you do with your blocks. a macro to insert the block would change each to the layer specified but you would have to explode your blocks to do it. I have 100's like this, most are a block inside a block so that when exploded components are still blocked like I need. I'm probably not clear but something like this *^C^C(command "insert" "[color=red]block name[/color]" "int" pause "" "" "")(command "explode" "l")(command "change" "p" "" "p" "la" (getvar "clayer") "") Quote
dbroada Posted December 19, 2008 Posted December 19, 2008 Ghost, why are you exploding the block? I don't follow the reason. CLS, I assume you have posted this here as you want to learn some LISP. If you only want the end result you can add your blocks to a tool palette and assign their default layer there. Quote
GhostRider Posted December 19, 2008 Posted December 19, 2008 my method probably doesn't apply to what CLS needs now that I think about it ... Dave the block that I insert this way and explode them are mostly drawings I want exploded like specific side view of a house . or a group of blocks that are saved to a single block to be imported into a drawing on different layers.......... I'm not being real cleear on why,... but it works good for me...LOL Quote
dbroada Posted December 19, 2008 Posted December 19, 2008 ah, so YOU could use a palette or the design centre then. Quote
GhostRider Posted December 19, 2008 Posted December 19, 2008 yes, I have most converted to palette's and use them also ... Quote
Lee Mac Posted December 19, 2008 Posted December 19, 2008 Try this: (defun c:cleanup (/ *error* varLst oldVars ss1) ; --- Error Trap --- (defun *error* (msg) (mapcar 'setvar varLst oldVars) (if (= msg "") (princ "\nFunction Complete.") (princ "\nError or Esc pressed... ") ) ;_ end if (princ) ) ; end of *error* (setq varLst (list "CMDECHO" "CLAYER") oldVars (mapcar 'getvar varLst) ) ; end setq ; --- Error Trap --- (if (not (tblsearch "Layer" "BLOCKS")) (command "-layer" "m" "BLOCKS" "c" "8" "BLOCKS" "") ) ;_ end if (setvar "cmdecho" 0) (setq ss1 (ssget "X" (list (cons 0 "INSERT")))) (if (/= (sslength ss1) nil) (vl-cmdf "_chprop" ss1 "" "LA" "BLOCKS" "") ) ;_ end if (*error* "") (princ) ) ;_ end defun (c:cleanup) Quote
CLS Posted January 6, 2009 Author Posted January 6, 2009 Hi all, Sorry for my late (delayed) reply. I really appreciate all the help I have gotten. When I posted this problem I was very busy with other stuff, followed by a vacation. I do not want to bypass people, but I have found a way to get it to work, only downside is I do not get certain blocks to not rotate. Here is what I came up with: ^C^C(setq a(getvar "clayer"));-layer;M;name_of_layer;;-Insert;"name_of_dwg";\1;1;\-layer;S;!a;; This really works for me, and I am very happy. I thank you all for the help, really appreciated. Quote
Lee Mac Posted January 6, 2009 Posted January 6, 2009 I suppose you could use a reactor - not sure if it would work or not. Quote
CLS Posted January 7, 2009 Author Posted January 7, 2009 Well, I read quite a lot on reactors, but did not get it to work. The thing with the macro is that I am not able to not rotate certain blocks. If I wanted to prevent certain blocks to rotate, and added a '0' to the macro as stated under here, then my initial idea of getting back to previous layer will not work. So it seems I have to choose between getting back to previous layer, or that certain blocks will not rotate. For some blocks I would like them not to rotate. This is the macro I am talking about: ^C^C(setq a(getvar "clayer"));-layer;M;name_of_layer;;-Insert;"name_of_dwg";\1;1;0;\-layer;S;!a;; The '0' added to the macro should prevent the block from rotating, this works. Still I also want the macro to put me back to previous layer, which does not work. I think this is very confusing for most of you. Maybe I should try to make it clearer. I am working on layer 1. I am inserting block A which is by default on layer 2 After placing block A, I will be put back to layer 1. I am satisfied with this. But if I also want block A not to rotate, and alter my macro (see macro above), then the macro does not switch back to previous layer. Is there a way I can alter the macro so that it prevents the block from rotating and also putting me back to previous layer? Quote
Lee Mac Posted January 7, 2009 Posted January 7, 2009 Maybe this? ^C^C(setq a(getvar "clayer"));-layer;M;name_of_layer;;-Insert;"name_of_dwg";\1;1;0;-layer;S;!a;; Quote
CLS Posted January 7, 2009 Author Posted January 7, 2009 ^C^C(setq a(getvar "clayer"));-layer;M;name_of_layer;;-Insert;"name_of_dwg";\1;1;0;\-layer;S;!a;; Think that is exactly the same as I posted. I tried this macro, but with the 0 added the block does not rotate, but then I will not be put back to previous layer. Quote
Lee Mac Posted January 7, 2009 Posted January 7, 2009 ^C^C(setq a(getvar "clayer"));-layer;M;name_of_layer;;-Insert;"name_of_dwg";\1;1;0;\-layer;S;!a;; Think that is exactly the same as I posted. I tried this macro, but with the 0 added the block does not rotate, but then I will not be put back to previous layer. Its not the same... ^C^C(setq a(getvar "clayer"));-layer;M;name_of_layer;;-Insert;"name_of_dwg";\1;1;0;\-layer;S;!a;; I removed this... Quote
Lee Mac Posted January 7, 2009 Posted January 7, 2009 But if it was a reactor you were looking for: ; Layer Director by Lee McDonnell (MODIFIED) (vl-load-com) (if (not cmdReactor) (setq cmdReactor (vlr-command-reactor nil '((:vlr-commandWillstart . startCommand) (:vlr-commandEnded . endcommand) (:vlr-commandCancelled . cancelCommand) ) ) ; end command reactor ) ; end setq ) ; end if (defun startCommand (calling-reactor startcommandInfo / thecommandstart) (setq oldlay (getvar "clayer")) (setq thecommandstart (car startcommandInfo)) (if (= thecommandstart "INSERT") (setvar "clayer" "2") ) ;_ end if (princ) ) ; end startcommand (defun endCommand (calling-reactor endcommandInfo / thecommandend) (setq thecommandend (car endcommandInfo)) (if (= thecommandend "INSERT") (setvar "clayer" oldlay) ) ;_ end if (princ) ) ; end endCommand (defun cancelCommand (calling-reactor cancelcommandInfo / thecommandcancel) (setq thecommandcancel (car cancelcommandInfo)) (if (= thecommandcancel "INSERT") (setvar "clayer" oldlay) ) ;_ end if (princ) ) ; end cancelCommand (defun makelay (lay / oldlay) (if (not (tblsearch "LAYER" lay)) (progn (setq oldlay (getvar "clayer")) (setvar "cmdecho" 0) (command "-layer" "m" lay "") (setvar "cmdecho" 1) (setvar "clayer" oldlay) ) ;_ end progn ) ;_ end if ) ;_ end defun (makelay "2") Quote
CLS Posted January 7, 2009 Author Posted January 7, 2009 Sorry, was looking for what changed, think I've seen too many macros lately ;-) I tried your macro, but it does not put me back to previous layer, basically the problem stays the same, although your macro does look different. Thanks for your effort and time by the way. Quote
Lee Mac Posted January 7, 2009 Posted January 7, 2009 The reactor should do the trick - and it means you don't have to alter any toolbar buttons Quote
Spinxy Posted October 21, 2010 Posted October 21, 2010 Hi all, as lee wrote ^C^C(setq a(getvar "clayer"));-layer;M;RDS_layer;;-Insert;"dcn_issue_stamp.dwg";\1;1;0;-layer;S;!a;; worked fine for me. I'm wondering what i would need to add if i wanted the block to be exploded? Thanks Quote
martinle Posted September 25, 2020 Posted September 25, 2020 On 7.1.2009 at 16:33, Lee Mac said: Hello Master Lee, It's an old entry but maybe you can tell me why this reactor won't work in ACAD 2021. Thank you! Quote
Recommended Posts
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.