After a few comments on this previous post I decided that, rather than rushing on to show palettes, I'd first extend the existing code to work with multiple entities and provide specific support for solids.
Here are the main changes to the way the code works:
Changes to the Commands class
- The form object is now a static member, and so is shared across documents
- To support this I added a DocumentCreated event to make sure it gets added when new documents are created or opened
- Initialization/termination happens via IExtensionApplication, not on class construction/destruction
- In OnMonitorPoint we get the first (rather than the last, which is what I'd done previously) ObjectID from each path
Changes to the TypeViewerForm class
- The form has been stretched to allow more information to be visible
- We now expose a SetObjectIds() function, to support multiple objects
- This function gets the type information for each of the objects passed in
- For Solid3d objects it gets the "solid type" via COM, as this is not exposed via the managed interface
- It also displays the colour index for each object, to help distinguish between multiple objects of the same type
- Now use Hide() rather than Close(), which allows subsequent uses of the VT command
Here's the modified C# code for the command implementation:
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using System;
using CustomDialogs;
namespace CustomDialogs
{
public class Commands : IExtensionApplication
{
static TypeViewerForm tvf;
public void Initialize()
{
tvf = new TypeViewerForm();
DocumentCollection dm =
Application.DocumentManager;
dm.DocumentCreated +=
new DocumentCollectionEventHandler(OnDocumentCreated);
foreach (Document doc in dm)
{
doc.Editor.PointMonitor +=
new PointMonitorEventHandler(OnMonitorPoint);
}
}
public void Terminate()
{
try
{
tvf.Dispose();
DocumentCollection dm =
Application.DocumentManager;
if (dm != null)
{
Editor ed = dm.MdiActiveDocument.Editor;
ed.PointMonitor -=
new PointMonitorEventHandler(OnMonitorPoint);
}
}
catch (System.Exception)
{
// The editor may no longer
// be available on unload
}
}
private void OnDocumentCreated(
object sender,
DocumentCollectionEventArgs e
)
{
e.Document.Editor.PointMonitor +=
new PointMonitorEventHandler(OnMonitorPoint);
}
private void OnMonitorPoint(
object sender,
PointMonitorEventArgs e
)
{
FullSubentityPath[] paths =
e.Context.GetPickedEntities();
if (paths.Length <= 0)
{
tvf.SetObjectId(ObjectId.Null);
return;
};
ObjectIdCollection idc = new ObjectIdCollection();
foreach (FullSubentityPath path in paths)
{
// Just add the first ID in the list from each path
ObjectId[] ids = path.GetObjectIds();
idc.Add(ids[0]);
}
tvf.SetObjectIds(idc);
}
[CommandMethod("vt",CommandFlags.UsePickSet)]
public void ViewType()
{
Application.ShowModelessDialog(null, tvf, false);
}
}
}
And for the form:
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Interop.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace CustomDialogs
{
public partial class TypeViewerForm : Form
{
public TypeViewerForm()
{
InitializeComponent();
}
public void SetObjectText(string text)
{
typeTextBox.Text = text;
}
public void SetObjectIds(ObjectIdCollection ids)
{
if (ids.Count < 0)
{
SetObjectText("");
}
else
{
Document doc =
Autodesk.AutoCAD.ApplicationServices.
Application.DocumentManager.MdiActiveDocument;
DocumentLock loc =
doc.LockDocument();
using (loc)
{
string info =
"Number of objects: " +
ids.Count.ToString() + "\r\n";
Transaction tr =
doc.TransactionManager.StartTransaction();
using (tr)
{
foreach (ObjectId id in ids)
{
Entity ent =
(Entity)tr.GetObject(id, OpenMode.ForRead);
Solid3d sol = ent as Solid3d;
if (sol != null)
{
Acad3DSolid oSol =
(Acad3DSolid)sol.AcadObject;
// Put in a try-catch block, as it's possible
// for solids to not support this property,
// it seems (better safe than sorry)
try
{
string solidType = oSol.SolidType;
info +=
ent.GetType().ToString() +
" (" + solidType + ") : " +
ent.ColorIndex.ToString() + "\r\n";
}
catch (System.Exception)
{
info +=
ent.GetType().ToString() +
" : " +
ent.ColorIndex.ToString() + "\r\n";
}
}
else
{
info +=
ent.GetType().ToString() +
" : " +
ent.ColorIndex.ToString() + "\r\n";
}
}
tr.Commit();
}
SetObjectText(info);
}
}
}
public void SetObjectId(ObjectId id)
{
if (id == ObjectId.Null)
{
SetObjectText("");
}
else
{
Document doc =
Autodesk.AutoCAD.ApplicationServices.
Application.DocumentManager.MdiActiveDocument;
DocumentLock loc =
doc.LockDocument();
using (loc)
{
Transaction tr =
doc.TransactionManager.StartTransaction();
using (tr)
{
DBObject obj =
tr.GetObject(id, OpenMode.ForRead);
SetObjectText(obj.GetType().ToString());
tr.Commit();
}
}
}
}
private void closeButton_Click(object sender, EventArgs e)
{
this.Hide();
}
}
}
The complete project can be downloaded from here.
Here's what you get when you run the VT command and hover over a number of Solid3d objects:
You'll see that a number of "duplicate" objects listed... this is because we have multiple sub-objects under the cursor, each of which is listed passed into the event via a separate sub-entity path. We could easily remove duplicates from the list as we create it, or make better use of the path information to find out more about the sub-entities themselves.