Issue with restoring references for dimensions via StableRepresentation.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Tested on Revit 2024.3.30.11 and 2026.4.0.32:
I have created the following rebar U shape, consisting of 3 segments in various orientations, clockwise and counterclockwise.
I have then created dimensions for this shape measuring the distance from the end/start of the bar to the outermost tangent of the arc section.
When converting these dimensions references to stable representation strings and back again, the behavior in the picture below is observed, where SOME references seem to attach to the wrong side of the circle used to create the arc section:
This is the code used to test with. To replicate, draw the rebar shapes as above, create a dimension as described in a section view. Then duplicate the dimension by activating this code via a button.
public class TestCommand : IExternalCommand
{
private UIDocument _uiDocument;
private Document _document;
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
_document = commandData.Application.ActiveUIDocument.Document;
_uiDocument = commandData.Application.ActiveUIDocument;
//Elements
var dimension = PickDimensionToCopy();
var view = _uiDocument.ActiveView;
if (dimension is null || view is null) return Result.Cancelled;
//Convert references to stable representations:
var stableRepresentations = new List<string>();
foreach (Reference reference in dimension.References)
{
var stableRepresentation = reference.ConvertToStableRepresentation(_document);
stableRepresentations.Add(stableRepresentation);
}
//Convert back to references:
ReferenceArray referenceArray = new ReferenceArray();
foreach (var stableRepresentation in stableRepresentations)
{
var reference = Reference.ParseFromStableRepresentation(_document, stableRepresentation);
referenceArray.Append(reference);
}
//Make a copy of original dimension
using (Transaction transaction = new Transaction(_document, "Restore dimension."))
{
transaction.Start();
Dimension dimensionCopy = _document.Create.NewDimension(
view,
Line.CreateUnbound(dimension.Origin, (dimension.Curve as Line).Direction),
referenceArray,
dimension.DimensionType);
transaction.Commit();
}
return Result.Succeeded;
}
private Dimension? PickDimensionToCopy()
{
try
{
var reference = _uiDocument.Selection.PickObject(ObjectType.Element, new SelectionFilterDimension(), "Select dimension.");
return _document.GetElement(reference) as Dimension;
}
catch (Autodesk.Revit.Exceptions.OperationCanceledException)
{
return null;
}
}
}
public class SelectionFilterDimension : ISelectionFilter
{
public bool AllowElement(Element element)
{
if (element is Dimension) return true;
return false;
}
public bool AllowReference(Reference reference, XYZ position)
{
return false;
}
}