Transformation application-based macro to document-based macro

Transformation application-based macro to document-based macro

piotr.spyra
Contributor Contributor
680 Views
2 Replies
Message 1 of 3

Transformation application-based macro to document-based macro

piotr.spyra
Contributor
Contributor

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. 

 

Revit_2020-01-15_11-30-57.png

 

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:

 

 Revit_2020-01-15_11-42-44.png


Can Someone explain for me how it works and how to fix it?

Thank You in advance
Regards

 

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

so-chong
Advocate
Advocate
Accepted solution

Hi,

 

Did you try to change your namespace "Rzędna" to something else like "Test" ?

 

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
{

It seems like you are using this name to find a parameter 

 if (param.Definition.Name.Equals("Rzędna"))

This maybe confusing to the SharpDevelep macro editor.

 

Try use another namespace name instead of "Rzędna"

 

0 Likes
Message 3 of 3

piotr.spyra
Contributor
Contributor

@so-chong Thank You very much, Your advice solved the problem. I wouldn't thought about it - valuable lesson 😉

I've got one more question. Is it possible to add a code: if user open document in English version -> change parameter "Rzędna" to "Elevation" in every apperance in the code?
 
Now, I made it this way:
Revit_2020-01-15_14-52-31.png
0 Likes