Get path from imported .dwg how?

Hello,

i get some hints

it works almost well, but i get this error so, where is the issue?


doc   = __revit__.ActiveUIDocument.Document #type: Document
uidoc = __revit__.ActiveUIDocument          #type: UIDocument
app   = __revit__.Application               # Application class

def get_abs_path(cad_link_type):
	lfs = cad_link_type.GetExternalFileReference().GetLinkedFileStatus()
	if all([lfs != LinkedFileStatus.Invalid, lfs != LinkedFileStatus.NotFound]):
		return ModelPathUtils.ConvertModelPathToUserVisiblePath(cad_link_type.GetExternalFileReference().GetAbsolutePath())


cad_links = FilteredElementCollector(doc).OfClass(CADLinkType).ToElements()

cad_files = list(filter(None, map(get_abs_path, cad_links)))

OUT = cad_links, cad_files

i tested with linked and imported .dwgs i got the same error

Andreas,
I have used the following approach. I think it does what you are trying to do. It first gets the ImportInstances, then the type from that. Let me know if it is helpful.

dwgs = DB.FilteredElementCollector(revit.doc).OfClass(DB.ImportInstance).WhereElementIsNotElementType().ToElements()

    if len(dwgs) > 0: # Collector not empty
        for ii, dwg in enumerate(dwgs):
            dwg_id = dwg.Id
            dwg_name = dwg.Parameter[DB.BuiltInParameter.IMPORT_SYMBOL_NAME].AsString()
            dwg_type = doc.GetElement(dwg.GetTypeId()) # Get link type from instance
            if dwg_type.IsExternalFileReference():
                ext_ref = dwg_type.GetExternalFileReference()
                dwg_link_path  = DB.ModelPathUtils.ConvertModelPathToUserVisiblePath(ext_ref.GetAbsolutePath())
1 Like

@KSalmon ,

i set also a else statment… and so it does not work :thinking:

the issue is it : to be or not to be…

does the path realy work for imported .dwg… is the filepath anywhere stored ?

here ends the topic too

Have you changed your collector to get ImportInstance instead of CadLinkType?

1 Like

@KSalmon ,

i even tested linked .dwg vs imported…

i am intrested in the imported ones , i am aware its wrong, but it happend. so the project is 2 years old. i hoped, that there is anywhere stored the origin of the .dwg

My code works for getting the path of an imported DWG.

1 Like

Really, @KSalmon ! you get the path tooo

@KSalmon

it remains empty

grafik

doc   = __revit__.ActiveUIDocument.Document #type: Document
uidoc = __revit__.ActiveUIDocument          #type: UIDocument
app   = __revit__.Application               # Application class

dwgs = FilteredElementCollector(revit.doc).OfClass(ImportInstance).WhereElementIsNotElementType().ToElements()


paths = []

if len(dwgs) > 0: # Collector not empty

    for ii, dwg in enumerate(dwgs):
        dwg_id = dwg.Id
        dwg_name = dwg.Parameter[BuiltInParameter.IMPORT_SYMBOL_NAME].AsString()
        dwg_type = doc.GetElement(dwg.GetTypeId()) # Get link type from instance

        if dwg_type.IsExternalFileReference():
            ext_ref = dwg_type.GetExternalFileReference()
            dwg_link_path  = ModelPathUtils.ConvertModelPathToUserVisiblePath(ext_ref.GetAbsolutePath())
            paths.append(dwg_link_path)

print(paths)

because you are not getting passed this “if” statement. put a simple command after that like print(“Yes”) and you will see it won’t print
Just do a quick compare on import and linked dwgs and you will see for the imported dwgs all the external file refrences are not available

1 Like

Sorry,
You are right
It only gets the path for those that pass the IsExternalFileReference() test
My mistake.

1 Like