【问题标题】:Get the Text of AutoCAD AcDbText object with python使用 python 获取 AutoCAD AcDbText 对象的文本
【发布时间】:2020-05-30 18:50:06
【问题描述】:

在 python 中,获取 AcDbText 对象的文本的最佳方法是什么?

我正在使用 python、win32com 和 autoCAD。我希望能够通过 python 程序执行以下操作:

  • 将对象放入选择集中
  • 确定哪些是 AcDbText 对象
  • 从中提取文本,然后删除。

我可以很好地完成前两件事。但是,假设 textObj 是正确的对象类型,以下实现了剩下的一半——t 将包含所需的文本作为 str:

t = textObj.copy().fieldcode()
  • 问题 1:正如代码所暗示的那样,这会在图形中创建对象的副本,并且似乎没有提供一种方法来识别它以便以后删除。

  • 问题 2:原始对象拒绝从选择集中删除。如果selection 是选择集,则selection.clear()selection.delete()selection.erase() 的变体没有任何作用。 (我在 fieldcode() 调用前后检查了选择集的长度——对象的数量保持不变。)

我很困惑,似乎没有办法在不复制对象的情况下将文本从对象中取出。我错过了什么?

在 cmets 中对答案的每个问题,pprint(dir(textObj)) 的输出是:

['AddRef', 
'Application', 
'ArrayPolar', 
'ArrayRectangular', 
'Copy', 
'Database', 
'Delete', 
'Document',  
'EntityName', 
'EntityTransparency', 
'EntityType', 
'Erase',  
'GetBoundingBox', 
'GetExtensionDictionary',  
'GetIDsOfNames', 
'GetTypeInfo', 
'GetTypeInfoCount', 
'GetXData', 
'Handle', 
'HasExtensionDictionary', 
'Highlight', 
'Hyperlinks', 
'IntersectWith', 
'Invoke', 
'Layer', 
'Linetype', 
'LinetypeScale', 
'Lineweight', 
'Material', 
'Mirror', 
'Mirror3D', 
'Move', 
'ObjectID', 
'ObjectName', 
'OwnerID', 
'PlotStyleName', 
'QueryInterface', 
'Release', 
'Rotate', 
'Rotate3D', 
'ScaleEntity', 
'SetXData', 
'TransformBy', 
'TrueColor', 
'Update', 
'Visible', 
'_AddRef', 
'_GetIDsOfNames', 
'_GetTypeInfo', 
'_IAcadEntity__com_ArrayPolar', 
'_IAcadEntity__com_ArrayRectangular', 
'_IAcadEntity__com_Copy', 
'_IAcadEntity__com_GetBoundingBox', 
'_IAcadEntity__com_Highlight', 
'_IAcadEntity__com_IntersectWith', 
'_IAcadEntity__com_Mirror', 
'_IAcadEntity__com_Mirror3D', 
'_IAcadEntity__com_Move', 
'_IAcadEntity__com_Rotate', 
'_IAcadEntity__com_Rotate3D', 
'_IAcadEntity__com_ScaleEntity', 
'_IAcadEntity__com_TransformBy', 
'_IAcadEntity__com_Update', 
'_IAcadEntity__com__get_EntityName', 
'_IAcadEntity__com__get_EntityTransparency', 
'_IAcadEntity__com__get_EntityType', 
'_IAcadEntity__com__get_Hyperlinks', 
'_IAcadEntity__com__get_Layer', 
'_IAcadEntity__com__get_Linetype', 
'_IAcadEntity__com__get_LinetypeScale', 
'_IAcadEntity__com__get_Lineweight', 
'_IAcadEntity__com__get_Material', 
'_IAcadEntity__com__get_PlotStyleName', 
'_IAcadEntity__com__get_TrueColor', 
'_IAcadEntity__com__get_Visible', 
'_IAcadEntity__com__get_color', 
'_IAcadEntity__com__set_EntityTransparency', 
'_IAcadEntity__com__set_Layer', 
'_IAcadEntity__com__set_Linetype', 
'_IAcadEntity__com__set_LinetypeScale', 
'_IAcadEntity__com__set_Lineweight', 
'_IAcadEntity__com__set_Material', 
'_IAcadEntity__com__set_PlotStyleName', 
'_IAcadEntity__com__set_TrueColor', 
'_IAcadEntity__com__set_Visible', 
'_IAcadEntity__com__set_color', 
'_IAcadObject__com_Delete', 
'_IAcadObject__com_Erase', 
'_IAcadObject__com_GetExtensionDictionary', 
'_IAcadObject__com_GetXData', 
'_IAcadObject__com_SetXData', 
'_IAcadObject__com__get_Application', 
'_IAcadObject__com__get_Database', 
'_IAcadObject__com__get_Document', 
'_IAcadObject__com__get_Handle', 
'_IAcadObject__com__get_HasExtensionDictionary', 
'_IAcadObject__com__get_ObjectID', 
'_IAcadObject__com__get_ObjectName', 
'_IAcadObject__com__get_OwnerID', 
'_IDispatch__com_GetIDsOfNames', 
'_IDispatch__com_GetTypeInfo', 
'_IDispatch__com_GetTypeInfoCount', 
'_IDispatch__com_Invoke', 
'_IUnknown__com_AddRef', 
'_IUnknown__com_QueryInterface', 
'_IUnknown__com_Release', 
'_Invoke', 
'_QueryInterface', 
'_Release', 
'__bool__', 
'__class__', 
'__cmp__', 
'__com_interface__', 
'__ctypes_from_outparam__', 
'__del__', 
'__delattr__', 
'__dict__', 
'__dir__', 
'__doc__', 
'__eq__', 
'__format__', 
'__ge__', 
'__getattr__', 
'__getattribute__', 
'__gt__', 
'__hash__', 
'__init__', 
'__init_subclass__', 
'__le__', 
'__lt__', 
'__map_case__', 
'__module__', 
'__ne__', 
'__new__', 
'__reduce__', 
'__reduce_ex__', 
'__repr__', 
'__setattr__', 
'__setstate__', 
'__sizeof__', 
'__str__', 
'__subclasshook__', 
'__weakref__', 
'_b_base_', 
'_b_needsfree_', 
'_case_insensitive_', 
'_compointer_base__get_value', 
'_idlflags_', 
'_iid_', 
'_invoke', 
'_methods_', 
'_needs_com_addref_', 
'_objects', 
'_type_', 
'color', 
'from_param', 
'value']

【问题讨论】:

    标签: python-3.x com autocad


    【解决方案1】:

    假设textObj是单行文本对象(AcDbText)或多行文本对象(AcDbMText),那么您应该可以使用TextString属性获取文本内容,例如:

    t = textObj.TextString
    

    请注意,在 ActiveX SelectionSet object 上调用方法 clear()delete() 时不会删除它包含的对象,而是从 SelectionSet 中删除对象并分别删除 SelectionSet 对象。不过,erase() 方法应该会成功擦除 SelectionSet 中包含的所有对象。

    不过,要删除一个对象,您通常只需在对象本身上调用 delete() 方法,例如:

    textObj.Delete()
    

    【讨论】:

    • textObj.entityname 产生AcDbText,但textObj 没有TextString 属性。在print(dir(textObj)) 上不显示,并在尝试此解决方案时引发错误。
    • 太多了,无法发表评论——我把它们放在主帖中。它们看起来都不相关。
    猜你喜欢
    • 2012-04-06
    • 1970-01-01
    • 1970-01-01
    • 2014-10-13
    • 2022-09-28
    • 2021-05-11
    • 2020-11-07
    • 2020-02-15
    相关资源
    最近更新 更多