admin管理员组

文章数量:1289911

I am using pyautocad to create drawings in autocad. I am attempting to group items together, but appear to be entering the items in the wrong format.

from pyautocad import Autocad, APoint, aDouble
import win32com.client

acadApp = win32com.client.Dispatch("AutoCAD.Application")

for doc in acadApp.Documents:
    if doc.Name == "Template.dwg":
        acadApp.ActiveDocument = doc

acad = Autocad()

group_name = "MyGroup"
group = doc.Groups.Add(group_name)

line1 = acad.model.AddLine(APoint(0, 0), APoint(100, 100))
line2 = acad.model.AddLine(APoint(100, 100), APoint(200, 0))

group.AppendItems([line1, line2])

I recieve this error:

File "COMObject Add", line 2, in AppendItems

TypeError: Objects for SAFEARRAYS must be sequences (of sequences), or a buffer object.

I have attempted to change the format to differrent ways suggested by chat gpt, for example:

group.AppendItems([[line1], [line2]])
group.AppendItems(((line1,), (line2,)))

or

for obj in (line1, line2):
    try:
        group.AppendItems([[obj]])  # Each object wrapped as a list inside a list
        print(f"Successfully appended object with Handle: {obj.Handle}")
    except Exception as e:
        print(f"Failed to append object with Handle {obj.Handle}: {e}")

or trying to create a safe array

sa = SafeArrayCreateVector(VT_VARIANT, 0, 2)
SafeArrayPutElement(sa, 0, line1)
SafeArrayPutElement(sa, 1, line2)

# Append the SAFEARRAY to the group
group.AppendItems(sa)

I have ran through every chatgpt response until it gave up.

I am using pyautocad to create drawings in autocad. I am attempting to group items together, but appear to be entering the items in the wrong format.

from pyautocad import Autocad, APoint, aDouble
import win32com.client

acadApp = win32com.client.Dispatch("AutoCAD.Application")

for doc in acadApp.Documents:
    if doc.Name == "Template.dwg":
        acadApp.ActiveDocument = doc

acad = Autocad()

group_name = "MyGroup"
group = doc.Groups.Add(group_name)

line1 = acad.model.AddLine(APoint(0, 0), APoint(100, 100))
line2 = acad.model.AddLine(APoint(100, 100), APoint(200, 0))

group.AppendItems([line1, line2])

I recieve this error:

File "COMObject Add", line 2, in AppendItems

TypeError: Objects for SAFEARRAYS must be sequences (of sequences), or a buffer object.

I have attempted to change the format to differrent ways suggested by chat gpt, for example:

group.AppendItems([[line1], [line2]])
group.AppendItems(((line1,), (line2,)))

or

for obj in (line1, line2):
    try:
        group.AppendItems([[obj]])  # Each object wrapped as a list inside a list
        print(f"Successfully appended object with Handle: {obj.Handle}")
    except Exception as e:
        print(f"Failed to append object with Handle {obj.Handle}: {e}")

or trying to create a safe array

sa = SafeArrayCreateVector(VT_VARIANT, 0, 2)
SafeArrayPutElement(sa, 0, line1)
SafeArrayPutElement(sa, 1, line2)

# Append the SAFEARRAY to the group
group.AppendItems(sa)

I have ran through every chatgpt response until it gave up.

Share Improve this question asked Feb 20 at 16:07 HG73HG73 331 silver badge5 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 0

I am new to Python but I have worked in CADD for several years. This sounds like an object enabler issue within the Autodesk program. You may need to download an object enabler for the specific program you are using. Below is the link of where to go for that. It is at least worth checking.

https://www.autodesk/support/technical/article/caas/sfdcarticles/sfdcarticles/Download-Object-Enablers.html

I was unable to solve this issue, but I found a work around, using send command instead:

line1 = acad.model.AddLine(APoint(0, 0), APoint(100, 100))
acad.doc.SendCommand('select ' + 'l  ')
acad.doc.SendCommand('ai_deselect ')

line2 = acad.model.AddLine(APoint(100, 100), APoint(200, 0))
acad.doc.SendCommand('select ' + 'p ' + 'l  ')
acad.doc.SendCommand('group ')

This draws a line, selects it, then deselect. Line 2 is drawn, the previous selection is highlighted (line 1) and then the last thing drawn (line 2). Then sends the group command.

The array type should be VT_ARRAY | VT_DISPATCH see VTDISPArrayOrVal

these maybe handy utility functions

def VTR8ArrayOrVal(__values):
    if isinstance(__values, list) or isinstance(__values, tuple):
        return win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, __values)
    return __values

def VTI2ArrayOrVal(__values):
    if isinstance(__values, list) or isinstance(__values, tuple):
        return win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_I2, __values)
    return __values

def VTVARArrayOrVal(__values):
    if isinstance(__values, list) or isinstance(__values, tuple):
        return win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_VARIANT, __values)
    return __values

def VTDISPArrayOrVal(__values):
    if isinstance(__values, list) or isinstance(__values, tuple):
        return win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_DISPATCH, __values)
return __values

If you’re using a recent version of AutoCAD, you might have a look at https://github/CEXT-Dan/PyRx wrappers for python

本文标签: pythonTrying to create a group using pyautocadStack Overflow