// Try to get the server path for the particular model on the server Application application = uiApp.Application; String hostId = application.GetRevitServerNetworkHosts().First(); String rootFolder = "|"; ModelPath serverPath = FindWSAPIModelPathOnServer(application, hostId, rootFolder, "Wall pin model for updaters.rvt");
using (Transaction t = new Transaction(doc, "Create link")) { t.Start(); RevitLinkOptions options = new RevitLinkOptions(false);
LinkLoadResult result = RevitLinkType.Create(doc, serverPath, options);
privatestatic ModelPath FindWSAPIModelPathOnServer(Application app, string hostId, string folderName, string fileName) { // Connect to host to find list of available models (the "/contents" flag) XmlDictionaryReader reader = GetResponse(app, hostId, folderName + "/contents"); bool found = false;
// Look for the target model name in top level folder List folders = new List(); while (reader.Read()) { // Save a list of subfolders, if found if (reader.NodeType == XmlNodeType.Element && reader.Name == "Folders") { while (reader.Read()) { if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "Folders") break;
if (reader.NodeType == XmlNodeType.Element && reader.Name == "Name") { reader.Read(); folders.Add(reader.Value); } } } // Check for a matching model at this folder level if (reader.NodeType == XmlNodeType.Element && reader.Name == "Models") { found = FindModelInServerResponseJson(reader, fileName); if (found) break; } }
reader.Close();
// Build the model path to match the found model on the server if (found) { // Server URLs use "|" for folder separation, Revit API uses "/" String folderNameFragment = folderName.Replace('|', '/');
// Add trailing "/" if not present if (!folderNameFragment.EndsWith("/")) folderNameFragment += "/";
// This string is different for each RevitServer version privatestaticstring s_revitServerVersion = "/RevitServerAdminRESTService2014/AdminRESTService.svc/";
// Read the response XmlDictionaryReaderQuotas quotas = new XmlDictionaryReaderQuotas(); XmlDictionaryReader jsonReader = JsonReaderWriterFactory.CreateJsonReader(request.GetResponse().GetResponseStream(), quotas);
return jsonReader; }
privatestaticboolFindModelInServerResponseJson(XmlDictionaryReader reader, string fileName) { // Read through entries in this section while (reader.Read()) { if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "Models") break;
if (reader.NodeType == XmlNodeType.Element && reader.Name == "Name") { reader.Read(); String modelName = reader.Value; if (modelName.Equals(fileName)) { // Match found, stop looping and return returntrue; } } }
publicboolCreateRevitLinkWithOneWorksetOpen(Document doc, string pathName, string worksetName) { FilePath path = new FilePath(pathName); RevitLinkOptions options = new RevitLinkOptions(true);
// Get info on all the user worksets in the project prior to opening IList worksets = WorksharingUtils.GetUserWorksetInfo(path); IList worksetIds = new List(); // Find worksetName foreach (WorksetPreview worksetPrev in worksets) { if (worksetPrev.Name.CompareTo(worksetName) == 0) { worksetIds.Add(worksetPrev.Id); break; } }
// close all worksets but the one specified WorksetConfiguration worksetConfig = new WorksetConfiguration(WorksetConfigurationOption.CloseAllWorksets); if (worksetIds.Count > 0) { worksetConfig.Open(worksetIds); } options.SetWorksetConfiguration(worksetConfig);
LinkLoadResult result = RevitLinkType.Create(doc, path, options); RevitLinkType type = doc.GetElement(result.ElementId) as RevitLinkType; return (result.LoadResult == LinkLoadResultType.LinkLoaded); }
publicvoidSelectElementsInLinkedDoc(Autodesk.Revit.DB.Document document) { UIDocument uidoc = new UIDocument(document); Selection choices = uidoc.Selection; // Pick one wall from Revit link WallInLinkSelectionFilter wallFilter = new WallInLinkSelectionFilter(); Reference elementRef = choices.PickObject(ObjectType.LinkedElement, wallFilter, "Select a wall in a linked document"); if (elementRef != null) { TaskDialog.Show("Revit", "Element from link document selected."); } }
// This filter allows selection of only a certain element type in a link instance. classWallInLinkSelectionFilter : ISelectionFilter { private RevitLinkInstance m_currentInstance = null;
publicboolAllowElement(Element e) { // Accept any link instance, and save the handle for use in AllowReference() m_currentInstance = e as RevitLinkInstance; return (m_currentInstance != null); }
publicboolAllowReference(Reference refer, XYZ point) { if (m_currentInstance == null) returnfalse;
// Get the handle to the element in the link Document linkedDoc = m_currentInstance.GetLinkDocument(); Element elem = linkedDoc.GetElement(refer.LinkedElementId);
// Accept the selection if the element exists and is of the correct type return elem != null && elem is Wall; } }
voidUnloadRevitLinks(ModelPath location) /// This method will set all Revit links to be unloaded the next time the document at the given location is opened. /// The TransmissionData for a given document only contains top-level Revit links, not nested links. /// However, nested links will be unloaded if their parent links are unloaded, so this function only needs to look at the document's immediate links. { // access transmission data in the given Revit file TransmissionData transData = TransmissionData.ReadTransmissionData(location); if (transData != null) { // collect all (immediate) external references in the model ICollection externalReferences = transData.GetAllExternalFileReferenceIds(); // find every reference that is a link foreach (ElementId refId in externalReferences) { ExternalFileReference extRef = transData.GetLastSavedReferenceData(refId); if (extRef.ExternalFileReferenceType == ExternalFileReferenceType.RevitLink) { // we do not want to change neither the path nor the path-type // we only want the links to be unloaded (shouldLoad = false) transData.SetDesiredReferenceData(refId, extRef.GetPath(), extRef.PathType, false); } }
// make sure the IsTransmitted property is set transData.IsTransmitted = true; // modified transmission data must be saved back to the model TransmissionData.WriteTransmissionData(location, transData); } else { Autodesk.Revit.UI.TaskDialog.Show("Unload Links", "The document does not have any transmission data"); } }
[TransactionAttribute(Autodesk.Revit.Attributes.TransactionMode.Manual)] publicclassRevitCommand : IExternalCommand { public Result Execute(ExternalCommandData commandData, refstring messages, ElementSet elements) { UIApplication app = commandData.Application; Document doc = app.ActiveUIDocument.Document; Transaction trans = new Transaction(doc, "ExComm"); trans.Start(); ServerPath serverPath = new ServerPath("SHACNG035WQRP", "testmodel.rvt"); TransmissionData transData = TransmissionData.ReadTransmissionData(serverPath); string mymessage = null; if (transData != null) { //access the data in the transData here. } else { Autodesk.Revit.UI.TaskDialog.Show("Unload Links", "The document does not have any transmission data"); } trans.Commit(); return Result.Succeeded; } }