Author Topic: smart array  (Read 4778 times)

0 Members and 1 Guest are viewing this topic.

rhino

  • Guest
smart array
« on: February 27, 2009, 10:46:10 AM »
hello all,

This routine uses the array command to array objects with the following enhancements:
- Calculates offset distance between 2 points                     
- Arrays objects on an inclined axis

I'm new to this whole autoLISP thing and just starting to get 'understand'  :ugly: the code.

Code: [Select]
;-------------------------------------------------------------------------------------------------------;
;  Smart Array - Enhanced Array Maker © CJC aka 'RHINO' 2009 ;
;-------------------------------------------------------------------------------------------------------;
; This routine uses the array command to array objects with the following enhancements: ;
; - Calculates offset distance between 2 points ;
; - Arrays objects on an inclined axis ;
;-------------------------------------------------------------------------------------------------------;

(defun c:sa (/) (c:smart_array))
(defun c:smart_array (/ obj mode typ hasangle qtx l dist_X dist_Y qty w ent en_typ vert tmp )

;-----------------------------------------------;
;  Variables & Error Handler ;
;-----------------------------------------------;
 
  (setq oldce   (getvar "cmdecho")
oldosmd (getvar "osmode")
oldangd (getvar "angdir")
)
  (setvar "cmdecho" 0)
  (setvar "osmode" 0)
  (setvar "angdir" 1)
  (setq olderr *error*)
  (defun *error* (errmes)
    (princ
      (strcat "n\Execution of Smart_Array halted by the following error: "
      errmes " **System variables have been reset**\n"
      )
    )
    (setvar "cmdecho" oldce)
    (setvar "osmode" oldosmd)
    (setvar "angdir" oldangd)
    (setq *error* olderr)
    (prin1)
  )
 
;-----------------------------------------------;
;  Get User Input ;
;-----------------------------------------------;

  (setq obj   (ssget)
)
  (while (null obj)
    (progn
      (princ (strcat "\n*Nothing Selected* "))
      (setq obj (ssget)
    )
      )
    )
  ;(command "undo" "mark") ; set an undo marker to if user wants to undo array
  (initget 0 "Calc Mnl")
    (setq mode  (getkword "\nEnter Mode for array [<Calculate points/distance>/Manual]: "))
      (if (null mode) (setq mode "Calc"))   
  (initget 0 "X Y Grid")
    (setq typ (getkword "\nEnter the type of array [<X axis>/Y axis/Grid]: "))
      (if (null typ) (setq typ "X"))
  (initget 1024 "Yes No")
    (setq hasangle (getkword "\nAre the objects on an inclined plane? [Yes/<No>]: "))
      (if (null hasangle) (setq hasangle "No"))
 
;-----------------------------------------------;
;  Evaluate user input ;
;-----------------------------------------------;

  ; evaluate array mode and set values 
    (if (= mode "Calc")
      (progn
        (if (= typ "X")
  (progn
    (initget 1)
    (setq qtx    (getint "\nEnter Quantity on X axis: "))
    (setvar "osmode" 191)
    (initget 1)
    (setq l      (getdist "\nDistance on X axis or click points: ")
  dist_X (/ l qtx))
    (setvar "osmode" 0)
    )
  )
(if (= typ "Y")
  (progn
    (initget 1)
    (setq qty    (getint "\nEnter Quantity on Y axis: "))
    (initget 1)
    (setvar "osmode" 191)
    (setq w      (getdist "\nDistance on Y axis or click points: ")
  dist_Y (/ w qty))
    (setvar "osmode" 0)
    )
  )
(if (= typ "Grid")
  (progn
    (initget 1)
    (setq qtx    (getint "\nEnter Quantity on X axis: "))
    (initget 1)
    (setq qty    (getint "\nEnter Quantity on Y axis: "))
    (setvar "osmode" 191)
    (initget 1)
    (setq l      (getdist "\nDistance on X axis or click points: ")
  dist_X (/ l qtx))
    (initget 1)
    (setq w      (getdist "\nDistance on Y axis or click points: ")
  dist_Y (/ w qty))
    (setvar "osmode" 0)
    )
  )
)
      ); if mode Calc      
   
    (if (= mode "Mnl")
      (progn
        (if (= typ "X")
  (progn
    (initget 1)
    (setq qtx    (getint "\nEnter Quantity on X axis: "))
    (setvar "osmode" 191)
    (initget 1)
    (setq dist_X (getdist "\nEnter offset distance on X axis or click points: "))
    (setvar "osmode" 0)
    )
  )
(if (= typ "Y")
  (progn
   (initget 1)
    (setq qty    (getint "\nEnter Quantity on Y axis: "))
    (setvar "osmode" 191)
    (initget 1)
    (setq dist_Y (getdist "\nEnter offset distance on Y axis or click points: "))
    (setvar "osmode" 0)
    )
  )
(if (= typ "Grid")
  (progn
    (initget 1)
    (setq qtx    (getint "\nEnter Quantity on X axis: "))
    (initget 1)
    (setq qty    (getint "\nEnter Quantity on Y axis: "))
    (setvar "osmode" 191)
    (initget 1)
    (setq dist_X (getdist "\nEnter offset distance on X axis or click points: "))
    (initget 1)
    (setq dist_Y (getdist "\nEnter offset distance on Y axis or click points: "))
    (setvar "osmode" 0)
    )
  )
)
      ); if mode Mnl

  ; evaluate object for inclination angle setting and set values
  (if (= hasangle "Yes")
    (progn
      (setq ent   (car (entsel "\nSelect object to set inclination angle: "))
      )
    (while (null ent)
      (progn
(princ (strcat "\n*Nothing Selected* "))
(setq ent (car (entsel "\nPlease select an object: "))
      )
)
      )
  (setq en_data (entget ent)
en_type (cdr (assoc 0 en_data))
)
      )
    ); hasangle
  (if (= en_type "LWPOLYLINE")
    (progn
      (setq vert (cdr (assoc 90 en_data)))
(if (= vert 4)
  (progn
    (setq en_type "T_1"
  pt1 (cdr (nth 14 en_data))
  pt2 (cdr (nth 18 en_data))
  )
    )
  ); for rectangle
      (if (= vert 3)
(progn
(setq en_type "T_1"
      pt1 (cdr (nth 14 en_data))
      pt2 (cdr (nth 18 en_data))
      )
)
)
      (if (= vert 2)
(setq en_type "T_2")
)
      (if (> vert 4)
(setq en_type "T_2")
      )
    )
    ); if lwpolyline
  (if (= en_type "LINE")
    (setq en_type "T_2")
    )
  (if (= en_type "INSERT")
    (setq en_type "T_2")
    )
  (if (= en_type "TEXT")
    (setq en_type "T_2")
    )
  (if (= en_type "MTEXT")
    (setq en_type "T_2")
    )
 
;-----------------------------------------------;
;  Make array ;
;-----------------------------------------------;

  (if (= typ "X")
   (progn
    (if (= hasangle "No")
      (progn
      (command "-array" obj "" "_rectangular" "1" (fix qtx) dist_X)
       )
    ); if no angle
    (princ)
   
    (if (= hasangle "Yes")
      (progn
(if (= en_type "T_1")
  (progn
    (command "line" pt1 pt2 ""); draw line to set UCS Angle #find a variable to fix this#
    (setq tmp (entlast))
    (command "_ucs" "_object" tmp
     "_-array" obj "" "_rectangular" "1" (fix qtx) dist_X
     "_ucs" "_top"
     "_erase" tmp ""
    )
    (princ)
  )
); if "T_1"
(if (= en_type "T_2")
  (progn
    (command "_ucs" "_object" ent
     "_-array"  obj "" "_rectangular" "1" (fix qtx) dist_X
     "_ucs" "_top"
    )
  )
); if "T_2"
      )
    ); if has angle
    (princ)
    )
    ); X_array

  (if (= typ "Y")
   (progn
    (if (= hasangle "No")
      (progn
      (command "_-array" obj "" "_rectangular" (fix qty) "1" dist_Y)
)
    ); if no angle
    (princ)
   
    (if (= hasangle "Yes")
      (progn
(if (= en_type "T_1")
  (progn
    (command "_line" pt1 pt2 "")
    (setq tmp (entlast))
    (command "_ucs" "_object" tmp
     "_-array" obj "" "_rectangular" (fix qty) "1" dist_Y
     "_ucs" "_top"
     "_erase" tmp ""
    )
    (princ)
  )
); if "T_1"
(if (= en_type "T_2")
  (progn
    (command "_ucs" "_object" ent
     "_-array"  obj "" "_rectangular" (fix qty) "1" dist_Y
     "_ucs" "_top"
    )
  )
); if "T_2"
      )
    ); if has angle
    (princ)
    )
    ); Y_array

  (if (= typ "Grid")
   (progn
    (if (= hasangle "No")
      (progn
      (command "_-array" obj "" "_rectangular" (fix qty) (fix qtx) dist_Y dist_X)
)
      ); if no angle
    (princ)

    (if (= hasangle "Yes")
      (progn
(if (= en_type "T_1")
  (progn
    (command "_line" pt1 pt2 "")
    (setq tmp (entlast))
    (command "_ucs" "_object" tmp
             "_-array" obj "" "_rectangular" (fix qty) (fix qtx) dist_Y dist_X
             "_ucs" "_top"
     "_erase" tmp ""
     )
    (princ)
    )
  ); if "T_1"
(if (= en_type "T_2") 
  (progn
    (command "_ucs" "_object" ent
             "_-array" obj "" "_rectangular" (fix qty) (fix qtx) dist_Y dist_X
     "_ucs" "_top"
     )
    )
  ); if "T_2"
)
      ); if has angle
    (princ)
    )
    ); Grid array
 
  (princ) 

  (setvar "cmdecho" oldce)
  (setvar "osmode" oldosmd)
  (setvar "angdir" oldangd)
  (setq *error* olderr)
  ;(prompt "\nIf you want to undo the array use undo - <back> to return to mark ")
  (prin1)
 
); smart array

(prompt "\n smart_array is now available - type <SA> to start! ")

    **updated for international language support**

suggestions and tips welcome :)

cheers!

-rhino   
« Last Edit: February 27, 2009, 11:06:29 AM by rhino »

Andrea

  • Water Moccasin
  • Posts: 2372
Re: smart array
« Reply #1 on: February 27, 2009, 10:50:34 AM »
First...Welcome to theswamp !

second...Thanks for sharing your code !

comments:
Not working...
maybe need to underscore all your command for language compatibilitie...
but knowing it is not easy to test when you have only 1 language version...
there we go.

Code: [Select]
;-------------------------------------------------------------------------------------------------------;
;  Smart Array - Enhanced Array Maker © CJC aka 'RHINO' 2009 ;
;-------------------------------------------------------------------------------------------------------;
; This routine uses the array command to array objects with the following enhancements: ;
; - Calculates offset distance between 2 points ;
; - Arrays objects on an inclined axis ;
;-------------------------------------------------------------------------------------------------------;

(defun c:sa (/) (c:smart_array))
(defun c:smart_array (/ obj mode typ hasangle qtx l dist_X dist_Y qty w ent en_typ vert tmp )

;-----------------------------------------------;
;  Variables & Error Handler ;
;-----------------------------------------------;
 
  (setq oldce   (getvar "cmdecho")
oldosmd (getvar "osmode")
oldangd (getvar "angdir")
)
  (setvar "cmdecho" 0)
  (setvar "osmode" 0)
  (setvar "angdir" 1)
  (setq olderr *error*)
  (defun *error* (errmes)
    (princ
      (strcat "n\Execution of Smart_Array halted by the following error: "
     errmes " **System variables have been reset**\n"
      )
    )
    (setvar "cmdecho" oldce)
    (setvar "osmode" oldosmd)
    (setvar "angdir" oldangd)
    (setq *error* olderr)
    (prin1)
  )
 
;-----------------------------------------------;
;  Get User Input ;
;-----------------------------------------------;

  (setq obj   (ssget)
)
  (while (null obj)
    (progn
      (princ (strcat "\n*Nothing Selected* "))
      (setq obj (ssget)
   )
      )
    )
  ;(command "undo" "mark") ; set an undo marker to if user wants to undo array
  (initget 0 "Calc Mnl")
    (setq mode  (getkword "\nEnter Mode for array [<Calculate points/distance>/Manual]: "))
      (if (null mode) (setq mode "Calc"))  
  (initget 0 "X Y Grid")
    (setq typ (getkword "\nEnter the type of array [<X axis>/Y axis/Grid]: "))
      (if (null typ) (setq typ "X"))
  (initget 1024 "Yes No")
    (setq hasangle (getkword "\nAre the objects on an inclined plane? [Yes/<No>]: "))
      (if (null hasangle) (setq hasangle "No"))
 
;-----------------------------------------------;
;  Evaluate user input ;
;-----------------------------------------------;

  ; evaluate array mode and set values 
    (if (= mode "Calc")
      (progn
        (if (= typ "X")
(progn
   (initget 1)
   (setq qtx    (getint "\nEnter Quantity on X axis: "))
   (setvar "osmode" 191)
   (initget 1)
   (setq l      (getdist "\nDistance on X axis or click points: ")
dist_X (/ l qtx))
   (setvar "osmode" 0)
   )
)
(if (= typ "Y")
(progn
   (initget 1)
   (setq qty    (getint "\nEnter Quantity on Y axis: "))
   (initget 1)
   (setvar "osmode" 191)
   (setq w      (getdist "\nDistance on Y axis or click points: ")
dist_Y (/ w qty))
   (setvar "osmode" 0)
   )
)
(if (= typ "Grid")
(progn
   (initget 1)
   (setq qtx    (getint "\nEnter Quantity on X axis: "))
   (initget 1)
   (setq qty    (getint "\nEnter Quantity on Y axis: "))
   (setvar "osmode" 191)
   (initget 1)
   (setq l      (getdist "\nDistance on X axis or click points: ")
dist_X (/ l qtx))
   (initget 1)
   (setq w      (getdist "\nDistance on Y axis or click points: ")
dist_Y (/ w qty))
   (setvar "osmode" 0)
   )
)
)
      ); if mode Calc     
   
    (if (= mode "Mnl")
      (progn
        (if (= typ "X")
(progn
   (initget 1)
   (setq qtx    (getint "\nEnter Quantity on X axis: "))
   (setvar "osmode" 191)
   (initget 1)
   (setq dist_X (getdist "\nEnter offset distance on X axis or click points: "))
   (setvar "osmode" 0)
   )
)
(if (= typ "Y")
(progn
  (initget 1)
   (setq qty    (getint "\nEnter Quantity on Y axis: "))
   (setvar "osmode" 191)
   (initget 1)
   (setq dist_Y (getdist "\nEnter offset distance on Y axis or click points: "))
   (setvar "osmode" 0)
   )
)
(if (= typ "Grid")
(progn
   (initget 1)
   (setq qtx    (getint "\nEnter Quantity on X axis: "))
   (initget 1)
   (setq qty    (getint "\nEnter Quantity on Y axis: "))
   (setvar "osmode" 191)
   (initget 1)
   (setq dist_X (getdist "\nEnter offset distance on X axis or click points: "))
   (initget 1)
   (setq dist_Y (getdist "\nEnter offset distance on Y axis or click points: "))
   (setvar "osmode" 0)
   )
)
)
      ); if mode Mnl

  ; evaluate object for inclination angle setting and set values
  (if (= hasangle "Yes")
    (progn
      (setq ent   (car (entsel "\nSelect object to set inclination angle: "))
     )
   (while (null ent)
     (progn
(princ (strcat "\n*Nothing Selected* "))
(setq ent (car (entsel "\nPlease select an object: "))
     )
)
     )
  (setq en_data (entget ent)
en_type (cdr (assoc 0 en_data))
)
      )
    ); hasangle
  (if (= en_type "LWPOLYLINE")
    (progn
      (setq vert (cdr (assoc 90 en_data)))
(if (= vert 4)
(progn
   (setq en_type "T_1"
pt1 (cdr (nth 14 en_data))
pt2 (cdr (nth 18 en_data))
)
   )
); for rectangle
      (if (= vert 3)
(progn
(setq en_type "T_1"
     pt1 (cdr (nth 14 en_data))
     pt2 (cdr (nth 18 en_data))
     )
)
)
      (if (= vert 2)
(setq en_type "T_2")
)
      (if (> vert 4)
(setq en_type "T_2")
      )
    )
    ); if lwpolyline
  (if (= en_type "LINE")
    (setq en_type "T_2")
    )
  (if (= en_type "INSERT")
    (setq en_type "T_2")
    )
  (if (= en_type "TEXT")
    (setq en_type "T_2")
    )
  (if (= en_type "MTEXT")
    (setq en_type "T_2")
    )
 
;-----------------------------------------------;
;  Make array ;
;-----------------------------------------------;

  (if (= typ "X")
   (progn
    (if (= hasangle "No")
      (progn
      (command "._-array" obj "" "_rectangular" "1" (fix qtx) dist_X)
       )
    ); if no angle
    (princ)
   
    (if (= hasangle "Yes")
      (progn
(if (= en_type "T_1")
(progn
   (command "._line" pt1 pt2 ""); draw line to set UCS Angle #find a variable to fix this#
   (setq tmp (entlast))
   (command "._ucs" "_object" tmp
    "._-array" obj "" "_rectangular" "1" (fix qtx) dist_X
    "._ucs" "_top"
    "._erase" tmp ""
   )
   (princ)
)
); if "T_1"
(if (= en_type "T_2")
(progn
   (command "._ucs" "_object" ent
    ".-array"  obj "" "_rectangular" "1" (fix qtx) dist_X
    "._ucs" "_top"
   )
)
); if "T_2"
      )
    ); if has angle
    (princ)
    )
    ); X_array

  (if (= typ "Y")
   (progn
    (if (= hasangle "No")
      (progn
      (command "._-array" obj "" "_rectangular" (fix qty) "1" dist_Y)
)
    ); if no angle
    (princ)
   
    (if (= hasangle "Yes")
      (progn
(if (= en_type "T_1")
(progn
   (command "._line" pt1 pt2 "")
   (setq tmp (entlast))
   (command "._ucs" "_object" tmp
    "._-array" obj "" "_rectangular" (fix qty) "1" dist_Y
    "_ucs" "_top"
    "._erase" tmp ""
   )
   (princ)
)
); if "T_1"
(if (= en_type "T_2")
(progn
   (command "._ucs" "_object" ent
    "._-array"  obj "" "_rectangular" (fix qty) "1" dist_Y
    "._ucs" "_top"
   )
)
); if "T_2"
      )
    ); if has angle
    (princ)
    )
    ); Y_array

  (if (= typ "Grid")
   (progn
    (if (= hasangle "No")
      (progn
      (command "._-array" obj "" "_rectangular" (fix qty) (fix qtx) dist_Y dist_X)
)
      ); if no angle
    (princ)

    (if (= hasangle "Yes")
      (progn
(if (= en_type "T_1")
(progn
   (command "._line" pt1 pt2 "")
   (setq tmp (entlast))
   (command "._ucs" "_object" tmp
            "._-array" obj "" "_rectangular" (fix qty) (fix qtx) dist_Y dist_X
            "._ucs" "_top"
    "._erase" tmp ""
    )
   (princ)
   )
); if "T_1"
(if (= en_type "T_2") 
(progn
   (command "._ucs" "_object" ent
            "._-array" obj "" "_rectangular" (fix qty) (fix qtx) dist_Y dist_X
    "._ucs" "_top"
    )
   )
); if "T_2"
)
      ); if has angle
    (princ)
    )
    ); Grid array
 
  (princ) 

  (setvar "cmdecho" oldce)
  (setvar "osmode" oldosmd)
  (setvar "angdir" oldangd)
  (setq *error* olderr)
  ;(prompt "\nIf you want to undo the array use undo - <back> to return to mark ")
  (prin1)
 
); smart array

