By Aaron Lu
In Revit, we can insert a link file easily via "Insert > Link Revit", but how to do that from API?
First I thought there might be some methods like Link or Load under Document class, unfortunately, there are, but they are only for DWG, DGN etc. rather than Revit Link file.
And also, there is no method named: Document.Create.NewRevitLinkInstance
So I inspected the RevitLinkInstance class itself, found a method: RevitLinkInstance.Create.
But new problem comes, the method takes an argument which is element id of RevitLinkType, how to create a RevitLinkType?
It comes to my mind that maybe LoadFamilySymbol is the one, but the document says it is only for loading .rfa file
so again, I inspect RevitLinkType class. Got this: RevitLinkType.Create
Well, the RevitLinkType.Create needs a ModelPath as one of its argument, but ModelPath can neigther be created from new (no public constructor), nor from Application.Create or Document.Create. Then how?
Fortunately I saw a class named ModelPathUtils, which contains a method ModelPathUtils.ConvertUserVisiblePathToModelPath, that is it!
Here comes the code:
ModelPath mp = ModelPathUtils.ConvertUserVisiblePathToModelPath( @"D:\Wall.rvt"); RevitLinkOptions rlo = new RevitLinkOptions(false); var linkType = RevitLinkType.Create(RevitDoc, mp, rlo); var instance = RevitLinkInstance.Create(RevitDoc, linkType.ElementId);
Note, the above code should be enclosed in a transaction