我在选择目标
acadObject
时遇到了一些麻烦。我通过 selectionset.SelectonScreen
方法获取输入。
在这里,我可以根据我的过滤条件从模型空间中获取更多数量的对象。但我只需要用户提供的一个对象。
我在下面提到了我的代码:
AcadSelectionSet selset= null;
selset=currDoc.selectionset.add("Selset");
short[] ftype=new short[1];
object[] fdata=new object[1];
ftype[0]=2;//for select the blockreference
fdata[0]=blkname;
selset.selectOnScreen ftype,fdata; // Here i can select any no. of blocks according to filter value but i need only one block reference.
请帮我解决这个问题。
这是来自 AutoCAD 开发人员帮助的直接引用
AutoCAD .NET API 有大量文档。
你需要有
[assembly: CommandClass(typeof(namespace.class))]
在命名空间上方,如果您希望能够在
NetLoad
.dll(如果它是类库)之后从命令行调用此命令。
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
[CommandMethod("SelectObjectsOnscreen")]
public static void SelectObjectsOnscreen()
{
// Get the current document and database
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
// Start a transaction
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Request for objects to be selected in the drawing area
PromptSelectionResult acSSPrompt = acDoc.Editor.GetSelection();
// If the prompt status is OK, objects were selected
if (acSSPrompt.Status == PromptStatus.OK)
{
SelectionSet acSSet = acSSPrompt.Value;
// Step through the objects in the selection set
foreach (SelectedObject acSSObj in acSSet)
{
// Check to make sure a valid SelectedObject object was returned
if (acSSObj != null)
{
// Open the selected object for write
Entity acEnt = acTrans.GetObject(acSSObj.ObjectId,
OpenMode.ForWrite) as Entity;
if (acEnt != null)
{
// Change the object's color to Green
acEnt.ColorIndex = 3;
}
}
}
// Save the new object to the database
acTrans.Commit();
}
// Dispose of the transaction
}
}
可以使用其他 Autocad .NET 库(而不是 Interop 库)。但幸运的是,两者并不排斥。
您将需要引用包含以下命名空间的库:
using Autodesk.Autocad.ApplicationServices
using Autodesk.Autocad.EditorInput
using Autodesk.Autocad.DatabaseServices
(您可以从 Autodesk 免费下载 Object Arx 库):
您需要从 autocad
Editor
访问 Document
。
根据您显示的代码,您可能正在使用 AcadDocument
文档。
因此,要将 AcadDocument
转换为 Document
,请执行以下操作:
//These are extension methods and must be in a static class
//Will only work if Doc is saved at least once (has full name) - if document is new, the name will be
public static Document GetAsAppServicesDoc(this IAcadDocument Doc)
{
return Application.DocumentManager.OfType<Document>().First(D => D.Name == Doc.FullOrNewName());
}
public static string FullOrNewName(this IAcadDocument Doc)
{
if (Doc.FullName == "")
return Doc.Name;
else
return Doc.FullName;
}
一旦您获得了
Document
,请获得 Editor
,然后获得 GetSelection(Options, Filter)
选项包含属性
SingleOnly
和 SinglePickInSpace
。将其设置为 true
即可满足您的要求。 (两个都试试看哪个效果更好)
//Seleciton options, with single selection
PromptSelectionOptions Options = new PromptSelectionOptions();
Options.SingleOnly = true;
Options.SinglePickInSpace = true;
//This is the filter for blockreferences
SelectionFilter Filter = new SelectionFilter(new TypedValue[] { new TypedValue(0, "INSERT") });
//calls the user selection
PromptSelectionResult Selection = Document.Editor.GetSelection(Options, Filter);
if (Selection.Status == PromptStatus.OK)
{
using (Transaction Trans = Document.Database.TransactionManager.StartTransaction())
{
//This line returns the selected items
AcadBlockReference SelectedRef = (AcadBlockReference)(Trans.GetObject(Selection.Value.OfType<SelectedObject>().First().ObjectId, OpenMode.ForRead).AcadObject);
}
}
选择一个实体的更好方法是使用 GetEntity 方法而不是 GetSelection。使用示例可以是:
/// <summary> Prompt the user to select one object. </summary>
/// <param name="promptMessage"> Message to display before prompting the user. </param>
/// <returns> ObjectId of the selected object, ObjectId.Null if selection fails. </returns>
internal static ObjectId SelectObject(string promptMessage = "")
{
AcDoc acDoc = AcAp.DocumentManager.MdiActiveDocument;
// Prepare objectId for returning
ObjectId selection;
// Prepare prompt options
PromptEntityOptions options = new PromptEntityOptions(promptMessage)
{
AllowNone = false, // Do not allow empty result
};
// Get the selection result
PromptEntityResult result = acDoc.Editor.GetEntity(options);
// If selection went OK, save the result, otherwise ObjectId.Null
if (result.Status == PromptStatus.OK)
{
selection = result.ObjectId;
}
else
{
acDoc.Editor.WriteMessage("\n Selection failure.");
selection = ObjectId.Null;
}
// Return the selected objectId
return selection;
}