automatically creating defun

Discussion in 'AutoCAD' started by Marcel Janmaat, Dec 8, 2004.

  1. Is it posible to create a defun from the first item in this list?
    ("B_PRS" "Personen, auto's, fietsen, bomen e.d." "0.25" "0.0" "00" "132")

    I want to create (defun c:prs () (myfunction"B" "PRS"))

    Anybody?
     
    Marcel Janmaat, Dec 8, 2004
    #1
  2. Marcel Janmaat

    David Kozina Guest

    Marcel, you are almost *exactly* one year too late...

    See thread:
    "auto-defun a list of function names"
    started December 09, 2003
    <twighlight_zone_theme.wav>

    http://tinyurl.com/438ed

    hth,
    David Kozina
     
    David Kozina, Dec 8, 2004
    #2
  3. I noticed your post and since I wrote this the other day, I thought I
    would share it with you. It works very well. I would have had 20 to 30
    similar functions in my code without this method, tedious to work with.
    This way anything relating to buttons is in one place and easy to find
    and work on.

    Larry

    al:CreatePictureBoxDefuns
    Instead of creating many separate button/image_OnClick functions,
    this function creates and loads them all at once into memory.

    This simplifies and reduces the amount of code required. All
    operations related to button clicks are in one place. No need to
    create and modify many similar functions.

    This function handles groups of three buttons times seven
    instances.

    This function defines the name of a function, in this case
    'al:GenericBtnHandler' which will be called when the button is
    clicked. This function is passed an argument a list which
    identifies which button group was clicked and any other data
    required by the called function.

    This function first creates a list strings formatted so that if
    read is applied to the string, a valid expression is returned. A
    list of strings is used, because some parts of the result are
    static and can be expressed as a simple string. Other parts of
    the expression require evaluation of the FunctList using
    vl-prin1-to-string which will create the string with the
    necessary formatt.

    After the list of strings is created, it is reassembled into one
    string which when read creates the expression. The eval of the
    expression loads the function into memory.

    The name of the function is a string version of the name that
    ODCL assigns to the OnClick event.

    The number of ButtonGroups can vary, as can the number of
    functions created. The argument list can contain any number of
    expressions of any type. The first expression in the argument
    list must be some sort of identifier so that the correct button
    group can be indentified by a button handler.

    The defined button handler function is written and uses the
    argument list to to decide what button group was called and then
    from that calls various functions to do the work. The second part
    of the argument list is passed to the functions in the button
    handler functions as required.

    I used strings for the BtnGroup_Identifier because it is more
    meaningful when reading the code. Other types could be used such
    as an INT.

    The data part of the argument list is the name of the CmdGroup
    the button is associated with.

    (defun al:CreatePictureBoxDefuns (/ FunctList FList Count)
    ;; This function creates and loads the functions
    (setq FunctList (list
    (cons "c:fmMain_GBtCn1_OnClicked"
    '(list "ACTIVEASSIGNMENT" "CONSTRUCTION"))
    (cons "c:fmMain_GBtCd1_OnClicked"
    '(list "CONDITIONALASSIGNMENT" "CONSTRUCTION"))
    (cons "c:fmMain_GBtSelectLayerCn_OnClicked"
    '(list "SELECTLAYER" "CONSTRUCTION"))

    ...
    )
    )

    (foreach N FunctList
    (setq FList (list "(defun " (car N) " (/)"
    " (al:GenericBtnHandler " (vl-prin1-to-string (cdr n)) "))"
    )
    )
    (setq FList (strcat (nth 0 FList) (nth 1 FList) (nth 2 FList)
    (nth 3 FList) " " (nth 4 FList) (nth 5 FList)))
    (eval (read FList))
    ) ;end foreach
    ) ;end al:CreatePictureBoxDefuns 12/06/2004

    This function us just a traffic cop. It does not do anything
    except determine which button was pressed and then call a
    function which does the work. Note that the argument is passed
    from funct list in the above function to this function, and then
    finally to the working function.

    (defun al:GenericBtnHandler (DataList / CmdGroup BtnGroup)
    (setq CmdGroup (cadr DataList)
    BtnGroup (car DataList)
    )
    (cond
    ( (= BtnGroup "ACTIVEASSIGNMENT")
    (alUtility:ToggleActiveState CmdGroup)
    )
    ( (= BtnGroup "CONDITIONASSIGNMENT")
    ; (alUtility:ToggleActiveState CmdGroup)
    )
    ( (= BtnGroup "LAYERASSIGNMENT")
    ; (alUtility:ToggleActiveState CmdGroup)
    )
    ( (= BtnGroup "SELECTLAYER")
    (al:ShowSelectLayerFm CmdGroup AutoLayV2_fmMain)
    )
    ) ;end cond.
    ) ;end al:GenericBtnHandler 12/06/2004
     
    Larry Leuallen, Dec 10, 2004
    #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.