Stable representation of reference is not the same as original
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
When trying to find solution to topic I posted here I found more general problem which seems that it is also connected to my original question. There I also have problem with CreateReferenceInLink but I don't do any conversion to stable representation so I opened new thread.
It looks that stable representation of reference or it parsing back don't restore all information from original. Check this code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Autodesk.Revit.DB; using Autodesk.Revit.Attributes; using Autodesk.Revit.UI; using System.Diagnostics; namespace CreateReferenceInLink { [Transaction(TransactionMode.Manual)] [Regeneration(RegenerationOption.Manual)] [Autodesk.Revit.DB.Macros.AddInId("37849cb3-79fb-4efd-b224-9c123eea191e")] public class CreateReferenceInLink: IExternalCommand { public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { Autodesk.Revit.ApplicationServices.Application m_revit; Document m_Document; try { m_revit = commandData.Application.Application; m_Document = commandData.Application.ActiveUIDocument.Document; FilteredElementCollector FamilyInstanceCollector = new FilteredElementCollector(m_Document); FamilyInstanceCollector.OfClass(typeof(FamilyInstance)); List<FamilyInstance> FamilyInstances = FamilyInstanceCollector.Cast<FamilyInstance>().ToList(); foreach (FamilyInstance fi in FamilyInstances) { Document linkedDocument= (m_Document.GetElement(fi.HostFace.ElementId) as RevitLinkInstance).GetLinkDocument(); string original = fi.HostFace.ConvertToStableRepresentation(m_Document); Debug.Print("Stable representation of original reference: " + original); Reference converted = Reference.ParseFromStableRepresentation(m_Document, original); string original1 = converted.ConvertToStableRepresentation(m_Document); Debug.Print("Stable representation of reference converted to string and back: " + original1); try { Debug.Print("Trying to CreateReferenceInLink from original reference..."); Reference rOriginal = fi.HostFace.CreateReferenceInLink(); Debug.Print("Succeded, string representation is " + rOriginal.ConvertToStableRepresentation(linkedDocument)); } catch(Exception e) { Debug.Print("FAILED with: " + e.Message); } try { Debug.Print("Trying to CreateReferenceInLink from reference converted to string and back..."); Reference rInLinkConverted = converted.CreateReferenceInLink(); Debug.Print("Succeded, string representation is " + rInLinkConverted.ConvertToStableRepresentation(linkedDocument)); } catch (Exception e) { Debug.Print("FAILED with: " + e.Message); } break; } return Result.Succeeded; } catch (Exception e) { message = e.ToString(); return Result.Failed; } } } }
If I run that code in a model where I have only one family instance hosted on face of element in linked file. I got the following output:
Stable representation of original reference: 2e14c424-28f1-434a-8881-3556ad3ae8b5-000cc8cb:0:RVTLINK:794084:6:SURFACE Stable representation of reference converted to string and back: 2e14c424-28f1-434a-8881-3556ad3ae8b5-000cc8cb:0:RVTLINK:794084:6:SURFACE Trying to CreateReferenceInLink from original reference... Succeded, string representation is cc961bf4-47ec-4689-b11a-19baa84ef814-000c1de4:6:SURFACE Trying to CreateReferenceInLink from reference converted to string and back... FAILED with: A managed exception was thrown by Revit or by one of its external applications.
As you can see I do exactly the same with original reference and reference converted to string (with ConvertToStableRepresentation) and back (with ParseFromStableRepresentation). In the case of "converted" reference, CreateReferenceInLink throws exception.
Why? Is this expected behaviour?
Žarko