Author Topic: Passing arguments between functions  (Read 1835 times)

0 Members and 1 Guest are viewing this topic.

debgun

  • Guest
Passing arguments between functions
« on: February 17, 2010, 04:08:55 PM »
I'm trying to re-tag all the terminals on the attached drawing.  Unfortunately, I'm not to good with passing arguments between functions and would like someone's assistance.  By the way, some of the commands are specifically for ACAD Electrical.  Here is the 2 functions:
Code: [Select]
(defun ladderrung (endetail / x y yadder digits ladnum)
  (setvar "LUNITS" 2)
  (setvar "LUPREC" 0)
  (setq x (nth 1 endetail))
  (setq y (nth 2 endetail))

  ;determine where the entity is located on the x-axis
  (if (and
(>= x 1.4)
(<= x 6.6))
    (setq yadder 1)
  )
  (if (and
(>= x 8.4)
(<= x 13.6))
    (setq yadder 41)
  )
  ;Now we know weather the entity is on the right or left side of page
  ;Locate where entity is on the y-axis
  (if (> yadder 0)
    (progn
      (setq ladnum (- 9 y))
      (setq ladnum (/ ladnum 0.2))
      (setq ladnum (+ yadder ladnum))
      (setq ladnum (rtos ladnum)) ;convert real number to string
    )
  )
  ;format ladnum before adding to tagstrip
  (setq digits (strlen ladnum))
  (if (= digits 1)
    (setq endetail (strcat "0" ladnum))
    (setq endetail ladnum)
  )
  (princ)
 )
 
(defun Retag_terminal (/ ss sslen bk en termdigit sheetnum termtag)
  (setq ss (ssget "X" '((0 . "INSERT") (2 . "HT0002"))))
  (if (/= ss nil)
    (progn
      (setq sslen (sslength ss)) ;# of blocks inserted
      (setq bk 0) ;counter
      (while (< bk sslen) ;process each terminal
(setq en (ssname ss bk)
      endetail (assoc 10 (entget en))
      termdigit (ladderrung endetail)
      sheetnum (nth 13 (c:ace_GBL_wd_m 2))
      termtag (strcat sheetnum termdigit))
(c:wd_modattrval en bk termtag nil) ;modifies attribute
      ) ;close while
     ) ;close progn
    ) ;close if
  ) ;close function

I get getting an error within the Retag_terminal function on because "endetail" value returns to the previous value before calling ladderrung.
Code: [Select]
(setq termtag (strcat sheetnum termdigit))  I haven't figured out how to return back to Retag_terminal function after completing ladderrung.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Passing arguments between functions
« Reply #1 on: February 17, 2010, 04:58:29 PM »
Can't tell what is happening from these two subroutines but I would make the following changes to fit my style.
Code: [Select]
(defun ladderrung (endetail / x y yadder digits ladnum)
  (setvar "LUNITS" 2)
  (setvar "LUPREC" 0)
  (setq x (nth 1 endetail))
  (setq y (nth 2 endetail))
  ;;determine where the entity is located on the x-axis
  (cond
    ( (<= 1.4 x 6.6) (setq yadder 1))
    ( (<= 8.4 x 13.6) (setq yadder 41))
    (t (setq yadder 0)) ; incase nothing fits make it a number
  )
  ;;Now we know weather the entity is on the right or left side of page
  ;;Locate where entity is on the y-axis
  (if (> yadder 0)
    (setq ladnum (rtos (+ yadder (/ (- 9 y) 0.2)) 2 2)) ;convert real number to string
    (setq ladnum "0") ; make sure it is a string
  )

  (if (= (strlen ladnum) 1) ; format ladnum before adding to tagstrip
    (setq endetail (strcat "0" ladnum))
    (setq endetail ladnum)
  )
  (princ)
)

(defun Retag_terminal (/ ss sslen bk en termdigit sheetnum termtag)
  
  (if (setq ss (ssget "X" '((0 . "INSERT") (2 . "HT0002"))))
    (progn
      (setq sslen (sslength ss)) ;# of blocks inserted
      (setq bk -1) ;counter
      (while (< (setq bk (1+ bk)) sslen) ;process each terminal
(setq en (ssname ss bk)
     endetail (assoc 10 (entget en))
     termdigit (ladderrung endetail)
     sheetnum (nth 13 (c:ace_GBL_wd_m 2))
     termtag (strcat sheetnum termdigit)
)
(c:wd_modattrval en bk termtag nil) ;modifies attribute
      ) ;close while
    ) ;close progn
  ) ;close if
) ;close function
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.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Passing arguments between functions
« Reply #2 on: February 17, 2010, 05:07:00 PM »
Just looked at the DWG, still can't tell what is supposed to be happening.
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.

debgun

  • Guest
Re: Passing arguments between functions
« Reply #3 on: February 17, 2010, 05:34:54 PM »
I'm trying to select each terminal (block name is HT0002) and modify attribute ("Tagstrip") value.  This value is based on the location of the terminal.  For example, the first terminal in the upper left corner would be 101.

Here is the process:
starting with retag_terminal function - select all blocks named HT0002; foreach terminal change attribute (tagstrip) value, but first build terminal value; pull sheet number value (which is 1); figure out which line terminal falls on (calls ladderrung function); the returned value will be a 2-digit number i.e. 01; combine sheet number & line number; change attribute number.

I like your condensed version, but I can figure out how to pass the value back to the main function.  I tried assigning the line number back to endetail variable, but once the ladderrung function ends and passes back to the main endetail value changes back to this
Code: [Select]
(setq endetail (assoc 10 (entget en))) which is the terminal coordinates.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Passing arguments between functions
« Reply #4 on: February 17, 2010, 06:19:54 PM »
If you need to return more than one value use a list.
But this will do for a single value:
Code: [Select]
(defun ladderrung (endetail / x y yadder digits ladnum)
  (setvar "LUNITS" 2)
  (setvar "LUPREC" 0)
  (setq x (nth 1 endetail))
  (setq y (nth 2 endetail))

;determine where the entity is located on the x-axis
  (cond
    ( (<= 1.4 x 6.6) (setq yadder 1))
    ( (<= 8.4 x 13.6) (setq yadder 41))
    (t (setq yadder 0)) ; incase nothing fits make it a number
  )
;Now we know weather the entity is on the right or left side of page
;Locate where entity is on the y-axis
  (if (> yadder 0)
    (setq ladnum (rtos (+ yadder (/ (- 9 y) 0.2)) 2 2)) ;convert real number to string
    (setq ladnum "0") ; make sure it is a string
  )

  (if (= (strlen ladnum) 1) ; format ladnum before adding to tagstrip
    (strcat "0" ladnum)
    ladnum
  )
)
     
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.

debgun

  • Guest
Re: Passing arguments between functions
« Reply #5 on: February 18, 2010, 09:20:18 AM »
CAB,

Thank you!  That worked perfectly!

Debbie

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Passing arguments between functions
« Reply #6 on: February 18, 2010, 09:52:11 AM »
You're quite welcome. 8-)
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.