Community
Hi
I want to get these lines of a ceiling grid from linked file
I tried this way but it seems not to be working when the ceiling is in a linked file
https://forums.autodesk.com/t5/revit-api-forum/dimension-on-hatch-pattern-slab/m-p/7063302/highlight...
private void test(Document ActiveDoc, Ceiling elem)
{
Reference r = HostObjectUtils.GetBottomFaces(elem).FirstOrDefault();
PlanarFace face = elem.GetGeometryObjectFromReference(r) as PlanarFace;
XYZ corner = null;
foreach (Curve cv in face.GetEdgesAsCurveLoops().FirstOrDefault())
{
corner = cv.GetEndPoint(0);
break;
}
var HatchLines = AnalyzeHatch(ActiveDoc, elem, r);
if (HatchLines.Count > 0)
{
using (TransactionGroup tg = new TransactionGroup(ActiveDoc, "align2RefPlane"))
{
tg.Start();
foreach (var HLine in HatchLines)
{
ReferencePlane pl = null;
using (Transaction t = new Transaction(ActiveDoc, "CreateRefPlane"))
{
t.Start();
pl = ActiveDoc.Create.NewReferencePlane(HLine.Item3.Add(HLine.Item4.Multiply(3)), HLine.Item3, face.FaceNormal.Multiply(3), doc.ActiveView);
pl.Name = string.Format("{0}_{1}", "ref", Guid.NewGuid());
t.Commit();
}
string stableRef = string.Format("{0}:0:{1}", pl.UniqueId, "SURFACE");
Reference ref2Plane = Reference.ParseFromStableRepresentation(doc, stableRef);
using (Transaction t = new Transaction(ActiveDoc, "align2RefPlane"))
{
t.Start();
ActiveDoc.Create.NewAlignment(ActiveDoc.ActiveView, ref2Plane, HLine.Item2);
t.Commit();
}
using (Transaction t = new Transaction(ActiveDoc, "MovePlane"))
{
t.Start();
XYZ translation = HLine.Item3.Subtract(corner);
ElementTransformUtils.MoveElement(ActiveDoc, pl.Id, translation);
t.Commit();
}
}
tg.Assimilate();
}
}
}
/// <summary>
/// Analyse the hatch
/// </summary>
/// <param name="elem">An element with surface hatch.</param>
/// <param name="hatchface">The reference to the face to analyse.</param>
/// <returns>Returns:
/// foreach hatchline in hatchpattern
/// item1 = index of hatchline in hatchpattern
/// item2 = reference to the hatchline
/// item3 = point on hatchline
/// item4 = direction of hatchline.</returns>
List<Tuple<int, Reference, XYZ, XYZ>> AnalyzeHatch(Document ActiveDoc, Element elem, Reference hatchface)
{
var elemDocument = elem.Document;
//check for model surfacepattern
List<Tuple<int, Reference, XYZ, XYZ>> res = new List<Tuple<int, Reference, XYZ, XYZ>>();
PlanarFace face = elem.GetGeometryObjectFromReference(hatchface) as PlanarFace;
Material mat = elemDocument.GetElement(face.MaterialElementId) as Material;
FillPatternElement patterntype = elemDocument.GetElement(mat.SurfaceForegroundPatternId) as FillPatternElement;
if (patterntype == null) return res;
FillPattern pattern = patterntype.GetFillPattern();
if (pattern.IsSolidFill || pattern.Target == FillPatternTarget.Drafting) return res;
// get number of gridLines in pattern
int _gridCount = pattern.GridCount;
// construct StableRepresentations and find the Reference to HatchLines
string StableRef = hatchface.ConvertToStableRepresentation(elemDocument);
using (Transaction t = new Transaction(ActiveDoc, "analyse hatch"))
{
if (!ActiveDoc.IsModifiable)
{
t.Start();
}
for (int hatchindex = 0; hatchindex < _gridCount; hatchindex++)
{
ReferenceArray _resArr = new ReferenceArray();
for (int ip = 0; ip < 2; ip++)
{
int index = (hatchindex + 1) + (ip * _gridCount * 2);
string StableHatchString = StableRef + string.Format("/{0}", index);
Reference HatchRef = null;
try
{
HatchRef = Reference.ParseFromStableRepresentation(elemDocument, StableHatchString);
}
catch
{ }
if (HatchRef == null) continue;
_resArr.Append(HatchRef);
}
// 2 or more References => create dimension
if (_resArr.Size > 1)
{
using (SubTransaction st = new SubTransaction(ActiveDoc))
{
st.Start();
Dimension _dimension = ActiveDoc.Create.NewDimension(ActiveDoc.ActiveView, Line.CreateBound(XYZ.Zero, new XYZ(10, 0, 0)), _resArr);
// move dimension a tiny amount to orient the dimension perpendicular to the hatchlines
// I can't say why it works, but it does.
ElementTransformUtils.MoveElement(ActiveDoc, _dimension.Id, new XYZ(.1, 0, 0));
Reference r1 = _dimension.References.get_Item(0);
XYZ direction = (_dimension.Curve as Line).Direction;
XYZ hatchDirection = direction.CrossProduct(face.FaceNormal).Normalize();
XYZ origin = _dimension.Origin.Subtract(direction.Multiply((double)_dimension.Value / 2));
res.Add(new Tuple<int, Reference, XYZ, XYZ>(hatchindex, r1, origin, hatchDirection));
st.RollBack();
}
}
}
}
return res;
}
Can't find what you're looking for? Ask the community or share your knowledge.