In the previous post we discussed the process for creating custom entities in the MultiCAD.NET API, and described the CustomObjects example from the SDK. In this post we’re going to focus on the issue of selecting custom entities of the same type in the drawing.
The CustomObjects example creates custom entities that display as framed text strings:
We’ll extend the code for the CustomObjects example by adding functionality to searching for one or more of these entities and changing the text for each of them.
In MultiCAD.NET, when selecting a single object in the drawing, the object manager’s method SelectObject()
is used. The method has the following signatures:
1 2 3 4 |
public static McObjectId SelectObject(string sPromt); public static McObjectId SelectObject(string sPromt, ref Point3d pnt); |
Both methods allow the user to select an object based on a prompt shown in the command line. The second method adds an additional parameter for the coordinates of the selection point.
For selecting multiple objects, the following methods are used:
1 2 3 4 |
public static List SelectObjects(ObjectFilter filter); public static McObjectId[] SelectObjects(string sPromt); |
The first method selects drawing objects using a specific filter, the second one shows a prompt in the command line and allows the user to pick objects by themself.
The filter is represented by an ObjectFilter
object and defines the selection criteria: object type, documents, layers, sheets or area for searching objects. In the following example, the SelectCircles
command gets a list of circle objects that are on the current sheet and intersect a specified rectangular area:
1 2 3 4 5 6 7 8 9 10 11 12 |
[CommandMethod("SelectCircles", CommandFlags.NoCheck | CommandFlags.NoPrefix)] static public void SelectCirclesCmd() { ObjectFilter filter = ObjectFilter.Create(true).AddType(DbCircle.TypeID); filter.Bound = new BoundBlock(Point3d.Origin, new Vector3d(10, 0, 0), new Vector3d(0, 10, 0), new Vector3d(0, 0, 10)); List ids = McObjectManager.SelectObjects(filter); } |
To find all circle entities from the entire drawing (not only on the current sheet), the drawing’s document should be set as a search area:
1 2 3 |
ObjectFilter filter = ObjectFilter.Create(false).SetCurentDocument().AddType(DbCircle.TypeID); |
The second method allows the user to select entities manually instead of using filters, but we need to make sure the user has selected only entities of desired type.
Here we create a new command called TextInBoxEdit
that allows user to select a set of any entities, then filters out only TextInBox
entities from the set and changes text for all of them:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
[CommandMethod("TextInBoxEdit", CommandFlags.NoCheck | CommandFlags.NoPrefix)] static public void TextInBoxEditCmd() { McObjectId[] idSelecteds = McObjectManager.SelectObjects("Select TextInBox primitives to edit"); McObjectId[] idSelectedTextinBox = Array.FindAll(idSelecteds, (s => (s.GetObject()) is TextInBox)); //If at least one entity is selected, sets a new Text value for each of them if (idSelectedTextinBox == null || idSelectedTextinBox.Length == 0) { MessageBox.Show("No TextInBox primitives selected!"); return; } foreach (McObjectId currID in idSelectedTextinBox) { (currID.GetObject() as TextInBox).Text = "Changed text"; } } |
Let’s see how it works.
The command prompts the user to select entities on the drawing:
After the command’s execution, the text strings have been changed in all selected TextInBox
entities: