After reviewing the sample code I have been generating for this blog, it has become obvious that there is certain functionality that is being used in every single example. When I started the blog, my goal was to start putting common functionality and reusable code into the Colibra library, so my readers and my-self can benefit from it. Don’t worry, Colibra still a go, and I will be extending the functionality and posting it here.
But there is some other repetitive code that I need to include in every example. It is so simple that it is not worth the effort to abstract it and make it part of Colibra, but it is so useful, that I end-up including it with every post.
In order to avoid code duplication, I created a base class ‘SimpleDrawingCommand’ that implements methods and properties I use in most posts. These are properties to access the Document or Database objects, or methods that perform simple functionality like start a new transaction. I am sure, as I create new samples, the functionality of this class will be extended, and I’ll make sure I make it available.
Here is the code for this simple base class, and I hope you find it useful in your short scripts:
C#
/// <summary>
/// Base class for simple command classes that implement commands
/// that access a single drawing. This class implements an interface
/// with basic, redundant functionality that derived classes can
/// leverage to simplify coding.
/// </summary>
public class SimpleDrawingCommand
{
/// <summary>
/// Returns the document object from where the command was
/// launched.
/// </summary>
protected Document _document
{
get
{
return Application.DocumentManager.MdiActiveDocument;
}
}
/// <summary>
/// Return the Civil 3D Document instance.
/// </summary>
protected CivilDocument _civildoc
{
get
{
return CivilApplication.ActiveDocument;
}
}
/// <summary>
/// Returns the current database from where the command
/// was launched.
/// </summary>
protected Database _database
{
get
{
return _document.Database;
}
}
/// <summary>
/// Returns the Editor instance for the current document.
/// </summary>
protected Editor _editor
{
get
{
return _document.Editor;
}
}
/// <summary>
/// Starts a new transaction in the current database.
/// </summary>
/// <returns></returns>
protected Transaction startTransaction()
{
return _database.TransactionManager.StartTransaction();
}
/// <summary>
/// Writes the specified message to the Editor output window.
/// </summary>
/// <param name="message"></param>
protected void write(string message)
{
_editor.WriteMessage(message);
}
}
VB.NET
''' <summary>
''' Base class for simple command classes that implement commands
''' that access a single drawing. This class implements an interface
''' with basic, redundant functionality that derived classes can
''' leverage to simplify coding.
''' </summary>
Public Class SimpleDrawingCommand
''' <summary>
''' Returns the document object from where the command was
''' launched.
''' </summary>
Protected ReadOnly Property _document() As Document
Get
Return Application.DocumentManager.MdiActiveDocument
End Get
End Property
''' <summary>
''' Return the Civil 3D Document instance.
''' </summary>
Protected ReadOnly Property _civildoc() As CivilDocument
Get
Return CivilApplication.ActiveDocument
End Get
End Property
''' <summary>
''' Returns the current database from where the command
''' was launched.
''' </summary>
Protected ReadOnly Property _database() As Database
Get
Return _document.Database
End Get
End Property
''' <summary>
''' Returns the Editor instance for the current document.
''' </summary>
Protected ReadOnly Property _editor() As Editor
Get
Return _document.Editor
End Get
End Property
''' <summary>
''' Starts a new transaction in the current database.
''' </summary>
''' <returns></returns>
Protected Function startTransaction() As Transaction
Return _database.TransactionManager.StartTransaction()
End Function
''' <summary>
''' Writes the specified message to the Editor output window.
''' </summary>
''' <param name="message"></param>
Protected Sub write(message As String)
_editor.WriteMessage(message)
End Sub
End Class
As the class name implies, this should serve as base class for Command Classes that only access the current active drawing and are used as simple scripts. If you are doing more involved development, you should spend some time thinking about the abstraction you should provide in your application to achieve a more solid solution.
Comments