Search code examples
lispadd-incadautolisp

Lisp Addin Call .dcl File


I'm trying to both learn and implement some lisp code for a cad addin, but I'm running into a calling issue. I'm currently trying to add an error handling for when the user activates this addin, but the file itself is either not located in the correct directory or the file is not saved to a location. I came across a method that will allow me to ask the user a question with a message box, but the lisp file isn't pulling the dcl file. How should this error be resolved?

Scope: The big picture is for the user to either click a button on the ribbon or by typing a command, and all of the files in the same folder as the open, active drawing will receive a revision update.

Application: DraftSight is where the lisp file is being added.

Original Tutorial: Here's the link to the original tutorial where I found the code to build the message box.

Error Message from DraftSight

Error Message

Code I'm building:

;; Global Constants
(defconstant msgboxPath "C:\\Users\\GarrettB\\Documents\\Visual Studio Code\\DraftSight LISP")

;; 
(defun C:ProjectRev()
    
    ;; Pulls the directory from the active file
    (setq dirPath (vl-catch-all-apply getvar 'dwgprefix))
    
    ;; Checks for an error
    (if not (vl-catch-all-error-p dirPath) 
    (progn ; No error - Ask user if this is the right path
        (setq UserRespond (lspYesNoCancel "Is this the correct path?" dirfile "" "PROJECT REVISION"))
        (princ (type UserRespond))
        (princ UserRespond)
        
        ;; if yes then continue
        ;; else ask for correct directory (function call)
        ;; (setq dirPath (browseForFolder "Select the project folder: " 1 "d:\\"))

    );progn
    (progn ; Error - File is not saved to a directory
        ;; ask for correct directory (function call)
        ;; (setq dirPath (browseForFolder "Select the project folder: " 1 "d:\\"))
    );progn
    );if
    
    ;; WORK IN PROGRESS
    ;; Gather drawings into a list
    ;; Copy drawings and place into "Past revisions" folder
    ;; Add revision to drawings
    ;; Modify drawing's names
    ;; Save, close, and end
)

;; Source: https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/lisp-on-all-dwg-directory/td-p/6214507
;; Browses the current directory for .dwg files
(defun browseForFolder (title options rootFolder / sh folder folderobject result)
    (vl-load-com)
    (setq sh (vla-getInterfaceObject (vlax-get-acad-object) "Shell.Application")) ; sets the Shell Terminal variable
    
    ;; Obtaining starting location
    (setq folder (vlax-invoke-method sh 'BrowseForFolder
        (vla-get-hwnd (vlax-get-acad-object)) title options rootFolder)) ; User sets the folder path and file name as an object?
    (vlax-release-object sh) ;-------------------------------------------; Releases the shell application object
    (setq sh nil) ;------------------------------------------------------; Sets the sh variable to nothing

    ; If folder is not nil
    (if folder
        (progn
            
            ;; Conversion
            (setq folderobject (vlax-get-property folder 'Self)) ; Sets the folder path as an object
            (setq result (vlax-get-property FolderObject 'Path)) ; Sets the folder path as a string
            
            ;; Release and nullify
            (vlax-release-object folder) ;-----; Releases the folder path and file name as an object?
            (vlax-release-object FolderObject) ; Releases the folder path as a string
            (setq folder nil) ;----------------; Sets the folder variable to nothing
            (setq FolderObject nil) ;----------; Sets the FolderObject variable to nothing
            
            ;; Returning variable
            result
        ); progn
    ); if
 ); defun browserForFolder


;; Source: https://www.afralisp.net/dialog-control-language/tutorials/the-autolisp-message-box.php
;; Lisp code from tutorial
(defun lspYesNoCancel (message1 message2 message3 main)

    ;; Creating dialoge box
    (setq dcl_id (load_dialog strcat(msgboxPath "\\" "msgbox.dcl")))

    ;; Error prevention
    (if (not (new_dialog "lspYesNoCancel" dcl_id)) (exit))

    ;; Dialoge Message
    (set_tile "message1" message1)
    (set_tile "message2" message2)
    (set_tile "message3" message3)
    (set_tile "main" main)

    ;; Command Buttons
    (action_tile "no" "(done_dialog) (setq result \"F\")")
    (action_tile "yes" "(done_dialog) (setq result T)")
    (action_tile "cancel" "(done_dialog) (setq result nil)")
    
    ;; Interaction
    (start_dialog) ; Show dialog box
    (unload_dialog dcl_id) ; Close dialoge box
    (princ)
)

Here's the msgbox.dcl file

// Source: https://www.afralisp.net/dialog-control-language/tutorials/the-autolisp-message-box.php
////////////////////////////////////////////////

lspOkCancel : dialog {
    key = "main";
    : column {
        : text {key = "message1";}
        : text {key = "message2";}
        : text {key = "message3";}
    }
    : row {
        : spacer {width = 1;}
        : button {
            label = "OK";
            key = "accept";
            width = 12;
            fixed_width = true;
            mnemonic = "O";
            is_default = true;
        }
        : button {
            label = "Cancel";
            key = "cancel";
            width = 12;
            fixed_width = true;
            mnemonic = "C";
            is_cancel = true;
        }
        : spacer { width = 1;}
    }
}
 
////////////////////////////////////////////////
 
lspYesNo : dialog {
    key = "main";
    : column {
        : text {key = "message1";}
        : text {key = "message2";}
        : text {key = "message3";}
    }
    : row {
        : spacer {width = 1;}
        : button {
            label = "Yes";
            key = "yes";
            width = 12;
            fixed_width = true;
            mnemonic = "Y";
            is_default = true;
        }
        : button {
            label = "No";
            key = "no";
            width = 12;
            fixed_width = true;
            mnemonic = "N";
            is_cancel = true;
        }
        : spacer { width = 1;}
    }
}
 
////////////////////////////////////////////
 
lspOkOnly : dialog {
    key = "main";
    : column {
        : text {key = "message1";}
        : text {key = "message2";}
        : text {key = "message3";}
    }
    : row {
        : spacer { width = 1; }
        : button {
            label = "OK";
            key = "accept";
            width = 12;
            fixed_width = true;
            mnemonic = "O";
            is_default = true;
            alignment = centered;
        }
        : spacer { width = 1;}
    }
}
 
////////////////////////////////////////////////
 
lspYesNoCancel : dialog {
 
    key = "main";
 
    : column {
        : text {Key = "message1";}
        : text {key = "message2";}
        : text {key = "message3";}
    }
    : row {
        : spacer {width = 1;}
        : button {
            label = "Yes";
            key = "yes";
            width = 12;
            fixed_width = true;
            mnemonic = "Y";
            is_default = true;
        }
        : button {
            label = "No";
            key = "no";
            width = 12;
            fixed_width = true;
            mnemonic = "N";
        }
        : button {
            label = "Cancel";
            key = "cancel";
            width = 12;
            fixed_width = true;
            mnemonic = "C";
            is_cancel = true;
        }
        : spacer {width = 1;}
    }
}
 
////////////////////////////////////////////
 
lspRentryCancel : dialog {
    key = "main";
    : column {
        : text {key = "message1";}
        : text {key = "message2";}
        : text {key = "message3";}
    }
    : row {
        : spacer { width = 1; }
        : button {
            label = "Rentry";
            key = "rentry";
            width = 12;
            fixed_width = true;
            mnemonic = "R";
            is_default = true;
        }
        : button {
            label = "Cancel";
            key = "Cancel";
            width = 12;
            fixed_width = true;
            mnemonic = "C";
            is_cancel = true;
        }
        : spacer {width = 1;}
    }
}
 
////////////////////////////////////////////

Solution

  • I think the problem is in line:

    (setq dcl_id (load_dialog strcat(msgboxPath "\\" "msgbox.dcl")))
    

    Where should be rather:

    (setq dcl_id (load_dialog ( strcat msgboxPath "\\" "msgbox.dcl")))