When accessing Autocad via COM using IronPython, how can I convert return value into a Python list?

123 Views Asked by At

I am using IronPython with the RevitPythonShell to access a DWG file via COM. I have the COM access portion working but I can not figure out how to convert the return value into a Python list.

My function ExplodeBlks takes a list of Autocad BlockRef objects and explodes them. Per the Autocad help file the return should be Variant (array of objects)

Inside the IronPython function ExplodeBlks, what I get when I call print type(Rst) is: <type 'Array[object]'>. The for loop in SortDwgEnts does not get executed with Rst is passed as an argument.

Can someone help me convert RST to a Python list?

Sincerely; Michelle

Here is my IronPython 2.7 code:

import sys
import clr
import rpw
from Autodesk.Revit.DB import *
import Autodesk.Revit.Creation as RevitCreate
import os
import math

sys.path.append (os.path.expanduser ("~") + "\\Desktop\\Revit Access To DWG\\_ScriptStructure\\(AUE)_Python_Revit_Lib\\")
from ImportCadIntoRevit.ReadAcadDwg import *
from ImportCadIntoRevit.ImportCadIntoRevitLib import *
#from ImportCadIntoRevit.ReadAcadDwg import ReadAutoCadFile, ProcessBlock

from Autodesk.Revit import Exceptions as RevitExceptions

BlkLst = []
DimLst = []
TextLst = []
GeoLst = []
IgnoredLst = []
UnknownLst = []

ListOfDimTypes = ["AcDbRotatedDimension", "AcDbAlignedDimension", 
                    "AcDbRadialDimension", "AcDbDiametricDimension",
                    "AcDb3PointAngularDimension", "AcDbOrdinateDimension",
                    "AcDbArcDimension", "AcDbRadialDimensionLarge"
                   ]
ListOfTextTypes = ["AcDbMText","AcDbText","AcDbLeader", "AcDbMLeader", "AcDbArcAlignedText"]
ListOfGeoTypes = ["AcDbLine", "AcDbPolyline", "AcDbArc", "AcDbCircle", 
                    "AcDbEllipse", "AcDbSpline", "AcDbHatch", "AcDbWipeout",
                    "AcDbMline"
                   ]
ListOfTypesToIgnore = ["AcDbPoint", "AcDbRay", "AcDbXline"]



def dd ():
  FnameList = []

  #temporary file picker
  from pyrevit.forms import pick_file
  fname = pick_file ()
  FnameList.append(fname)
  
  with ReadAutoCadFile() as AcadApp:
    for fname in FnameList:
      print fname
      try:
        AcadApp.OpenDWG (fname)
        SortDwgEnts (AcadApp.Dwg.ModelSpace)
        ExplodeBlks()
        #with rpw.db.Transaction("test", doc) as T:
          #print AcadApp
          #aa(AcadApp.Dwg.ModelSpace)
      except ReadAutoCadFileERROR_FileOpen:
        #Skip file. Post messages to dialog
        print "DWG File Open Error"
        continue
      except Exception as E:
        print E

def SortDwgEnts (EntCollection):
  for item in EntCollection:
    if item.EntityName == "AcDbBlockReference":
      BlkLst.append(item)
    elif item.EntityName in ListOfDimTypes:
      DimLst.append(item)
    elif item.EntityName in ListOfTextTypes:
      TextLst.append(item)
    elif item.EntityName in ListOfGeoTypes:
      GeoLst.append(item)
    elif item.EntityName in ListOfTypesToIgnore:
      IgnoredLst.append(item)
    else:
      UnknownLst.append(item)

def ExplodeBlks ():
  for item in BlkLst:
    Rst = item.Explode()
    print type(Rst)
    SortDwgEnts (Rst)
1

There are 1 best solutions below

0
Michelle On

For those who might be interested, the error I was experiencing was not a Python/COM error but a bug with Autocad. When exploding uniformly scaled blocks AutoCad gives the documented and expected results to Iron Python. However, if the block is scaled non-uniformly, AutoCad returns an empty array. The block, by the way, is actually exploded (at least with AutoCad 2022).

Michelle