Author Topic: Three codes not working but written to do the same thing.  (Read 960 times)

0 Members and 1 Guest are viewing this topic.

cadd4la

  • Newt
  • Posts: 27
Three codes not working but written to do the same thing.
« on: May 10, 2023, 12:41:35 AM »
Hi everyone,

I need help getting either one of these codes to work. I'm looking for the code to offset an object in feet and then ask if the user wants to delete the original object.

Code - Auto/Visual Lisp: [Select]
  1. (defun c:offset_and_delete (/ dist ent)
  2.   (setq dist (getdist "\nEnter offset distance in feet: "))
  3.   (if (not dist)
  4.     (progn
  5.       (princ "\nInvalid distance.")
  6.       (exit)
  7.     )
  8.     (setq dist (* dist 12.0)) ; Convert feet to inches
  9.     (setq ent (car (entsel "\nSelect object to offset: ")))
  10.     (if (not ent)
  11.       (progn
  12.         (princ "\nNo object selected.")
  13.         (exit)
  14.       )
  15.       (progn
  16.         (command "_.OFFSET" ent "" dist)
  17.         (if (y-or-n-p "\nDelete original object? ")
  18.           (command "_.ERASE" ent "")
  19.         )
  20.       )
  21.     )
  22.   )
  23.   (princ)
  24. )

Code - Auto/Visual Lisp: [Select]
  1. (defun c:offset_and_delete (/ dist ent)
  2.   (setq dist (getdist "\nEnter offset distance: "))
  3.   (if (not dist)
  4.     (progn
  5.       (princ "\nInvalid distance.")
  6.       (exit)
  7.     )
  8.     (setq ent (car (entsel "\nSelect object to offset: ")))
  9.     (if (not ent)
  10.       (progn
  11.         (princ "\nNo object selected.")
  12.         (exit)
  13.       )
  14.       (progn
  15.         (command "_.OFFSET" ent "" dist)
  16.         (if (setq delete (getkword "\nDelete original object? [Y/N]: "))
  17.           (progn
  18.             (if (= delete "Y")
  19.               (command "_.ERASE" ent "")
  20.             )
  21.           )
  22.         )
  23.       )
  24.     )
  25.   )
  26.   (princ)
  27. )

Code - Auto/Visual Lisp: [Select]
  1. (defun c:offset_and_delete (/ dist sel ent)
  2.   (setq dist (getdist "\nEnter the offset distance (in feet): "))
  3.   (setq sel (ssget))
  4.   (if (not sel)
  5.     (progn
  6.       (princ "\nNo objects selected.")
  7.       (exit)
  8.     )
  9.     (progn
  10.       (repeat (setq i (sslength sel))
  11.         (setq ent (ssname sel (setq i (1- i))))
  12.         (command "_.offset" ent "" dist)
  13.       )
  14.       (setq delete (getkword "\nDelete original objects? [Yes/No]: "))
  15.       (cond
  16.         ((or (string-equal delete "Y") (string-equal delete "YES"))
  17.           (command "_.erase" sel "")
  18.         )
  19.         (t
  20.           (princ "\nOriginal objects not deleted.")
  21.         )
  22.       )
  23.     )
  24.   )
  25.   (princ)
  26. )

When I load either one of the three codes I get ; error: syntax error.

Thanks,

Cadd4la
Windows 10 x64 - AutoCAD 2023

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2149
  • class keyThumper<T>:ILazy<T>
Re: Three codes not working but written to do the same thing.
« Reply #1 on: May 10, 2023, 01:13:28 AM »
haven't tried, but :

Have a look at your OFFSET command parameter sequence !
distance should be before selection.

There may be more, but start there.

perhaps look at defining an error trap that includes a backtrace ie  (vl-bt)
« Last Edit: May 10, 2023, 01:37:56 AM by kdub_nz »
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8783
  • AKA Daniel
Re: Three codes not working but written to do the same thing.
« Reply #2 on: May 10, 2023, 04:33:56 AM »
missing initget ?

cadd4la

  • Newt
  • Posts: 27
Re: Three codes not working but written to do the same thing.
« Reply #3 on: May 12, 2023, 05:31:16 PM »
Beavis/Kdub_nz,

Thank you for your input however, I had taken each code as far as I can and need someone to help me get either one of them to work.

Regards,

Cadd4la
Windows 10 x64 - AutoCAD 2023

BIGAL

  • Swamp Rat
  • Posts: 1429
  • 40 + years of using Autocad
