/// <summary> /// Return a `StableRepresentation` for a linked wall's exterior face. /// </summary> public string GetFaceRefRepresentation( Wall wall, Document doc, RevitLinkInstance instance ) { Reference faceRef = HostObjectUtils.GetSideFaces( wall, ShellLayerType.Exterior ).FirstOrDefault(); Reference stRef = faceRef.CreateLinkReference( instance ); string stable = stRef.ConvertToStableRepresentation( doc ); return stable; }
public void AddFaceBasedFamilyToLinks(Document doc) { ElementId alignedLinkId = new ElementId(125929); // Get symbol ElementId symbolId = new ElementId(126580); FamilySymbol fs = doc.GetElement(symbolId) as FamilySymbol; // Aligned RevitLinkInstance linkInstance = doc.GetElement( alignedLinkId) as RevitLinkInstance; Document linkDocument = linkInstance .GetLinkDocument(); FilteredElementCollector wallCollector = new FilteredElementCollector(linkDocument); wallCollector.OfClass(typeof(Wall)); Wall targetWall = wallCollector.FirstElement() as Wall; Reference exteriorFaceRef = HostObjectUtils.GetSideFaces( targetWall, ShellLayerType.Exterior) .First <Reference>(); Reference linkToExteriorFaceRef = exteriorFaceRef.CreateLinkReference( linkInstance); Line wallLine = (targetWall.Location as LocationCurve).Curve as Line; XYZ wallVector = (wallLine.GetEndPoint(1) - wallLine.GetEndPoint(0)).Normalize(); using (Transaction t = new Transaction(doc)) { t.Start("Add to face"); doc.Create.NewFamilyInstance( linkToExteriorFaceRef, XYZ.Zero, wallVector, fs); t.Commit(); } }
/***************************************************/ /**** Private methods ****/ /***************************************************/ private static XYZ CeilingPatternAlignment(this Ceiling ceiling, Material material, RevitSettings settings, out double rotation) { rotation = 0; if (ceiling == null || material == null) { return(null); } Document doc = ceiling.Document; FillPatternElement fillPatternElement; #if (REVIT2020 || REVIT2021) fillPatternElement = doc.GetElement(material.SurfaceForegroundPatternId) as FillPatternElement; #else fillPatternElement = doc.GetElement(material.SurfacePatternId) as FillPatternElement; #endif FillPattern fp = fillPatternElement?.GetFillPattern(); if (fp == null || fp.GridCount != 2) { return(null); } XYZ result = null; settings = settings.DefaultIfNull(); Options o = new Options(); o.ComputeReferences = true; Document hostDoc = doc.IsLinked ? doc.HostDocument() : doc; RevitLinkInstance linkInstance = doc.IsLinked ? doc.LinkInstance() : null; Transform linkTransform = linkInstance == null ? Transform.Identity : linkInstance.GetTotalTransform(); foreach (GeometryObject go in ceiling.get_Geometry(o)) { if (go is Solid) { foreach (Autodesk.Revit.DB.Face f in ((Solid)go).Faces) { PlanarFace pf = f as PlanarFace; if (pf == null) { continue; } if (1 + pf.FaceNormal.DotProduct(XYZ.BasisZ) > settings.AngleTolerance) { continue; } Reference faceRef = f.Reference; if (doc.IsLinked) { faceRef = faceRef.CreateLinkReference(linkInstance).PrepareForLinkDimensioning(hostDoc); } string stableRef = faceRef.ConvertToStableRepresentation(hostDoc); ReferenceArray horR = new ReferenceArray(); horR.Append(Reference.ParseFromStableRepresentation(hostDoc, stableRef + "/2")); horR.Append(Reference.ParseFromStableRepresentation(hostDoc, stableRef + "/" + (2 + fp.GridCount * 2).ToString())); ReferenceArray verR = new ReferenceArray(); verR.Append(Reference.ParseFromStableRepresentation(hostDoc, stableRef + "/1")); verR.Append(Reference.ParseFromStableRepresentation(hostDoc, stableRef + "/" + (1 + fp.GridCount * 2).ToString())); using (Transaction t = new Transaction(hostDoc, "temp dim")) { t.Start(); Dimension horDim = hostDoc.Create.NewDimension(hostDoc.ActiveView, Autodesk.Revit.DB.Line.CreateBound(XYZ.Zero, pf.XVector), horR); Dimension verDim = hostDoc.Create.NewDimension(hostDoc.ActiveView, Autodesk.Revit.DB.Line.CreateBound(XYZ.Zero, pf.YVector), verR); ElementTransformUtils.MoveElement(hostDoc, horDim.Id, XYZ.BasisX); ElementTransformUtils.MoveElement(hostDoc, verDim.Id, XYZ.BasisX); rotation = -(horDim.Curve as Autodesk.Revit.DB.Line).Direction.AngleOnPlaneTo(XYZ.BasisX, XYZ.BasisZ); rotation += linkTransform.BasisX.AngleOnPlaneTo(XYZ.BasisX, XYZ.BasisZ); Transform tr = Transform.CreateRotation(XYZ.BasisZ, rotation); double x = tr.Inverse.OfPoint(linkTransform.Inverse.OfPoint(horDim.Origin)).X; double y = tr.Inverse.OfPoint(linkTransform.Inverse.OfPoint(verDim.Origin)).Y; t.RollBack(); foreach (FillGrid fg in fp.GetFillGrids()) { if (fg.Angle.ToSI(UnitType.UT_Angle) > settings.AngleTolerance) { y += fg.Offset * 0.5; } else { x += fg.Offset * 0.5; } } result = tr.OfPoint(new XYZ(x, y, 0)); break; } } } } return(result); }