No idea how the layer thing works but here is the code for importing from CAD link.
Only used this for polylines.
doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application
uidoc = uiapp.ActiveUIDocument
dwg_link_instance = UnwrapElement(IN[0])[0]
options = Options()
options.ComputeReferences = True
options.DetailLevel = Autodesk.Revit.DB.ViewDetailLevel.Fine
# Get the geometry from the linked instance
geo_elem = dwg_link_instance.get_Geometry(options)
# Function to extract and convert all geometry elements recursively
def extract_and_convert_geometry(geo_element, geometries=[]):
for geo in geo_element:
if hasattr(geo, "ToProtoType"):
# Convert Revit API geometry to Dynamo geometry
dynamo_geo = geo.ToProtoType()
geometries.append(dynamo_geo)
elif hasattr(geo, "GetInstanceGeometry"):
# For instances, get and convert the geometry recursively
extract_and_convert_geometry(geo.GetInstanceGeometry(), geometries)
return geometries
# Get all geometry elements and convert them to Dynamo geometry
all_geometries = extract_and_convert_geometry(geo_elem)
# Output the Dynamo geometries
OUT = all_geometries
import clr
import sys
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Structure import *
clr.AddReference('RevitAPIUI')
from Autodesk.Revit.UI import *
clr.AddReference('System')
from System.Collections.Generic import List
clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.GeometryConversion)
clr.ImportExtensions(Revit.Elements)
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application
uidoc = uiapp.ActiveUIDocument
dwg_link_instance = UnwrapElement(IN[0])
options = Options()
options.ComputeReferences = True
options.DetailLevel = Autodesk.Revit.DB.ViewDetailLevel.Fine
# Get the geometry from the linked instance
geo_elem = dwg_link_instance.get_Geometry(options)
# Function to extract and convert all geometry elements recursively
def extract_and_convert_geometry(geo_element, geometries=[]):
for geo in geo_element:
if hasattr(geo, "ToProtoType"):
# Convert Revit API geometry to Dynamo geometry
dynamo_geo = geo.ToProtoType()
geometries.append(dynamo_geo)
else hasattr(geo, "GetInstanceGeometry"):
# For instances, get and convert the geometry recursively
extract_and_convert_geometry(geo.GetInstanceGeometry(), geometries)
return geometries
# Get all geometry elements and convert them to Dynamo geometry
all_geometries = extract_and_convert_geometry(geo_elem)
# Output the Dynamo geometries
OUT = all_geometries
the error comes from the fact that some coordinates are too close
here is the fixed code
import clr
import sys
import System
# Add references
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
import Autodesk.DesignScript.Geometry as DS
clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import *
import Autodesk.Revit.DB as DB
# Add references to 'RevitNodes' and 'RevitServices' for Revit-specific functionality
clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.GeometryConversion)
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
doc = DocumentManager.Instance.CurrentDBDocument
def purge_points(coords_xyz):
out_pts = []
for c_xyz in coords_xyz:
if all(p.DistanceTo(c_xyz) > 0.001 for p in out_pts):
out_pts.append(c_xyz)
return out_pts
def get_Curves_from_CAD(importinstance, lst_layerName=[]):
datas = [] # Initialize an empty list to store data
# Create an Options object for importing geometry
opt = Options()
# Get the geometry of the import instance
geoSet = importinstance.get_Geometry(opt)
# Iterate through the geometry set
for geo in geoSet:
if isinstance(geo, GeometryInstance):
for g in geo.GetInstanceGeometry():
if isinstance(g, DB.PolyLine):
# Get the graphics style and name of the current polyline
gStyle = doc.GetElement(g.GraphicsStyleId)
gstyleName = gStyle.GraphicsStyleCategory.Name
# Check if the layer name is in the specified list
if gstyleName in lst_layerName:
purge_coordinates = purge_points(g.GetCoordinates())
# Create a DesignScript polycurve from the polyline
ds_polycurve = DS.PolyCurve.ByPoints([p.ToPoint() for p in purge_coordinates], False)
datas.append(ds_polycurve)
return datas
# Unwrap the import instance and layer name list from the Revit input
importinstance = UnwrapElement(IN[0])
lst_layerName = IN[1]
# Call the function and assign the result to the output variable
OUT = get_Curves_from_CAD(importinstance, lst_layerName)