(prompt "\n smart_array is now available - type <SA> to start! ")
« Last Edit: February 27, 2009, 10:57:18 AM by Andrea »
Keep smile...

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: smart array
« Reply #2 on: February 27, 2009, 04:34:08 PM »
Welcome to theSwamp.

I haven't looked at your code functionality , but you really should look at making the *error* function local to the routine.
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.

rhino

  • Guest
Re: smart array
« Reply #3 on: March 02, 2009, 12:20:17 PM »

Andrea:

Hi and thanks - i guess we both edited at the same time but you were the quicker :)

Kerry:
Welcome to theSwamp.

I haven't looked at your code functionality , but you really should look at making the *error* function local to the routine.

I searched around the 'swamp' and found a post that  explained the whole error routine thing...so have updated the code

Questions & Help:

This routine arrays the objects on angles based on UCS - 'object' and 'points' for the closed polylines; however there seems to be a bug for the Y axis only option - is there a better way of setting the angle; other than asking the user to click points to set the angle?

I wonder if i used 'if' too much?

Code: [Select]
;-------------------------------------------------------------------------------------------------------;
;  Smart Array - Enhanced Array Maker © CJC aka 'RHINO' 2009 ;
;-------------------------------------------------------------------------------------------------------;
; This routine uses the array command to array objects with the following enhancements: ;
; - Calculates offset distance between 2 points ;
; - Arrays objects on an inclined axis ;
;-------------------------------------------------------------------------------------------------------;

(defun c:sa (/) (c:smart_array))
(defun c:smart_array (/ oldce oldosmd oldangd olderr obj mode typ hasangle qtx l
        dist_X dist_Y qty w ent en_typ vert lp1 lp2 pt1 pt2 tmp *error*)

;-----------------------------------------------;
;  Variables & Error Handler ;
;-----------------------------------------------;
 
  (setq oldce   (getvar "cmdecho")
oldosmd (getvar "osmode")
oldangd (getvar "angdir")
)
  (setvar "cmdecho" 0)
  (setvar "osmode" 0)
  (setvar "angdir" 1)
  (setq olderr *error*)
  (defun *error* (errmes)
    (princ
      (strcat "n\Execution of Smart_Array halted by the following error: "
      errmes " **System variables have been reset**\n"
      )
    )
    (setvar "cmdecho" oldce)
    (setvar "osmode" oldosmd)
    (setvar "angdir" oldangd)
    (setq *error* olderr)
    (prin1)
  )
 
;-----------------------------------------------;
;  Get User Input ;
;-----------------------------------------------;

  (setq obj   (ssget)
)
  (while (null obj)
    (progn
      (princ (strcat "\n*Nothing Selected* "))
      (setq obj (ssget)
    )
      )
    )
  (command "undo" "mark") ; set an undo marker to if user wants to undo array
  (initget 0 "Calc Mnl")
    (setq mode  (getkword "\nEnter Mode for array [<Calculate points/distance>/Manual]: "))
      (if (null mode) (setq mode "Calc"))   
  (initget 0 "X Y Grid")
    (setq typ (getkword "\nEnter the type of array [<X axis>/Y axis/Grid]: "))
      (if (null typ) (setq typ "X"))
  (initget 1024 "Yes No")
    (setq hasangle (getkword "\nAre the objects on an inclined plane? [Yes/<No>]: "))
      (if (null hasangle) (setq hasangle "No"))
 
;-----------------------------------------------;
;  Evaluate user input ;
;-----------------------------------------------;

  ; evaluate array mode and set values 
    (if (= mode "Calc")
      (progn
        (if (= typ "X")
  (progn
    (initget 1)
    (setq qtx    (getint "\nEnter Quantity on X axis: "))
    (setvar "osmode" 191)
    (initget 1)
    (setq l      (getdist "\nDistance on X axis or click points: ")
  dist_X (/ l qtx))
    (setvar "osmode" 0)
    )
  )
(if (= typ "Y")
  (progn
    (initget 1)
    (setq qty    (getint "\nEnter Quantity on Y axis: "))
    (initget 1)
    (setvar "osmode" 191)
    (setq w      (getdist "\nDistance on Y axis or click points: ")
  dist_Y (/ w qty))
    (setvar "osmode" 0)
    )
  )
(if (= typ "Grid")
  (progn
    (initget 1)
    (setq qtx    (getint "\nEnter Quantity on X axis: "))
    (initget 1)
    (setq qty    (getint "\nEnter Quantity on Y axis: "))
    (setvar "osmode" 191)
    (initget 1)
    (setq l      (getdist "\nDistance on X axis or click points: ")
  dist_X (/ l qtx))
    (initget 1)
    (setq w      (getdist "\nDistance on Y axis or click points: ")
  dist_Y (/ w qty))
    (setvar "osmode" 0)
    )
  )
)
      ); if mode Calc      
   
    (if (= mode "Mnl")
      (progn
        (if (= typ "X")
  (progn
    (initget 1)
    (setq qtx    (getint "\nEnter Quantity on X axis: "))
    (setvar "osmode" 191)
    (initget 1)
    (setq dist_X (getdist "\nEnter offset distance on X axis or click points: "))
    (setvar "osmode" 0)
    )
  )
(if (= typ "Y")
  (progn
   (initget 1)
    (setq qty    (getint "\nEnter Quantity on Y axis: "))
    (setvar "osmode" 191)
    (initget 1)
    (setq dist_Y (getdist "\nEnter offset distance on Y axis or click points: "))
    (setvar "osmode" 0)
    )
  )
(if (= typ "Grid")
  (progn
    (initget 1)
    (setq qtx    (getint "\nEnter Quantity on X axis: "))
    (initget 1)
    (setq qty    (getint "\nEnter Quantity on Y axis: "))
    (setvar "osmode" 191)
    (initget 1)
    (setq dist_X (getdist "\nEnter offset distance on X axis or click points: "))
    (initget 1)
    (setq dist_Y (getdist "\nEnter offset distance on Y axis or click points: "))
    (setvar "osmode" 0)
    )
  )
)
      ); if mode Mnl

  ; evaluate object for inclination angle setting and set values
  (if (= hasangle "Yes")
    (progn
      (setq ent   (car (entsel "\nSelect object to set inclination angle: "))
      )
    (while (null ent)
      (progn
(princ (strcat "\n*Nothing Selected* "))
(setq ent (car (entsel "\nPlease select an object: "))
      )
)
      )
  (setq en_data (entget ent)
en_type (cdr (assoc 0 en_data))
)
      )
    ); hasangle
  (if (= en_type "LWPOLYLINE")
    (progn
      (setq vert (cdr (assoc 90 en_data)))
(if (= vert 4)
  (progn
    (setq en_type "T_1"
  pt1 (cdr (nth 14 en_data))
  pt2 (cdr (nth 18 en_data))
  )
    )
  ); for rectangle
      (if (= vert 3)
(progn
(setq en_type "T_1"
      pt1 (cdr (nth 14 en_data))
      pt2 (cdr (nth 18 en_data))
      )
)
)
      (if (= vert 2)
(setq en_type "T_2")
)
      (if (> vert 4)
(setq en_type "T_2")
      )
    )
    ); if lwpolyline
  (if (= en_type "LINE")
    (progn
      (if (= typ "X")
(progn
(setq lp1 (cdr (assoc 10 en_data))
      lp2 (cdr (assoc 11 en_data))
      en_type (if (> (car lp1) (car lp2)) "T_1" "T_2")
      )
(if (= en_type "T_1")
(progn
(setq pt1 lp2
       pt2 lp1
       )
)
); T_1
      )
    ); typ "X"
      )
    )
  (if (= en_type "INSERT")
    (setq en_type "T_2")
    )
  (if (= en_type "TEXT")
    (setq en_type "T_2")
    )
  (if (= en_type "MTEXT")
    (setq en_type "T_2")
    )
 
;-----------------------------------------------;
;  Make array ;
;-----------------------------------------------;

  (if (= typ "X")
   (progn
    (if (= hasangle "No")
      (progn
      (command "._-array" obj "" "_rectangular" "1" (fix qtx) dist_X)
       )
    ); if no angle
    (princ)
   
    (if (= hasangle "Yes")
      (progn
(if (= en_type "T_1")
  (progn
    (command "._line" pt1 pt2 ""); draw line to set UCS Angle #find a variable to fix this#
    (setq tmp (entlast))
    (command "._ucs" "_object" tmp
     "._-array" obj "" "_rectangular" "1" (fix qtx) dist_X
     "._ucs" "_top"
     "._erase" tmp ""
    )
    (princ)
  )
); if "T_1"
(if (= en_type "T_2")
  (progn
    (command "._ucs" "_object" ent
     "._-array"  obj "" "_rectangular" "1" (fix qtx) dist_X
     "._ucs" "_top"
    )
  )
); if "T_2"
      )
    ); if has angle
    (princ)
    )
    ); X_array

  (if (= typ "Y")
   (progn
    (if (= hasangle "No")
      (progn
      (command "._-array" obj "" "_rectangular" (fix qty) "1" dist_Y)
)
    ); if no angle
    (princ)
   
    (if (= hasangle "Yes")
      (progn
(if (= en_type "T_1")
  (progn
    (command "._line" pt1 pt2 "")
    (setq tmp (entlast))
    (command "._ucs" "_object" tmp
     "._-array" obj "" "_rectangular" (fix qty) "1" dist_Y
     "._ucs" "_top"
     "._erase" tmp ""
    )
    (princ)
  )
); if "T_1"
(if (= en_type "T_2")
  (progn
    (command "._ucs" "_object" ent
     "._-array"  obj "" "_rectangular" (fix qty) "1" dist_Y
     "._ucs" "_top"
    )
  )
); if "T_2"
      )
    ); if has angle
    (princ)
    )
    ); Y_array

  (if (= typ "Grid")
   (progn
    (if (= hasangle "No")
      (progn
      (command "._-array" obj "" "_rectangular" (fix qty) (fix qtx) dist_Y dist_X)
)
      ); if no angle
    (princ)

    (if (= hasangle "Yes")
      (progn
(if (= en_type "T_1")
  (progn
    (command "._line" pt1 pt2 "")
    (setq tmp (entlast))
    (command "._ucs" "_object" tmp
             "._-array" obj "" "_rectangular" (fix qty) (fix qtx) dist_Y dist_X
             "._ucs" "_top"
     "._erase" tmp ""
     )
    (princ)
    )
  ); if "T_1"
(if (= en_type "T_2") 
  (progn
    (command "._ucs" "_object" ent
             "._-array" obj "" "_rectangular" (fix qty) (fix qtx) dist_Y dist_X
     "._ucs" "_top"
     )
    )
  ); if "T_2"
)
      ); if has angle
    (princ)
    )
    ); Grid array
 
  (princ) 

  (setvar "cmdecho" oldce)
  (setvar "osmode" oldosmd)
  (setvar "angdir" oldangd)
  (setq *error* olderr)
  ;(prompt "\nIf you want to undo the array use undo - <back> to return to mark ")
  (prin1)
 
); smart array

(prompt "\n smart_array is now available - type <SA> to start! ")


CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: smart array
« Reply #4 on: March 02, 2009, 12:45:55 PM »
Nice job.
Food for thought : http://www.theswamp.org/index.php?topic=7152.msg88168#msg88168

Welcome to TheSwamp.  :-)
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.

gskelly

  • Newt
  • Posts: 185
Re: smart array
« Reply #5 on: March 02, 2009, 07:46:26 PM »

I wonder if i used 'if' too much?

Hi,

I'm working on improving my LISP as well so take what I say with a
grain of salt (and check me against the help file)  :-)

Regarding the "if" question... you can use cond where you have a series
of options where only one is going to match. Similar to:

Code: [Select]
;-----------------------------------------------;
;  Evaluate user input                          ;
;-----------------------------------------------;

  ; evaluate array mode and set values
  (cond
    ((= mode "Calc")
      (cond
        ((= typ "X")
            (initget 1)
            (setq qtx    (getint "\nEnter Quantity on X axis: "))
            (setvar "osmode" 191)
            (initget 1)
            (setq l      (getdist "\nDistance on X axis or click points: ")
                  dist_X (/ l qtx))
            (setvar "osmode" 0)
        )
        ((= typ "Y")
            (initget 1)
            (setq qty    (getint "\nEnter Quantity on Y axis: "))
            (initget 1)
            (setvar "osmode" 191)
            (setq w      (getdist "\nDistance on Y axis or click points: ")
                  dist_Y (/ w qty))
            (setvar "osmode" 0)
        )
        ((= typ "Grid")
            (initget 1)
            (setq qtx    (getint "\nEnter Quantity on X axis: "))
            (initget 1)
            (setq qty    (getint "\nEnter Quantity on Y axis: "))
            (setvar "osmode" 191)
            (initget 1)
            (setq l      (getdist "\nDistance on X axis or click points: ")
                  dist_X (/ l qtx))
            (initget 1)
            (setq w      (getdist "\nDistance on Y axis or click points: ")
                  dist_Y (/ w qty))
            (setvar "osmode" 0)
        )
      )
    ); mode Calc             
   
    ((= mode "Mnl")
      (cond
        ((= typ "X")
            (initget 1)
            (setq qtx    (getint "\nEnter Quantity on X axis: "))
            (setvar "osmode" 191)
            (initget 1)
            (setq dist_X (getdist "\nEnter offset distance on X axis or click points: "))
            (setvar "osmode" 0)
        )
        ((= typ "Y")
           (initget 1)
            (setq qty    (getint "\nEnter Quantity on Y axis: "))
            (setvar "osmode" 191)
            (initget 1)
            (setq dist_Y (getdist "\nEnter offset distance on Y axis or click points: "))
            (setvar "osmode" 0)
        )
        ((= typ "Grid")
            (initget 1)
            (setq qtx    (getint "\nEnter Quantity on X axis: "))
            (initget 1)
            (setq qty    (getint "\nEnter Quantity on Y axis: "))
            (setvar "osmode" 191)
            (initget 1)
            (setq dist_X (getdist "\nEnter offset distance on X axis or click points: "))
            (initget 1)
            (setq dist_Y (getdist "\nEnter offset distance on Y axis or click points: "))
            (setvar "osmode" 0)
        )
      )
    ); mode Mnl
  ); end mode cond

