Saving a file with password (Lisp)

By Augusto Goncalves

First we need to create a AcadSecurityParams ActiveX object with some required properties, then call the SaveAs passing this parameter. The following code sample demonstrate it using AutoCAD default values.

(setq secparam 
(vla-getinterfaceobject (vlax-get-acad-object)
(strcat "autocad.securityparams.19")))

(vla-put-Action secparam 1) ;; ACADSECURITYPARAMS_ENCRYPT_DATA
(vla-put-Algorithm secparam 26625) ;; ACADSECURITYPARAMS_ALGID_RC4
(vla-put-KeyLength secparam 40) ;; default
(vla-put-password secparam "MyPass") ;; specify here…
(vla-put-ProviderName secparam 
"Microsoft Base Cryptographic Provider v1.0") ;; default
(vla-put-ProviderType secparam 1) ;; default

(setq activedoc (vla-get-ActiveDocument (vlax-get-Acad-Object)))
(setq fileName "c:\temp\passwordFile.dwg")
(setq version 60) ;; 2013 DWG
(vlax-invoke-method activedoc ‘SaveAs fileName version secparam)


Comments

6 responses to “Saving a file with password (Lisp)”

  1. I used a similar approach saving a file with password.
    But opening it using:
    (vla-open activedoc fileName :vlax-false “MyPass”)
    returns an error:
    ; error: Automation Error. Error Decrypting Data
    Any idea?
    Thanks in advance

  2. Hi Leo,
    I tried with the code below and worked fine. Are you sure the password is correct?
    (setq filename “c:/temp/password.dwg”)
    (setq password “MyPass”)
    (setq docs (vla-get-documents (vlax-get-acad-object)))
    (vla-open docs filename :vlax-false password)

  3. (defun c:lock ()
    (setq secparam
    (vla-getinterfaceobject (vlax-get-acad-object)
    (strcat “autocad.securityparams.19”)))
    (vla-put-Action secparam 1)
    (vla-put-Algorithm secparam 26625)
    (vla-put-KeyLength secparam 40)
    (vla-put-password secparam “MyPass”)
    (vla-put-ProviderName secparam
    “Microsoft Base Cryptographic Provider v1.0”)
    (vla-put-ProviderType secparam 1
    (setq activedoc (vla-get-ActiveDocument (vlax-get-Acad-Object)))
    (setq fileName “c:\temp\passwordFile.dwg”)
    (setq version 60
    (vlax-invoke-method activedoc ‘SaveAs fileName version secparam)
    (alert “You successfully locked this drawing.”)
    (princ)
    )
    (defun c:un_lock ()
    (setq filename “c:/temp/passwordFile.dwg”)
    (setq password “MyPass”)
    (setq docs (vla-get-documents (vlax-get-acad-object)))
    (vla-open docs filename :vlax-false password)
    (princ)
    )
    (princ)
    Sir,
    This is what I have so far.
    I changed “c:/temp/password.dwg” to “c:/temp/passwordFile.dwg”
    lock – should lock the drawing (OK)
    un_lock – should open the drawing without password popup (Not OK, still popup the password, if cancel then error)
    I also tried different values of “dwgcheck” variable but still not OK
    Thank you very much for the time sir.

  4. Got it to work only for insert.
    (vla-InsertBlock obj pnt filName 1 1 1 0 “MyPass”)
    Thank you sir,

  5. it doesn’t work.
    ;errors: Automation Error. Invalid argument
    so i replace the first line as below to work with another version.
    (setq secparam (vla-getinterfaceobject (vlax-get-acad-object) (strcat
    “autocad.securityparams.” (rtos (fix (atof (getvar “ACADVER”))) 2 0))))
    then got error
    ; error: Automation Error. Error saving the document
    and i tried to change version to 61, it saves as DXF without error.
    Please help.
    thanks.

  6. Dear Nath,
    In AutoCAD 2016 and up, this feature is not supported.

Leave a Reply to Augusto GoncalvesCancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading