Postcommand doesn't read AutoLisp expression [Python 2.7 & AutoCAD 2016 Windows]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I'm trying to run a simple AutoLISP file from a python script, But there are 3 issues facing me.
My purpose is to obtain a test case, that demonstrates if this approach can be implemented or not.
[1] The Code:-
1- The Python Script:
#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.
#Note that "\\" is transformed automatically to "\", & in order to comply with
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)
#doc.PostCommand("_qsave") #an error occurs here print "AutoLISP Expression is sent" #don't believe what this line tells you. print "########" #6- Save & 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()
2- The AutoLisp script:
;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)
[2]The Issues:-
1- ((The main issue)): the AutoLISP expression isn't sent/posted to the drawing.
I did check this by leaving AutoCAD actively running, running the python script and looking for what happen in the command line & the Model Space.
2- "Save" & "Close" methods of the "document" cause errors and don't work.
3- Two invocations of "postcommand" creates an error.
________________________________________
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.
P.S: The real challenge is to NOT use AutoCAD script files (of extension .scr), AutoCAD must not be -by necessary- opened by the user.