You can also find a lot of examples around here where people use and to
continue executing statements until one returns nil as in:
Code: [Select]
(and
    (do_this)
    (do_that)
    (do_the_other_thing)
)

where each of do_this, do_that and do_the_other_thing will happen as long
as the preceding statement does not return NIL.

Note the and statement will return either T or NIL... not the result of the last
statement.

While I say this I will qualify with: I have not found it in my documentation
where execution is guaranteed to stop on the first NIL but it seems to work
for me. Perhaps someone with more knowledge than I can clarify if this is
the case and always was.

Have fun!

Bricscad v12

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: smart array
« Reply #6 on: March 02, 2009, 08:35:21 PM »
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.

rhino

  • Guest
Re: smart array
« Reply #7 on: March 03, 2009, 12:31:59 PM »
CAB:

Thanks for the links - food for thought hehe - really awesome stuff what can be done

I am looking for some more help/guidance on using the visual lisp functions though for now sticking to just the lisp.

gskelly:

Thanks for your input - i had tried the 'cond' function earlier but as I'm a novice did not figure out the correct way to use it :-o

I'll update the code over the weekend and also will work on making the routine remember the last user input when used

cheers!


gskelly

  • Newt
  • Posts: 185
Re: smart array
« Reply #8 on: March 03, 2009, 10:04:14 PM »
Your welcome rhino...

