Message 1 of 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Good morning,
I am trying to convert application-based macro to document-based one. I want to put macro into the construction template file, which is copied for every new project (name of the file changes for every new project).
Macro gets information from "Elevation" parameter and sets parameter "Opening Axis" as equals. That's all.
This code works fine, but it's application macro:
/* * Created by SharpDevelop. * User: ps * Date: 25/09/2019 * Time: 1:25 PM * * 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 Rzędna { [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)] [Autodesk.Revit.DB.Macros.AddInId("18C6F354-98C4-474E-9A4E-B4DEBF3ED8F4")] 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 OpisOśOtworu() { //UIDocument uidoc = ActiveUIDocument; Document doc = ActiveUIDocument.Document; Autodesk.Revit.DB.View pView = ActiveUIDocument.Document.ActiveView; // 1. All Generic Instances ElementCategoryFilter filter = new ElementCategoryFilter(BuiltInCategory.OST_GenericModel); IList<Element> instanceList = new FilteredElementCollector(doc).WherePasses(filter).WhereElementIsNotElementType().ToElements(); // To select and count instances at completion IList<ElementId> toSelect = new List<ElementId>(); long count = 0; // 2. Conditioned Instances foreach (Element ins in instanceList) { double elevation = 0; IList<bool> flag = new List<bool>(); bool heightIsAvailable = false; bool BasedLevelIsAvailable = false; ParameterSet paramSet = ins.Parameters; foreach (Parameter param in paramSet) { if (param.Definition.Name.Equals("Rzędna")) { elevation = param.AsDouble(); } } // The separation of the two loops is necessary to avoid confusion in taking parameter value foreach (Parameter param in paramSet) { if (heightIsAvailable = BasedLevelIsAvailable = true && param.Definition.Name.Equals("Oś otworu")) // "Oś otworu" - custom instance parameter" { using (Transaction t = new Transaction(doc, "Set parameters value")) { t.Start(); param.Set(elevation); t.Commit(); count += 1; flag.Add(true); } } } if (flag.Contains(true)) { toSelect.Add(ins.Id); } } // 4. Report if (toSelect.Count > 0) { TaskDialog.Show("Wykonano na " + count + " otworach", "Zakończono: Rzędna - oś otworu - otwór okrągły oraz otwór prostokątny"); } else { TaskDialog.Show("Revit", "Nie znaleziono obiektów"); } UIDocument uidoc = new UIDocument(doc); uidoc.Selection.SetElementIds(toSelect); } } }
I was trying to change this lines of code, accordingly to an advice from another post:
//UIDocument uidoc = ActiveUIDocument; Document doc = ActiveUIDocument.Document; Autodesk.Revit.DB.View pView = ActiveUIDocument.Document.ActiveView;
to this:
//UIDocument uidoc = ActiveUIDocument; Document doc = this.Application.ActiveUIDocument.Document;
so, new code looks like this:
/* * Created by SharpDevelop. * User: ps * Date: 2020-01-15 * Time: 10:03 * * 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 Rzędna { [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)] [Autodesk.Revit.DB.Macros.AddInId("7F8685A7-AE6A-4C21-B955-A0C038E4AE61")] public partial class ThisDocument { 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 OpisOśOtworu() { //UIDocument uidoc = ActiveUIDocument; Document doc = this.Application.ActiveUIDocument.Document; // 1. All Generic Instances ElementCategoryFilter filter = new ElementCategoryFilter(BuiltInCategory.OST_GenericModel); IList<Element> instanceList = new FilteredElementCollector(doc).WherePasses(filter).WhereElementIsNotElementType().ToElements(); // To select and count instances at completion IList<ElementId> toSelect = new List<ElementId>(); long count = 0; // 2. Conditioned Instances foreach (Element ins in instanceList) { double elevation = 0; IList<bool> flag = new List<bool>(); bool heightIsAvailable = false; bool BasedLevelIsAvailable = false; ParameterSet paramSet = ins.Parameters; foreach (Parameter param in paramSet) { if (param.Definition.Name.Equals("Rzędna")) { elevation = param.AsDouble(); } } // The separation of the two loops is necessary to avoid confusion in taking parameter value foreach (Parameter param in paramSet) { if (heightIsAvailable = BasedLevelIsAvailable = true && param.Definition.Name.Equals("Oś otworu")) // "Oś otworu" - custom instance parameter" { using (Transaction t = new Transaction(doc, "Set parameters value")) { t.Start(); param.Set(elevation); t.Commit(); count += 1; flag.Add(true); } } } if (flag.Contains(true)) { toSelect.Add(ins.Id); } } // 4. Report if (toSelect.Count > 0) { TaskDialog.Show("Wykonano na " + count + " otworach", "Zakończono: Rzędna - oś otworu - otwór okrągły oraz otwór prostokątny"); } else { TaskDialog.Show("Revit", "Nie znaleziono obiektów"); } UIDocument uidoc = new UIDocument(doc); uidoc.Selection.SetElementIds(toSelect); } } }
and it works, but after closing the file, macro dissapear:
Can Someone explain for me how it works and how to fix it?
Thank You in advance
Regards
Solved! Go to Solution.