Results 1 to 6 of 6

Thread: Convert Revit Macro to Revit API

  1. #1
    Member
    Join Date
    2009-10
    Posts
    2
    Login to Give a bone
    0

    Exclamation Convert Revit Macro to Revit API

    I am trying to convert this macro to revit api but i get "Expected class, delegate, enum, interface, or struct" error.
    How can i solve this problem?

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Autodesk.Revit.DB;
    using Autodesk.Revit.UI;
    using Autodesk.Revit.UI.Selection;
    
    namespace Walls
    {
    public class WallPickFilter : ISelectionFilter
    {
    public bool AllowElement(Element e)
    {
    return (e.Category.Id.IntegerValue.Equals(
    (int)BuiltInCategory.OST_Walls));
    }
    
    public bool AllowReference(Reference r, XYZ p)
    {
    return false;
    }
    }
    
    public void WallJoinsMiterByPick()
    {
    UIDocument uidoc = this.ActiveUIDocument;
    Document doc = uidoc.Document;
    ISelectionFilter wallfilter=new WallPickFilter();
    IList<Reference> refList = new List<Reference>();
    try
    {
    // create a loop to repeatedly prompt the user to select a wall
    while (true)
    refList.Add(uidoc.Selection.PickObject(ObjectType. Element,wallfilter, 
    "Select elements in order to change join type to miter. ESC when finished."));
    }
    // When the user hits ESC Revit will throw an OperationCanceledException which will get them out of the while loop
    catch
    { }
    using(Transaction t = new Transaction(doc, "Wall joins to Miter"))
    {
    t.Start();
    foreach (Reference r in refList)
    {
    Element wall1= doc.GetElement(r);
    LocationCurve wCurve = wall1.Location as LocationCurve;
    wCurve.set_JoinType(0,JoinType.Miter);
    wCurve.set_JoinType(1,JoinType.Miter);
    }
    doc.Regenerate();
    t.Commit();
    }
    }	
    }
    Last edited by CADastrophe; 2015-02-11 at 04:38 PM. Reason: Added [CODE] [/CODE] Formatting

  2. #2
    Administrator Ed Jobe's Avatar
    Join Date
    2000-11
    Location
    Turlock, CA
    Posts
    6,420
    Login to Give a bone
    0

    Default Re: Convert Revit Macro to Revit API

    At what point do you get the error?
    C:> ED WORKING....


    LinkedIn

  3. #3
    Member
    Join Date
    2009-10
    Posts
    2
    Login to Give a bone
    0

    Default Re: Convert Revit Macro to Revit API

    public void WallJoinsMiterByPick()
    {
    UIDocument uidoc = this.ActiveUIDocument;
    Document doc = uidoc.Document;
    ISelectionFilter wallfilter=new WallPickFilter();
    IList<Reference> refList = new List<Reference>();
    try
    {
    // create a loop to repeatedly prompt the user to select a wall
    while (true)
    refList.Add(uidoc.Selection.PickObject(ObjectType. Element,wallfilter,
    "Select elements in order to change join type to miter. ESC when finished."));
    }
    // When the user hits ESC Revit will throw an OperationCanceledException which will get them out of the while loop
    catch
    { }
    using(Transaction t = new Transaction(doc, "Wall joins to Miter"))
    {
    t.Start();
    foreach (Reference r in refList)
    {
    Element wall1= doc.GetElement(r);
    LocationCurve wCurve = wall1.Location as LocationCurve;
    wCurve.set_JoinType(0,JoinType.Miter);
    wCurve.set_JoinType(1,JoinType.Miter);
    }
    doc.Regenerate();
    t.Commit();
    }
    }
    }
    Attached Images Attached Images

  4. #4
    Administrator Ed Jobe's Avatar
    Join Date
    2000-11
    Location
    Turlock, CA
    Posts
    6,420
    Login to Give a bone
    0

    Default Re: Convert Revit Macro to Revit API

    Looks like you've got an extra or missing bracket somewhere.
    C:> ED WORKING....


    LinkedIn

  5. #5
    Revit Mararishi aaronrumple's Avatar
    Join Date
    2002-02
    Location
    St. Louis, MO
    Posts
    4,695
    Login to Give a bone
    0

    Default Re: Convert Revit Macro to Revit API

    When moving to an external command from a macro, you have to remember that there isn't a this.ActiveIDocument. The macro helps out in providing the command data as "this" You first need to create an instance of the document.

    [Transaction(TransactionMode.Manual)]
    class cmdFilters : IExternalCommand
    {
    public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
    {
    /*Single line call to active document...
    //Document myDocument = commandData.Application.ActiveUIDocument.Document;*/
    UIApplication uiApp = commandData.Application;
    Autodesk.Revit.ApplicationServices.Application myApp = uiApp.Application;
    UIDocument uiDoc = uiApp.ActiveUIDocument;
    Document myDocument = uiDoc.Document;
    ...and more programming.

    You'll see the application is first grabbed from the commandData. Then the UI from that and then the Document from that.

    You'll see my note on how to grab the document directly in a single line. (But often you need the other objects as well.)

  6. #6
    Revit Mararishi aaronrumple's Avatar
    Join Date
    2002-02
    Location
    St. Louis, MO
    Posts
    4,695
    Login to Give a bone
    0

    Default Re: Convert Revit Macro to Revit API

    Here's a simple template I use for starting a new Revit command in C#:
    ---
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using Autodesk.Revit.ApplicationServices;
    using Autodesk.Revit.Attributes;
    using Autodesk.Revit.DB;
    using Autodesk.Revit.UI;
    using Autodesk.Revit.UI.Selection;
    using Autodesk.Revit.DB.Architecture;
    
    
    namespace MyNameSpace
    {
        [Transaction(TransactionMode.Manual)]
        class cmdTemplate : IExternalCommand
        {
            public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
            {
                /*Single line call to active document...
                //Document myDocument = commandData.Application.ActiveUIDocument.Document;*/
                UIApplication uiApp = commandData.Application;
                Autodesk.Revit.ApplicationServices.Application myApp = uiApp.Application;
                UIDocument uiDoc = uiApp.ActiveUIDocument;
                Document myDocument = uiDoc.Document;
    
    
                Transaction myTransaction = new Transaction(myDocument);
                myTransaction.Start("MyCommand");
    
    
                TaskDialog OkCancel = new TaskDialog("Revit");
                OkCancel.Title = "Revit";
                OkCancel.MainContent = "This is a sample command...";
                OkCancel.CommonButtons = TaskDialogCommonButtons.Ok | TaskDialogCommonButtons.Cancel | TaskDialogCommonButtons.Close;
    
    
                OkCancel.FooterText = "<a href=\"http://wwwautodesk.com \">" +
                                      "Click here for Autodesk Web Site...</a>";
    
    
                
                TaskDialogResult myResult = OkCancel.Show();
    
    
                try
                {
                    if (myResult == TaskDialogResult.Ok)
                    {
                        myTransaction.Commit();
                        return Result.Succeeded;
                    }
                }
                catch
                {
                    if (myResult == TaskDialogResult.Cancel)
                    {
                        myTransaction.RollBack();
                        return Result.Cancelled;
                    }
                }
                myTransaction.RollBack();
                return Result.Failed;
            }
        }
    }
    Last edited by Ed Jobe; 2015-02-23 at 03:29 PM. Reason: Added Code Tags

Similar Threads

  1. CP222-2: Autodesk Revit VSTA: Writing Your First Macro An Introduction to the Macro Manager
    By Autodesk University in forum Customization and Programming
    Replies: 0
    Last Post: 2014-12-01, 02:12 AM
  2. 2013: delete a Revit Macro
    By MikeJarosz in forum Revit Architecture - General
    Replies: 0
    Last Post: 2013-01-14, 03:40 PM
  3. Convert macro into lsp - help...
    By Arterius in forum AutoLISP
    Replies: 2
    Last Post: 2011-11-16, 09:24 AM
  4. Convert to Application Macro
    By ducpham in forum Revit - API
    Replies: 1
    Last Post: 2009-11-05, 03:58 PM
  5. Macro's in Revit
    By mike.vroegindeweij in forum Revit Structure - Wish List
    Replies: 5
    Last Post: 2008-08-02, 05:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •