Location Point is returning null value. Please anyone provide the solution .

Location Point is returning null value. Please anyone provide the solution .

jadhavabhi309
Explorer Explorer
367 Views
2 Replies
Message 1 of 3

Location Point is returning null value. Please anyone provide the solution .

jadhavabhi309
Explorer
Explorer

The code appears to aim at selecting multiple text elements and then arranging them with a specific horizontal distance between them.

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.Creation;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;


namespace Demo1
{
[Transaction(TransactionMode.Manual)]
public class MoveTextInstancesEquallyHorizontalCommand : IExternalCommand
{
public Result Execute(
ExternalCommandData commandData,
ref string message,
ElementSet elements)
{
try
{
UIApplication uiApp = commandData.Application;
UIDocument uiDoc = uiApp.ActiveUIDocument;
Autodesk.Revit.DB.Document doc = uiDoc.Document;

// Select the text instances you want to move
IList<Reference> selectedRefs = uiDoc.Selection.PickObjects(ObjectType.Element, "Select text instances");

if (selectedRefs.Count < 2)
{
TaskDialog.Show("Error", "Select at least two text instances.");
return Result.Cancelled;
}

// Calculate the desired equal horizontal distance between text instances
double desiredHorizontalDistance = 50.0; // Change this value as needed

using (Transaction tx = new Transaction(doc, "Move Text Instances Equally Horizontal"))
{
tx.Start();

// Sort selected text instances by their X-coordinates
List<Element> selectedTextInstances = new List<Element>();
foreach (Reference reference in selectedRefs)
{
Element textElement = doc.GetElement(reference);
selectedTextInstances.Add(textElement);
}

selectedTextInstances.Sort((elem1, elem2) =>
{
XYZ point1 = (elem1.Location as LocationPoint).Point;
//XYZ point1 = elem1.Coord.ToXYZ();
XYZ point2 = (elem2.Location as LocationPoint).Point;
return point1.X.CompareTo(point2.X);
});

// Sort selected text instances by their X-coordinates
//List<Element> selectedTextInstances = new List<Element>();
//foreach (Reference reference in selectedRefs) { Element textElement = doc.GetElement(reference);
// selectedTextInstances.Add(textElement); }
//selectedTextInstances.Sort((elem1, elem2) => { LocationPoint location1 = elem1.Location as LocationPoint;
// LocationPoint location2 = elem2.Location as LocationPoint;
// XYZ point1 = location1.Point;
// XYZ point2 = location2.Point;
// return point1.X.CompareTo(point2.X); });

// Sort selected text instances by their X-coordinates
//List<Element> selectedTextInstances = new List<Element>();
//foreach (Reference reference in selectedRefs)
//{
// Element textElement = doc.GetElement(reference);
// selectedTextInstances.Add(textElement);
//}

 

//selectedTextInstances.Sort((elem1, elem2) =>
//{
// LocationPoint location1 = elem1.Location as LocationPoint;
// LocationPoint location2 = elem2.Location as LocationPoint;

 

// // Check if the location is a LocationPoint before accessing it
// if (location1 != null && location2 != null)
// {
// XYZ point1 = location1.Point;
// XYZ point2 = location2.Point;

 

// return point1.X.CompareTo(point2.X);
// }
// else
// {
// // Handle the case where the location is not a LocationPoint
// return 0; // You can modify this value as needed
// }
//});

//List<AnnotationElement> annotationElements = RetriveAnnotationElementsFromSelection(doc, tx, selectedRefs);

//List<AnnotationElement> sortedAnnotationElements = annotationElements.OrderBy(x => x.UpRight.X).ToList();
//rightAnnotation = sortedAnnotationElements.LastOrDefault();
//leftAnnotation = sortedAnnotationElements.FirstOrDefault();
//spacing = (rightAnnotation.Center.X - leftAnnotation.Center.X) / (annotationElements.Count - 1);
//i = 0;
//foreach (AnnotationElement annotationElement in sortedAnnotationElements)
//{
// XYZ resultingPoint = new XYZ(leftAnnotation.Center.X + i * spacing, annotationElement.Center.Y, 0);
// annotationElement.MoveTo(resultingPoint, AlignType.Horizontally);
// i++;
//}

// Sort selected text instances by their X-coordinates
//selectedTextInstances.Sort((elem1, elem2) =>
//{
// XYZ point1;
// if (elem1.Location is LocationPoint location1)
// {
// point1 = location1.Point;
// }
// else
// {
// // Handle the case where elem1 does not have a LocationPoint
// // For example, you might want to skip or log these cases
// return 0; // Or any other value that makes sense for your case
// }

// XYZ point2;
// if (elem2.Location is LocationPoint location2)
// {
// point2 = location2.Point;
// }
// else
// {
// // Handle the case where elem2 does not have a LocationPoint
// // For example, you might want to skip or log these cases
// return 0; // Or any other value that makes sense for your case
// }

// return point1.X.CompareTo(point2.X);
//});

// Calculate the movement vector based on desired horizontal distance
XYZ firstPoint = (selectedTextInstances[0].Location as LocationPoint).Point;
XYZ lastPoint = (selectedTextInstances[selectedTextInstances.Count - 1].Location as LocationPoint).Point;
XYZ movementVector = new XYZ(desiredHorizontalDistance, 0, 0);
double totalDistance = lastPoint.X - firstPoint.X;

// Move each text instance horizontally by the calculated movement vector
for (int i = 0; i < selectedTextInstances.Count; i++)
{
Element textElement = selectedTextInstances[i];
LocationPoint textLocation = textElement.Location as LocationPoint;
XYZ currentPoint = textLocation.Point;

XYZ newPosition = currentPoint + movementVector * (i * desiredHorizontalDistance / totalDistance);
ElementTransformUtils.MoveElement(doc, textElement.Id, newPosition - currentPoint);
}

tx.Commit();
}

return Result.Succeeded;
}
catch (Exception ex)
{
message = ex.Message;
return Result.Failed;
}
}
}
}

0 Likes
368 Views
2 Replies
Replies (2)
Message 2 of 3

jeremy_tammik
Alumni
Alumni

It seems that you asked a similar question on StackOverflow that I answered yesterday:

  

  

I repeat my answer from there:

  

The Location property of a text element can consist of multiple points, depending on the text alignment settings. Therefore, the property value cannot be represented in either LocationPoint or LocationCurve. Therefore, unfortunately, the Revit API does not provide any possibility to either read or write this property.

 

   

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 3 of 3

rhanzlick
Advocate
Advocate

I didn't see this on stackoverflow, but it seems you have a few options that would get you SOME TYPE of representation of a location...

1. you can calculate the mean of the text boundingboxXYZ (0.5*max+min)

2. you can analyze the leaders and look at their end/anchor points

3. You can use a combination of the TextElement.Coord which seems to give something meaningful

 

those are all the options I can think of. Hopefully one of them helps.

 

Please be respectful of others' time. If @jeremy_tammik already answered you through one forum and the answer didn't make sense, why not respond to his answer on the respective forum and ask him to elaborate?