<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Postcommand doesn't read AutoLisp expression [Python 2.7 &amp;amp; AutoCAD 2016 in Visual LISP, AutoLISP and General Customization Forum</title>
    <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/postcommand-doesn-t-read-autolisp-expression-python-2-7-amp/m-p/8243349#M101874</link>
    <description>&lt;P&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/6332124"&gt;@serag.hassouna&lt;/a&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for the update&lt;/P&gt;&lt;P&gt;Really appriciate this&amp;nbsp;&lt;/P&gt;&lt;P&gt;If possible keep me informed&amp;nbsp;&lt;BR /&gt;Maybe i can be of any help in a later stadium&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 04 Sep 2018 11:12:48 GMT</pubDate>
    <dc:creator>DGRL</dc:creator>
    <dc:date>2018-09-04T11:12:48Z</dc:date>
    <item>
      <title>Postcommand doesn't read AutoLisp expression [Python 2.7 &amp; AutoCAD 2016 Windows]</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/postcommand-doesn-t-read-autolisp-expression-python-2-7-amp/m-p/8159022#M101868</link>
      <description>&lt;P&gt;I'm trying to run a simple AutoLISP file from a python script, But there are 3 issues facing me.&lt;BR /&gt;My purpose is to obtain a test case, that demonstrates if this approach can be implemented or not.&lt;BR /&gt;[1] The Code:-&lt;/P&gt;&lt;P&gt;1- The Python Script:&lt;/P&gt;&lt;PRE&gt;#import needed modules
import os
import comtypes.client
from comtypes import COMError
from comtypes.client import CreateObject, GetActiveObject

def main():
    #1- Get the AutoCAD instance
    try:
        acad = GetActiveObject("AutoCAD.Application.20")
        print "AutoCAD is Active"
        print "########"
    except(OSError, COMError): #If AutoCAD isn't running, run it
        acad = CreateObject("AutoCAD.Application.20",dynamic=True)
        print "AutoCAD is successfuly Opened"
        print "########"

    #2- Get the paths to the lisp file and the dwg file
    directory_name = "E:\\Dir1\\Dir2" #replace it with a real path, use "\\" as directory delimiters.&lt;BR /&gt;    #Note that "\\" is transformed automatically to "\", &amp;amp; in order to comply with&lt;BR /&gt;    the AutoLISP "load" function, every "\" must be transformed again to "/".

    temp=""
    for char in directory_name:
        if char == "\\":
            temp += "/"
        else:
            temp += char
    directory_name = temp
    filename = directory_name + "/TestDWG.dwg"
    lispfile = directory_name + "/linedraw.lsp"

    #3- Open the drawing file
    print "Opening Drawing File ..."
    doc = acad.Documents.Open(filename)
    print "Drawing is successsfuly Opened"
    print "########"

    #4- Construct the AutoLISP expression that loads AutoLISP files
    command_str = '(load ' + '"' + lispfile + '")'
    #command_str = '_line 0,0 5,3 ' #an ordinary command that worked as expected

    #5-Execute the AutoLISP expression
    print "Sending AutoLISP Expression ..."
    print "Expression: " + command_str
    doc.PostCommand(command_str)&lt;BR /&gt;    #doc.PostCommand("_qsave") #an error occurs here
    print "AutoLISP Expression is sent" #don't believe what this line tells you.
    print "########"

    #6- Save &amp;amp; Close the drawing file and AutoCAD application
    #doc.Save() #doesn't work!
    #doc.Close() #doesn't work!
    acad.Quit()

    print "Process Finished"
    print "__________"

if __name__ == '__main__':
    main()&lt;/PRE&gt;&lt;P&gt;2- The AutoLisp script:&lt;/P&gt;&lt;PRE&gt;;Just a simple script to draw a line from (0,0) to (5,3)
(defun linedraw ()
  (command "._line" '(0 0) '(5 3) "")
  (command "_qsave") ;added in response to the failure of "doc.Save()" in the python script
  );end defun

(linedraw)&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;[2]The Issues:-&lt;/P&gt;&lt;P&gt;1- ((The main issue)): the AutoLISP expression isn't sent/posted to the drawing.&lt;BR /&gt;I did check this by leaving AutoCAD actively running, running the python script and looking for what happen in the command line &amp;amp; the Model Space.&lt;/P&gt;&lt;P&gt;2- "Save" &amp;amp; "Close" methods of the "document" cause errors and don't work.&lt;/P&gt;&lt;P&gt;3- Two invocations of "postcommand" creates an error.&lt;BR /&gt;&lt;BR /&gt;________________________________________&lt;BR /&gt;&lt;BR /&gt;I really appreciate any help, and keep notified that if this "test case" succeed we will have another way of running AutoLISP routines from outside without the need to open AutoCAD itself.&lt;BR /&gt;P.S: The real challenge is to NOT use AutoCAD script files (of extension .scr), AutoCAD must not be -by necessary-&amp;nbsp; opened by the user.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jul 2018 22:29:42 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/postcommand-doesn-t-read-autolisp-expression-python-2-7-amp/m-p/8159022#M101868</guid>
      <dc:creator>serag.hassouna</dc:creator>
      <dc:date>2018-07-26T22:29:42Z</dc:date>
    </item>
    <item>
      <title>Re: Postcommand doesn't read AutoLisp expression [Python 2.7 &amp; AutoCAD 2016</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/postcommand-doesn-t-read-autolisp-expression-python-2-7-amp/m-p/8231951#M101869</link>
      <description>&lt;P&gt;hI&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/6332124"&gt;@serag.hassouna&lt;/a&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Nice test case your opening&lt;/P&gt;&lt;P&gt;Do know that not every command invoked using LISP can be executed when ACAD IS NOT RUNNING&lt;/P&gt;&lt;P&gt;So you are very limited in posibilties&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Aug 2018 09:03:33 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/postcommand-doesn-t-read-autolisp-expression-python-2-7-amp/m-p/8231951#M101869</guid>
      <dc:creator>DGRL</dc:creator>
      <dc:date>2018-08-29T09:03:33Z</dc:date>
    </item>
    <item>
      <title>Re: Postcommand doesn't read AutoLisp expression [Python 2.7 &amp; AutoCAD 2016</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/postcommand-doesn-t-read-autolisp-expression-python-2-7-amp/m-p/8239172#M101870</link>
      <description>&lt;P&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/3499869"&gt;@DGRL&lt;/a&gt;&lt;BR /&gt;Well, your response has inspired me in a magical way.&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;&lt;EM&gt;GetActiveObject&lt;/EM&gt;&amp;nbsp; (which fetches the running ACAD) was throwing an error, its description and how it was solved with me are&amp;nbsp;&lt;A href="https://stackoverflow.com/questions/52111215/get-the-running-autocad-application-using-comtypes-in-python-2-7" target="_blank"&gt;here&lt;/A&gt;.&lt;/LI&gt;&lt;LI&gt;Slightly changed the code to be&lt;BR /&gt;&lt;PRE&gt;#Import needed modules&lt;BR /&gt;import os&lt;BR /&gt;import comtypes.client&lt;BR /&gt;from comtypes import COMError&lt;BR /&gt;from comtypes.client import CreateObject, GetModule, GetActiveObject&lt;BR /&gt;&lt;BR /&gt;#Uncomment it if you need to load these type libraries.&lt;BR /&gt;'''&lt;BR /&gt;#Load all needed type libraries&lt;BR /&gt;GetModule("C:/Windows/System32/stdole2.tlb")&lt;BR /&gt;import comtypes.gen.stdole as ole&lt;BR /&gt;print "stdole2 successfully loaded"&lt;BR /&gt;GetModule("C:/Program Files/Common Files/Autodesk Shared/acax20enu.tlb")&lt;BR /&gt;import comtypes.gen._4E3F492A_FB57_4439_9BF0_1567ED84A3A9_0_1_0 as acax&lt;BR /&gt;print "acax20enu successfully loaded"&lt;BR /&gt;GetModule("C:/Program Files/Common Files/Autodesk Shared/AcSmComponents20.tlb")&lt;BR /&gt;import comtypes.gen._ED125AFF_6294_4BE4_81E2_B98DCBBA214E_0_1_0 as AcSm&lt;BR /&gt;print "AcSmComponents20 successfully loaded"&lt;BR /&gt;'''&lt;BR /&gt;&lt;BR /&gt;def main():&lt;BR /&gt; #1- Get the AutoCAD instance&lt;BR /&gt; try:&lt;BR /&gt; acad = GetActiveObject("AutoCAD.Application.20")&lt;BR /&gt; print "AutoCAD is Active"&lt;BR /&gt; print "########"&lt;BR /&gt; except(OSError, COMError): #If AutoCAD isn't running, run it&lt;BR /&gt; acad = CreateObject("AutoCAD.Application.20",dynamic=True)&lt;BR /&gt; print "AutoCAD is successfuly Opened"&lt;BR /&gt; print "########"&lt;BR /&gt;&lt;BR /&gt; #2- Get the paths to the lisp file and the dwg file&lt;BR /&gt; directory_name = "E:\\Dir1\\Dir2" #replace it with a real path, use "\\" as directory delimiters.&lt;BR /&gt; #Note that "\\" is transformed automatically to "\", &amp;amp; in order to comply with&lt;BR /&gt; the AutoLISP "load" function, every "\" must be transformed again to "/". &lt;BR /&gt;&lt;BR /&gt; temp=""&lt;BR /&gt; for char in directory_name:&lt;BR /&gt; if char == "\\":&lt;BR /&gt; temp += "/"&lt;BR /&gt; else:&lt;BR /&gt; temp += char&lt;BR /&gt; directory_name = temp&lt;BR /&gt; filename = directory_name + "/TestDWG.dwg"&lt;BR /&gt; lispfile = directory_name + "/linedraw.lsp"&lt;BR /&gt;&lt;BR /&gt; #3- Open the drawing file&lt;BR /&gt; print "Opening Drawing File ..."&lt;BR /&gt; doc = acad.Documents.Open(filename)&lt;BR /&gt; print "Drawing is successsfuly Opened"&lt;BR /&gt; print "########"&lt;BR /&gt;&lt;BR /&gt; #4- Construct the AutoLISP expression that loads AutoLISP files&lt;BR /&gt; command_str = '(load ' + '"' + lispfile + '")' + " "&lt;BR /&gt;&lt;BR /&gt; #5-Execute the AutoLISP expression&lt;BR /&gt; print "Sending AutoLISP Expression ..."&lt;BR /&gt; print "Expression: " + command_str&lt;BR /&gt; doc.SendCommand("(setq *LOAD_SECURITY_STATE* (getvar 'SECURELOAD)) ")&lt;BR /&gt; doc.SendCommand("(setvar \"SECURELOAD\" 0) ")&lt;BR /&gt; doc.SendCommand(command_str)&lt;BR /&gt; doc.SendCommand("(setvar \"SECURELOAD\" *LOAD_SECURITY_STATE*) ")&lt;BR /&gt; print "AutoLISP Expression is successfuly sent"&lt;BR /&gt; print "########"&lt;BR /&gt;&lt;BR /&gt; #6- Close the drawing file and AutoCAD application&lt;BR /&gt; doc.Close()&lt;BR /&gt; acad.Quit()&lt;BR /&gt;&lt;BR /&gt; print "Process Finished"&lt;BR /&gt; print "__________"&lt;BR /&gt;&lt;BR /&gt;if __name__ == '__main__':&lt;BR /&gt; main()&lt;/PRE&gt;&amp;nbsp;&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;And voila, it works fine.&lt;BR /&gt;___________________&lt;BR /&gt;The differences here are:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Using&amp;nbsp;&lt;EM&gt;SendCommand&lt;/EM&gt; instead of&amp;nbsp;&lt;EM&gt;PostCommand&lt;/EM&gt;.&lt;/LI&gt;&lt;LI&gt;To successfully invoke an AutoLISP expressions it must followed an empty space, which is equivalent to hitting ENTER on the keyboard.&lt;/LI&gt;&lt;LI&gt;Before using the&amp;nbsp;&lt;STRONG&gt;&lt;EM&gt;load&lt;/EM&gt;&lt;/STRONG&gt; routine, the&amp;nbsp;&lt;EM&gt;&lt;STRONG&gt;SECURELOAD&lt;/STRONG&gt;&lt;/EM&gt;&amp;nbsp;system variable must be set to 0, this is to prevent the security message, which I guess is the main reason for making the program previously crash with&amp;nbsp;&lt;EM&gt;SendCommand&lt;/EM&gt;.&lt;/LI&gt;&lt;/OL&gt;</description>
      <pubDate>Fri, 31 Aug 2018 23:36:25 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/postcommand-doesn-t-read-autolisp-expression-python-2-7-amp/m-p/8239172#M101870</guid>
      <dc:creator>serag.hassouna</dc:creator>
      <dc:date>2018-08-31T23:36:25Z</dc:date>
    </item>
    <item>
      <title>Re: Postcommand doesn't read AutoLisp expression [Python 2.7 &amp; AutoCAD 2016</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/postcommand-doesn-t-read-autolisp-expression-python-2-7-amp/m-p/8240696#M101871</link>
      <description>&lt;P&gt;Same code but with the right indentations.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;#Import needed modules
import os
import comtypes.client
from comtypes import COMError
from comtypes.client import CreateObject, GetModule, GetActiveObject

#Uncomment it if you need to load these type libraries.
'''
#Load all needed type libraries
GetModule("C:/Windows/System32/stdole2.tlb")
import comtypes.gen.stdole as ole
print "stdole2 successfully loaded"
GetModule("C:/Program Files/Common Files/Autodesk Shared/acax20enu.tlb")
import comtypes.gen._4E3F492A_FB57_4439_9BF0_1567ED84A3A9_0_1_0 as acax
print "acax20enu successfully loaded"
GetModule("C:/Program Files/Common Files/Autodesk Shared/AcSmComponents20.tlb")
import comtypes.gen._ED125AFF_6294_4BE4_81E2_B98DCBBA214E_0_1_0 as AcSm
print "AcSmComponents20 successfully loaded"
'''

def main():
&amp;nbsp; &amp;nbsp;&amp;nbsp;#1- Get the AutoCAD instance
        try:
            acad = GetActiveObject("AutoCAD.Application.20")
            print "AutoCAD is Active"
            print "########"
        except(OSError, COMError): #If AutoCAD isn't running, run it
            acad = CreateObject("AutoCAD.Application.20",dynamic=True)
            print "AutoCAD is successfuly Opened"
            print "########"

    #2- Get the paths to the lisp file and the dwg file
    directory_name = "E:\\Dir1\\Dir2" #replace it with a real path, use "\\" as directory delimiters.
    '''&lt;BR /&gt;    Note that "\\" is transformed automatically to "\", &amp;amp; in order to comply with
    the AutoLISP "load" function, every "\" must be transformed again to "/".&lt;BR /&gt;    '''

    temp=""
    for char in directory_name:
        if char == "\\":
            temp += "/"
        else:
            temp += char
    directory_name = temp
    filename = directory_name + "/TestDWG.dwg"
    lispfile = directory_name + "/linedraw.lsp"

    #3- Open the drawing file
    print "Opening Drawing File ..."
    doc = acad.Documents.Open(filename)
    print "Drawing is successsfuly Opened"
    print "########"

    #4- Construct the AutoLISP expression that loads AutoLISP files
    command_str = '(load ' + '"' + lispfile + '")' + " "

    #5-Execute the AutoLISP expression
    print "Sending AutoLISP Expression ..."
    print "Expression: " + command_str
    doc.SendCommand("(setq *LOAD_SECURITY_STATE* (getvar 'SECURELOAD)) ")
    doc.SendCommand("(setvar \"SECURELOAD\" 0) ")
    doc.SendCommand(command_str)
    doc.SendCommand("(setvar \"SECURELOAD\" *LOAD_SECURITY_STATE*) ")
    print "AutoLISP Expression is successfuly sent"
    print "########"

    #6- Close the drawing file and AutoCAD application
    doc.Close()
    acad.Quit()

    print "Process Finished"
    print "__________"

if __name__ == '__main__':
    main()&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 02 Sep 2018 22:35:07 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/postcommand-doesn-t-read-autolisp-expression-python-2-7-amp/m-p/8240696#M101871</guid>
      <dc:creator>serag.hassouna</dc:creator>
      <dc:date>2018-09-02T22:35:07Z</dc:date>
    </item>
    <item>
      <title>Re: Postcommand doesn't read AutoLisp expression [Python 2.7 &amp; AutoCAD 2016</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/postcommand-doesn-t-read-autolisp-expression-python-2-7-amp/m-p/8241172#M101872</link>
      <description>&lt;P&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/6332124"&gt;@serag.hassouna&lt;/a&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Nice work&amp;nbsp;&lt;BR /&gt;Is it working now as it should?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Sep 2018 07:46:45 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/postcommand-doesn-t-read-autolisp-expression-python-2-7-amp/m-p/8241172#M101872</guid>
      <dc:creator>DGRL</dc:creator>
      <dc:date>2018-09-03T07:46:45Z</dc:date>
    </item>
    <item>
      <title>Re: Postcommand doesn't read AutoLisp expression [Python 2.7 &amp; AutoCAD 2016</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/postcommand-doesn-t-read-autolisp-expression-python-2-7-amp/m-p/8242476#M101873</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/3499869"&gt;@DGRL&lt;/a&gt;&lt;BR /&gt;Yes, it works fine whether AutoCAD is opened or not .. the code will deal with the running AutoCAD application, and if AutoCAD is not opened this python code will open it, load the lisp file to the drawing,then close the drawing itself.&lt;BR /&gt;This is really useful if you don't want to lean on the traditional solution of "&lt;EM&gt;script creation&lt;/EM&gt;" &amp;amp; if you want to deal with AutoCAD externally.&lt;BR /&gt;&lt;EM&gt;Script Creation&lt;/EM&gt; is described in more detail in this&amp;nbsp;&lt;A href="https://stackoverflow.com/a/48798152/8070933" target="_blank"&gt;answer&lt;/A&gt; of Lee Mac in stackoverflow.&lt;BR /&gt;In fact, this test code is another answer to the same problem, &amp;amp; I think it's more &lt;STRONG&gt;compact&lt;/STRONG&gt; if the equivalently-created script has more lines than the python code itself, in other words, with large number of drawings.&lt;BR /&gt;______________&lt;BR /&gt;P.S. I removed the line&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;(command "_qsave")&lt;/PRE&gt;&lt;P&gt;from the lisp file, and added the line&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;    doc.Save()&lt;/PRE&gt;&lt;P&gt;just before&lt;/P&gt;&lt;PRE&gt;    doc.Close()&lt;/PRE&gt;&lt;P&gt;into the python code, and it is working as it should.&lt;/P&gt;</description>
      <pubDate>Mon, 03 Sep 2018 22:45:31 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/postcommand-doesn-t-read-autolisp-expression-python-2-7-amp/m-p/8242476#M101873</guid>
      <dc:creator>serag.hassouna</dc:creator>
      <dc:date>2018-09-03T22:45:31Z</dc:date>
    </item>
    <item>
      <title>Re: Postcommand doesn't read AutoLisp expression [Python 2.7 &amp; AutoCAD 2016</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/postcommand-doesn-t-read-autolisp-expression-python-2-7-amp/m-p/8243349#M101874</link>
      <description>&lt;P&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/6332124"&gt;@serag.hassouna&lt;/a&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for the update&lt;/P&gt;&lt;P&gt;Really appriciate this&amp;nbsp;&lt;/P&gt;&lt;P&gt;If possible keep me informed&amp;nbsp;&lt;BR /&gt;Maybe i can be of any help in a later stadium&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Sep 2018 11:12:48 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/postcommand-doesn-t-read-autolisp-expression-python-2-7-amp/m-p/8243349#M101874</guid>
      <dc:creator>DGRL</dc:creator>
      <dc:date>2018-09-04T11:12:48Z</dc:date>
    </item>
  </channel>
</rss>

