We talked about the 128 count limit of available selection sets in the AutoLISP (Visual LISP) API and demonstrated about it with both code examples and running results. What about it in the modern AutoCAD .NET API?
Let us figure it out with some simple but functional code.
[CommandMethod("TestSelectionSetCountLimit", CommandFlags.Modal)]
public void TestSelectionSetCountLimit()
{
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
try
{
List<SelectionSet> sses = new List<SelectionSet>();
for (int i = 0; i < 130; i++)
sses.Add(ed.SelectAll().Value);
int j = 0;
foreach (SelectionSet ss in sses)
{
if (ss != null)
ed.WriteMessage("\nThe #{0} SS is good and has {1} entities.", ++j, ss.Count);
else
ed.WriteMessage("\nThe #{0} SS is bad!", ++j);
}
}
catch (System.Exception ex)
{
ed.WriteMessage(ex.ToString());
}
}
If an AutoCAD session is launched, a new drawing is created, a circle and two lines are added to the database as shown in this sample test, and the command is run, we will see some results as follows.
Regenerating model.
AutoCAD menu utilities loaded.
Command:
Command:
Command: c CIRCLE Specify center point for circle or [3P/2P/Ttr (tan tan
radius)]:
Specify radius of circle or [Diameter]:
Command: l LINE Specify first point:
Specify next point or [Undo]:
Specify next point or [Undo]:
Specify next point or [Close/Undo]:
Command: TestSelectionSetCountLimit
The #1 SS is good and has 3 entities.
The #2 SS is good and has 3 entities.
The #3 SS is good and has 3 entities.
The #4 SS is good and has 3 entities.
...
The #127 SS is good and has 3 entities.
The #128 SS is good and has 3 entities.
The #129 SS is bad!
The #130 SS is bad!
As can be seen, in the AutoCAD .NET API, the limitation is still there, the amount of all active selection sets cannot be more than 128 (the biggest number that a Signed Char can hold!).
By the way, at this moment, no valid selection sets can be created anymore by anybody including but not limited to .NET API and LISP anywhere either in the current document or in a newly opened one. Worse than the LISP situation that was demonstrated previously, it won’t help even if to close the active document/drawing in which the above test is performed. Apparently, closing the document is not enough to release those selection sets that are created and maintained by the .NET system. The .NET Garbage Collector may help but it’s kind of random by default. If having interest, please feel free to force the GC to collect the selection sets back to the stack pool and see if things change.
Even if the GC can do some work, it’s obvious not a good idea to depend on it after all. Rather, it is good to explicitly dispose of the selection sets of concern when we are done to them.
[CommandMethod("TestSelectionSetCountLimit2", CommandFlags.Modal)]
public void TestSelectionSetCountLimit2()
{
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
try
{
for (int i = 0; i < 130; i++)
{
SelectionSet ss = ed.SelectAll().Value;
if (ss != null)
{
ed.WriteMessage("\nThe #{0} SS is good and has {1} entities.", i + 1, ss.Count);
//Do anything to the selection set such as iterations.
//...
ss.Dispose();
}
else
ed.WriteMessage("\nThe #{0} SS is bad!", i + 1);
}
}
catch (System.Exception ex)
{
ed.WriteMessage(ex.ToString());
}
}
Once again, if we launch an AutoCAD session, create a new drawing, add two lines to it for example, and run the command, we will get the following output this time.
Regenerating model.
AutoCAD menu utilities loaded.
Command:
Command:
Command: l LINE Specify first point:
Specify next point or [Undo]:
Specify next point or [Undo]:
Specify next point or [Close/Undo]:
Command: TestSelectionSetCountLimit2
The #1 SS is good and has 2 entities.
The #2 SS is good and has 2 entities.
The #3 SS is good and has 2 entities.
The #4 SS is good and has 2 entities.
...
The #127 SS is good and has 2 entities.
The #128 SS is good and has 2 entities.
The #129 SS is good and has 2 entities.
The #130 SS is good and has 2 entities.
As can be seen, the #129 and #130 selection sets are successfully created.
To recap, the 128 count limit of selection sets are still there in the AutoCAD .NET API, and the good practice is to dispose of the selection sets of concern as soon as possible. Of course, the using statement can save the Dispose() call and address potential exceptions nicely, so please feel free to use it as well for the SelectionSet objects as usually done to transactions.
The leading edge AutoCAD .NET Addin Wizard (AcadNetAddinWizard) provides a coder, Entity Jigger, to help us create entity jig code automatically, quickly and reliably.
Posted by: |