PDA

View Full Version : 2018 Following Udemy tutorial: ThisDocument does not contian a definition for 'ActiveUIDocument'...



polylop
2017-12-03, 10:15 AM
I'm currently viewing tutorials for the RevitAPI by udemy, currently on the second video i.e. "Prompt user to select an element, get element info".

When I build the code however, which I just followed from the tutorial, I get the error in the title, like it's saying that it can't find ActiveUIDocument or something.


namespace promptuser
{
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
[Autodesk.Revit.DB.Macros.AddInId("4C3F6AF8-A7AF-4E43-9D90-2DE1A9DB7BA3")]
public partial class ThisDocument
{
public void selectElement()
{
// info about how the revit user interacts with the file
// icludes info about view windows and selection of elements in those windows
// then we get the activeUiDocument use the ff syntax
UIDocument uidoc = this.ActiveUIDocument;

// Database doc = stores info about walls, floors, parameters, etc.
Document doc = uidoc.Document;

// prompt the user to select an object
// Namespace: Autodesk.Revit.UI.Selection
// this would have been the code = uidoc.Selection.PickObject(ObjectType.Element);

// but since this returns a Reference object, store that in a variable
Reference myRef = uidoc.Selection.PickObject(ObjectType.Element);

// get the element itself
Element e = doc.GetElement(myRef);

// display its name, and ID
TaskDialog.Show(e.Name + Environment.NewLine + e.Id);
}

public void SimpleDialog()
{
TaskDialog.Show("This is a title", "Hello Revit WOrld");
}


}
}

I have no idea why this is happening.

polylop
2017-12-03, 11:32 AM
Nevermind, just discovered the problem, just checked the RevitAPI chm, apparently ActiveUIDocument is a property of UIApplication which in turn is part of the Autodesk.Revit.UI namespace.

The solution would simply be


UIApplication uiapp = this.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;


What's weird though is that the tutorial worked despite the lack of the code above. What could be the reason for this?

willw663499
2018-01-14, 07:05 PM
The original udemy tutorial is for an application macro. It does not need to access the application.

sdarali
2018-01-15, 02:01 PM
The original udemy tutorial is for an application macro. It does not need to access the application.

That is correct. Accessing the active document from an application macro is through referencing the application first and then grabbing the active document from it, while accessing the active document from a document macro is direct because the macro would be running from within the active document itself.