Jump to content

change draw order if layer exists


bohoon

Recommended Posts

Hi everyone

I am trying to write short lisp to check if layer (specified in a lisp) exists and if so change it's draw order (send it to back or front). I have combined my 2 other lisps which both work just fine and found out that now it doesn't work: "error: bad function: "

 

The code goes like this:

 

(defun C:cdo( );  Correct Draw Order
(progn
(if (tblsearch "LAYER" "l1");test if layer exists
(
(setq la1 (ssget "X" '((8 . "l1"))))
(command "draworder" la1 "" "F")
);else
(command "");do this
);end if
(princ)
)
)

 

I didn't put anything after "else", however the error appears even when the layer exists. It must be something really simple but I got confused and can't find the error by myself. Can someone point it out for me, please?

Edited by SLW210
Changed Quote Tags to Code Tags
Link to comment
Share on other sites

(defun c:SendLayerToBack (/ ss)
 (if (setq ss (ssget "_x" '((8 . "L1"))))
   (progn
     (sssetfirst nil ss)
     (ai_draworder "_b")
   )
   (prompt "\n** Layer not found ** ")
 )
 (princ)
)

(defun c:BringLayerToFront (/ ss)
 (if (setq ss (ssget "_x" '((8 . "L1"))))
   (progn
     (sssetfirst nil ss)
     (ai_draworder "_f")
   )
   (prompt "\n** Layer not found ** ")
 )
 (princ)
)

Link to comment
Share on other sites

Thank you for quick response. Your code works perfect:) I was just wondering what am I doing wrong? Can you tell me where this error may come from in my code? It is just for my understanding of the lisp.

 

Thanks again

Link to comment
Share on other sites

Thank you for quick response. Your code works perfect:) I was just wondering what am I doing wrong? Can you tell me where this error may come from in my code? It is just for my understanding of the lisp.

 

You're welcome, bohoon; I'm happy to help. :)

 

 

 

By simply checking for a valid selection set, you both verify that the layer exists, and that there are objects on said layer - note that this does not check for locked layers, as is just an example. Once that returns non-Nil, the previously made selection set is selected on screen, as the draw order sub-function (ai_draworder) requires an implicit selection be made first, and then we call the sub-function supplying the necessary 'back' or 'front' parameters to complete the Command.

 

Hope that make (more?) sense now.

 

Cheers

Link to comment
Share on other sites

OK, So your function ai_draworder requires selection to be made first. However I thought I did the same in this 2 lines:

(setq la1 (ssget "X" '((8 . "l1"))))
(command "draworder" la1 "" "F")

 

made a selection la1 and then use it in regular draworder command

It did work before I added an "if" function :

 

(if (tblsearch "LAYER" "l1");test if layer exists

 

Does it mean that somehow inside "if" it switches the order of my commands?

Link to comment
Share on other sites

OK, So your function ai_draworder requires selection to be made first.

 

It's not my sub-function; it's AutoCAD's and is defined within Acad20##Doc.lsp

 

 

 

However I thought I did the same in this 2 lines:

 

(setq la1 (ssget "X" '((8 . "l1"))))
(command "draworder" la1 "" "F")

 

made a selection la1 and then use it in regular draworder command

It did work before I added an "if" function :

 

(if (tblsearch "LAYER" "l1");test if layer exists

 

Does it mean that somehow inside "if" it switches the order of my commands?

 

No, your call to TBLSEARCH returns non-Nil, so your IF statement's test expression is fine - it is unnecessary given the fact that you want to manipulate entities found on said layer, just call SSGET instead which tests for a valid selection set of entities on said layer (which implicitly exists, if entities are assigned that layer).

 

In a quick test, these also work fine:

 

(defun c:SendLayerToBack2 (/ ss)
 (if (setq ss (ssget "_x" '((8 . "L1"))))
   (command "._draworder" ss "" "_b")
   (prompt "\n** Layer not found ** ")
 )
 (princ)
)

(defun c:BringLayerToFront2 (/ ss)
 (if (setq ss (ssget "_x" '((8 . "L1"))))
   (command "._draworder" ss "" "_f")
   (prompt "\n** Layer not found ** ")
 )
 (princ)
)

 

 

... The main difference being that my functions supply the "_b" and "_f" localized options, and your original code above does not. In one test adding this localized option does 'fix' your code.

 

HTH

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