Jump to content

If [Layer prefix] exists, do stuff


Recommended Posts

Posted

Hello,

 

Long time reader and implementer but I have just signed up because I have hit a wall. 

 

What I need to do is check to see if any layer that starts with HPF exists and then do some stuff after. 

 

Here is what I have tried 

(if (= (tblsearch "LAYER" "HPF*") nil)
	(princ "\nTrue.")
	(princ "\nFalse.")
)

I have simplified it down to just testing because I know I can get it to work if I ask for a specific layer name but I need it to be a wildcard if possible.

 

I think instead of a simple test like is this not nil I rather need to test if the list of layers has HPF but my google fu is failing me to find out what that function is called in autolisp. 

 

Thanks in advance. 

Posted

After doing some reading, I remembered that ChatGPT exists and could help me. 

 

It did and this is the code it gave:

(defun c:CheckHPFLayers ()
  (setq found nil) ; Assume no layers are found initially
  (setq layerTable (tblnext "LAYER" T)) ; Get first layer

  (while layerTable
    (if (wcmatch (cdr (assoc 2 layerTable)) "HPF*") ; Check if layer name starts with "HPF"
      (setq found T) ; Set found to true if a match is found
    )
    (setq layerTable (tblnext "LAYER")) ; Get next layer
  )

  (if found
    (princ "TRUE\n") ; Print "TRUE" if a layer was found
    (princ "FALSE\n") ; Print "FALSE" if no layer was found
  )

  (princ) ; Suppress extra return values
)

 

I hope this can help anyone further with a similar issue

Posted

FWIW, the code can be condensed to:

(defun c:checkhpf ( / def fnd )
    (while (and (not fnd) (setq def (tblnext "layer" (not def))))
        (setq fnd (wcmatch (strcase (cdr (assoc 2 def))) "HPF*"))
    )
    (if fnd
        (princ "\nLayer found.")
        (princ "\nLayer not found.")
    )
    (princ)
)

 

  • Like 1
Posted
  On 4/2/2025 at 5:21 PM, Lee Mac said:

FWIW, the code can be condensed to:

(defun c:checkhpf ( / def fnd )
    (while (and (not fnd) (setq def (tblnext "layer" (not def))))
        (setq fnd (wcmatch (strcase (cdr (assoc 2 def))) "HPF*"))
    )
    (if fnd
        (princ "\nLayer found.")
        (princ "\nLayer not found.")
    )
    (princ)
)

 

Expand  

Many thanks Lee!

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