LoadFrom API is throwing exception for CADLinkType

LoadFrom API is throwing exception for CADLinkType

Harshil.JAINS8RVT
Contributor Contributor
472 Views
3 Replies
Message 1 of 4

LoadFrom API is throwing exception for CADLinkType

Harshil.JAINS8RVT
Contributor
Contributor

Hello,

 

I have linked a few drawing files to the Revit file and set Path as Absolute.

Now I have moved all the Revit files and linked drawing files to some other directory and opened the main Revit file programmatically.

I have written below code to resolve links before opening the file but my code is throwing exception at LaodFrom API.

Could you help me why it is throwing an exception ("An internal error has occurred.")?

am I doing anything wrong here?

 

private bool ResolveCADFileLinks(string iPath)
{
bool bSucceeded = true;
try
{

Document document = null;
DocumentSet documentSet = _uiapp.Application.Documents;
foreach (Document pDocument in documentSet)
{
if (String.Equals(iPath.ToLower(), pDocument.PathName.ToLower()))
{
document = pDocument;
break;
}
}

if (document == null)
{
document = _app.OpenDocumentFile(iPath);
}

string sCheckoutPath = "C:\temp";
if (document != null)
{
FilteredElementCollector linksCollector = new FilteredElementCollector(document).OfClass(typeof(CADLinkType));
int nCount = linksCollector.GetElementCount();

foreach (Element e in linksCollector)
{
if (e is CADLinkType)
{
CADLinkType cadLinkType = e as CADLinkType;
ExternalFileReference externalFile = e.GetExternalFileReference();
LinkedFileStatus linkedFileStatus = externalFile.GetLinkedFileStatus();
if (linkedFileStatus == LinkedFileStatus.NotFound)
{
string fileName, extAbsPath, newFilePath;
extAbsPath = ModelPathUtils.ConvertModelPathToUserVisiblePath(externalFile.GetAbsolutePath());
fileName = Path.GetFileName(extAbsPath);

newFilePath = Path.Combine(sCheckoutPath, fileName);
LinkLoadResult linkLoadResult = cadLinkType.LoadFrom(newFilePath);
}
}
}

linksCollector.Dispose();
}
}
catch (Autodesk.Revit.Exceptions.ArgumentNullException ex)
{
bSucceeded = false;
MessageBox.Show(ex.ToString());
}
catch (FileArgumentNotFoundException ex)
{
bSucceeded = false;
MessageBox.Show(ex.ToString());
}
catch (Autodesk.Revit.Exceptions.InvalidOperationException ex)
{
bSucceeded = false;
MessageBox.Show(ex.ToString());
}
catch (Exception ex)
{
bSucceeded = false;
MessageBox.Show(ex.ToString());
}

return bSucceeded;
}

0 Likes
473 Views
3 Replies
Replies (3)
Message 2 of 4

caroline.gitonga
Autodesk
Autodesk

Hi @Harshil.JAINS8RVT 

I think there is no overload of LoadFrom with one argument. 

The available options are as follows: 

LoadFrom(ExternalResourceReference resourceReference, WorksetConfiguration config)

LoadFrom(ModelPath path, WorksetConfiguration config)

with the second argument  WorksetConfiguration object indicating which worksets in the link to open.

If you want to load the same set of worksets the link previously had, leave this argument as a null reference.(as per the documentation). Maybe you could pass a second argument as null.

 

I hope this helps

Carol Gitonga, Developer Advocacy and Support, ADN Open
0 Likes
Message 3 of 4

Harshil.JAINS8RVT
Contributor
Contributor

Hello Caroline,

 

Thanks for the reply.

Here LoadFrom API has two argument is for RevitLinkType.

But I am facing an issue with CADLinkType. Where LoadFrom API has one argument only.

For reference please check the below link.

https://www.revitapidocs.com/2021.1/41db0b8b-4bd4-02b0-f06a-a7a169802e1b.htm

 

Thanks

 

Message 4 of 4

ajimenezcu
Participant
Participant

Hi @Harshil.JAINS8RVT
Do you solve the problem?

Thanks.

0 Likes