Hi Haider,
Yes i had tested the macro, there seems to me nothing wrong with the code.
I had compiled succesfully in Sharpdevelop Editor.
I will insert the code completely for you to examine the whole macro.
Look where the difference are with your code, beside the different namespace name.
If you can do it, insert your whole code just like i did.
Good Luck!
/*
* Created by SharpDevelop.
* User: S. Li
* Date: 2017-10-04
* Time: 19:20
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
using System.Collections.Generic;
using System.Linq;
namespace MacroFromInternet
{
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
[Autodesk.Revit.DB.Macros.AddInId("CBE2490D-447D-4713-9EA4-789F888A1672")]
public partial class ThisApplication
{
private void Module_Startup(object sender, EventArgs e)
{
}
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 legendOnSheets()
{
Document doc = this.ActiveUIDocument.Document;
// create list of element ids for the sheets that will get the legends
List<ElementId> sheetIds = new List<ElementId>();
// use SheetNumber to find the desired sheets
// add each sheet to the list of sheet ids
sheetIds.Add(new FilteredElementCollector(doc)
.OfClass(typeof(ViewSheet))
.Cast<ViewSheet>()
.FirstOrDefault(q => q.SheetNumber == "A101").Id);
sheetIds.Add(new FilteredElementCollector(doc)
.OfClass(typeof(ViewSheet))
.Cast<ViewSheet>()
.FirstOrDefault(q => q.SheetNumber == "A102").Id);
sheetIds.Add(new FilteredElementCollector(doc)
.OfClass(typeof(ViewSheet))
.Cast<ViewSheet>()
.FirstOrDefault(q => q.SheetNumber == "A103").Id);
// find the legend to put on the sheets
// use ViewType.Legend and the view name to find the legend view
Element legend = new FilteredElementCollector(doc)
.OfClass(typeof(View))
.Cast<View>()
.FirstOrDefault(q => q.ViewType == ViewType.Legend && q.Name == "Legend 1");
// create a transaction so that the document can be modified
using (Transaction t = new Transaction(doc, "Legends on Sheets"))
{
// start the transaction
t.Start();
// loop through the list of sheet ids
foreach (ElementId sheetid in sheetIds)
{
// create a new viewport for the legend on each sheet
// place the legend view at the 0,0,0 location on each sheet
// user will need to move the legend to the desired location
Viewport.Create(doc, sheetid, legend.Id, new XYZ(0,0,0));
}
// commit the changes
t.Commit();
}
}
}
}