Run macro on Synchronize with Central

Run macro on Synchronize with Central

NiclasSassHald
Contributor Contributor
924 Views
2 Replies
Message 1 of 3

Run macro on Synchronize with Central

NiclasSassHald
Contributor
Contributor

Hi Forum

I have made a project macro, and would like to run it when Synchronizing.

We have an AddIn, where it works great, but i can't figure out how, to achive the same functionallity in a macro.

 

I've managed to create an event for Saving the Document - but would really like to do some other when synchronizing.

My code below builds fine - but it dosn't run the Sync-event.

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

namespace RoomNdArea
{
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    [Autodesk.Revit.DB.Macros.AddInId("0158DC3D-D3E0-41DB-A020-338B0F039E6F")]
	public partial class ThisDocument
	{
		private void Module_Startup(object sender, EventArgs e)
		{
			ReferenceLevel();
			OnStartup();

		}

		private void Module_Shutdown(object sender, EventArgs e)
		{
			this.Document.DocumentSaved -= SavedDocument;
			OnShutdown();

		}

		#region Revit Macros generated code
		private void InternalStartup()
		{
			this.Startup += new System.EventHandler(Module_Startup);
			this.Shutdown += new System.EventHandler(Module_Shutdown);
			this.Document.DocumentSaved += new EventHandler<Autodesk.Revit.DB.Events.DocumentSavedEventArgs>(SavedDocument);
			
			
		}
		#endregion
		public void ReferenceLevel()
		{
			Document doc = this.Application.ActiveUIDocument.Document;
			
			FilteredElementCollector rooms = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Rooms);

			int r = rooms.ToElements().Count;
			
			Transaction t = new Transaction(doc, "ReferenceLevel");
			t.Start();

			foreach (Element room in rooms.ToElements()) {
				string sVal = room.get_Parameter(BuiltInParameter.LEVEL_NAME).AsString();
				string newVal = sVal.Remove(0,sVal.Length - 2);
				Parameter p = room.LookupParameter("ZoneName");
				p.Set(newVal);
			}
			
			t.Commit();
			
			TaskDialog.Show("TEST",r + " Rooms in this model");
		}
		private void SavedDocument(object sender, EventArgs e){
			TaskDialog.Show("TEST","SAVED");
		}
		
		public class Sync : IExternalApplication{
			public Result OnStartup(Autodesk.Revit.UI.UIControlledApplication application)
			{
				application.ControlledApplication.DocumentSynchronizingWithCentral += new EventHandler<Autodesk.Revit.DB.Events.DocumentSynchronizingWithCentralEventArgs>(application_ControlledApplication_DocumentSynchronizingWithCentral);

				return Result.Succeeded;
			}
			
			public Result OnShutdown(Autodesk.Revit.UI.UIControlledApplication application)
			{
				application.ControlledApplication.DocumentSynchronizingWithCentral -= application_ControlledApplication_DocumentSynchronizingWithCentral;
				return Result.Succeeded;
			}
			
			public void application_ControlledApplication_DocumentSynchronizingWithCentral(object sender, Autodesk.Revit.DB.Events.DocumentSynchronizingWithCentralEventArgs args){
				TaskDialog.Show("SYNC","SyncToCentral");
			}
		}
	}
}

 

Best regards 

Niclas

0 Likes
Accepted solutions (2)
925 Views
2 Replies
Replies (2)
Message 2 of 3

RPTHOMAS108
Mentor
Mentor
Accepted solution

I don't see the complication here?

 

If you want to set up an event in a macro module that runs a macro in a different module then perhaps explore what is possible with the MacroManager API object. Otherwise below test works fine for pre-synchronization event:

 

 

Imports System
Imports Autodesk.Revit.UI
Imports Autodesk.Revit.DB
Imports Autodesk.Revit.UI.Selection
Imports System.Collections.Generic
Imports System.Linq

<Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)> _
<Autodesk.Revit.DB.Macros.AddInId("9C3FB51E-D218-4F59-90DB-93ED44B3DE2E")> _
Partial Public Class ThisDocument

Private Sub Module_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
	TaskDialog.Show("Open","XX")
	
	
    	 AddHandler Me.Application.Application.DocumentSynchronizingWithCentral, AddressOf Test1
    End Sub
	
    Private Sub Module_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown
		RemoveHandler Me.Application.Application.DocumentSynchronizingWithCentral, AddressOf Test1
    End Sub
    
    
    Public Sub Test1(s As Object, e As Autodesk.Revit.DB.Events.DocumentSynchronizingWithCentralEventArgs)
    	
    	Dim TD As New TaskDialog("Event")
    	TD.MainContent ="Sync ?"
    	TD.CommonButtons = CType( TaskDialogCommonButtons.Ok + TaskDialogCommonButtons.Cancel, TaskDialogCommonButtons)
    	Dim res As TaskDialogResult = TD.Show
    	
    	If res = TaskDialogResult.Cancel Then
    		e.Cancel
    	End If
    	   	
    	
    End Sub
	
End Class

 

 

0 Likes
Message 3 of 3

NiclasSassHald
Contributor
Contributor
Accepted solution

Thanks for your reply.
I forgot to write that it should preferably be in C# - but your code works!
I have had some help to read- and translate it, so that my own code has become the following:

		#region Revit Macros generated code
		private void InternalStartup()
		{
			this.Startup += new System.EventHandler(Module_Startup);
			this.Shutdown += new System.EventHandler(Module_Shutdown);
			this.Application.Application.DocumentSynchronizingWithCentral += SyncingEvent;
			
			
		}
		#endregion



    public void SyncingEvent(object s, Autodesk.Revit.DB.Events.DocumentSynchronizingWithCentralEventArgs e)

    {

            TaskDialog.Show("Test", "Sync");

    }

 

 

0 Likes