Author Topic: Help on my Leader Routine  (Read 3109 times)

0 Members and 1 Guest are viewing this topic.

RolandOrzabal

  • Newt
  • Posts: 86
  • "memories fade but the scars still linger"
Help on my Leader Routine
« on: August 30, 2012, 02:22:39 AM »
Hi swampers! Am new to LISP. I'm trying to make a lisp that will turn the Leaders Color to ByLayer according to the Layer it is drawn overriding the Color defined in the Dimension Style.

So far this is what i got.

Code: [Select]
(defun c:try (/ )
 (setq cmd1 (getvar "cmdecho"))
 (setvar "cmdecho" 0)
   (if (progn (princ "\nSelect Leaders to Modify:")
        (setq ssLead (ssget '((0 . "LEADER")))))
     (command "dimoverride" "dimclrd" "256" "dimclre" "256" "" ssLead)
     (princ "\nNo Leaders Selected.")
   )
 (setvar "cmdecho" cmd1)
(princ))

My question is how come i need to issue Enter twice at the end to end the routine? i cannot trace where in the code it is happening...

And if no Leader is found...the routine is ended. How to pause the routine and continue until the user selected a leader to modify?
"memories fade but the scars still linger"

RolandOrzabal

  • Newt
  • Posts: 86
  • "memories fade but the scars still linger"
Re: Help on my Leader Routine
« Reply #1 on: August 30, 2012, 03:29:05 AM »
Got the solution to my first question :)

This line :
Code: [Select]
(command "dimoverride" "dimclrd" "256" "dimclre" "256" "" ssLead)
should be  :
Code: [Select]
(command "dimoverride" "dimclrd" "256" "dimclre" "256" "" ssLead "")
i missed the Enter   :lmao:



« Last Edit: August 30, 2012, 03:32:17 AM by NOD684 »
"memories fade but the scars still linger"

Lee Mac

  • Seagull
  • Posts: 12926
  • London, England
Re: Help on my Leader Routine
« Reply #2 on: August 30, 2012, 07:34:28 AM »
And if no Leader is found...the routine is ended. How to pause the routine and continue until the user selected a leader to modify?

You could use a while loop to continuously prompt the user until a valid selection is made:

Code: [Select]
(defun c:try ( / cm ss )
    (while (null (setq ss (ssget "_:L" '((0 . "LEADER")))))
        (princ "\nNo Leaders Selected.")
    )
    (setq cm (getvar 'cmdecho))
    (setvar 'cmdecho 0)
    (command "_.dimoverride" "dimclrd" 256 "dimclre" 256 "" ss "")
    (setvar 'cmdecho cm)
    (princ)
)

Though note that by using this construct the user must press Esc should he/she wish to exit the program without modifying any Leaders.

I would also recommend that you ensure that your code is indented in a logical manner, as this will improve the readability of your code and also help you to spot mistakes. What editor are you using to write your code?

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Help on my Leader Routine
« Reply #3 on: August 30, 2012, 08:35:31 AM »
I would offer a variation on Lee's routine that will exit on ENTER when no leader was selected.
It will repeat as long as a selection is made each time through the loop.
Code: [Select]
(defun c:try (/ cm ss)
  (while (and (Princ "\nSelect LEADERS to change.")
              (setq ss (ssget "_:L" '((0 . "LEADER")))))
    (setq cm (getvar 'cmdecho))
    (setvar 'cmdecho 0)
    (command "_.dimoverride" "dimclrd" 256 "dimclre" 256 "" ss "")
    (setvar 'cmdecho cm)
  )
  (princ)
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

RolandOrzabal

  • Newt
  • Posts: 86
  • "memories fade but the scars still linger"
Re: Help on my Leader Routine
« Reply #4 on: August 30, 2012, 08:51:47 AM »
And if no Leader is found...the routine is ended. How to pause the routine and continue until the user selected a leader to modify?

You could use a while loop to continuously prompt the user until a valid selection is made:

Code: [Select]
(defun c:try ( / cm ss )
    (while (null (setq ss (ssget "_:L" '((0 . "LEADER")))))
        (princ "\nNo Leaders Selected.")
    )
    (setq cm (getvar 'cmdecho))
    (setvar 'cmdecho 0)
    (command "_.dimoverride" "dimclrd" 256 "dimclre" 256 "" ss "")
    (setvar 'cmdecho cm)
    (princ)
)

Though note that by using this construct the user must press Esc should he/she wish to exit the program without modifying any Leaders.

I would also recommend that you ensure that your code is indented in a logical manner, as this will improve the readability of your code and also help you to spot mistakes. What editor are you using to write your code?

Thanks a lot Lee, i read this WHILE before but dont know where to place it.
i'll try this out tomorrow as i am only using mobile now.

by the way am using Notepad only...
"memories fade but the scars still linger"

RolandOrzabal

  • Newt
  • Posts: 86
  • "memories fade but the scars still linger"
Re: Help on my Leader Routine
« Reply #5 on: August 30, 2012, 08:55:05 AM »
I would offer a variation on Lee's routine that will exit on ENTER when no leader was selected.
It will repeat as long as a selection is made each time through the loop.
Code: [Select]
(defun c:try (/ cm ss)
  (while (and (Princ "\nSelect LEADERS to change.")
              (setq ss (ssget "_:L" '((0 . "LEADER")))))
    (setq cm (getvar 'cmdecho))
    (setvar 'cmdecho 0)
    (command "_.dimoverride" "dimclrd" 256 "dimclre" 256 "" ss "")
    (setvar 'cmdecho cm)
  )
  (princ)
)

thanks a lot sir Alan, i will try tomorrow these variations.

"memories fade but the scars still linger"

Lee Mac

  • Seagull
  • Posts: 12926
  • London, England
Re: Help on my Leader Routine
« Reply #6 on: August 30, 2012, 09:00:58 AM »
by the way am using Notepad only...

I would strongly suggest that you use a code editor such as the Visual LISP IDE (provided with AutoCAD) with which to write your AutoLISP programs. There are many generic code editors available on the web, (such as Notepad++ / Ultraedit / Eclipse / Emacs / Vim etc.), but in my opinion, the Visual LISP IDE offers the most functionality of any editor for writing and debugging AutoLISP.

RolandOrzabal

  • Newt
  • Posts: 86
  • "memories fade but the scars still linger"
Re: Help on my Leader Routine
« Reply #7 on: August 30, 2012, 09:11:25 AM »
by the way am using Notepad only...

I would strongly suggest that you use a code editor such as the Visual LISP IDE (provided with AutoCAD) with which to write your AutoLISP programs. There are many generic code editors available on the web, (such as Notepad++ / Ultraedit / Eclipse / Emacs / Vim etc.), but in my opinion, the Visual LISP IDE offers the most functionality of any editor for writing and debugging AutoLISP.

thanks Lee, haven't tried using Visual LISP IDE yet though ive seen it already...i'll try to learn more on that too....
appreciate your help
"memories fade but the scars still linger"

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Help on my Leader Routine
« Reply #8 on: August 30, 2012, 10:20:33 AM »
Lots of VLIDE help here: http://goo.gl/n58YD
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

RolandOrzabal

  • Newt
  • Posts: 86
  • "memories fade but the scars still linger"
Re: Help on my Leader Routine
« Reply #9 on: August 31, 2012, 12:26:30 AM »
"memories fade but the scars still linger"

RolandOrzabal

  • Newt
  • Posts: 86
  • "memories fade but the scars still linger"
Re: Help on my Leader Routine
« Reply #10 on: September 02, 2012, 09:28:12 PM »
if the user missed selecting the leader, how would i alert /  inform him to retry?
"memories fade but the scars still linger"

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Help on my Leader Routine
« Reply #11 on: September 02, 2012, 09:56:42 PM »

Perhaps try something like :

Code - Auto/Visual Lisp: [Select]
  1.  
  2. (if (setq result (kdub:objsel "Select Leader" (list "LEADER") nil))
  3.   (setq ent (car result))
  4.   (progn
  5.     (setq ent nil)
  6.     (alert "Fail")
  7.    )
  8. )
  9.  

Code - Auto/Visual Lisp: [Select]
  1.  
  2. ;;;------------------------------------------------------------------
  3. ;;;------------------------------------------------------------------
  4. ;; (KDUB:OBJSEL <Promptmsg> <typelist > <nentselflag > )
  5. (defun kdub:objsel (promptmsg                ;
  6.                     typelist                 ; List of entity types allowed to be selected
  7.                     nentselflag              ; If true nentsel permitted , otherwise use entsel.
  8.                     / pickok returnvalue tmp etype)
  9.   (setq promptmsg (strcat "\n"
  10.                           (cond (promptmsg)
  11.                                 ("Select object")
  12.                           )
  13.                           " : "
  14.                   )
  15.   )
  16.   (while (not pickok)
  17.     (setvar "ERRNO" 0)
  18.     (setq returnvalue (if nentselflag
  19.                         (nentsel promptmsg)
  20.                         (entsel promptmsg)
  21.                       )
  22.     )
  23.     (cond
  24.       ((= (getvar "ERRNO") 52)               ; enter
  25.        ;; skip out
  26.        (setq pickok t)
  27.       )
  28.       ((= (getvar "ERRNO") 7) (princ "Nothing found at selectedpoint. "))
  29.       ((and (setq tmp (entget (car returnvalue))) ; object type
  30.             ;; If selection is for Nested Entity,
  31.             ;; If the entity type is a VERTEX
  32.             ;; Return the Parent 2dPOLYLINE
  33.             ;;
  34.             typelist
  35.             (if (and nentselflag (= (cdr (assoc 0 tmp)) "VERTEX"))
  36.               (setq returnvalue (list (cdar (entget (cdr (assoc 330 tmp))))
  37.                                       (cadr returnvalue)
  38.                                 )
  39.                     tmp         (entget (car returnvalue))
  40.               )
  41.               ;; else
  42.               t
  43.             )
  44.             (setq etype (cdr (assoc 0 tmp)))
  45.             (not (member (cdr (assoc 0 tmp)) (mapcar 'strcase typelist)))
  46.        )                                     ; wrong type
  47.        (alert
  48.          (strcat
  49.            "Selected object is not"
  50.            "\na "
  51.            (apply 'strcat
  52.                   (cons (car typelist)
  53.                         (mapcar '(lambda (x) (strcat "\nor " x)) (cdr typelist))
  54.                   )
  55.            )
  56.            ". "
  57.          )
  58.        )
  59.       )
  60.       ;; skip out
  61.       ((setq pickok t))
  62.     )
  63.   )
  64.   returnvalue
  65. )
  66.  
  67.  
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.