Stable representation of reference is not the same as original

Stable representation of reference is not the same as original

ZarkoMocnik
Contributor Contributor
1,046 Views
3 Replies
Message 1 of 4

Stable representation of reference is not the same as original

ZarkoMocnik
Contributor
Contributor

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

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

jeremytammik
Autodesk
Autodesk

Dear Zarko,

 

Nope, I do not think this behaviour is expected.

 

Meanwhile, I have also seen and processed your subsequent ADN case 15853735 [CreateReferenceInLink throws exception when calling from reference created from StableRepresentation] and raised a ticket with the development team for that issue, REVIT-153657 [CreateReferenceInLink throws exception when called with StableRepresentation reference -- 15853735].
 

I'll let you know as soon as I hear back from the development team on that.

 

Best regards,

 

Jeremy

 



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

0 Likes
Message 3 of 4

ZarkoMocnik
Contributor
Contributor

Thanks Jeremy,

 

Would you be so kind and look also to the following thread which describes original problem I have with CreateReferenceInLink. Do you have any suggestion related to that problem (maybe the reason is the same as in this thread or I wrong understand the required procedure).

 

Kind regards,

Žarko

 

0 Likes
Message 4 of 4

jeremytammik
Autodesk
Autodesk

Yes. sure. I answered you in the thread you point to.

  

Best regards,

 

Jeremy

 



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

0 Likes