Re: Three codes not working but written to do the same thing.
« Reply #4 on: May 12, 2023, 08:14:13 PM »
Try

Code: [Select]
(progn
        (command "_.OFFSET" dist ent "")
(initget "Yes No")
        (setq delete (getkword "\nDelete original object? [Y/N]: "))
            (if (= delete "Yes")
              (command "_.ERASE" ent "")
            )
      )
A man who never made a mistake never made anything

EWCAD

  • Mosquito
  • Posts: 14
Re: Three codes not working but written to do the same thing.
« Reply #5 on: May 12, 2023, 08:15:43 PM »
I'm assuming you are learning so I'll leave some notes for you.
Code: [Select]
(defun c:offset_and_delete (/ dist ent)
  (setq dist (getdist "\nEnter offset distance in feet: "))

 ;none of this is needed. Just use an initget to check if it is a valid entry.
;vvvvvvvvvvv
  (if (not dist)
    (progn
      (princ "\nInvalid distance.")
      (exit)
    )
;^^^^^^^^
;instead of converting the distance to feet after it has been set, just convert it at the same time
;for example, (setq dist (*(getdist \n do distance: ")12))
    (setq dist (* dist 12.0)) ; Convert feet to inches
;instead of quitting the routine when the entity is missed, loop the part where you select
;until you have a valid selection
    (setq ent (car (entsel "\nSelect object to offset: ")))
    (if (not ent)
      (progn
        (princ "\nNo object selected.")
        (exit)
      )
      (progn
;The order of this command is wrong, its distance->Object->Direction
;Just run through the command in AutoCAD and take note of the order.
        (command "_.OFFSET" ent "" dist)
;You need to set a variable Yes or No and then check it.
;Look at updated code for example.
        (if (y-or-n-p "\nDelete original object? ")
          (command "_.ERASE" ent "")
        )
      )
    )
  )
  (princ)
)

Here's my version, seems to work as you want.
Code: [Select]
(defun c:ODEL (/ dist ent side del)
(initget 7)
(setq dist (*(getdist "\nEnter offset distance in Feet: ")12))
(while
(not ent)
(setvar 'errno 0)
(setq ent (car (entsel "\nSelect object to offset: ")))
(and (= 7 (getvar 'errno))
(princ "\nNo Entity Selected!")
)
)
(setq side (getpoint "\n Pick Offset Direction"))
    (command "_.OFFSET" dist ent side "")
(initget "Yes, No")
(setq del (getkword "\nDelete original object? [Yes/No]: "))
        (if (= del "Yes")
(command "_.ERASE" ent "")
        )
  (princ)
)

ribarm

  • Gator
  • Posts: 3304
  • Marko Ribar, architect
Re: Three codes not working but written to do the same thing.
« Reply #6 on: May 13, 2023, 08:31:01 AM »
Just a small remark...
I assume you meant :
(initget "Yes No") instead of (initget "Yes, No"), but still maybe it'll behave identically... I'd still stick with (initget "Yes No") as it's shorter and no need to check if after (getkword) "Yes," would be taken instead of just "Yes"...

My version of last paragraph :
Code: [Select]
;;; check if layer of reference object ent is locked and unlock it ;;;
(if (= 4 (logand 4 (cdr (assoc 70 (tblsearch "LAYER" (cdr (assoc 8 (entget ent))))))))
  (progn
    (vl-cmdf "_.-LAYER" "_U" (cdr (assoc 8 (entget ent))))
    (while (< 0 (getvar 'cmdactive))
      (vl-cmdf "")
    )
  )
)
;;; assuming that layer is unlocked, erase reference entity if you wish ;;;
(initget "Yes No")
(if (= "Yes" (cond ( (getkword "\nErase original entity [Yes / No] <Yes> : ") ) ( "Yes" )))
  (if (not (vlax-erased-p ent))
    (entdel ent)
  )
  (if (vlax-erased-p ent)
    (entdel ent)
  )
)
« Last Edit: May 13, 2023, 08:34:37 AM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

cadd4la

  • Newt
  • Posts: 27
Re: Three codes not working but written to do the same thing.
« Reply #7 on: May 15, 2023, 06:06:15 PM »
Ribarm/Bigal,

Thank you both for your input unfortunately with my limited knowledge of coding I need more hand-holding to understand each code and how to use them.

EWCAD,
Thank you for your code, it works great.

Cadd4la
Windows 10 x64 - AutoCAD 2023