Open a new instance of AutoCAD via Python - python

Does anyone have any experience using Python with AutoCAD? I'm just trying, as a test to see if I can open a new instance of AutoCAD via Python and though that PyAutocad worked well (feel free to offer other suggestions, if you have any).
Anyway based on the doc (https://pypi.python.org/pypi/pyautocad/#downloads) - it says these lines of code should do it, but nothing's happened as of yet.
from pyautocad import Autocad, APoint
acad = Autocad()
acad.prompt("Hello, Autocad from Python\n")
Just these lines of code should generate info on the commandline but instead results in about 50 lines worth of traceback (which I can post as well if anyone's interested) - any ideas?
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
acad.prompt("Hello, Autocad")
File "C:\Python27\lib\site-packages\pyautocad\api.py", line 153, in prompt
self.doc.Utility.Prompt(u"%s\n" % text)
File "C:\Python27\lib\site-packages\pyautocad\api.py", line 65, in doc
return self.app.ActiveDocument
File "C:\Python27\lib\site-packages\pyautocad\api.py", line 54, in app
self._app = comtypes.client.GetActiveObject('AutoCAD.Application')
File "C:\Python27\lib\site-packages\comtypes\client\__init__.py", line 180, in GetActiveObject
obj = comtypes.GetActiveObject(clsid, interface=interface)
File "C:\Python27\lib\site-packages\comtypes\__init__.py", line 1165, in GetActiveObject
oledll.oleaut32.GetActiveObject(byref(clsid), None, byref(p))
File "_ctypes/callproc.c", line 941, in GetResult
WindowsError: [Error -2147221021] Operation unavailable
After adding the VBA Module, the traceback now reads:
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
acad.prompt("Hello")
File "C:\Python27\lib\site-packages\pyautocad\api.py", line 153, in prompt
self.doc.Utility.Prompt(u"%s\n" % text)
File "C:\Python27\lib\site-packages\pyautocad\api.py", line 65, in doc
return self.app.ActiveDocument
File "C:\Python27\lib\site-packages\pyautocad\api.py", line 54, in app
self._app = comtypes.client.GetActiveObject('AutoCAD.Application')
File "C:\Python27\lib\site-packages\comtypes\client\__init__.py", line 183, in GetActiveObject
return _manage(obj, clsid, interface=interface)
File "C:\Python27\lib\site-packages\comtypes\client\__init__.py", line 188, in _manage
obj = GetBestInterface(obj)
File "C:\Python27\lib\site-packages\comtypes\client\__init__.py", line 112, in GetBestInterface
interface = getattr(mod, itf_name)
AttributeError: 'module' object has no attribute 'IAcadApplication'
Now after deleting comtypes' "gen" folder per #reclosedev, Autocad now opens but the string of text still will not appear on the command line - the traceback reads:
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
acad.prompt("Hello")
File "C:\Python27\lib\site-packages\pyautocad\api.py", line 153, in prompt
self.doc.Utility.Prompt(u"%s\n" % text)
File "C:\Python27\lib\site-packages\pyautocad\api.py", line 65, in doc
return self.app.ActiveDocument
File "C:\Python27\lib\site-packages\pyautocad\api.py", line 59, in app
self._app.Visible = self._visible
File "C:\Python27\lib\site-packages\comtypes\client\dynamic.py", line 116, in __setattr__
dispid = self._comobj.GetIDsOfNames(name)[0]
File "C:\Python27\lib\site-packages\comtypes\automation.py", line 643, in GetIDsOfNames
self.__com_GetIDsOfNames(riid_null, arr, len(names), lcid, ids)
COMError: (-2147418111, 'Call was rejected by callee.', (None, None, None, 0, None))

Have you considered IronPython?
http://through-the-interface.typepad.com/through_the_interface/2009/03/using-ironpython-with-autocad.html
Here's a quick example of how it could be leveraged:
import clr
import System
from System import Type, Activator
acApp = Activator.CreateInstance(Type.GetTypeFromProgID("AutoCAD.Application"))
acApp.Visible = 1
acApp.ActiveDocument.SendCommand("(Princ \"Hello World from Python!\")(Princ)\n")
Obviously the ProgId will correspond to whichever version of AutoCAD was opened last.

Related

When I need Initialize a new site in ERPNext

when I write the command line in terminal
bench new-site site1.local
This Error Will shown
> Traceback (most recent call last):
File "/home/ubuntu/.local/bin/bench", line 8, in <module>
sys.exit(cli())
File "/home/ubuntu/bench/bench/cli.py", line 71, in cli
cmd_from_sys = get_cmd_from_sysargv()
File "/home/ubuntu/bench/bench/utils/__init__.py", line 554, in get_cmd_from_sysargv
if sys_argv.index(arg) == 0 and arg in Bench(".").apps:
File "/home/ubuntu/bench/bench/bench.py", line 67, in __init__
self.apps = BenchApps(self)
File "/home/ubuntu/bench/bench/bench.py", line 174, in __init__
self.initialize_apps()
File "/home/ubuntu/bench/bench/bench.py", line 281, in initialize_apps
self.apps.remove("frappe")
ValueError: list.remove(x): x not in list
Note I use EC2 On AWS Ubunto server
Can Any One Help With This Problem Please.
I tried to install many lib but no help with this problem

MultiProcessing Python not working in windows

Traceback (most recent call last): File
"C:\Users\HP\Downloads\1\main.py", line 27, in <module>
p.start() File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\process.py",
line 121, in start
self._popen = self._Popen(self) File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\context.py",
line 224, in _Popen
return _default_context.get_context().Process._Popen(process_obj) File
"C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\context.py",
line 326, in _Popen
return Popen(process_obj) File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\popen_spawn_win32.py",
line 93, in _init_
reduction.dump(process_obj, to_child) File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\reduction.py",
line 60, in dump
ForkingPickler(file, protocol).dump(obj) pickle.PicklingError: Can't pickle <function <lambda> at 0x039E9538>: attribute lookup
<lambda> on __main_ failed
C:\Users\HP\Downloads\1>Traceback (most recent call last): File
"<string>", line 1, in <module> File
"C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\spawn.py",
line 102, in spawn_main
source_process = _winapi.OpenProcess( OSError: [WinError 87] The parameter is incorrect
""
Im trying to open multiple chromedrivers/files.py at the same time/instant.
But it shows the error.
The code is to get the sample from file.py and create multiple same files like file_1.py.....
Then run all the files that has created but is not working.
The Code is:
for proxy in file:
shutil.copyfile('/home/hp/Documents/pr/proxy_project/template.py', '/home/hp/Documents/pr/proxy_project/file_{}.py'.format(i))
file_name = "file_{}".format(i)
file_list.append(file_name)
with open('/home/hp/Documents/pr/proxy_project/file_{}.py'.format(i), 'r') as replicated_file:
data = replicated_file.readlines()
data_to_be_added = "proxy = '{}'\n".format(proxy.strip("\n"))
data[16] = data_to_be_added
with open('/home/hp/Documents/pr/proxy_project/file_{}.py'.format(i), 'w') as new_replicated_file:
new_replicated_file.writelines(data)
i += 1
for file_py in tuple(file_list):
print("File name = {}".format(file_py))
p = multiprocessing.Process(target=lambda: __import__(file_py))
p.start()
There is another file that will be replicated when this code runs
On Windows when using multiprocessing you have to protect your main code with if name==‘main’: - try reading the multiprocessing documentation. – barny Mar 23 at 19:46

pygame game crashes when I edit tilesheets in tiled

I am trying to make a game with pygame using tiled. When I edit tilesheets (to for example add collision) I get a bunch of error messages when I run my code.
When I use the same tilesheet without editing I get no errors and all the files work.
Traceback (most recent call last):
File "C:/Users/47988/PycharmProjects/Terrible/Main.py", line 94, in
<module>
g = Game()
File "C:/Users/47988/PycharmProjects/Terrible/Main.py", line 16, in init
self.load_data()
File "C:/Users/47988/PycharmProjects/Terrible/Main.py", line 22, in load_data
self.map = TiledMap(path.join(map_folder, "map6.tmx"))
File "C:\Users\47988\PycharmProjects\Terrible\Tilemap.py", line 21, in init
tm = pytmx.load_pygame(filename, pixelalpha=True)
File "C:\Users\47988\PycharmProjects\Terrible\venv\lib\site- packages\pytmx\util_pygame.py", line 141, in load_pygame
return pytmx.TiledMap(filename, *args, **kwargs)
File "C:\Users\47988\PycharmProjects\Terrible\venv\lib\site-packages\pytmx\pytmx.py", line 360, in init
self.parse_xml(ElementTree.parse(self.filename).getroot())
File "C:\Users\47988\PycharmProjects\Terrible\venv\lib\site- packages\pytmx\pytmx.py", line 400, in parse_xml
self.add_tileset(TiledTileset(self, subnode))
File "C:\Users\47988\PycharmProjects\Terrible\venv\lib\site-packages\pytmx\pytmx.py", line 845, in init
self.parse_xml(node)
File "C:\Users\47988\PycharmProjects\Terrible\venv\lib\site-packages\pytmx\pytmx.py", line 874, in parse_xml
raise Exception
Exception
Is there any way of solving this?

How to run playbook api in Ansible with vault

I have a playbook with vault, and I can run it through:
ansible-playbook info.yml --ask-vault-pass
Now, I want to run my playbook api in Ansible. The answer in How to run playbook api in Ansible v2 with vault said that we can set
loader = DataLoader()
loader.set_vault_password('mypass')
in 2.2.0.0 API, and I also set these in my api,but it doesn't work. The error message is as follows:
Traceback (most recent call last):
File "test2.py", line 63, in <module>
playbook.run()
File "/usr/lib/python2.7/site-packages/ansible/executor/playbook_executor.py", line 82, in run
pb = Playbook.load(playbook_path, variable_manager=self._variable_manager, loader=self._loader)
File "/usr/lib/python2.7/site-packages/ansible/playbook/__init__.py", line 54, in load
pb._load_playbook_data(file_name=file_name, variable_manager=variable_manager)
File "/usr/lib/python2.7/site-packages/ansible/playbook/__init__.py", line 78, in _load_playbook_data
ds = self._loader.load_from_file(os.path.basename(file_name))
File "/usr/lib/python2.7/site-packages/ansible/parsing/dataloader.py", line 130, in load_from_file
(b_file_data, show_content) = self._get_file_contents(file_name)
File "/usr/lib/python2.7/site-packages/ansible/parsing/dataloader.py", line 206, in _get_file_contents
data = self._vault.decrypt(data, filename=b_file_name)
File "/usr/lib/python2.7/site-packages/ansible/parsing/vault/__init__.py", line 561, in decrypt
plaintext, vault_id = self.decrypt_and_get_vault_id(vaulttext, filename=filename)
File "/usr/lib/python2.7/site-packages/ansible/parsing/vault/__init__.py", line 617, in decrypt_and_get_vault_id
_matches = match_secrets(self.secrets, vault_id_matchers)
File "/usr/lib/python2.7/site-packages/ansible/parsing/vault/__init__.py", line 456, in match_secrets
matches = [(vault_id, secret) for vault_id, secret in secrets if vault_id in target_vault_ids]
ValueError: need more than 1 value to unpack
Then I change the
loader.set_vault_password('mypass')
to
loader.set_vault_secrets([('default','mypass')])
But it doesn't work either. The error message is:
Traceback (most recent call last):
File "test2.py", line 63, in <module>
playbook.run()
File "/usr/lib/python2.7/site-packages/ansible/executor/playbook_executor.py", line 82, in
pb = Playbook.load(playbook_path, variable_manager=self._variable_manager, loader=self._l
File "/usr/lib/python2.7/site-packages/ansible/playbook/__init__.py", line 54, in load
pb._load_playbook_data(file_name=file_name, variable_manager=variable_manager)
File "/usr/lib/python2.7/site-packages/ansible/playbook/__init__.py", line 78, in _load_pla
ds = self._loader.load_from_file(os.path.basename(file_name))
File "/usr/lib/python2.7/site-packages/ansible/parsing/dataloader.py", line 130, in load_fr
(b_file_data, show_content) = self._get_file_contents(file_name)
File "/usr/lib/python2.7/site-packages/ansible/parsing/dataloader.py", line 206, in _get_fi
data = self._vault.decrypt(data, filename=b_file_name)
File "/usr/lib/python2.7/site-packages/ansible/parsing/vault/__init__.py", line 561, in dec
plaintext, vault_id = self.decrypt_and_get_vault_id(vaulttext, filename=filename)
File "/usr/lib/python2.7/site-packages/ansible/parsing/vault/__init__.py", line 638, in dec
b_plaintext = this_cipher.decrypt(b_vaulttext, vault_secret)
File "/usr/lib/python2.7/site-packages/ansible/parsing/vault/__init__.py", line 1344, in de
b_password = secret.bytes
AttributeError: 'str' object has no attribute 'bytes'
I try to change the line
b_password = secret.bytes
in file /usr/lib/python2.7/site-packages/ansible/parsing/vault/__init__.py to
b_password = secret
then the api works normally. But I think it's not a good way to basically solve my problem.
Use VaultSecret class instead of string password:
from ansible.module_utils._text import to_bytes
from ansible.parsing.vault import VaultSecret
loader.set_vault_secrets([('default',VaultSecret(_bytes=to_bytes('123456')))])

AttributeError when running rqt commands

I am trying to use rqt in ROS hydro but I see the following errors:
Traceback (most recent call last): File
"/opt/ros/hydro/bin/rqt_graph", line 8, in
sys.exit(main.main(sys.argv, standalone='rqt_graph.ros_graph.RosGraph')) File
"/opt/ros/hydro/lib/python2.7/dist-packages/rqt_gui/main.py", line 59,
in main
return super(Main, self).main(argv, standalone=standalone, plugin_argument_provider=plugin_argument_provider,
plugin_manager_settings_prefix=str(hash(os.environ['ROS_PACKAGE_PATH'])))
File "/opt/ros/hydro/lib/python2.7/dist-packages/qt_gui/main.py", line
336, in main
from python_qt_binding import QT_BINDING File "/opt/ros/hydro/lib/python2.7/dist-packages/python_qt_binding/init.py",
line 55, in
from .binding_helper import loadUi, QT_BINDING, QT_BINDING_MODULES, QT_BINDING_VERSION # #UnusedImport File
"/opt/ros/hydro/lib/python2.7/dist-packages/python_qt_binding/binding_helper.py",
line 265, in
getattr(sys, 'SELECT_QT_BINDING_ORDER', None), File "/opt/ros/hydro/lib/python2.7/dist-packages/python_qt_binding/binding_helper.py",
line 84, in _select_qt_binding
QT_BINDING_VERSION = binding_loader(required_modules, optional_modules) File
"/opt/ros/hydro/lib/python2.7/dist-packages/python_qt_binding/binding_helper.py",
line 139, in _load_pyqt
_named_import('PyQt4.%s' % module_name) File "/opt/ros/hydro/lib/python2.7/dist-packages/python_qt_binding/binding_helper.py",
line 106, in _named_import
module = builtins.import(name) AttributeError: 'module' object has no attribute 'import'

Categories

Resources