How to get curves from a .dwg?

Hello,

this is a perfect node :wink:


i want exactly that, but is there an analog way with dynamo/ Python ?

the fast-forward-way works too :slight_smile:

how can i access .dwg? Convert to Geometry (geo.ToProtoType) ? Did someone similar ?

just how to filter layers.

final aim is to create pipes, ducts, trays and segements passed on 2D-drawings

KR

Andreas

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
2 Likes

@gerhard.p ,

is the elif correct, has to be not else ?

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

grafik

KR

Andreas

Hi @Draxl_Andreas

You can use this node from Brimohareb pak.

here i use c# and accsess autocad from com API
you need to open the dwg file befor run the revit dynamo


3 Likes

You can’t use else like that, an else statement cant take a conditon, only if and elif can.

1 Like

@gerhard.p

ok i will check

You can find an example here

1 Like

@c.poupin ,

grafik

i got this message, the code runs well when the string(LayerName) does not exist…

Has it to be modeled clean? i mean the .dwg to get the layer i testet this

hmmm:/

KR

Andreas

@Draxl_Andreas

you need to adapt the code, can you post your dwg (sample file)?

@c.poupin ,

the code works well , when i have a clean and not overlapping line…

E02_SoloCurveAirSegment.dwg (137.1 KB)

is there a comment in autocad like OverKill ? to clean my .dwg ?

KR

ANdreas

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)
2 Likes