Message 1 of 5
Set a new XYZ position to FamilyInstance through an EventRegisterHandler or IUpdater
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi,
I would like to create a tracker that when it detects a new FamilyInstance element created by the user, the tracker can apply an automatic translation of XYZ(-30,0,0).
In other words, I want to change the position of this instance.
Here is some code I have retrieved, which detects the new elements. Only, I can't manage to apply a transaction to apply a new XYZ location.
Thank you for you answers 🙂
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System.Windows.Forms;
namespace TestScript
{
[TransactionAttribute(TransactionMode.Manual)]
public class EventRegistrationInModelessDialogViaExternalEvent
: IExternalCommand
{
public Document doc;
public Autodesk.Revit.ApplicationServices.Application RevitApp;
ExternalEvent _exEvent;
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
EventRegisterHandler _exeventHander = new EventRegisterHandler();
_exEvent = ExternalEvent.Create(_exeventHander);
MyForm form = new MyForm();
form.ExEvent = _exEvent;
form.Show();
return Result.Succeeded;
}
}
public class MyForm : System.Windows.Forms.Form
{
public MyForm()
: base()
{
Button btn = new Button();
btn.Text = "Toggle DocumentChanged Event Registration";
btn.Click += btn_Click;
btn.Width = 250;
this.Controls.Add(btn);
}
public ExternalEvent ExEvent { get; set; }
void btn_Click(object sender, EventArgs e)
{
if (ExEvent != null)
ExEvent.Raise();
else
MessageBox.Show("external event handler is null");
}
}
public class EventRegisterHandler : IExternalEventHandler
{
public bool EventRegistered { get; set; }
public void Execute(UIApplication app)
{
if (EventRegistered)
{
EventRegistered = false;
app.Application.DocumentChanged -= Application_DocumentChanged;
}
else
{
EventRegistered = true;
app.Application.DocumentChanged += Application_DocumentChanged;
}
}
void Application_DocumentChanged(object sender,
Autodesk.Revit.DB.Events.DocumentChangedEventArgs e)
{
var sb = new StringBuilder();
var added = "added:" + e.GetAddedElementIds()
.Aggregate("", (ss, el) => ss + "," + el).TrimStart(',');
var modified = "modified:" + e.GetModifiedElementIds()
.Aggregate("", (ss, el) => ss + "," + el).TrimStart(',');
var deleted = "deleted:" + e.GetDeletedElementIds()
.Aggregate("", (ss, el) => ss + "," + el).TrimStart(',');
sb.AppendLine(added);
sb.AppendLine(modified);
sb.AppendLine(deleted);
TaskDialog.Show("Changes", sb.ToString());
//
// Task To Achieve : move the fresh fi to new position
//
Document doc = e.GetDocument();
Element element = doc.GetElement(e.GetAddedElementIds().First());
if(element is FamilyInstance)
{
FamilyInstance fi = element as FamilyInstance;
// Set new position
XYZ translation = new XYZ(-30, 0, 0);
using (Transaction t = new Transaction(doc))
{
t.Start("t");
ElementTransformUtils.MoveElement(doc, fi.Id, translation);
t.Commit();
}
}
}
public string GetName()
{
return "EventRegisterHandler";
}
}
}