Trying to get a macro I copied to work...Insert & Rotate (boost your bim)

Trying to get a macro I copied to work...Insert & Rotate (boost your bim)

Anonymous
Not applicable
764 Views
2 Replies
Message 1 of 3

Trying to get a macro I copied to work...Insert & Rotate (boost your bim)

Anonymous
Not applicable

I am getting a "'Autodesk.Revit.UI.Macros.ApplicationEntryPoint.Application' is a 'property' but is used like a 'type' (CS0118) - C:\ProgramData\Autodesk\Revit\Macros\2014\Revit\AppHookup\module1\Source\module1\ThisApplication.cs:44,5" error at the underlined part of the code. I am new to coding and I just want to be able to use the macro below. Any help would be appreciated.

 

 

/*
 * Created by SharpDevelop.
 * User: JGomez
 * Date: 10/6/2015
 * Time: 8:43 AM
 * 
 * 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 module1
{
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    [Autodesk.Revit.DB.Macros.AddInId("7ABD34CC-5DC0-43E5-8B0C-99B95643040A")]
    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 macro1()
        {
            
    UIDocument uidoc = this.ActiveUIDocument;
    Document doc = uidoc.Document;
    Application app = doc.Application;

    // Get the family symbol named "North Arrow 2"
    FamilySymbol famSym = new FilteredElementCollector(doc).OfClass(typeof(FamilySymbol)).Where(q => q.Name == "North Arrow 2").First() as FamilySymbol;

    // use a transaction group so that all the individual transactions are merged into a single entry in the Undo menu
    // this is optional
    using (TransactionGroup tg = new TransactionGroup(doc,"Create and Orient Instances"))
    {
        tg.Start();
        // create an infinite loop so user can create multiple instances in a single command
        // ESC when prompted to select a point will thrown an exception which is how the loop is exited
        while (true)
        {
            try
            {                
                XYZ pickPoint = uidoc.Selection.PickPoint("Click to specify instance location. ESC to stop placing instances.");

                FamilyInstance familyInstance = null;    
                // Create the instance with the default orientation
                // This is done in its own transaction so that the user can see the new instance when they are prompted for the orientation
                using (Transaction t = new Transaction(doc,"Place Instance"))
                {
                    t.Start();
                    familyInstance = doc.Create.NewFamilyInstance(pickPoint, famSym, doc.ActiveView);
                    t.Commit();
                }

                XYZ orientPoint = uidoc.Selection.PickPoint("Click to specify orientation. ESC to stop placing instances.");

                // Create a line between the two points
                // A transaction is not needed because the line is a transient element created in the application, not in the document
                Line orientLine = app.Create.NewLineBound(pickPoint, orientPoint);

                // Compute the angle between the vertical direction (XZY.BasisY) and the orientLine
                double angle = XYZ.BasisY.AngleTo(orientLine.Direction);

                // For diagnostics in Task dialog below
                double angleDegrees = angle * 180 / Math.PI;

                // AngleTo always returns the smaller angle between the two lines (for example, it will always return 10 degrees, never 350)
                // so if the orient point is to the left of the pick point, then correct the angle by subtracting it from 2PI (Revit measures angles in degrees)
                if (orientPoint.X < pickPoint.X)
                    angle = 2 * Math.PI - angle;

                // To show the need for angle corrections
                double angleDegreesCorrected = angle * 180 / Math.PI;
                //TaskDialog.Show("info","Angle directly from AngleTo = " + angleDegrees + "\n Angle after X correction = " + angleDegreesCorrected);

                // Create an axis in the Z direction 
                Line axis = app.Create.NewLineBound(pickPoint, new XYZ(pickPoint.X, pickPoint.Y, pickPoint.Z + 10));

                using (Transaction t = new Transaction(doc,"Orient Instance"))
                {
                    t.Start();
                    ElementTransformUtils.RotateElement(doc, familyInstance.Id, axis, -angle);
                    t.Commit();
                }
            }
            catch
            {
                // Get here when the user hits ESC when prompted for selection
                // "break" exits from the while loop
                break;
            }
        }
    // Consolidate all the transactions for the individual creation / rotation transactions into a single Undo item    
    tg.Assimilate();
    }

        }
    }
}

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

rosalesduquej
Alumni
Alumni
Accepted solution

Hi Jepy,

 

The reason you are getting that error is because you are missing a reference in your macro. By clicking over the underlined Application once, you will notice an edit icon appears on the left side, if you click once on it, 4 options will display and the first one saying using Autodesk.Revit.ApplicationServices will be the one you need. 

 

Now you will encounter 2 new erros after that one is fixed, the NewLineBound will get underlined where ever is used. The reason being that in Revit 2015, this functionality became obsolete and removed from the API. But don't worry I have switched it with the one that solves the problem and I have tested the macro and the same functionality will maintain. 

 

Remember, add the reference first and then change the following two lines with the Obsolete NewLineBound ones.

 

Here is the code: 

 

   Old Line

//Line orientLine = app.Create.NewLineBound(pickPoint, orientPoint);
Line orientLine = Line.CreateBound(pickPoint, orientPoint); // New Line :)
 

Old Line 

Line axis = app.Create.NewLineBound(pickPoint, new XYZ(pickPoint.X, pickPoint.Y, pickPoint.Z + 10));

Line axis = Line.CreateBound(pickPoint, new XYZ(pickPoint.X, pickPoint.Y, pickPoint.Z + 10));//New Line :)
 

Please try it and let me know how it goes. My advice if you want to become more familiar with the use of Macros and the Revit API will be to start from the My first Plugin website tutorial. It can help you out develop your macros in a faster way and also understanding what are you doing with them. 

 

http://usa.autodesk.com/adsk/servlet/index?siteID=123112&id=16849339

 

Cheers,

 

 



Jaime Rosales D.
Sr. Developer Consultant
Twitter | AEC ADN DevBlog
Message 3 of 3

Anonymous
Not applicable

It works now - thank you so much! Thank you for the tutorial info too.

 

Have a nice day!

0 Likes