Dear @jeremytammik ,
Thanks for your great tutorial. I have success with the code as following image:

For the code, first I made the reference code from your guide:
static public Transform GetProjectLocationTransform(Document doc)
{
// Retrieve the active project location position.
ProjectPosition projectPosition
= doc.ActiveProjectLocation.GetProjectPosition(
XYZ.Zero);
// Create a translation vector for the offsets
XYZ translationVector = new XYZ(
projectPosition.EastWest,
projectPosition.NorthSouth,
projectPosition.Elevation);
Transform translationTransform
= Transform.CreateTranslation(
translationVector);
// Create a rotation for the angle about true north
//Transform rotationTransform
// = Transform.get_Rotation( XYZ.Zero,
// XYZ.BasisZ, projectPosition.Angle );
Transform rotationTransform
= Transform.CreateRotation(
XYZ.BasisZ, projectPosition.Angle);
// Combine the transforms
Transform finalTransform
= translationTransform.Multiply(
rotationTransform);
return finalTransform;
}
static public bool GetBasePoint( Document doc, out XYZ basePoint, out double north)
{
BuiltInParameter[] bip = new[] {
BuiltInParameter.BASEPOINT_EASTWEST_PARAM,
BuiltInParameter.BASEPOINT_NORTHSOUTH_PARAM,
BuiltInParameter.BASEPOINT_ELEVATION_PARAM,
BuiltInParameter.BASEPOINT_ANGLETON_PARAM
};
FilteredElementCollector col
= new FilteredElementCollector(doc)
.OfClass(typeof(BasePoint));
Parameter p = null;
basePoint = null;
north = 0;
foreach (BasePoint bp in col)
{
basePoint = new XYZ(
bp.get_Parameter(bip[0]).AsDouble(),
bp.get_Parameter(bip[1]).AsDouble(),
bp.get_Parameter(bip[2]).AsDouble());
p = bp.get_Parameter(bip[3]);
if (null != p)
{
north = p.AsDouble();
Debug.Print("north {0}", north);
break;
}
}
return null != p;
}
And then the main Code:
string PileFamilyName = "SelectedFamily";
string XCoordinateParaName = "X_Coordinate";
string YCoordinateParaName = "Y_Coordinate";
int unit = "SelectedUnit";
int precesion = "SelectedPrecesion";
FilteredElementCollector FamilyInstanceCollector = new FilteredElementCollector(document, currentview.Id);
List<FamilyInstance> AllPilesInstance = FamilyInstanceCollector.OfClass(typeof(FamilyInstance)).Cast<FamilyInstance>().ToList()
.Where(a => a.Category.Name + "_" + a.Symbol.FamilyName == PileFamilyName).ToList();
if (AllPilesInstance.Count > 0)
{
XYZ basePoint;
double north;
refcode.GetBasePoint(document, out basePoint, out north);
Transform projectLocationTransform = refcode.GetProjectLocationTransform(document);
using (Transaction t = new Transaction(document, "Write Coordinate"))
{
t.Start();
foreach (FamilyInstance singlePile in AllPilesInstance)
{
LocationPoint point = singlePile.Location as LocationPoint;
XYZ r2 = projectLocationTransform.OfPoint(point.Point);
double Xcoordinate = 0;
double Ycoordinate = 0;
if(unit==0)
{
Xcoordinate = Math.Round( UnitUtils.ConvertFromInternalUnits(r2.X, DisplayUnitType.DUT_METERS),precesion);
Ycoordinate = Math.Round( UnitUtils.ConvertFromInternalUnits(r2.Y, DisplayUnitType.DUT_METERS),precesion);
}
if(unit==1)
{
Xcoordinate = Math.Round(UnitUtils.ConvertFromInternalUnits(r2.X, DisplayUnitType.DUT_MILLIMETERS),precesion);
Ycoordinate = Math.Round( UnitUtils.ConvertFromInternalUnits(r2.Y, DisplayUnitType.DUT_MILLIMETERS),precesion);
}
Parameter XcoordinatePara = singlePile.Parameters.Cast<Parameter>().ToList().Where(a => a.Definition.Name == XCoordinateParaName).First();
Parameter YcoordinatePara = singlePile.Parameters.Cast<Parameter>().ToList().Where(a => a.Definition.Name == YCoordinateParaName).First();
if (XcoordinatePara.StorageType == StorageType.Double)
{
XcoordinatePara.Set(Xcoordinate);
}
else
{
if (XcoordinatePara.StorageType == StorageType.String)
{
XcoordinatePara.Set(Xcoordinate.ToString());
}
}
if (YcoordinatePara.StorageType == StorageType.Double)
{
YcoordinatePara.Set(Ycoordinate);
}
else
{
if (YcoordinatePara.StorageType == StorageType.String)
{
YcoordinatePara.Set(Ycoordinate.ToString());
}
}
}
t.Commit();
}
TaskDialog.Show("Congrats", "Task is accomplished");
I hope that this will help other user to save times for this insance task (if manually do it) :D.
Thanks again @jeremytammik