Load AutoCad DWG files in Python - python

There a requirement to rendering AutoCad DWG file in Python.
My first Question is,
1:- Is is possible to render/load dwg file in python and view the file/image in python environment?
2:- How can I do that? I tried using arcpy, but as it is a proprietary software of ArcGIS, it is not possible to directly import the library? What are the alternatives to that?
I even explored a lot of sources on the internet but not found anything useful.
Tried comtypes.client also for importing and viewing the same.
import array
import comtypes.client
acad = comtypes.client.GetActiveObject("AutoCAD.Application")
doc = acad.ActiveDocument
ms = doc.ModelSpace
doc.Import("sample.dwg", array.array('d', [0, 0, 0]), 1.0)
I am getting this error, I have downloaded AutoCAD software also still I am getting this error. Please suggest what I am missing and what is the error actually means.
OSError Traceback (most recent call last)
in
3 try:
----> 4 acad = comtypes.client.GetActiveObject("AutoCAD.Application")
5 except:
~\Anaconda3\envs\py36\lib\site-packages\comtypes\client_init_.py in GetActiveObject(progid, interface, dynamic)
172 """
--> 173 clsid = comtypes.GUID.from_progid(progid)
174 if dynamic:
~\Anaconda3\envs\py36\lib\site-packages\comtypes\GUID.py in from_progid(cls, progid)
77 inst = cls()
---> 78 _CLSIDFromProgID(str(progid), byref(inst))
79 return inst
_ctypes/callproc.c in GetResult()
OSError: [WinError -2147221005] Invalid class string

You can try something like this:
import comtypes.client
import time
try:
acad = comtypes.client.GetActiveObject("AutoCAD.Application")
except:
acad = comtypes.client.CreateObject("AutoCAD.Application")
while not acad.GetAcadState().IsQuiescent :
time.sleep(5)
acad.Visible = True
doc = acad.Documents.Open("C:\\Temp\\Sample.dwg")

Related

ModuleNotFoundError: No java install detected. Please install java to use language-tool-python

I would like to check the number if issues in a given sentence.
my code is
import language_tool_python
tl = language_tool_python.LanguageTool('en-US')
txt = "good mooorning sirr and medam my namee anderen i am from amerecia !"
m = tl.check(txt)
len(m)
Instead of returning the number i am getting error message as shown below.
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-1c4c9134d6f4> in <module>
1 import language_tool_python
----> 2 tool = language_tool_python.LanguageTool('en-US')
3
4 text = "Your the best but their are allso good !"
5 matches = tool.check(text)
E:\Anaconda\lib\site-packages\language_tool_python\server.py in __init__(self, language, motherTongue, remote_server, newSpellings, new_spellings_persist)
43 self._update_remote_server_config(self._url)
44 elif not self._server_is_alive():
---> 45 self._start_server_on_free_port()
46 if language is None:
47 try:
E:\Anaconda\lib\site-packages\language_tool_python\server.py in _start_server_on_free_port(self)
212 self._url = 'http://{}:{}/v2/'.format(self._HOST, self._port)
213 try:
--> 214 self._start_local_server()
215 break
216 except ServerError:
E:\Anaconda\lib\site-packages\language_tool_python\server.py in _start_local_server(self)
222 def _start_local_server(self):
223 # Before starting local server, download language tool if needed.
--> 224 download_lt()
225 err = None
226 try:
E:\Anaconda\lib\site-packages\language_tool_python\download_lt.py in download_lt(update)
142 ]
143
--> 144 confirm_java_compatibility()
145 version = LATEST_VERSION
146 filename = FILENAME.format(version=version)
E:\Anaconda\lib\site-packages\language_tool_python\download_lt.py in confirm_java_compatibility()
73 # found because of a PATHEXT-related issue
74 # (https://bugs.python.org/issue2200).
---> 75 raise ModuleNotFoundError('No java install detected. Please install java to use language-tool-python.')
76
77 output = subprocess.check_output([java_path, '-version'],
ModuleNotFoundError: No java install detected. Please install java to use language-tool-python.
When I run the code I get no java install detected
How to solve this issue?
I think this is not an issue with the Code itself when I run the code you provided
import language_tool_python
tl = language_tool_python.LanguageTool('en-US')
txt = "good mooorning sirr and medam my namee anderen i am from amerecia !"
m = tl.check(txt)
len(m)
I get as result a number in this case
OUT: 8
In the Documentation of the language-tool-python is written:
By default, language_tool_python will download a LanguageTool server .jar and run that in the background to detect grammar errors locally. However, LanguageTool also offers a Public HTTP Proofreading API that is supported as well. Follow the link for rate-limiting details. (Running locally won't have the same restrictions.)
So You will need Java (JRE and SKD). Also it's Written in the Requirements of the library:
Prerequisites
Python 3.5+
LanguageTool (Java 8.0 or higher)
The installation process should take care of downloading LanguageTool (it may take a few minutes). Otherwise, you can manually download LanguageTool-stable.zip and unzip it into where the language_tool_python package resides.
Source:
https://pypi.org/project/language-tool-python/
Python 2.7 - JavaError when using grammar-check 1.3.1 library
I Hope I could help.

NameError: name 'request' is not defined in python 3

I am trying to run the basic feature extraction code from the following site:
musicinformationretrieval.
When I try to run the following code line:
kick_filepaths, snare_filepaths = stanford_mir.download_samples(collection="drum_samples_train")
it shows the following error message:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-3-7c764e7836ee> in <module>()
----> 1 kick_filepaths, snare_filepaths = stanford_mir.download_samples(collection="drum_samples_train")
C:\Users\dell\Desktop\stanford-mir-gh-pages\stanford_mir.py in download_samples(collection, download)
89 for i in range(1, 11):
90 filename = '%s_%02d.wav' % (drum_type, i)
---> 91 urllib.urlretrieve('http://audio.musicinformationretrieval.com/drum_samples/%s' % filename,
92 filename=os.path.join(collection, filename))
93 kick_filepaths = [os.path.join(collection, 'kick_%02d.wav' % i) for i in range(1, 11)]
AttributeError: module 'urllib' has no attribute 'urlretrieve'
Please help me to solve this issue.
It seems you should use Python 2 insteed of 3. In instruction to stanford_mir there is line:
If you’re totally new, the simplest solution is to download and install Anaconda for Python 2 (2.7), not Python 3.
Also you can read why it is not working on Python 3 here.
UPD:
for using Python 3 you can try add code before using the lib:
import sys
if sys.version_info[0] >= 3:
from urllib.request import urlretrieve
else:
# Not Python 3 - today, it is most likely to be Python 2
# But note that this might need an update when Python 4
# might be around one day
from urllib import urlretrieve
UPD2:
urlretrieve import does not help)

Why can't my Python code load *.SO files when running in a IPython Notebook on Linux?

I have some Python code, which loads a DLL/SO, via the ctypes.CDLL() function.
It runs fine when run under python/ipython, from the command prompt on all 3 OSes:
Windows 7 Pro
Mac OS X
Linux (Ubuntu 14.04)
It also runs fine in a IPython Notebook, under Windows and Mac OS X.
However, it does NOT work in a IPython Notebook, under Linux. It gives this error:
OSError: example_tx_x86_amd64.so: cannot open shared object file: No such file or directory
The funny thing is: the *.SO file doesn't show up in the directory listing shown in the Home tab of the browser (i.e. - the one from which you select/launch your desired notebook file), either! (All other files in the directory appear there.)
This seems awfully suspicious to me. It's almost as if *.SO files are made invisible to the IPython Notebook Server, for security reasons, but only under Linux! I checked all the Notebook Server options available, but didn't see anything apropos.
Does anyone know what's going on?
Thanks!
Code and backtrace, as requested:
%matplotlib inline
from matplotlib import pyplot as plt
from numpy import array
from pyibisami import amimodel as ami
gTxDLLName = "example_tx_x86_amd64.so"
gBitRate = 10.e9
gSampsPerBit = 32
gNumBits = 100
bit_time = 1. / gBitRate
sample_interval = bit_time / gSampsPerBit
row_size = gNumBits * gSampsPerBit
channel_response = array([0.0, 1.0 / bit_time,])
channel_response.resize(row_size)
my_tx = ami.AMIModel(gTxDLLName)
tx_init = ami.AMIModelInitializer({'root_name' : "example_tx",
'tx_tap_nm1' : 10,
})
tx_init.bit_time = bit_time
tx_init.sample_interval = sample_interval
tx_init.channel_response = channel_response
my_tx.initialize(tx_init)
print "Message from model:"
print "\t", my_tx.msg
print "Parameter string from model:"
print "\t", my_tx.ami_params_out
--------------------------------------------------------------------------- OSError Traceback (most recent call
last) in ()
15 channel_response.resize(row_size)
16
---> 17 my_tx = ami.AMIModel(gTxDLLName)
18 tx_init = ami.AMIModelInitializer({'root_name' : "example_tx",
19 'tx_tap_nm1' : 10,
/home/dbanas/prj/PyAMI/pyibisami/amimodel.pyc in init(self,
filename)
214 " Load the dll and bind the 3 AMI functions."
215
--> 216 my_dll = CDLL(filename)
217 self._amiInit = my_dll.AMI_Init
218 try:
/home/dbanas/anaconda/lib/python2.7/ctypes/init.pyc in
init(self, name, mode, handle, use_errno, use_last_error)
363
364 if handle is None:
--> 365 self._handle = _dlopen(self._name, mode)
366 else:
367 self._handle = handle
OSError: example_tx_x86_amd64.so: cannot open shared object file: No
such file or directory

import nltk error in xampp, or on wamp

I am using nltk (installed and works fine in IDLE) for a project in Python (2.7.4) which runs perfectly fine on IDLE, but using the same code in xampp or wamp (cgi-bin), everything related to 'nltk' doesn't works and this is the error shown by adding these lines
import cgitb
cgitb.enable()
The errors are in lines are marked by '=>' and details are enclosed within ** and **. I've tried printing 'sys.path' after importing os, which shows the 'site-packages' directory in which 'nltk' resides. I even copied and pasted this folder into 'C:\Python27\' but still the same errors. Things other than nltk works as desired.
<type 'exceptions.ValueError'> Python 2.7.4: C:\python27\python.exe
Wed May 14 16:13:34 2014
A problem occurred in a Python script. Here is the sequence of function calls
leading up to the error, in the order they occurred.
C:\xampp\cgi-bin\Major\project.py in ()
=> 73 import nltk
**nltk undefined**
C:\python27\nltk\__init__.py in ()
159 import cluster; from cluster import *
160
=> 161 from downloader import download, download_shell
162 try:
163 import Tkinter
**downloader undefined, download undefined, download_shell undefined**
C:\python27\nltk\downloader.py in ()
2199
2200 # Aliases
=> 2201 _downloader = Downloader()
2202 download = _downloader.download
2203 def download_shell(): DownloaderShell(_downloader).run()
**_downloader undefined, Downloader = None**
C:\python27\nltk\downloader.py in __init__(self=<nltk.downloader.Downl
oader object>, server_index_url=None, download_dir=None)
425 # decide where we're going to save things to.
426 if self._download_dir is None:
=> 427 self._download_dir = self.default_download_dir()
428
429 #/////////////////////////////////////////////////////////////////
**self = <nltk.downloader.Downloader object>, self._download_dir = None,
self.default_download_dir = <bound method Downloader.default_download_dir of
<nltk.downloader.Downloader object>>**
C:\python27\nltk\downloader.py in default_download_dir(self=<nltk.downloa
der.Downloader object>)
926 homedir = os.path.expanduser('~/')
927 if homedir == '~/':
=> 928 raise ValueError("Could not find a default
download directory")
929
930 # append "nltk_data" to the home directory
**<pre>builtin ValueError = < type 'exceptions.ValueError' > <pre/>**
<type 'exceptions.ValueError'>: Could not find a default download directory
args = ('Could not find a default download directory',)
message = 'Could not find a default download directory'

"Exception: No extension found at None" when trying on use Selenium Firefox WebDriver on a Mac

Any ideas?
In [1]: from selenium.firefox.webdriver import WebDriver
In [2]: d=WebDriver()
---------------------------------------------------------------------------
Exception Traceback (most recent call last)
/usr/local/selenium-read-only/<ipython console> in <module>()
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/selenium-2.0_dev-py2.6.egg/selenium/firefox/webdriver.pyc in __init__(self, profile, timeout)
48 profile = FirefoxProfile(name=profile)
49 if not profile:
---> 50 profile = FirefoxProfile()
51 self.browser.launch_browser(profile)
52 RemoteWebDriver.__init__(self,
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/selenium-2.0_dev-py2.6.egg/selenium/firefox/firefox_profile.pyc in __init__(self, name, port, template_profile, extension_path)
72
73 if name == ANONYMOUS_PROFILE_NAME:
---> 74 self._create_anonymous_profile(template_profile)
75 self._refresh_ini()
76 else:
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/selenium-2.0_dev-py2.6.egg/selenium/firefox/firefox_profile.pyc in _create_anonymous_profile(self, template_profile)
82 self._copy_profile_source(template_profile)
83 self._update_user_preference()
---> 84 self.add_extension(extension_zip_path=self.extension_path)
85 self._launch_in_silent()
86
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/selenium-2.0_dev-py2.6.egg/selenium/firefox/firefox_profile.pyc in add_extension(self, force_create, extension_zip_path)
152 not os.path.exists(extension_source_path)):
153 raise Exception(
--> 154 "No extension found at %s" % extension_source_path)
155
156 logging.debug("extension_source_path : %s" % extension_source_path)
Exception: No extension found at None
Okay, I've gotten past this Exception by manually creating the webdriver-extension.zip file and copying it to the selenium/firefox folder in my site-packages directory.
Everything that follows assumes you have gotten a copy of the source code from http://selenium.googlecode.com/svn/trunk/ (using subversion/svn)
If you have Ruby installed you might be able to run rake firefox (that's Ruby make), which is the way you are supposed to build the zip file. Since I don't have ruby installed, I just looked at the /Rakefile and figured out what was supposed to be in the zip.
Basically you just need to add everything in the /firefox/src/extension/ folder. On windows I added the /firefox/prebuilt/Win32/Release/webdriver-firefox.dll to it as well, but I'm not sure I needed to. (On linux you may need to add the appropriate .so file).
OK, I solved it by discovering that the virtualenv was I installed via macports was "missing",
sudo ln -s `which virtualenv-2.6` /opt/local/bin/virtualenv
then running rake firefox and finally rake firefox_xpi ...
what is the following code doing?
from selenium.firefox.webdriver import WebDriver
d = WebDriver()
a proper webdriver import and firefox instantiation looks like this:
from selenium import webdriver
d = webdriver.Firefox()

Categories