Update Linked Files Path

Update Linked Files Path

john.allanjones
Explorer Explorer
1,947 Views
2 Replies
Message 1 of 3

Update Linked Files Path

john.allanjones
Explorer
Explorer

 

Hello,

 

I am trying to update an linked files path in Revit 2014 using a IExternalApplication. This code runs all the way trough with no errors but the link is not changed, It dosen't seem to actually commit the changes, any suggestions please ?

 

Many thanks in advance

 

 

 

 private static void LinksUpdate(ModelPath path, DocRvt doc)
        {
            // access transmission data in the given Revit file
            TransmissionData transData = TransmissionData.ReadTransmissionData(path);
            if (transData != null)
            {
                // collect all (immediate) external references in the model
                ICollection<ElementId> 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)
                    {
                        bool ShouldLoad;
                        if (extRef.GetLinkedFileStatus().ToString() == "Loaded")
                        {
                            ShouldLoad = true;
                        }
                        else
                        {
                            ShouldLoad = false;
                        }

                        using (Transaction trans = new Transaction(doc, "Links Updater"))
                        {
                            trans.Start();

                            ModelPath ExsiitngModelPath = extRef.GetAbsolutePath();
                            string ExistingStringPath = ModelPathUtils.ConvertModelPathToUserVisiblePath(ExsiitngModelPath);

                            string NewStringPath = ExistingStringPath.Replace(@"Testing\A", @"Testing\B");

                            ModelPath NewPath = ModelPathUtils.ConvertUserVisiblePathToModelPath(NewStringPath);
                            PathType NewPathType = PathType.Relative;

                            transData.SetDesiredReferenceData(refId, NewPath, NewPathType, ShouldLoad);
                            transData.IsTransmitted = true;

                            TransmissionData.WriteTransmissionData(path, transData);
                            
                            trans.Commit();
                        }
                    }
                }                
            }
            else
            {
                TaskDialog.Show("Unload Links", "The document does not have any transmission data");
            }
        }

 

0 Likes
1,948 Views
2 Replies
Replies (2)
Message 2 of 3

jeremytammik
Autodesk
Autodesk

Dear John,

 

Thank you for your query.

 

I am pretty sure that you do not need to open or commit any transactions for this operation, since the TransmissionData is not a Revit database element.  The call to WriteTransmissionData takes care of saving the data back to the external file.

 

I think the TransmissionData is part of the external file.

 

From you question, you seem to want to update the link to it saved in the currenly open Revit project.

 

For that, you should probably be looking at the RevitLinkInstance class instead, which represents an instance of a RevitLinkType, i.e., a linked file within the project.

 

Here is an old post of its path property:

 

http://adndevblog.typepad.com/aec/2012/10/accessing-linked-file-path-information-using-revit-net-api...

 

The ExternalFileReference.GetPath method read and returns the path of the link, relative or absolute according to the link's settings

 

That all seems to be read-only, though.

 

Somebody seems to have tried to achieve the same as you in the past:

 

http://forums.autodesk.com/t5/revit-api/find-and-replace-linked-revit-and-autocad-file-paths/td-p/35...

 

The suggestions there lead back to the transmission data approach.

 

Have you tried modifying the transmission data information without having the project loaded?

 

Maybe the changes that you set up will only be noticeable if the file is not loaded, and you can see the effect next time you load it.

 

I hope this helps.

 

Please let us know how you get on with this and what solution you end up with.

 

Thank you!

 

Best regards,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 3 of 3

NonicaTeam
Enthusiast
Enthusiast

A few years later, I have been working with this and found a way that seems to work, leave it here if it can of help to anyone out there;


1. I open the doc and extract the RevitLinkType with a FEC.

2. Then save and close.

3. After closing with no transaction:

	def updateLinksPathInDoc(self, file_path, all_paths_and_ids):
		try:
			model_ini_path = ModelPathUtils.ConvertUserVisiblePathToModelPath(file_path);		
			transData = TransmissionData.ReadTransmissionData(model_ini_path)
			if transData:
				for path, id in all_paths_and_ids:
					model_path = ModelPathUtils.ConvertUserVisiblePathToModelPath(path);
					transData.SetDesiredReferenceData(id, model_path, PathType.Absolute, True);
					transData.IsTransmitted = True
				TransmissionData.WriteTransmissionData(model_ini_path, transData)	
		except Exception as e:
			Autodesk.Revit.UI.TaskDialog.Show("Exception Extracting Linked Documents", str(e) + " - " + str(sys.exc_info()[-1].tb_lineno))