reclosedev / pyautocad

AutoCAD Automation for Python ⛺
http://pypi.python.org/pypi/pyautocad/
BSD 2-Clause "Simplified" License
496 stars 142 forks source link

_ctypes.COMError: -2147467262 #4

Closed shaief closed 11 years ago

shaief commented 11 years ago

Hi Roman, First of all - thank you so much for this library. It is working great and allows me to do what i wanted just in Python. I wrote a simple script (with gui) and it worked well on my machine, but after compiling i tried it on two different machines and received an error. Here is a version of the error from each of the machines:

  1. _ctypes.COMError: (-2147467262, 'No such interface supported', (None, None))
  2. _ctypes.COMError: (-2147467262, '\xfe\xfe\xee\xee\xf9\xf7 \xee\xf1\xe5\xe2 \xe6\xe4 \xe0\xe9\xf0\xe5 \xf0\xfa\xee\xea', (None, None, None, 0, None))

Here is the code i used:

import sys
import pygtk
if not sys.platform == 'win32':
    pygtk.require('2.0')

import gtk,gobject,time
import os.path
import datetime
from pyautocad import Autocad, utils
from pyautocad.contrib.tables import Table

now = datetime.datetime.now()
today_date = str(now.year) + str(now.month) + str(now.day) + "_" + str(now.hour) + "-" + str(now.minute)
acad = Autocad()

def set_file_name(self):
#This method checks the existance of an XLS file, and allows the user to overwrite it, 
#or use a different file.
    tableFilename = raw_input("Enter table name: ")
    tableFilename = tableFilename + ".xls"
    if os.path.isfile(tableFilename):
        fileoverwrite = 'n'
        while (fileoverwrite != 'y' or fileoverwrite != 'Y' or (os.path.isfile(tableFilename))):
            fileoverwrite = raw_input("File " + tableFilename + " exist. Overwrite (y/n)?")
            if fileoverwrite == 'y' or fileoverwrite == 'Y':
                break
            elif fileoverwrite == 'n' or fileoverwrite == 'N':
                tableFilename = raw_input("Enter table name: ")
                tableFilename = tableFilename + ".xls"
                if os.path.isfile(tableFilename):
                    continue
                else:
                    break
            else:
                print "Goodbye!"
                sys.exit(0)
    return tableFilename            

def line_lengths_excel(filename, draw_units):
# This function iterate over all the layers in the opened DWG and write sum of line lengths of each layer into one MS-Excel sheet.
# Parameters needed:
#1. Name of an MS-Excel file (doesn't have to exist)
#2. Units of the drwaing

        tableFilename = filename + '.xls'#set_file_name(self)
        table = Table()
        layers = []
        total_length = []
        units_scale = {"m":1, "cm":100, "mm":1000}
        units = draw_units #raw_input("Enter the used units for scaling(m/cm/mm): ")
        scale = units_scale[units]
        acad.prompt("Creating a table of line lengths")
        # value = 0.0
        for line in acad.iter_objects('line'):
            # pbar.set_fraction(value)
            # value = value + 0.01
            l1 = line.Length
            #print line.Layer
            if line.Layer in layers:
                i = layers.index(line.Layer)
                total_length[i] += l1
            else:
                layers.append(line.Layer)
                total_length.append(l1)
        print layers
        print total_length
        # Add data to table
        table.writerow(["COMPANY NAME", "Lines Lengths, Created:"+today_date])
        table.writerow(["Layer", "Length [" + units+"]"])
        for i in range(len(layers)):
            table.writerow([layers[i], total_length[i]])
        # Save table in xls
        table.save(tableFilename, 'xls')
        # raw_input("Press Enter to continue...")

def count_blocks_excel(filename):
# This function iterate over all the layers in the opened DWG and summing up all the blocks in the file into one MS-Excel sheet.
# Parameters needed:
#1. Name of an MS-Excel file (doesn't have to exist)

        tableFilename = filename + '.xls'#set_file_name(self)
        table = Table()
        block_list = []
        block_layer = []
        total_blocks = []
        acad.prompt("Creating a table of blocks count")
        for layer in acad.iter_layouts():
            for block in acad.iter_objects('block'):
                b1 = block
                if block.name in block_list:
                    i = block_list.index(block.name)
                    total_blocks[i] += 1
                else:
                    block_list.append(block.name)
                    block_layer.append(block.Layer)
                    total_blocks.append(1)
        print block_list
        print total_blocks
        # Add data to table
        table.writerow(["Layer","Block", "Amount"])
        for i in range(len(block_list)):
            table.writerow([block_layer[i], block_list[i], total_blocks[i]])
        # Save table in xls
        table.save(tableFilename, 'xls')
        # raw_input("Press Enter to continue...")

