Revit API has two ways of access, either via a IExternalCommand or Via IExternalApplication calling the a IExternalCommand class.
Let's start with IExternalCommand
Let's start with IExternalCommand
IExternalCommand
Your starting point to instruct Revit by what to do in a document is this class. It is your starting point to add your core code in. But remember this class must be shaped/decorated in a way it allows Revit understand it, and that is called interface.
When you Start opening Revit, Revit starts looking for any available IExternalCommand and IExternalApplication Classes that can be executed, but to ensure everything fits perfect, it checks if the class implement a specific interface or not. if yes, then such a class is accepted to allow this code log into Revit World, else it will be ignored.
When you Start opening Revit, Revit starts looking for any available IExternalCommand and IExternalApplication Classes that can be executed, but to ensure everything fits perfect, it checks if the class implement a specific interface or not. if yes, then such a class is accepted to allow this code log into Revit World, else it will be ignored.
You can think of this like your USB stick, your USB stick size/shape/pins must exactly match the port in the PC, otherwise no connection will occur.
as an example of implementation:
```[Transaction(TransactionMode.Manual)]
public class Command: IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
// your code will be here
return Result.Succeeded;
}
}
```
Well you can see and notice there are some new terms that may be you are not aware of yet, but no worries, all will be explained in due course.
But as an abstracted definitions:
Transaction
`[Transaction(TransactionMode.Manual)]` this is called attribute for a class, and basically it is like some additional information on how do you want revit interact with it. `Transaction` generally as defined in wiki:
We will talk more about transaction in other posts but for now you need to know that we must decorate our class with this attribute in order to get our addin works.
IExternalCommand
this is what this post all about.
Result
is a signal that return back to revit confirming back to Revit how was your addin performed, did it run and finished successfully or not. it has three values
Succeeded, Failed, Cancelled.
It is your (as a developer) responsibility to feed revit back, how your application performed.
and finally a full class.cs file would look something like this:
```using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
//Let's API It
namespace LAI
{
[Transaction(TransactionMode.Manual)]
public class Command: IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
try
{
// Do something on Revit
return Result.Succeeded;
}
catch(Exception ex)
{
// There is a problem in my code
return Result.Failed;
}
}
}
}
```
The target of this post is to cast some shadows on what is the IExternalCommand and how it is implemented. That's it for now, in the next post we will talk about how to setup your program to get connected to Revit, and create your 1st addin.
as an example of implementation:
```[Transaction(TransactionMode.Manual)]
public class Command: IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
// your code will be here
return Result.Succeeded;
}
}
```
Well you can see and notice there are some new terms that may be you are not aware of yet, but no worries, all will be explained in due course.
But as an abstracted definitions:
Transaction
`[Transaction(TransactionMode.Manual)]` this is called attribute for a class, and basically it is like some additional information on how do you want revit interact with it. `Transaction` generally as defined in wiki:
is information processing in computer science that is divided into individual, indivisible operations called transactions. Each transaction must succeed or fail as a complete unit; it can never be only partially complete.and RevitAPIDoc
Any change to a document can only be made while there is an active transaction open for that document. Changes do not become part of the document until the active transaction is committed . Consequently, all changes made in a transaction can be rolled back either explicitly or implicitly by the transaction's destructor.To make it easier, It is to tell revit: Hey Revit! Please pay attention to this class as there may be some changes to the current opened Document, and I need the `Manual` Mode option. so basically, don't worry about when to open and close the transaction, I am good enough to handle it myself.
We will talk more about transaction in other posts but for now you need to know that we must decorate our class with this attribute in order to get our addin works.
IExternalCommand
this is what this post all about.
Result
is a signal that return back to revit confirming back to Revit how was your addin performed, did it run and finished successfully or not. it has three values
Succeeded, Failed, Cancelled.
It is your (as a developer) responsibility to feed revit back, how your application performed.
and finally a full class.cs file would look something like this:
```using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
//Let's API It
namespace LAI
{
[Transaction(TransactionMode.Manual)]
public class Command: IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
try
{
// Do something on Revit
return Result.Succeeded;
}
catch(Exception ex)
{
// There is a problem in my code
return Result.Failed;
}
}
}
}
```
The target of this post is to cast some shadows on what is the IExternalCommand and how it is implemented. That's it for now, in the next post we will talk about how to setup your program to get connected to Revit, and create your 1st addin.
No comments:
Post a Comment