Hi,
This work for all link except DGN Link...
Please help me.
Transaction ParentTransaction = new Transaction(Doc);
ParentTransaction.Start("Create Link");
// Create Linked
try
{
String Suffix = Path.GetExtension(NewDocPathname).ToLower();
ElementId ElemId;
if ((Suffix != ".rvt") && (Suffix != ".ifc"))
{
if (Doc.ActiveView != null)
{
if (!Doc.ActiveView.CanBePrinted)
{
TaskDialog.Show("error", "NoPrintableView");
ParentTransaction.RollBack();
return Autodesk.Revit.UI.Result.Failed;
}
}
else
{
TaskDialog.Show("Error"t, "InsertNOView");
ParentTransaction.RollBack();
return Autodesk.Revit.UI.Result.Failed;
}
if ((Suffix == ".dwg") || (Suffix == ".dxf"))
{
DWGImportOptions DWGopt = new DWGImportOptions();
if (!Doc.Link(NewDocPathname, DWGopt, Doc.ActiveView, out ElemId))
{
TaskDialog.Show("Error","ErrorInsert");
ParentTransaction.RollBack();
return Autodesk.Revit.UI.Result.Failed;
}
}
else if (Suffix == ".dgn")
{
DGNImportOptions DGNopt = new DGNImportOptions();
DGNopt.DGNModelViewName = Path.GetFileName(NewDocPathname) + "-Model";
#if false
DGNopt.AutoCorrectAlmostVHLines = true;
DGNopt.ColorMode = ImportColorMode.Preserved;
DGNopt.Unit = ImportUnit.Default;
DGNopt.CustomScale = 1.0;
DGNopt.OrientToView = true;
DGNopt.Placement = ImportPlacement.Origin;
DGNopt.IgnoreUnsupportedElementWarning = false;
DGNopt.ThisViewOnly = false;
#endif
if (!Doc.Link(NewDocPathname, DGNopt, Doc.ActiveView, out ElemId)) <== Return always False
{
TaskDialog.Show("Error", "ErrorInsert");
ParentTransaction.RollBack();
return Autodesk.Revit.UI.Result.Failed;
}
}
else if ((Suffix == ".dwf") || (Suffix == ".dwfx"))
{
if (Doc.ActiveView.ViewType == ViewType.DrawingSheet)
{
IList<ElementId> ListView = new List<ElementId>();
ListView.Add(Doc.ActiveView.Id);
DWFImportOptions DWFopt = new DWFImportOptions(ListView);
Doc.Link(NewDocPathname, DWFopt);
}
else
{
TaskDialog.Show("Error", "InsertNOView");
ParentTransaction.RollBack();
return Autodesk.Revit.UI.Result.Failed;
}
}
else if (Suffix == ".sat")
{
SATImportOptions SATopt = new SATImportOptions();
Doc.Link(NewDocPathname, SATopt, Doc.ActiveView);
}
else if (Suffix == ".skp")
{
SKPImportOptions SKPopt = new SKPImportOptions();
Doc.Link(NewDocPathname, SKPopt, Doc.ActiveView);
}
}
else
{
ModelPath mp = ModelPathUtils.ConvertUserVisiblePathToModelPath(NewDocPathname);
RevitLinkOptions rlo = new RevitLinkOptions(true);
#if !RVT_2018
RevitLinkLoadResult linkType;
#else
LinkLoadResult linkType;
#endif
if (Suffix == ".ifc")
{
String NewDocRevitPathname = NewDocPathname + ".RVT";
Document DocIFC = revit.Application.Application.OpenIFCDocument(NewDocPathname);
SaveAsOptions SaveOpt = new SaveAsOptions();
SaveOpt.OverwriteExistingFile = true;
DocIFC.SaveAs(NewDocRevitPathname, SaveOpt);
DocIFC.Close();
#if !RVT_2018
RevitLinkLoadResult LoadlinkType = RevitLinkType.CreateFromIFC(Doc, NewDocPathname, NewDocRevitPathname, true, rlo);
#else
LinkLoadResult LoadlinkType = RevitLinkType.CreateFromIFC(Doc, NewDocPathname, NewDocRevitPathname, true, rlo);
#endif
RevitLinkInstance instance = RevitLinkInstance.Create(Doc, LoadlinkType.ElementId);
}
else
{
linkType = RevitLinkType.Create(Doc, mp, rlo);
RevitLinkInstance instance = RevitLinkInstance.Create(Doc, linkType.ElementId);
RevitLinkType rvtType = Doc.GetElement(instance.GetTypeId()) as RevitLinkType;
rvtType.AttachmentType = AttachmentType.Attachment;
}
}
Doc.Regenerate();
ParentTransaction.Commit();
}
catch (Autodesk.Revit.Exceptions.ArgumentNullException e)
{
TaskDialog.Show("Error", "ErrorInsert :" + e);
ParentTransaction.RollBack();
return Autodesk.Revit.UI.Result.Failed;
}
catch (Autodesk.Revit.Exceptions.ArgumentException e)
{
TaskDialog.Show("Error", "ErrorInsert :" + e);
ParentTransaction.RollBack();
return Autodesk.Revit.UI.Result.Failed;
}
catch (Autodesk.Revit.Exceptions.ModificationForbiddenException e)
{
TaskDialog.Show("Error", "ErrorInsert :" + e);
ParentTransaction.RollBack();
return Autodesk.Revit.UI.Result.Failed;
}
}
Doc.Link(NewDocPathname, DGNopt, Doc.ActiveView, out ElemId)) <== Return always False
Philippe.