这是基于Adesk指南的C#版本
轻微测试
- using System;
- using Autodesk.AutoCAD.Geometry;
- using Autodesk.AutoCAD.Runtime;
- using Autodesk.AutoCAD.EditorInput;
- using Autodesk.AutoCAD.DatabaseServices;
- using AcDb = Autodesk.AutoCAD.DatabaseServices;
- using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;
- using Autodesk.AutoCAD.ApplicationServices;
- [assembly: CommandClass(typeof(SelectionCommands.FilterSelect))]
- namespace SelectionCommands
- {
- public class FilterSelect
- {
- [CommandMethod("FM")]
- public static void AddParenthesis()
- {
- // Get the current document
- Document doc = acadApp.DocumentManager.MdiActiveDocument;
- // Get the current document editor
- Editor ed = doc.Editor;
- // Get the current database
- Database db = doc.Database;
- using (Transaction tr = db.TransactionManager.StartTransaction())
- {
- // Create a TypedValue array to define the filter criteria
- TypedValue[] ftype =
- new TypedValue[]
- {
- new TypedValue((int)DxfCode.Operator, "<OR"),
- new TypedValue((int)DxfCode.Start, "*TEXT"),//Text and MText
- new TypedValue((int)DxfCode.Start, "DIMENSION"),
- new TypedValue((int)DxfCode.Start, "*LEADER"),//Leader and MLeader
- new TypedValue((int)DxfCode.Operator, "OR>")
- };
- // Assign the filter criteria to a SelectionFilter object
- SelectionFilter fltr = new SelectionFilter(ftype);
- // Request for objects to be selected in the drawing area
- PromptSelectionResult psr;
- psr = ed.GetSelection(fltr);
- SelectionSet sset = null;
- // If the prompt status is OK, objects were selected
- if (psr.Status == PromptStatus.OK)
- {
- sset = psr.Value;
- acadApp.ShowAlertDialog("Number of objects selected: " +
- sset.Count.ToString());
- }
- else
- {
- acadApp.ShowAlertDialog("Number of objects selected: 0");
- }
- // iterate through selected objects
- foreach (ObjectId id in sset.GetObjectIds())
- {
- DBObject obj = (DBObject)tr.GetObject(id, OpenMode.ForRead);
- // catch object name
- string objname = obj.GetRXClass().Name;
- ed.WriteMessage("\n{0}", objname);// debug only
- try
- {
- switch (objname)
- {
- case "AcDbText":
- DBText txt = obj as DBText;
- txt.UpgradeOpen();
- txt.TextString = "(" + txt.TextString + ")";
- break;
- case "AcDbMText":
- MText mtx = obj as MText;
- mtx.UpgradeOpen();
- mtx.Contents = "(" + mtx.Contents + ")";
- break;
- case "AcDbLeader":
- Leader ld = obj as Leader;
- if (ld.AnnoType == AnnotationType.MText)
- {
- MText ldtxt = (MText)tr.GetObject(ld.Annotation, OpenMode.ForRead);
- if (ldtxt != null)
- {
- ld.UpgradeOpen();
- ldtxt.UpgradeOpen();
- ldtxt.Contents = "(" + ldtxt.Contents + ")";
- }
- }
- break;
- case "AcDbMLeader":
- MLeader mld = obj as MLeader;
- if (mld.ContentType == ContentType.MTextContent)
- {
- MText mt = new MText();
- mt.SetDatabaseDefaults();
- mt.SetPropertiesFrom(mld.MText as Entity);
- mt.Contents = "(" + mld.MText.Text + ")";
- mld.UpgradeOpen();
- mld.MText = mt;
- }
- break;
- //varios object names:
- //AcDbRotatedDimension
- //AcDbAlignedDimension
- //AcDb2LineAngularDimension
- //AcDbRadialDimension
- //AcDbDiametricDimension
- //AcDbOrdinateDimension
- //AcDbArcDimension
- case "AcDbRotatedDimension":
- RotatedDimension rd = obj as RotatedDimension;
|