Rotate an element around XYZ at end point

Rotate an element around XYZ at end point

minix6800
Community Visitor Community Visitor
100 Views
2 Replies
Message 1 of 3

Rotate an element around XYZ at end point

minix6800
Community Visitor
Community Visitor

Hi,

I need your expert views / your help on converting the input angle (string) used to rotate an element to internal units. I am using Revit.ScriptCS.ScriptRunner addin to try / check my code

using Autodesk.Revit.UI.Selection;

Selection sel = uidoc.Selection;
ICollection<ElementId> selectedIds=uidoc.Selection.GetElementIds();
Reference r = sel.PickObject(ObjectType.Element, "Pick element to rotate");
var elementId = r.ElementId;
var element = doc.GetElement(elementId);
Location location = element.Location;
 List<Line> allAxis = new List<Line>();
if (location is LocationCurve)
{
    LocationCurve locationCurve = element.Location as LocationCurve;
    Curve curve = locationCurve.Curve as Line;
    allAxis.Add(Line.CreateBound(curve.GetEndPoint(0), curve.GetEndPoint(0).Add (XYZ.BasisX)));
    allAxis.Add(Line.CreateBound(curve.GetEndPoint(0), curve.GetEndPoint(0).Add (XYZ.BasisY)));
    allAxis.Add(Line.CreateBound(curve.GetEndPoint(0), curve.GetEndPoint(0).Add (XYZ.BasisZ)));  
}
else 
{
    Instance ins = doc.GetElement(r) as Instance;
    Transform transform = ins.GetTransform();
    allAxis.Add(Line.CreateBound(transform.Origin, transform.Origin.Add(XYZ.BasisX)));
    allAxis.Add(Line.CreateBound(transform.Origin, transform.Origin.Add(XYZ.BasisY)));
    allAxis.Add(Line.CreateBound(transform.Origin, transform.Origin.Add(XYZ.BasisZ)));
}

var angelUnit = doc.GetUnits().GetFormatOptions(SpecTypeId.Angle).GetUnitTypeId();
List<double> internalAngles = new List<double>();
// Angles used to rotate the elemnet (x_angle, y_angle, z_angle)
var angles = new List<string>{"45","0","45"};
// convert from string to internal
foreach (var angle in angles)
{
    var intagel = double.Parse(angle);
    internalAngles.Add(UnitUtils.ConvertToInternalUnits(intagel, angelUnit));
    
}
 Transaction transaction = new Transaction(doc);
 transaction.Start("Rotate Elelemnt");
for (int i = 0; i<3; i++)
{
    ElementTransformUtils.RotateElement(doc, elementId, allAxis[i],  internalAngles[i]);   
}
transaction.Commit();

 

all seems to be working ok, but I would like to know if this is the correct / reliable approch to convert from string to internal units.

 

Also, is there a better way to check if the element will return a transform, rather than checking the locationg to see if its is LocationPoint or LocationCurve?

 

Thanks for your help in advance

 

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

halukuzuner
Advocate
Advocate

I can't concentrate on your code for now. Long time ago I wrote a code which worked perfectly. It may help you. 
https://thebuildingcoder.typepad.com/blog/2022/09/aligning-connectors.html

Message 3 of 3

minix6800
Community Visitor
Community Visitor

Thanks, I will have a look

0 Likes