hi @k005
just create .NetFramwork ClassLibrary and add System.Windows.Forms assembly to your project references,
and try this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using aDB = Autodesk.AutoCAD.DatabaseServices;
using aApp = Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using aGeom = Autodesk.AutoCAD.Geometry;
namespace ACADAPITutorial
{
public class TextToListBox
{
#region current doc
static aApp.Document aDoc => aApp.Application.DocumentManager.MdiActiveDocument;
#endregion
#region initialize
private static bool isDialogOpen = false;
private static SelectionDialog dialog;
static TextToListBox()
{
dialog = new SelectionDialog();
dialog.SelectBtm.Click += SelectBtm_Click;
dialog.FormClosing += Dialog_FormClosing;
}
static void Dialog_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
{
e.Cancel = true;
dialog.Hide();
isDialogOpen = false;
}
#endregion
// select text
private static void SelectBtm_Click(object sender, EventArgs e)
{
// define filter
// TypedValue /filter criteria
var typeValueArray = new aDB.TypedValue[]
{
new aDB.TypedValue((int)aDB.DxfCode.Operator, "<or"), // start ORing
new aDB.TypedValue((int)aDB.DxfCode.Start, "Text"),
new aDB.TypedValue((int)aDB.DxfCode.Start, "MText"),
new aDB.TypedValue((int)aDB.DxfCode.Operator, "or>"), // end ORing
// to get any ACAD Obj dxf name (Text, MText , ...) use ACAD LIST command.
};
var filter = new SelectionFilter(typeValueArray);
// selection options
var selectionOptions = new PromptSelectionOptions()
{
MessageForAdding = "select texts",
};
// Request for objects to be selected in the drawing area
var selectionResult = aDoc.Editor.GetSelection(selectionOptions, filter);
// If the prompt status is OK, objects were selected
if (selectionResult.Status != PromptStatus.OK)
{
aDoc.Editor.WriteMessage($"\n----- No selection found ------");
return;
}
// get object IDs
var allTextIDs = selectionResult.Value.GetObjectIds();
List<string> allText = new List<string>();
using (var ts = aDB.HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction())
{
Type type;
foreach (var id in allTextIDs)
{
var dbObj = id.GetObject(aDB.OpenMode.ForRead);
type = dbObj.GetType();
if (type.Equals(typeof(aDB.DBText)))
{
allText.Add(((aDB.DBText)dbObj).TextString);
}
else if (type.Equals(typeof(aDB.MText)))
{
allText.Add(((aDB.MText)dbObj).Text);
}
}
}
dialog.ListBox.DataSource = allText;
}
[CommandMethod("cieSelectText")]
public void SelectText()
{
// check already opened
if (isDialogOpen) return;
// show modeless dialog
aApp.Application.ShowModelessDialog(dialog);
isDialogOpen = true;
}
}
}
and here is a simple form:
public class SelectionDialog : System.Windows.Forms.Form
{
public System.Windows.Forms.ListBox ListBox { get; private set; }
public System.Windows.Forms.Button SelectBtm { get; private set; }
public SelectionDialog()
{
SelectBtm = new System.Windows.Forms.Button()
{
Text = "select...",
};
ListBox = new System.Windows.Forms.ListBox()
{
};
// wrap all controls into a panel
var panel = new System.Windows.Forms.FlowLayoutPanel()
{
AutoScroll = true,
FlowDirection = System.Windows.Forms.FlowDirection.TopDown,
WrapContents = false,
};
panel.Controls.Add(SelectBtm);
panel.Controls.Add(ListBox);
// add controls to dialog
this.Controls.Add(panel);
}
}