class PyAPP():
    def __init__(self):
        self.window = gtk.Window()
        self.window.set_title("Automating AutoCAD Calculations")

        self.create_widgets()
        self.connect_signals()
        try:
            self.window.set_icon_from_file("Logo25.png")
        except Exception, e:
            print e.message
            sys.exit(1)
        self.window.show_all()
        gtk.main()

    def create_widgets(self):
        self.vbox = gtk.VBox(spacing=10)

        self.hbox_0 = gtk.HBox(spacing=10)      
        self.company_logo = gtk.Image()
        self.company_logo.set_from_file("company25mm90.png")
        self.hbox_0.pack_start(self.company_logo)

        self.hbox_1 = gtk.HBox(spacing=10)
        self.label = gtk.Label("File Name: ")
        self.hbox_1.pack_start(self.label)
        self.entry = gtk.Entry()
        self.hbox_1.pack_start(self.entry)
        self.unitsLabel = gtk.Label("DWG units: ")
        self.hbox_1.pack_start(self.unitsLabel)
        self.units = gtk.combo_box_new_text()
        self.units.append_text('m')
        self.units.append_text('cm')
        self.units.append_text('mm')
        self.units.set_active(1)
        self.hbox_1.pack_start(self.units)

        self.hbox_2 = gtk.HBox(spacing=10)
        self.bLineLength = gtk.Button("Sum Lines Lengths in a DWG to MS-Excel")
        self.hbox_2.pack_start(self.bLineLength)
        self.bBlocksCount = gtk.Button("Count Blocks in a DWG to MS-Excel")
        self.hbox_2.pack_start(self.bBlocksCount)

        self.hbox_3 = gtk.HBox(spacing=10)
        self.pbar = gtk.ProgressBar()
        self.hbox_3.pack_start(self.pbar)
        self.button_exit = gtk.Button("Exit")
        self.hbox_3.pack_start(self.button_exit)

        self.hbox_4 = gtk.HBox(spacing=10)
        self.se_logo = gtk.Image()
        self.se_logo.set_from_file("Logo25.png")
        self.hbox_4.pack_start(self.se_logo)
        self.se_label = gtk.Label("(2013)")
        self.hbox_4.pack_start(self.se_label)

        self.vbox.pack_start(self.hbox_0)
        self.vbox.pack_start(self.hbox_1)
        self.vbox.pack_start(self.hbox_2)
        self.vbox.pack_start(self.hbox_3)
        self.vbox.pack_start(self.hbox_4)

        self.window.add(self.vbox)

    def connect_signals(self):
        self.filename = self.entry.set_text('temp'+today_date)
        self.button_exit.connect("clicked", self.callback_exit)
        self.bLineLength.connect('clicked', self.callback_lines_lengths)
        self.bBlocksCount.connect('clicked', self.callback_blocks_count)    

    def callback_lines_lengths(self, widget, callback_data=None):
        filename = self.entry.get_text()
        draw_units = self.units.get_active_text()
        line_lengths_excel(filename, draw_units)

    def callback_blocks_count(self, widget, callback_data=None):
        filename = self.entry.get_text()
        count_blocks_excel(filename) 

    def callback_exit(self, widget, callback_data=None):
        gtk.main_quit()

if __name__ == "__main__":
    app = PyAPP()

and a trace back attached as an image.

Do you have any idea what went wrong?

Thanks,

Shai. AAC_SE_Error 1

reclosedev commented 11 years ago

Hi Shai.

Looks like the problem is in wrong type library loaded. Please try latest version from github. Or just replace code in installed package Python27\Lib\site-packages\pyautocad\api.py:

