蟒蛇pyautocad,如何去构造一个链表类为COM [英] python pyautocad,how to contruct an objectlist for COM

查看:977
本文介绍了蟒蛇pyautocad,如何去构造一个链表类为COM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是由VBA中,真正的项目写入前。
我想将其更改为蟒蛇进行数据处理heavely。而且,使用'为AutoCAD的ActiveX自动化脚本与Python的方法。 code是below.My问题是,如何去构造为AddRegion对象列表?也许comtypes对VARINT一些话题。我真的没有关于COM的经验等等...

 # -  *  - 编码:UTF-8  -  *  -     从pyautocad导入AutoCAD,则将aPoint,aDouble
    ACAD =的Autocad(假,真)
    acad.prompt(你好,AUTOCAD在Python \\ n)
    打印acad.doc.Name    XX = acad.model.AddCircle(的aPoint(0,0),10)
    打印XX
    YY = acad.model.Add3Dpoly(aDouble([0,0,0,10,10,10,30,20,30,0,0,0]))
    打印yy.ObjectName
    打印yy.PlotStyleName    #如何去构造了一个AddRegion链表类?
    #regions = acad.model.AddRegion([YY])
    #acad.model.AddExtrudedSolid(地区[0],20,0)


解决方案

获得这一切工作的权利可以更多的工作,好像是应该的。读取使用python的数据;没有那么糟糕。写入数据时,有点麻烦。休闲用户/初学者;被警告的你的进入。


什么你可能需要

这帮助很大,如果你熟悉的AutoLISP,因为它只是更好地工作(在这种情况下),被记录较好,并集成更好的......你可能会需要它来奶未知/隐藏/无证信息蟒是不会告诉你的。 (请参阅明经 - VLA - 的一系列LISP函数)。

接下来,你需要的 win32com make_py和命令行脚本gen_py,或者您也可以使用 win32com.client.gen code 而在蟒蛇住居多。

要ppared直观地解析非常难看的文字$ P $(我甚至没有谈论口齿不清=])。被prepared失败,并感到兴奋,找出原因。

大多数它与COM变体做。然后你得到像变变阵奇怪的事情。如果您签出的 win32com.client.pythoncom 后,你会发现所有的数据类型都映射为整数。 (VT_BOOL是11为例)。


细节问题

您尝试ModelSpace.AddCircle,注意调试输出你下一次;
所有传递给InvokeTypes在参数是你所需要的以观看...(这是从我化妆PY输出采取AutoCAD的注册接口

 高清AddLine(个体经营,StartPoint可以= defaultNamedNotOptArg,终点= defaultNamedNotOptArg):
    RET = self._oleobj_.InvokeTypes(
                        1581,LCID,1,(9,0),((12,1),(12,1)),StartPoint可以,端点)
    如果RET不是无:
        RET =调度(RET,u'AddLine','{DF524ECB-D59E-464B-89B6-D32822282778}

这告诉你到底是哪COM类型win32com 认为就是了,所以一定要确保你至少有匹配的。

我发现许多输入功能实际上是记录并调用错误(我得知这个经过多次来回AutoLisp的)。我们在上面看到有 1581 在外面的值(这有点像一个类名,不是一个真正的数据类型),然后元组,基本上说的(调度,空) :( 9,0),然后一个VT_VARIANTS的阵列:((12,1),(12,1))。

有通常的COM期待缺少的外包装,以及由于某种原因使-PY没有意识到这一点。如果你去通过广泛AutoLisp的的明经 - 的废话,你会发现他们的周围是一个额外的包装。我相信,它要么是一个VARIANT_ARRAY,或毫不夸张,一个VARIANT变-ARRAY(四指针或东西)。在codeS这个是(VT_ARRAY = 8192,VT_VARIANT = 12)。

我很抱歉,我不记得具体细节,但我相信阅读部分的((12,1),(12,1)),应成为(8192,12,( (12,1),(12,1))),或者类似的东西。甚至有一次你弄清楚它应该是什么,林不知道,如果他们是速战速决。截至2010年的AutoCAD,对我来说,这意味着经历了恶人大gen_py输出,找到我真正想要的功能,以及手动更改InvokeTypes()调用以匹配COM期待。

一切都只是工作后如预期。


可能的解决方法

COM是丑陋的。在AutoCAD如果你是新的蟒蛇,但半经验(这意味着你想要做一些相当沉重的自动化),远离的 python-> win32com->的AutoCAD 的管道了。使用LISP。尽可能的痛苦,我说,你要去最终写了这么多LISP测试用例和调试陪你的Python的痛苦,你可能也只是承诺。


  • IronPython和.NET

    • 我相信这个界面更一般
    • 比COM支持

  • 的Visual Studio专业版(2008+)

    • 我从来没有使用过正式的VS-Pro的工具(我用的PythonWin和MinGW),林不知道是否有任何额外的魔法,只要将改变win32com如何处理的AutoCAD。我知道正式的AutoCAD ARX扩展提供了一个Studio项目源。最坏的情况下,你将有实际的文件带在身边,这就是蟒蛇-的AutoCAD这整个主题变得沾染。


This is an real project write by VBA before. I want change it to python for data processing is heavely. And, use 'ActiveX Automation scripts for AutoCAD with Python' method. code is below.My question is, how to contruct an object list for AddRegion? Maybe comtypes have some topic about VARINT. I really have no experience about COM and so on...

    # -*- coding: utf-8 -*-

    from pyautocad import Autocad, APoint, aDouble


    acad = Autocad(False,  True)
    acad.prompt("Hello, Autocad from Python\n")
    print acad.doc.Name

    xx = acad.model.AddCircle(APoint(0, 0),  10)
    print xx
    yy = acad.model.Add3Dpoly(aDouble([0,  0,  0,  10,  10,  10,  30,  20,  30,  0,  0,  0]))
    print yy.ObjectName
    print yy.PlotStyleName

    # How to contruct an objectlist for AddRegion?
    #regions = acad.model.AddRegion([yy])
    #acad.model.AddExtrudedSolid(regions[0], 20, 0)

解决方案

Getting it all to work right can be more work than it seems like it should be. Reading data using python; not so bad. Writing data, bit trickier. Casual user / Beginners; be warned of what your getting into.


What you will probably need

It helps tremendously if you are familiar with autolisp, because it just works better (in this case), is documented better, and integrates better,... and you will likely need it to milk 'unknown/hidden/undocumented' information that python isn't telling you. (see the vlax- and vla- series of lisp functions).

Next you need win32com make_py and gen_py scripts from the command line, or you can use win32com.client.gencode while staying mostly in python.

Be prepared to visually parse very ugly text (and I wasn't even talking about lisp =] ). Be prepared to fail, and get excited to find out why.

Most of it has to do with COM-Variants. And then you get wierd stuff like Variant-Variant-Arrays. If you check out win32com.client.pythoncom, you will notice all of the datatypes are mapped to integers. (VT_BOOL is 11 for example).


The Nitty Gritty

Next time you attempt a ModelSpace.AddCircle, pay attention to the debug output you get; All of the parameters passed to InvokeTypes are what you need to watch for... (this is taken from my make-py output for the Autocad Registered Interfaces

def AddLine(self, StartPoint=defaultNamedNotOptArg, EndPoint=defaultNamedNotOptArg):       
    ret = self._oleobj_.InvokeTypes(
                        1581, LCID, 1, (9, 0), ((12, 1), (12, 1)),StartPoint, EndPoint)
    if ret is not None:
        ret = Dispatch(ret, u'AddLine', '{DF524ECB-D59E-464B-89B6-D32822282778}'

This tells you exactly which COM types win32com THINKS it wants, so make sure you are at least matching that.

I have found that many input functions are actually documented and invoked wrong (I learned this after much back and forth with AutoLisp). What we see above has a value of 1581 on the outside (which is something like a class name, not really a datatype), and then tuple that basically says (DISPATCH, EMPTY):(9,0), and then an array of VT_VARIANTS:((12,1),(12,1)).

There is usually a missing outer wrapper that COM was expecting, and for some reason make-py does not realize this. if you go through extensive AutoLisp vlax- nonsense, you will notice their is an additional wrapper around that one. I believe it is either a VARIANT_ARRAY, or quite literally, a VARIANT-VARIANT-ARRAY (quadruple pointer or something). The codes for this are (vt_array=8192, vt_variant=12).

Im sorry I don't remember specifics, but I believe the portion reading ((12,1),(12,1)), should become (8192, 12, ((12,1),(12,1))), or something like that. Even once you do figure out what it should be, Im not sure if their is a quick fix. As of AutoCAD 2010, for me, this meant going through the ungodly large gen_py output, finding the functions I really wanted, and manually changing the InvokeTypes() call to match what COM was expecting.

Everything worked simply as expected after that.


Possible Workarounds

COM is ugly. If you are new to python, but semi-experienced in AutoCAD (meaning you want to do some fairly hefty automation), stay away from the python->win32com->AutoCAD pipeline. Use LISP. As much as that pains me to say, your gonna end up writing so many LISP test cases and debuggers to accompany your python pains, you might as well just commit.

  • Ironpython and .NET
    • I believe this interface is much more supported than COM in general
  • Visual Studio Professional (2008+)
    • I never used official VS-Pro tools (I used PythonWIN and MINGW), Im not sure if there is any extra magic provided that would change how win32com handles AutoCAD. I know the official AutoCAD ARX extensions provide their source in a Studio project. Worst case you would have actual documentation close at hand, which is where this entire topic of python-AutoCAD becomes tainted.

这篇关于蟒蛇pyautocad,如何去构造一个链表类为COM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