VBA is faster because it's running in-process inside AutoCAD (everything is loaded into acad.exe). Python runs out-of-process (AutoCAD objects need to be passed back and forth between acad.exe and python.exe).<div><br>
</div><div>You can use AutoCAD's ObjectARX API to embed python inside AutoCAD and make python in-process; you'll get a significant speedup. You'll need to know C++ in order to do that. Let me know if you'd like further details.</div>
<div><br></div><div>64-bit AutoCAD runs VBA out-of-process because there is no 64-bit VBA; you'd see the same slowness there that you're seeing with python.</div><div><br></div><div>-drg</div><div><br></div><div><br>
</div><div><br><div class="gmail_quote">On Mon, May 7, 2012 at 5:26 PM, DANIEL POSE <span dir="ltr"><<a href="mailto:iinjdpa@gmail.com" target="_blank">iinjdpa@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hello,<br><br>I am writing code to program AutoCAD access from Python using pywin32. When I need to work with a high number of AutoCAD objects, it is faster to use vba than python.<br>Are there some way to speed up python code in order to work faster with AutoCAD elements?<br>
For example in the next code when I work with 512 AutoCAD blocks:<br><br>import win32com.client
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px">import time</p>
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px">t1=time.clock()</p>
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px">acad= win32com.client.Dispatch("AutoCAD.Application")</p>
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px">doc = acad.ActiveDocument</p>
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px">seleccion=doc.SelectionSets.Add('selection6')</p>
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px">seleccion.SelectOnScreen()</p>
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px">t2=time.clock()</p>
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px">M=[]</p>
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px">for objeto in seleccion:</p>
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px"> if objeto.ObjectName=='AcDbBlockReference':</p>
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px"> M.append(objeto.InsertionPoint)</p>
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px">t3=time.clock()</p>
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px">print 'M_dimension=',len(M)</p>
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px">R=[]</p>
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px">for m in M:</p>
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px"> for x in M:</p>
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px"> R.append(max(m)+max(x))</p>
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px">print 'R_dimension=',len(R)</p>
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px">t4=time.clock()</p>
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px">t_block1=t2-t1</p>
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px">t_block2=t3-t2</p>
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px">t_block3=t4-t3</p>
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px">print 't_block1=',t_block1</p>
<p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px">print 't_block2=',t_block2</p>
<p style="margin:0px;text-indent:0px">print 't_block3=',t_block3</p><p style="margin:0px;text-indent:0px"><br></p><p style="margin:0px;text-indent:0px">The output for the code is the following:</p><p style="margin:0px;text-indent:0px">
M_dimension= 512<br>R_dimension= 262144<br>t_block1= 4.25343304805<br>t_block2= 3.88635510938<br>t_block3= 0.487477319045</p><p style="margin:0px;text-indent:0px"><br></p><p style="margin:0px;text-indent:0px">Then it is faster to work with R than M, even though R is bigger.</p>
<p style="margin:0px;text-indent:0px">Some suggestions for speed up pywin32 code in this example?</p><p style="margin:0px;text-indent:0px"><br></p><p style="margin:0px;text-indent:0px">Best Regards,</p><p style="margin:0px;text-indent:0px">
Daniel Pose.<br></p><p style="margin:0px;text-indent:0px"><br></p><p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px"><br></p><br><br>
<br>_______________________________________________<br>
python-win32 mailing list<br>
<a href="mailto:python-win32@python.org" target="_blank">python-win32@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/python-win32" target="_blank">http://mail.python.org/mailman/listinfo/python-win32</a><br>
<br></blockquote></div><br>
</div>