while function inside a command function (attributes)

Discussion in 'AutoCAD' started by Mark Dubbelaar, Aug 26, 2003.

  1. hi

    i was wondering if you can have a while function return a string value
    inside a command function... what i am trying to do is insert a viewport
    title block, which could have 1,2,3 or 4 lines (depending on what the user
    wants).... i am trying to use a while loop, to save on some duplication of
    code, but it doesn't seem to work (i think the following code will describe
    what i am trying to do)... the sTitleLines is a list of the title lines, the
    sTitleScale is a string, the dTitleInsertPoint is a 3d point....

    ;code
    (defun InsertTitle
    (sTitleLines sTitleScale dTitleInsertionPoint / iTitleCount)

    (setq iTitleCount 1)

    (if (not (tblsearch "layer" "D-BLCK"))
    (command "-layer" "m" "D-BLCK" "c" "white" "" "")
    (command "-layer" "s" "D-BLCK" "")
    ) ;if

    (command "-insert"
    (strcat "Common Blocks/ViewportTitle"
    (rtos (vl-list-length sTitleLines) 2 0)
    )
    dTitleInsertionPoint
    "1"
    "1"
    "0"
    (while (<= iTitleCount (vl-list-length sTitleLines))
    (nth iTitleCount sTitleLines))
    (setq iTitleCount (1+ iTitleCount))
    )
    sTitleScale
    ""
    )
    (princ)
    )

    any help would be great

    cheers

    mark
     
    Mark Dubbelaar, Aug 26, 2003
    #1
  2. Mark Dubbelaar

    Mike Weaver Guest

    Mark,
    Take a look at this:


    ;;;Start your command function
    (command "line" "0,0,0")
    ;;;Remember, back in acad, the command is still active
    ;;;so we can do whatever we want before we run this
    ;;;lisp command function again. In this case we'll
    ;;;start a repeat loop
    (repeat 3
    ;;within which we'll run the command function each
    ;;time through the loop, pausing for user input (a point)
    (command pause)
    )
    ;;;And then close the command function at the end.
    (command)


    Hope that helps,
    Mike Weaver
     
    Mike Weaver, Aug 26, 2003
    #2
  3. A (while) loop can't be used to feed values to AutoCAD,
    unless it contains a call to (command).

    It seems that your basic problem is how to feed a variable
    number of attribute values to AutoCAD, to satisfy the
    number of variable attribute prompts it will issue for
    a given block.

    Assuming you know that AutoCAD is going to prompt you for
    the number of attributes equal to the number of elements
    in your list, you can do that using the (apply) function:

    (apply 'command <input list>)

    Where <input list> is a list of values to be fed to the
    command line. In other words, these two are equivalent:

    (command "One" "two" "three")

    verses

    (setq alist '("One" "two" "three"))
    (apply 'command alist)

    So, this should do what you need:

    (defun InsertTitle
    (sTitleLines sTitleScale dTitleInsertionPoint / iTitleCount)

    (setq iTitleCount 1)

    (if (not (tblsearch "layer" "D-BLCK"))
    (command "-layer" "m" "D-BLCK" "c" "white" "" "")
    (command "-layer" "s" "D-BLCK" "")
    ) ;if

    (command "-insert"
    (strcat "Common Blocks/ViewportTitle"
    (rtos (vl-list-length sTitleLines) 2 0)
    )
    dTitleInsertionPoint
    "1"
    "1"
    "0"
    )

    (apply 'command (append sTitleLines (list sTitleScale)))

    (princ)
    )

    Side note: While (mapcar 'command <input list>) also works,
    (mapcar) is generally designed to process one or more lists
    and return a resulting list of processed elements. When you
    are simply calling the command function or any other function
    soley for its side-effects, you don't care about results, so
    (foreach) or (apply) would be used rather than (mapcar).
     
    Tony Tanzillo, Aug 26, 2003
    #3
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.