Glad I could help someone a little... this place has helped much more than I can give back!
Bricscad v12

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: smart array
« Reply #9 on: March 04, 2009, 12:29:45 AM »
rhino,
I took a few minutes and changed the user input section.
Too sleepy to continue, maybe tomorrow.
Didn't even test it, sorry.
Code: [Select]
          ;-----------------------------------------------;
          ;  Get User Input 
          ;-----------------------------------------------;

 
  (while
    (and
      (princ "\nSelect objects to array.")
      (null(setq obj (ssget)))
    )
   (princ (strcat "\n*Nothing Selected* "))
  )
  (command "undo" "mark") ; set an undo marker to if user wants to undo array
  (initget 0 "Calc Mnl")
  (setq mode (getkword "\nEnter Mode for array [<Calculate points/distance>/Manual]: "))
  (or mode (setq mode "Calc"))
  (initget 0 "X Y Grid")
  (setq typ (getkword "\nEnter the type of array [<X axis>/Y axis/Grid]: "))
  (or typ (setq typ "X"))
  (initget "Yes No")
  (setq hasangle (getkword "\nAre the objects on an inclined plane? [Yes/<No>]: "))
  (or hasangle (setq hasangle "No"))

          ;-----------------------------------------------;
          ;  Evaluate user input
          ;-----------------------------------------------;

          ; evaluate array mode and set values 
  (if (member typ '("X" "Grid"))
    (progn
      (initget 1)
      (setq qtx (getint "\nEnter Quantity on X axis: "))
      (setvar "osmode" 191)
      (initget 1)
      (setq l (getdist "\nDistance on X axis or click points: "))
      (if (= mode "Calc")
        (setq dist_X (/ l qtx))
      )
      (setvar "osmode" 0)
    )
  )
  (if (member typ '("Y" "Grid"))
    (progn
      (initget 1)
      (setq qty (getint "\nEnter Quantity on Y axis: "))
      (initget 1)
      (setvar "osmode" 191)
      (setq w (getdist "\nDistance on Y axis or click points: "))
      (if (= mode "Calc")
        (setq dist_Y (/ w qty))
      )
      (setvar "osmode" 0)
    )
  )
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.