Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Toggle VG Categories

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
Anonymous
837 Views, 5 Replies

Toggle VG Categories

This is one that has been answered for previous versions of Revit ie 2008, but does not seem to work in 2012. I want to create a toggle for a couple of VG categories, one being sections, but cannot seem to get an application I can even complie to test. i have had limited sucess with other applications I have written and am not very experinced with API programming which is probably obvious from the code below. I have taken one out of the SDK samples and tried to adapt it to suit my needs, and this is what I have ended up with.

 

namespace SectionToggle
{
    public class SectionsToggle
       {
        private Autodesk.Revit.UI.UIDocument m_document;    // the active document
   
        public VisibilityCtrl(Autodesk.Revit.UI.UIDocument document)
        {
            if (null == document)
            {
                throw new ArgumentNullException("document");
                return;
            }
            else
            {
                m_document = document;
                return;
            }
                    
        }

        public bool SetVisibility(bool visible, string name)
        {
            try
            {
               Category cat =  m_document.Document.Settings.Categories.get_Item("OST_Sections") as Category;
                m_document.Document.ActiveView.setVisibility(cat, visible);
            }
            catch (Exception)
            {
                return false;
            }

            return true;
        }

        }
    }

 The problem is VisibilityCtrl tells me I need a return type, but I can not work out what or where I need to put it. Any assistance would be greatly appreciated.

5 REPLIES 5
Message 2 of 6
mikako_harada
in reply to: Anonymous

Hello,

 

Missing "void" before VisibilityCtrl() perhaps?


Mikako Harada
Developer Technical Services
Message 3 of 6
Anonymous
in reply to: mikako_harada

Thanks for the response Mikako. This has now allowed me to build a solution, however after adding the Transaction attribute, I now get the following error within Revit.

"Autodesk.Revit.Exceptions.InvalidOperationException.

does not inherit IExternalCommand.

 

The modified code looks like this.

namespace SectionToggle
{

    [TransactionAttribute(TransactionMode.Automatic)]
    [RegenerationAttribute(RegenerationOption.Manual)]
    public class SectionsToggle

   
       {
        private Autodesk.Revit.UI.UIDocument m_document;    // the active document
   
   
        public void VisibilityCtrl(Autodesk.Revit.UI.UIDocument document)
        {
            if (null == document)
            {
                throw new ArgumentNullException("document");
               
            }
            else
            {
                m_document = document;
                return;
            }
                    
        }


        public bool SetVisibility(bool visible, string name)
        {
            try
            {
               Category cat =  m_document.Document.Settings.Categories.get_Item("OST_Sections") as Category;
                m_document.Document.ActiveView.setVisibility(cat, visible);
            }
            catch (Exception)
            {
                return false;
            }

            return true;
        }


        }
    }

 I have tried adding " : IExternalCommand" to the end of the class statement, but then cannot build the solution again.

Any ideas?

Message 4 of 6
mikako_harada
in reply to: Anonymous

Channond,

 

As the message suggest, you will need to implement IExternalCommand in order for Revit to recognize that the given dll is written for Revit and have a "entry" point that Revit can call.  Upon the command invocation, Revit will try to call Execuse method of a class which implement IExternalCommand.  Any external command has a certain pattern.  I have copy and pasted the complete code below.  I would also suggest you go through "My First Revit Plugin"

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

if you hit another issue.  The My First Plugin will clarify many issues before you hit. 

 

All the best.

 

    [Transaction(TransactionMode.Automatic)]

    publicclassrvtCmd_SetVisibility : IExternalCommand

    {

        //  member variables

        Document m_rvtDoc;

 

        publicResult Execute(

            ExternalCommandData commandData,

            refstring message,

            ElementSet elements)

        {

            //  get the access to the top most objects.

            //

            UIApplication rvtUIApp = commandData.Application;

            UIDocument rvtUIDoc = rvtUIApp.ActiveUIDocument;

            m_rvtDoc = rvtUIDoc.Document;

 

            SetVisibility(false, null);

 

            returnResult.Succeeded;

        }

 

        publicbool SetVisibility(bool visible, string name)

        {

            try

            {

                Category cat = 

                   m_rvtDoc.Settings.Categories.get_Item(

                   BuiltInCategory.OST_Sections);

                m_rvtDoc.ActiveView.setVisibility(cat, visible);

            }

            catch (Exception)

            {

                return false;

            }

 

            return true;

        }

 

    }


Mikako Harada
Developer Technical Services
Message 5 of 6
Anonymous
in reply to: mikako_harada

Thank you Mikako.

This has been bugging me for ages. Good to finally have a working answer. I find trying to debug this program extremely painful. Unlike lisp where you can pretty much step things out on the autocad command line to find where you are going wrong. Revit really needs an ide like in autocad to make life for programmers a little easier. 

I will reread through the first plug in tutorial and try and make sense of all this.

Message 6 of 6
mikako_harada
in reply to: Anonymous

Hi Channond,

 

Glad to hear it's working now. 

 

About  your comment about Revit .NET API being difficult to debug - I agree it is probably difficult to get over the first hurdle of getting the basic skeleton running. (e.g., you cannot really see anything happening in a single line. I now see why you try to jump onto the function above.)

 

Once you get to a point where you can run a basic skeleton, however, there is a good debugging environment with Visual Studio. Please take a look at "Lesson 4: Debugging your code".

 

Thank you for the feedback. 

 

All the best. 


Mikako Harada
Developer Technical Services

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Rail Community