Announcements

The Autodesk Community Forums has a new look. Read more about what's changed on the Community Announcements board.

Aligning a FamilyInstance to Stacked wall's interior face

harilalmn
Advocate

Aligning a FamilyInstance to Stacked wall's interior face

harilalmn
Advocate
Advocate

Hi All,

I know stacked walls are not easy to work with. I, however, am trying to align a family instance, say a desk, to the interior face of a stacked wall. For getting the interior face, I am getting the interior face of the first wall of the stacked wall members, which is the bottom wall. But this approach throws error saying;

 

"The two references are not geometrically aligned"

 

Here is the code (I am in Macro environnment);

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

namespace FamilyInstanceStudy
{
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    [Autodesk.Revit.DB.Macros.AddInId("6380B2D3-2530-414B-9253-2BE5EDE6EA3A")]
	public partial class ThisDocument
	{
		UIDocument uidoc;
		Document doc;
		
		private void Module_Startup(object sender, EventArgs e)
		{
			uidoc = Application.ActiveUIDocument;
			doc = uidoc.Document;
		}

		private void Module_Shutdown(object sender, EventArgs e)
		{

		}

		#region Revit Macros generated code
		private void InternalStartup()
		{
			this.Startup += new System.EventHandler(Module_Startup);
			this.Shutdown += new System.EventHandler(Module_Shutdown);
		}
		#endregion
		public void AlignTest()
		{
			
			Wall stackedWall = doc.GetElement(uidoc.Selection.PickObject(ObjectType.Element, new WallFilter(), "Pick a stacked wall")) as Wall;
			
			FamilyInstance familyInstance = doc.GetElement(uidoc.Selection.PickObject(ObjectType.Element, new FamilyInstanceFilter(), "Pick the desk")) as FamilyInstance;
			
			Reference frontBack = familyInstance.GetReferences(FamilyInstanceReferenceType.CenterFrontBack).FirstOrDefault();
			
			
			if (stackedWall != null)
			{
				Wall bottomWall = doc.GetElement(stackedWall.GetStackedWallMemberIds().FirstOrDefault()) as Wall;
				
				Reference sideFaceInterior = HostObjectUtils.GetSideFaces(bottomWall, ShellLayerType.Interior).FirstOrDefault();
				
				using (Transaction t = new Transaction(doc, "al;ign family instance"))
				{
					t.Start();
					
					doc.Create.NewAlignment(doc.ActiveView, frontBack, sideFaceInterior);
					
					t.Commit();
				}
			}
		}
	}
	
	public class WallFilter : ISelectionFilter
	{
		
		
		public bool AllowElement(Element elem)
		{
			return elem is Wall && ((Wall)elem).IsStackedWall;
		}
		
		public bool AllowReference(Reference reference, XYZ position)
		{
			return false;
		}
	}

	public class FamilyInstanceFilter : ISelectionFilter
	{
		
		
		public bool AllowElement(Element elem)
		{
			return elem is FamilyInstance;
		}
		
		public bool AllowReference(Reference reference, XYZ position)
		{
			return false;
		}
	}

	
}

 

Revit_oE3exDlLef.png

 

0 Likes
Reply
Accepted solutions (1)
311 Views
2 Replies
Replies (2)

caroline.gitonga
Autodesk
Autodesk
Accepted solution

Hi @harilalmn,

Just as the exception suggests, it means the 2 references are not geometrically aligned.As the NewAlignment Method confirms on the remarks:  These references must be already geometrically aligned (this function will not force them to become aligned).

I wish to suggest you have a look at our colleague's deep dive resource blog on this issue:https://thebuildingcoder.typepad.com/blog/2023/05/aligning-elements-finding-and-visualising-wires.ht... addressing similar case as yours on this forum thread:https://forums.autodesk.com/t5/revit-api-forum/how-to-use-the-alignment-method-using-for-family-inst...

Kindly, see if you the suggestions offered could help you achieve the goal.

Carol Gitonga, Developer Advocacy and Support, ADN Open

harilalmn
Advocate
Advocate

I am so sorry.. My under standing on NewAlignment was wrong. I thought it would align the first reference with the second one as we perform manual alignment in Revit, though I was wondering which side would it align. Well, my bad! Thanks for the insight.

0 Likes