Linked DWG PolyLine Redraw

Hi Everybody. I’m trying to redraw the line by layer of a CAD link. I have obtained all the coordinates of the layer that I want to get. So is there a way from these coordinates I can draw a line like a CAD file? Please help me. Thanks a lot

import clr
import System
from System.Collections.Generic import *

clr.AddReference("ProtoGeometry")
from Autodesk.DesignScript.Geometry import *

clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *

clr.AddReference("RevitAPIUI")
from Autodesk.Revit.UI import *

clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)

clr.AddReference("RevitServices")
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument
view = doc.ActiveView
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application

importDWG = IN[1]
nameOfLayersToImport = IN[2]

if isinstance(importDWG, list):
    importDWG = UnwrapElement(importDWG)
else:
    importDWG = UnwrapElement([importDWG])

def GetGeometry(element):
    opt = Options()
    opt.ComputeReferences = True
    opt.IncludeNonVisibleObjects = True
    opt.View = doc.ActiveView
    geometry = element.get_Geometry(opt)
    return [i for i in geometry]
def Flatten(listgeo):
    re = []
    for i in listgeo:
        if len(i) != 0:
            for j in i:
                re.append(j)
        else:
            re.append(i)
    return re
getgeo = Flatten([GetGeometry(i) for i in importDWG])
TransactionManager.Instance.EnsureInTransaction(doc)
for geo_int in getgeo:
    geo_ele = geo_int.GetInstanceGeometry()
    for polyline in geo_ele:
        element = doc.GetElement(polyline.GraphicsStyleId)
        if not element:
            continue
        is_target_layer = element.GraphicsStyleCategory.Name == nameOfLayersToImport
        is_polyline = polyline.GetType().Name == "PolyLine"
        if is_polyline and is_target_layer:
            begin = None
            for pts in polyline.GetCoordinates():
                if not begin:
                    begin = pts
                    continue
                #end = pts
                #line = Line.CreateBound(begin,end)

TransactionManager.Instance.TransactionTaskDone()
OUT = polyline.GetCoordinates()

Something like that, you can use trick create polygon to auto sort point clockwise before create again detail line or detail curve in Revit.

coors = [] #list point get from cad link
TransactionManager.Instance.EnsureInTransaction(doc)
pol = PolyLine.Create(coors) # Create ploygon
corrdinates = pol.GetCoordinates()# list you has get sort clockwise
lines =[]
for i in range(len(corrdinates)):
    while (i< len(coordinates)):
       l =  Line.CreateBound(coordinates[i], coordinates[i+1])
       lines.append(l)
    lines.append(Line.CreateBound(coordinates[0], coordinates[len(corrdinates)-1]))

for j in range(len(lines)):
   #TODO Something, Ex create detail curve
  doc.Create.NewDetailCurve(doc.ActiveView,lines[j])
TransactionManager.Instance.TransactionTaskDone()

2 Likes

Hello. Thank you for responding to my question. However, when I followed the code you instructed. Revit doesn’t work. just keep running with no results

import clr
import System
from System.Collections.Generic import *

clr.AddReference("ProtoGeometry")
from Autodesk.DesignScript.Geometry import *

clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *

clr.AddReference("RevitAPIUI")
from Autodesk.Revit.UI import *

clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)

clr.AddReference("RevitServices")
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument
view = doc.ActiveView
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application

importDWG = IN[1]
nameOfLayersToImport = IN[2]

if isinstance(importDWG, list):
    importDWG = UnwrapElement(importDWG)
else:
    importDWG = UnwrapElement([importDWG])

def GetGeometry(element):
    opt = Options()
    opt.ComputeReferences = True
    opt.IncludeNonVisibleObjects = True
    opt.View = doc.ActiveView
    geometry = element.get_Geometry(opt)
    return [i for i in geometry]
def Flatten(listgeo):
    re = []
    for i in listgeo:
        if len(i) != 0:
            for j in i:
                re.append(j)
        else:
            re.append(i)
    return re
getgeo = Flatten([GetGeometry(i) for i in importDWG])
TransactionManager.Instance.EnsureInTransaction(doc)
for geo_int in getgeo:
    geo_ele = geo_int.GetInstanceGeometry()
    for polyline in geo_ele:
        element = doc.GetElement(polyline.GraphicsStyleId)
        if not element:
            continue
        is_target_layer = element.GraphicsStyleCategory.Name == nameOfLayersToImport
        is_polyline = polyline.GetType().Name == "PolyLine"
        if is_polyline and is_target_layer:
            begin = None
            pts = polyline.GetCoordinates()
            count = polyline.NumberOfCoordinates
            line = []
            for i in range(len(pts)):
                while (i< len(pts)):
                    l = Line.CreateBound(pts[i],pts[i+1])
                    line.append(l)
                line.append(Line.CreateBound(pts[0],pts[len(pts)-1]))


TransactionManager.Instance.TransactionTaskDone()
OUT = line

Please add i+=1 for while stop and create modelline or detailline if you want show to project.

Fixed

import clr
import System
from System.Collections.Generic import *

clr.AddReference("ProtoGeometry")
from Autodesk.DesignScript.Geometry import *

clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *

clr.AddReference("RevitAPIUI")
from Autodesk.Revit.UI import *

clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)

clr.AddReference("RevitServices")
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument
view = doc.ActiveView
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application

importDWG = IN[0]
nameOfLayersToImport = IN[1]

if isinstance(importDWG, list):
    importDWG = UnwrapElement(importDWG)
else:
    importDWG = UnwrapElement([importDWG])

def GetGeometry(element):
    opt = Options()
    opt.ComputeReferences = True
    opt.IncludeNonVisibleObjects = True
    opt.View = doc.ActiveView
    geometry = element.get_Geometry(opt)
    return [i for i in geometry]
def Flatten(listgeo):
    re = []
    for i in listgeo:
        if len(i) != 0:
            for j in i:
                re.append(j)
        else:
            re.append(i)
    return re
getgeo = Flatten([GetGeometry(i) for i in importDWG])
Points = []
TransactionManager.Instance.EnsureInTransaction(doc)
for geo_int in getgeo:
    geo_ele = geo_int.GetInstanceGeometry()
    for polyline in geo_ele:
        element = doc.GetElement(polyline.GraphicsStyleId)
        if not element:
            continue
        is_target_layer = element.GraphicsStyleCategory.Name == nameOfLayersToImport
        is_polyline = polyline.GetType().Name == "PolyLine"
        if is_polyline and is_target_layer:
            begin = None
            pts = polyline.GetCoordinates()
            for p in pts:
            	Points.append(p.ToPoint())
            count = polyline.NumberOfCoordinates
            line = []
            #for i in range(len(pts)):
             #   while (i< len(pts)-1):
             #       l = Line.CreateBound(pts[i],pts[i+1])
             #       line.append(l)
             #       i+=1
             #   line.append(Line.CreateBound(pts[0],pts[len(pts)-1]))
                
TransactionManager.Instance.TransactionTaskDone()
OUT = Points


Home.dyn (22.8 KB)

1 Like

Thank You…! :heart_eyes:

is possible to get directly the curves or polylines existing in the CAD file instead of points and trying to join them in a curve? many thanks

You can try to get geometry inside geometry instance and show object inside that, next convert it to dynamo by .ToProtoType()