AutoCAD .NET has wrapped almost all ObjectARX events through one way or another using still the event concept or something slightly different. As a whole, the AutoCAD .NET event or notification system is powerful and useful and a professional and relatively big addin cannot totally ignore the important force.
In this article, we will be talking about the AutoCAD .NET BlockTableRecord events. It is always good to start with some cool, concise and still full sample code. Here it is:
using System;
using System.Text;
using System.Linq;
using System.Xml;
using System.Reflection;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Forms;
using System.IO;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Windows;
using Autodesk.AutoCAD.GraphicsInterface;
using Autodesk.AutoCAD.GraphicsSystem;
using GSManager = Autodesk.AutoCAD.GraphicsSystem.Manager;
using GSView = Autodesk.AutoCAD.GraphicsSystem.View;
using GSDevice = Autodesk.AutoCAD.GraphicsSystem.Device;
using GSModel = Autodesk.AutoCAD.GraphicsSystem.Model;
using AcadApplication = Autodesk.AutoCAD.ApplicationServices.Application;
using AcadDocument = Autodesk.AutoCAD.ApplicationServices.Document;
using AcadWindows = Autodesk.AutoCAD.Windows;
namespace AcadNetAddinWizard_Namespace
{
public class BlockTableRecordEvents
{
[CommandMethod("TestBlockTableRecordEvents")]
public void BlockTableRecordEvents_Method()
{
BlockTableRecordEvents eventManager = new BlockTableRecordEvents();
eventManager.Register();
}
public void Register()
{
BlockTableRecord.BlockInsertionPoints += new BlockInsertionPointsEventHandler(BlockTableRecord_BlockInsertionPoints);
}
void BlockTableRecord_BlockInsertionPoints(object sender, BlockInsertionPointsEventArgs e)
{
AcadApplication.DocumentManager.MdiActiveDocument.Editor.WriteMessage(
"BlockTableRecord_BlockInsertionPoints - Block Name: {0}\n", e.BlockTableRecord.Name);
}
}
}
The code is short and self-explanatory so we will not waste our words here. Let us play around the command and look at some command outputs:
Command: TestBlockTableRecordEvents
Command: insert
BlockTableRecord_BlockInsertionPoints - Block Name: test
Specify insertion point or [Basepoint/Scale/X/Y/Z/Rotate]:
Command: -INSERT
Enter block name or [?] <test>:
Units: Inches Conversion: 1.0000
BlockTableRecord_BlockInsertionPoints - Block Name: test
Specify insertion point or [Basepoint/Scale/X/Y/Z/Rotate]:
Enter X scale factor, specify opposite corner, or [Corner/XYZ] <1>:
Enter Y scale factor <use X scale factor>:
Specify rotation angle <0>: 90
As can be noticed, whenever a block reference is being inserted into the drawing (right before the insertion point prompt showing up), the BlockInsertionPoints event of the BlockTableRecord class would be fired off. Please note the event is static, meaning it can only apply to the whole BlockTableRecord type rather than some individual block references (AutoCAD INSERTs). In terms of how to treat different INSERTs differently, checking the block name is the way as demonstrated.
By the way, the sample code does not cover how to unsubscribe these events. I would like to leave it as an exercise to readers who have interest.
The leading edge AutoCAD .NET Addin Wizard (AcadNetAddinWizard) provides kinds of AutoCAD .NET Event Handler Wizards including an AutoCAD BlockTableRecord Event Wizard to help create start code of event handlers automatically, quickly, flexibly and reliably.
Posted by: |