Jump to content

Recommended Posts

Posted

Hi,

 

Is it possible to create Lisp, what searches, if fail contains region objects  -> then changes all region objects color to like yellow and gives warning if finds region. If don't find region, no warning.

Posted (edited)

Welcome aboard.

 

Do you know anything about using SSGET with filters ? What you ask for is easy. A few more comments.

 

Have a look at Lee-mac SSGGET https://lee-mac.com/ssget.html a really good reference.

https://help.autodesk.com/view/ACDLT/2024/ENU/?guid=GUID-0F37CC5E-1559-4011-B8CF-A3BA0973B2C3

SSGET "X" finds all

Using a filter can look in model space only (cons 410 "Model")

Filters can help find matches examples  (cons 0 "Region") (cons 0 "Insert") (0 . "LWPolyline") 

 

I have not posted code as it is about learning a very basic lisp function, it is used in a major way in lisp code. 

 

Just do a google say "use ssget regions autolisp" you will be swamped with results.

 

Even just use "Qselect".
 

 

 

 

Edited by BIGAL
Posted (edited)

Thank you for links!!

 

Im not werry strong on lisp programming.

I manage to make so fare, but im getting error: <Selection Set: ffdc26c0>

(defun c:ent (/)
 (ssget "_X" '((0 . "REGION")))
  (setq ss1 (ssget "P"))
)

if i remove "P" it find my region entities but will not keep active.

i will like to continue code with -> change properties color ... I cant do that if select previous is with error

Edited by SLW210
Added Code Tags!!
Posted

Use Code Tags for your code in the future. (<> in the editor toolbar)

 

Probably should let you keep working at it, but I'll give you a start, at least you took a crack at it.

 

If I understood your requirements, maybe this is it, if not, I tried to do a good job commenting.

 

For reference, I'm not that great at LISP, I just usually start with a similar code or look through my references.

 

See if you can amend this code if it's not exactly what you want.

 

;;; Checks for regions and changes color (yellow)
;;;
;;; https://www.cadtutor.net/forum/topic/97768-lisp-to-find-object/#findComment-669972
;;;
;;; By SLW210 (a.k.a. Steve Wilson)
;;; 

(defun c:CheckRegions ( / sel regions i ent entData )
  ;; Selection
  (setq sel (ssget))

  ;; Filter it for REGION objects
  (setq regions
        (if sel
          (ssget "P" '((0 . "REGION"))) ; P = previous selection
          (ssget "X" '((0 . "REGION"))) ; X = entire drawing
        )
  )

  ;; If REGION objects exist
  (if regions
    (progn
      ;; Show warning popup
      (alert "WARNING: Region objects found! Their color will be changed to yellow.")

      ;; Set color to yellow 
      (repeat (setq i (sslength regions))
        (setq ent (ssname regions (setq i (1- i))))
        (setq entData (entget ent))

        ;; Add/update color property (color index 2)
        (if (assoc 62 entData)
          (setq entData (subst (cons 62 2) (assoc 62 entData) entData))
          (setq entData (append entData (list (cons 62 2))))
        )

        (entmod entData)
        (entupd ent)
      )

      (prompt "\nRegion objects changed to yellow.")
    )
    (prompt "\nNo REGION objects found.")
  )

  (princ)
) ; End defun


 

Posted

Good answer by @SLW210 

 

For your info.

(defun c:chgcol (/)
  (setq ss1  (ssget "_X" '((0 . "REGION")))) ; LEAVE THE "X" OUT IF YOU WANT MANUAL PICK.
  (WHILE (/= SS1 NIL)  ; CHECKS IS THERE ANY REGIONS
    ; DO YOUR THING HERE
  )
)


; FOR MODEL ONLY
(setq ss1 (ssget "_X" '((0 . "REGION")(cons 410 "Model"))))

 

 

Posted

Since it's related, I will go ahead and preempt the next request...

 

;;; Detect objects, if detected change color, by selection or entire drawing choice (non DCL version)
;;;
;;; https://www.cadtutor.net/forum/topic/97768-lisp-to-find-object/#findComment-670063
;;;
;;; By SLW210 (a.k.a. Steve Wilson)
;;;

(defun c:ChkObj ( / obj_choice color_choice scope ss i ent edata )

  ;; Prompt for object type
  (setq obj_choice (strcase (getstring T "\nEnter object type (e.g., REGION" "HATCH" "LINE" "CIRCLE" "ARC" "LWPOLYLINE" "TEXT" "MTEXT" "SPLINE" "INSERT" "BLOCK): ")))
  (if (not (member obj_choice '("REGION" "LINE" "CIRCLE" "ARC" "LWPOLYLINE" "TEXT" "MTEXT" "SPLINE" "INSERT" "BLOCK" "HATCH")))
    (progn
      (prompt "\nInvalid object type. Defaulting to REGION.")
      (setq obj_choice "REGION")
    )
  )

  ;; Prompt for color
  (setq color_choice (getint "\nEnter color index (1-255): "))
  (if (or (not color_choice) (< color_choice 1) (> color_choice 255))
    (progn
      (prompt "\nInvalid color. Defaulting to yellow (2).")
      (setq color_choice 2)
    )
  )

  ;; Prompt for scope
  (setq scope (strcase (getstring T "\nEnter scope (SELECTION/DRAWING): ")))
  (if (not (member scope '("SELECTION" "DRAWING")))
    (progn
      (prompt "\nInvalid scope. Defaulting to DRAWING.")
      (setq scope "DRAWING")
    )
  )

  ;; Begin undo group
  (command "_.UNDO" "_Begin")

  ;; Select objects
  (cond
    ((= scope "SELECTION")
     (prompt "\nSelect objects: ")
     (setq ss (ssget (list (cons 0 obj_choice))))
    )
    ((= scope "DRAWING")
     (setq ss (ssget "X" (list (cons 0 obj_choice))))
    )
  )

  ;; Apply changes if any found
  (if ss
    (progn
      (alert (strcat "WARNING: Found " (itoa (sslength ss)) " " obj_choice "(s). Changing color..."))
      (repeat (setq i (sslength ss))
        (setq ent (ssname ss (setq i (1- i))))
        (setq edata (entget ent))

        ;; Force color override (even if BYLAYER or unset)
        (if (assoc 62 edata)
          (setq edata (subst (cons 62 color_choice) (assoc 62 edata) edata))
          (setq edata (append edata (list (cons 62 color_choice))))
        )

        (entmod edata)
        (entupd ent)
      )
      (prompt (strcat "\n" (itoa (sslength ss)) " object(s) modified."))
    )
    (prompt (strcat "\nNo " obj_choice " objects found."))
  )

  ;; End undo group
  (command "_.UNDO" "_End")

  (princ)
)

 

I have a DCL option that was in the works, not sure if I will get back to it, been a while since I have mussed about with DCLs.

Posted (edited)

@SLW210 just use the Multi radio buttons.lsp for the dcl. Just make sure Multi radio buttons.lsp is saved in a support path or add full path to the Load.

 

 

(if (not AH:Butts)(load "Multi Radio buttons.lsp"))
(setq ans (ah:butts 1 "v"  '("please choose" "REGION" "HATCH" "LINE" "CIRCLE" "ARC" "LWPOLYLINE" "TEXT" "MTEXT" "SPLINE" "INSERT" "BLOCK"))) ; ans holds the button picked value

 

image.png.e0527527afcb66c0e065d9966c25ff24.png

 

 

Multi radio buttons.lsp

 

Edited by BIGAL
  • Thanks 1
Posted

I have that, was looking to make my own.

 

That was originally part of something more complex.

Posted

Hi,

 

i manage to make some script. It works fine for me, but i get in the end --Error: undefined function - nil--. Is that error because i have second  part of script? Idea is, that first part runs script automatically on load file and second part gives me option to call it up with "ent" command.

Its for me first time to write lisp script, so i don't understand lot of things. 

 

 (if (/= (ssget "_X" '((0 . "REGION"))) nil)                   
  ((alert "REGIOON LEITUD\color 51 kollane")                   
   (ssget "_X" '((0 . "REGION")))                              
   (command "change" "P" "" "Properties" "Color" "51" "")      
   (command "zoom" "Object" "Previous" "")                     
   (command "explode" "P" ""))                                 
 )                                                                                                                
            


 ;---------------------------Defun command ENT use ------------------------
(defun c:ent (/)
 (if (/= (ssget "_X" '((0 . "REGION"))) nil)                  
  (alert "REGIOON LEITUD\color 51 kollane")                   
 )                                                            
  (ssget "_X" '((0 . "REGION")))                              
  (command "change" "P" "" "Properties" "Color" "51" "")      
  (command "zoom" "Previous" "")                              
  (command "explode" "P" "")                                  
)                                                 

            
 

Posted

You need to place your code in code tags. I have already told you previously. (<> in the editor toolbar)

Posted

What are you trying to do?

 

If I understand your intent...

 

This modification would make it run when loaded.

 

(defun c:ent (/)
 (if (/= (ssget "_X" '((0 . "REGION"))) nil)                  
  (alert "REGIOON LEITUD\color 51 kollane")                   
 )                                                            
  (ssget "_X" '((0 . "REGION")))                              
  (command "change" "P" "" "Properties" "Color" "51" "")      
  (command "zoom" "Previous" "")                              
  (command "explode" "P" "")                                  
) 

(c:ent) ; Runs the code on load

 

 

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