import logging
import comtypes
import glob
import os
try:
    import comtypes.client
    # generate modules for work with ACAD constants
    for pattern in ("acax*enu.tlb", "axdb*enu.tlb"):
        pattern = os.path.join(
            r"C:\Program Files\Common Files\Autodesk Shared",
            pattern
        )
        tlib = glob.glob(pattern)[0]
        comtypes.client.GetModule(tlib)
    import comtypes.gen.AutoCAD as ACAD
except Exception:
    # we are under readthedocs.org and need to mock this
    ACAD = None

Also, what tool do you use for compiling your app to exe? If it's py2exe and previous fix didn't work, Please try cx_Ffreeze.

shaief commented 11 years ago

Hi Roman, Thanks! Did you update it recently? i downloaded it a week ago from PyPi (https://pypi.python.org/pypi/pyautocad/). Should i use your gitHub repository? I did compile with cx_Freeze, and it works on my machine perfectly.

Shai.

reclosedev commented 11 years ago

I didn't update PyPI yet. Please check it on machine where you had problem when you can. If it works, I'll update PyPI package. Did previous version worked on your machine?

shaief commented 11 years ago

Previews version worked perfectly on my machine. I did as you suggested, and now i get the attached error. What did i do wrong? AAC_Error_20130330

reclosedev commented 11 years ago

It's strange.

What AutoCad version do you use? Do you have AutoCAD 20xx VBA module installed?

shaief commented 11 years ago

It all worked before, so i guess i have. I try it on AutoCAD 2009.

What is the best way to clean install your gitHub version?

reclosedev commented 11 years ago

What is the best way to clean install your gitHub version?

Delete Python27\Lib\site-packages\pyautocad\ download https://github.com/reclosedev/pyautocad/archive/master.zip unpack and run

python setup.py install

Also, delete comtypes cache Python27\Lib\site-packages\comtypes\gen\

shaief commented 11 years ago

OK. Done that and stayed with the previous error. It is the machine that worked before, now i can't use the code here as well. That's weird.

shaief commented 11 years ago

Installation log:

C:\Users\-----\Desktop\pyautocad-master>python setup.py install
running install
running bdist_egg
running egg_info
creating pyautocad.egg-info
writing requirements to pyautocad.egg-info\requires.txt
writing pyautocad.egg-info\PKG-INFO
writing top-level names to pyautocad.egg-info\top_level.txt
writing dependency_links to pyautocad.egg-info\dependency_links.txt
writing manifest file 'pyautocad.egg-info\SOURCES.txt'
reading manifest file 'pyautocad.egg-info\SOURCES.txt'
reading manifest template 'MANIFEST.in'
warning: no files found matching 'AUTHORS'
warning: no previously-included files matching '*.pyc' found under directory 'do
cs'
warning: no previously-included files matching '*.pyo' found under directory 'do
cs'
no previously-included directories found matching 'docs\_build'
no previously-included directories found matching 'docs\_themes\.git'
writing manifest file 'pyautocad.egg-info\SOURCES.txt'
installing library code to build\bdist.win32\egg
running install_lib
running build_py
creating build\lib
creating build\lib\pyautocad
copying pyautocad\api.py -> build\lib\pyautocad
copying pyautocad\cache.py -> build\lib\pyautocad
copying pyautocad\types.py -> build\lib\pyautocad
copying pyautocad\utils.py -> build\lib\pyautocad
copying pyautocad\__init__.py -> build\lib\pyautocad
creating build\lib\pyautocad\contrib
copying pyautocad\contrib\tables.py -> build\lib\pyautocad\contrib
copying pyautocad\contrib\__init__.py -> build\lib\pyautocad\contrib
creating build\bdist.win32
creating build\bdist.win32\egg
creating build\bdist.win32\egg\pyautocad
copying build\lib\pyautocad\api.py -> build\bdist.win32\egg\pyautocad
copying build\lib\pyautocad\cache.py -> build\bdist.win32\egg\pyautocad
creating build\bdist.win32\egg\pyautocad\contrib
copying build\lib\pyautocad\contrib\tables.py -> build\bdist.win32\egg\pyautocad
\contrib
copying build\lib\pyautocad\contrib\__init__.py -> build\bdist.win32\egg\pyautoc
ad\contrib
copying build\lib\pyautocad\types.py -> build\bdist.win32\egg\pyautocad
copying build\lib\pyautocad\utils.py -> build\bdist.win32\egg\pyautocad
copying build\lib\pyautocad\__init__.py -> build\bdist.win32\egg\pyautocad
byte-compiling build\bdist.win32\egg\pyautocad\api.py to api.pyc
byte-compiling build\bdist.win32\egg\pyautocad\cache.py to cache.pyc
byte-compiling build\bdist.win32\egg\pyautocad\contrib\tables.py to tables.pyc
byte-compiling build\bdist.win32\egg\pyautocad\contrib\__init__.py to __init__.p
yc
byte-compiling build\bdist.win32\egg\pyautocad\types.py to types.pyc
byte-compiling build\bdist.win32\egg\pyautocad\utils.py to utils.pyc
byte-compiling build\bdist.win32\egg\pyautocad\__init__.py to __init__.pyc
creating build\bdist.win32\egg\EGG-INFO
copying pyautocad.egg-info\PKG-INFO -> build\bdist.win32\egg\EGG-INFO
copying pyautocad.egg-info\SOURCES.txt -> build\bdist.win32\egg\EGG-INFO
copying pyautocad.egg-info\dependency_links.txt -> build\bdist.win32\egg\EGG-INF
O
copying pyautocad.egg-info\requires.txt -> build\bdist.win32\egg\EGG-INFO
copying pyautocad.egg-info\top_level.txt -> build\bdist.win32\egg\EGG-INFO
zip_safe flag not set; analyzing archive contents...
creating dist
creating 'dist\pyautocad-0.1.2-py2.7.egg' and adding 'build\bdist.win32\egg' to
it
removing 'build\bdist.win32\egg' (and everything under it)
Processing pyautocad-0.1.2-py2.7.egg
creating c:\python27\lib\site-packages\pyautocad-0.1.2-py2.7.egg
Extracting pyautocad-0.1.2-py2.7.egg to c:\python27\lib\site-packages
Adding pyautocad 0.1.2 to easy-install.pth file

Installed c:\python27\lib\site-packages\pyautocad-0.1.2-py2.7.egg
Processing dependencies for pyautocad==0.1.2
Searching for comtypes
Reading http://pypi.python.org/simple/comtypes/
Reading http://starship.python.net/crew/theller/wiki/the_comtypes_package
Reading http://sourceforge.net/project/showfiles.php?group_id=115265
Reading http://starship.python.net/crew/theller/comtypes
Reading http://starship.python.net/crew/wiki/the_comtypes_package
Best match: comtypes dev
Downloading https://comtypes.svn.sourceforge.net/svnroot/comtypes/#egg=comtypes-
dev
Doing subversion checkout from https://comtypes.svn.sourceforge.net/svnroot/comt
ypes/ to c:\users\shai\appdata\local\temp\easy_install-lsax6k\comtypes
'svn' is not recognized as an internal or external command,
operable program or batch file.
Processing comtypes
error: Couldn't find a setup script in c:\users\shai\appdata\local\temp\easy_ins
tall-lsax6k\comtypes
reclosedev commented 11 years ago

Looks like comtypes setup is kinda broken (it links to svn version of package). Try to comment out comtypes requirement in setup.py

   install_requires=[
        #'comtypes',
        ],

And if you are trying to run compiled exe, delete c:\Users\%USERNAME%\AppData\Local\Temp\comtypes_cache\ directory

shaief commented 11 years ago

Did it, and still the same error while running my code.

The installation log is:

C:\Users\-----\Desktop\pyautocad-master>python setup.py install
running install
running bdist_egg
running egg_info
writing pyautocad.egg-info\PKG-INFO
writing top-level names to pyautocad.egg-info\top_level.txt
writing dependency_links to pyautocad.egg-info\dependency_links.txt
reading manifest file 'pyautocad.egg-info\SOURCES.txt'
reading manifest template 'MANIFEST.in'
warning: no files found matching 'AUTHORS'
warning: no previously-included files matching '*.pyc' found under directory 'docs'
warning: no previously-included files matching '*.pyo' found under directory 'docs'
no previously-included directories found matching 'docs\_build'
no previously-included directories found matching 'docs\_themes\.git'
writing manifest file 'pyautocad.egg-info\SOURCES.txt'
installing library code to build\bdist.win32\egg
running install_lib
running build_py
copying pyautocad\api.py -> build\lib\pyautocad
copying pyautocad\cache.py -> build\lib\pyautocad
copying pyautocad\types.py -> build\lib\pyautocad
copying pyautocad\utils.py -> build\lib\pyautocad
copying pyautocad\__init__.py -> build\lib\pyautocad
copying pyautocad\contrib\tables.py -> build\lib\pyautocad\contrib
copying pyautocad\contrib\__init__.py -> build\lib\pyautocad\contrib
creating build\bdist.win32\egg
creating build\bdist.win32\egg\pyautocad
copying build\lib\pyautocad\api.py -> build\bdist.win32\egg\pyautocad
copying build\lib\pyautocad\cache.py -> build\bdist.win32\egg\pyautocad
creating build\bdist.win32\egg\pyautocad\contrib
copying build\lib\pyautocad\contrib\tables.py -> build\bdist.win32\egg\pyautocad\contrib
copying build\lib\pyautocad\contrib\__init__.py -> build\bdist.win32\egg\pyautocad\contrib
copying build\lib\pyautocad\types.py -> build\bdist.win32\egg\pyautocad
copying build\lib\pyautocad\utils.py -> build\bdist.win32\egg\pyautocad
copying build\lib\pyautocad\__init__.py -> build\bdist.win32\egg\pyautocad
byte-compiling build\bdist.win32\egg\pyautocad\api.py to api.pyc
byte-compiling build\bdist.win32\egg\pyautocad\cache.py to cache.pyc
byte-compiling build\bdist.win32\egg\pyautocad\contrib\tables.py to tables.pyc
byte-compiling build\bdist.win32\egg\pyautocad\contrib\__init__.py to __init__.pyc
byte-compiling build\bdist.win32\egg\pyautocad\types.py to types.pyc
byte-compiling build\bdist.win32\egg\pyautocad\utils.py to utils.pyc
byte-compiling build\bdist.win32\egg\pyautocad\__init__.py to __init__.pyc
creating build\bdist.win32\egg\EGG-INFO
copying pyautocad.egg-info\PKG-INFO -> build\bdist.win32\egg\EGG-INFO
copying pyautocad.egg-info\SOURCES.txt -> build\bdist.win32\egg\EGG-INFO
copying pyautocad.egg-info\dependency_links.txt -> build\bdist.win32\egg\EGG-INFO
copying pyautocad.egg-info\top_level.txt -> build\bdist.win32\egg\EGG-INFO
zip_safe flag not set; analyzing archive contents...
creating 'dist\pyautocad-0.1.2-py2.7.egg' and adding 'build\bdist.win32\egg' to
it
removing 'build\bdist.win32\egg' (and everything under it)
Processing pyautocad-0.1.2-py2.7.egg
creating c:\python27\lib\site-packages\pyautocad-0.1.2-py2.7.egg
Extracting pyautocad-0.1.2-py2.7.egg to c:\python27\lib\site-packages
Adding pyautocad 0.1.2 to easy-install.pth file

Installed c:\python27\lib\site-packages\pyautocad-0.1.2-py2.7.egg
Processing dependencies for pyautocad==0.1.2
Finished processing dependencies for pyautocad==0.1.2
reclosedev commented 11 years ago

It's weird. I'd try to restart AutoCAD or/and to use PyPI version.

shaief commented 11 years ago

Oh, my bad. After installing the new version my AutoCAD was closed. I now opened it and it works.

Sorry!

Now i'll re-compile it and try on the other machines. I'll update if it will work.

Thanks a lot for your help and for the library!

shaief commented 11 years ago

Hi Roman, Here is an update: I still face the same error on three out of five computers running the compiled version of my code.

Do you have any idea what can cause this issue?

Thanks!

Shai.

reclosedev commented 11 years ago

Sorry, can't reproduce this issue. But I've tested compiled version only on following configurations: WinXP 32bit, Autocad 2008 Win7 32bit, Autocad 2013 Win7 64bit, Autocad 2013

shaief commented 11 years ago

Hi Roman, Well, it was an issue with my compilation. I didn't take into consideration differences between 64bit and 32bit systems, as i was initially compiling it on 64bit. Sorry for the hassle, i'm closing this issue.

Thanks a lot!

Shai.