讓用戶選取點(diǎn),線,實(shí)體,角度,距離,............................................
//?Copyright?2004-2006?by?Autodesk,?Inc.
//
//Permission?to?use,?copy,?modify,?and?distribute?this?software?in
//object?code?form?for?any?purpose?and?without?fee?is?hereby?granted,?
//provided?that?the?above?copyright?notice?appears?in?all?copies?and?
//that?both?that?copyright?notice?and?the?limited?warranty?and
//restricted?rights?notice?below?appear?in?all?supporting?
//documentation.
//
//AUTODESK?PROVIDES?THIS?PROGRAM?"AS?IS"?AND?WITH?ALL?FAULTS.?
//AUTODESK?SPECIFICALLY?DISCLAIMS?ANY?IMPLIED?WARRANTY?OF
//MERCHANTABILITY?OR?FITNESS?FOR?A?PARTICULAR?USE.??AUTODESK,?INC.?
//DOES?NOT?WARRANT?THAT?THE?OPERATION?OF?THE?PROGRAM?WILL?BE
//UNINTERRUPTED?OR?ERROR?FREE.
//
//Use,?duplication,?or?disclosure?by?the?U.S.?Government?is?subject?to?
//restrictions?set?forth?in?FAR?52.227-19?(Commercial?Computer
//Software?-?Restricted?Rights)?and?DFAR?252.227-7013(c)(1)(ii)
//(Rights?in?Technical?Data?and?Computer?Software),?as?applicable.

using?System;
using?Autodesk.AutoCAD.DatabaseServices;
using?Autodesk.AutoCAD.Runtime;
using?Autodesk.AutoCAD.Geometry;
using?Autodesk.AutoCAD.ApplicationServices;
using?Autodesk.AutoCAD.EditorInput;

namespace?Prompts


{
????public?class?PromptsTest

????
{
????????static?PromptAngleOptions?useThisAngleOption;
????????static?PromptDoubleResult?useThisAngleResult;
????????static?PromptPointOptions?useThisPointOption;
????????static?PromptPointResult?useThisPointResult;
????????static?PromptEntityOptions?useThisEntityOption;
????????static?PromptEntityResult?useThisEntityResult;

????????//A?small?function?that?shows?how?to?prompt?for?an?integer
????????[CommandMethod("GetInteger")]?
????????public?void?integerTest()

????????
{
????????????Editor?ed?=?Application.DocumentManager.MdiActiveDocument.Editor;
????????????PromptIntegerOptions?opt0?=?new?PromptIntegerOptions("Enter?your?age");
????????????opt0.AllowNegative?=?false;
????????????opt0.AllowNone?=?false;
????????????opt0.AllowZero?=?false;
????????????opt0.DefaultValue?=?1;
????????????PromptIntegerResult?IntRes?=?ed.GetInteger(opt0);
????????????if(IntRes.Status?==?PromptStatus.OK)

????????????
{
????????????????ed.WriteMessage(string.Format("\nYou?entered?{0}",IntRes.Value));
????????????}
????????}

????????//This?method?prompts?for?a?double?value.
????????//Pi,Two-pi??are?valid?keywords?that?can?be?entered
????????//by?the?user?at?the?prompt.
????????[CommandMethod("GetDouble")]?
????????public?void?DoubleTest()

????????
{
????????????Editor?ed?=?Application.DocumentManager.MdiActiveDocument.Editor;
????????????PromptDoubleOptions?opt1?=?new?PromptDoubleOptions("Enter?a?number?or?Pi/Two-pi");
????????????opt1.Keywords.Add("Pi");
????????????opt1.Keywords.Add("Two-pi");
????????????opt1.AllowNone?=?false;
????????????opt1.AllowZero?=?false;
????????????opt1.DefaultValue?=?1.0;

????????????PromptDoubleResult?doubleRes?=?ed.GetDouble(opt1);
????????????if(doubleRes.Status?==?PromptStatus.Keyword)

????????????
{
????????????????switch?(doubleRes.StringResult)

????????????????
{
????????????????????case?"Pi":
????????????????????????ed.WriteMessage("Value?is?3.14");
????????????????????????break;
????????????????????case?"Two-pi":
????????????????????????ed.WriteMessage("\nValue?is?6.28");
????????????????????????break;
????????????????????default:
????????????????????????ed.WriteMessage("\nKeyword?unknown");
????????????????????????break;
????????????????}
????????????}
????????????if(doubleRes.Status?!=?PromptStatus.OK)

????????????
{
????????????????ed.WriteMessage("\nUser?entered:?"?+?doubleRes.Status.ToString());
????????????}
????????}

????????//Gets?the?radius?of?the?circle?from?the?user?using?GetDistance?command
????????//and?draw?the?circle.
????????//The?user?can?either?specify?the?number?in?the?command?prompt?or
????????//The?user?can?set?the?distance?(in?this?case?radius?of?circle)?also
????????//by?specifying?two?locations?on?the?graphics?screen.
????????//AutoCAD?draws?a?rubber-band?line?from?the?first?point?to
????????//the?current?crosshair?position?to?help?the?user?visualize?the?distance.
????????[CommandMethod("GetDistance")]?
????????public?void?DistanceTest()

????????
{
????????????Editor?ed?=?Application.DocumentManager.MdiActiveDocument.Editor;
????????????PromptDistanceOptions?opt1?=?new?PromptDistanceOptions("Enter?the?radius?of?the?circle");
????
????????????opt1.AllowNegative?=?false;
????????????opt1.AllowZero?=?false;
????????????opt1.AllowNone?=?false;
????????????opt1.UseDashedLine?=?true;

????????????PromptDoubleResult?res?=?ed.GetDistance(opt1);
????????????
????????????if(res.Status?==?PromptStatus.OK)

????????????
{
????????????????Point3d?center?=?new?Point3d(9.0,?3.0,?0.0);
????????????????Vector3d?normal?=?new?Vector3d(0.0,?0.0,?1.0);
????????????????Database?db?=?Application.DocumentManager.MdiActiveDocument.Database;
????????????????Autodesk.AutoCAD.DatabaseServices.TransactionManager?tm?=?db.TransactionManager;
????????????????using?(Transaction?myT?=?tm.StartTransaction())

????????????????
{
????????????????????BlockTable?bt?=?(BlockTable)tm.GetObject(db.BlockTableId,?OpenMode.ForRead,?false);
????????????????????BlockTableRecord?btr?=?(BlockTableRecord)tm.GetObject(bt[BlockTableRecord.ModelSpace],?OpenMode.ForWrite,?false);
????????????????????using?(Circle?pcirc?=?new?Circle(center,?normal,?res.Value))

????????????????????
{
????????????????????????btr.AppendEntity(pcirc);
????????????????????????tm.AddNewlyCreatedDBObject(pcirc,?true);
????????????????????}
????????????????????myT.Commit();
????????????????}
????????????????
????????????}
????????????
????????}

????

????????//The?user?is?prompted?to?enter?the?start?angle?and?end?angle?at?the
????????//command?prompt.??Using?which?an?arc?is?created.

????????//Not?only?by?entering?the?values?but?The?user?can?set?the?angle?also?by?
????????//specifying?two?2D?locations?on?the?graphics?screen.?AutoCAD?draws?a
????????//rubber-band?line?from?the?first?point?to?the?current?crosshair?position?
????????//to?help?the?user?visualize?the?angle.

????????//Also?attached?to?this?function?is?the?input?context?reactor?event
????????//PromptingForAngle?and?PromptedForAngle.?During?ed.GetAngle(),?these
????????//events?gets?fired.?The?call?back?function?just?remembers?the?prompt?option
????????//that?the?user?has?set?initially?and?also?the?prompt?result?that?the
????????//user?sees?after?he?calls?GetAngle()?method.


????????[CommandMethod("GetAngle")]?
????????public?void?AngleTest()

????????
{
????????????Editor?ed?=?Application.DocumentManager.MdiActiveDocument.Editor;
????????????PromptAngleOptions?opt1?=?new?PromptAngleOptions("Enter?start?angle?of?the?arc");
????????????
????????????opt1.AllowNone?=?false;
????????????opt1.UseDashedLine?=?true;
????????????
????????????//USAGE?OF?INPUT?CONTEXT?REACTORS
????????????ed.PromptingForAngle?+=?new?PromptAngleOptionsEventHandler(handle_promptangleOptions);
????????????ed.PromptedForAngle?+=?new?PromptDoubleResultEventHandler(handle_promptAngleResult);
????????????
????????????PromptDoubleResult?startAngle?=?ed.GetAngle(opt1);
????????????
????????????ed.PromptingForAngle?-=?new?PromptAngleOptionsEventHandler(handle_promptangleOptions);
????????????ed.PromptedForAngle?-=?new?PromptDoubleResultEventHandler(handle_promptAngleResult);


????????????opt1.Message?=?"Enter?end?angle?of?the?arc";
????????????PromptDoubleResult?endAngle?=?ed.GetAngle(opt1);

????????????PromptDoubleOptions?opt2?=?new?PromptDoubleOptions("Enter?the?radius?of?the?arc(double)");
????????????opt2.Message?=?"Enter?the?radius?of?the?arc(double)";
????????????PromptDoubleResult?radius?=?ed.GetDouble(opt2);

????????????if(startAngle.Status?==?PromptStatus.OK?&&?endAngle.Status?==?PromptStatus.OK?&&?radius.Status?==?PromptStatus.OK)

????????????
{
????????????????Point3d?center?=?new?Point3d(30.0,?19.0,?0.0);
????????????????Database?db?=?Application.DocumentManager.MdiActiveDocument.Database;
????????????????Autodesk.AutoCAD.DatabaseServices.TransactionManager?tm?=?db.TransactionManager;
????????????????
????????????????using?(Transaction?myT?=?tm.StartTransaction())

????????????????
{
????????????????????BlockTable?bt?=?(BlockTable)tm.GetObject(db.BlockTableId,?OpenMode.ForRead,?false);
????????????????????BlockTableRecord?btr?=?(BlockTableRecord)tm.GetObject(bt[BlockTableRecord.ModelSpace],?OpenMode.ForWrite,?false);
????????????????????using?(Arc?arc1?=?new?Arc(center,?radius.Value,?startAngle.Value,?endAngle.Value))

????????????????????
{
????????????????????????btr.AppendEntity(arc1);
????????????????????????tm.AddNewlyCreatedDBObject(arc1,?true);
????????????????????}
????????????????????myT.Commit();
????????????????}
????????????????
????????????}
????????????else

????????????
{
????????????????ed.WriteMessage("Arc?cannot?be?constructed");
????????????}
????????
????????}



????????[CommandMethod("useAngle")]?
????????public?void?UsingAngleOptionsAndResults()

????????
{
????????????Editor?ed?=?Application.DocumentManager.MdiActiveDocument.Editor;
????????????//Verify?GetAngle?has?been?ran?once?before?executing?this?one
????????????if?(?useThisAngleOption?==?null?||?useThisAngleResult?==?null?)

????????????
{
????????????????ed.WriteMessage("Please?run?GetAngle?command?first");
????????????????return;
????????????}
????????????//Using?the?stored?PromptAngleOption.
????????????PromptDoubleResult?res1?=?ed.GetAngle(useThisAngleOption);
????????????//Using?the?stored?PromptAngleResult.
????????????PromptDoubleResult?res2?=?useThisAngleResult;
????????????if(res1.Status?==?PromptStatus.OK?&&?res2.Status?==?PromptStatus.OK)

????????????
{
????????????????Database?db?=?Application.DocumentManager.MdiActiveDocument.Database;
????????????????Autodesk.AutoCAD.DatabaseServices.TransactionManager?tm?=?db.TransactionManager;
????????????????
????????????????using?(Transaction?myT?=?tm.StartTransaction())

????????????????
{?????????????
????????????????????BlockTable?bt?=?(BlockTable)tm.GetObject(db.BlockTableId,OpenMode.ForRead,false);
????????????????????BlockTableRecord?btr?=?(BlockTableRecord)tm.GetObject(bt[BlockTableRecord.ModelSpace],OpenMode.ForWrite,false);
????????????????????Point3d?center?=?new?Point3d(30.0,?19.0,?0.0);
????????????????????using?(Arc?arc1?=?new?Arc(center,?res1.Value,?res2.Value,?5.0))

????????????????????
{
????????????????????????arc1.ColorIndex?=?3;
????????????????????????btr.AppendEntity(arc1);
????????????????????????myT.AddNewlyCreatedDBObject(arc1,?true);
????????????????????}
????????????????????myT.Commit();
????????????????}
????????????????
????????????}
????????????else

????????????
{
????????????????ed.WriteMessage("Arc?cannot?be?constructed");
????????????}
????????}

//????????Drawing?a?line?using?the?points?entered?by?the?user.
//????????Prompt?the?user?for?the?start?point?and?end?point?of?the?line.

//????????The?AutoCAD?user?can?specify?the?point?by?entering?a?coordinate?in
//????????the?current?units?format;?The?user?can?specify?the?point?also?by?specifying?
//????????a?location?on?the?graphics?screen.
????????[CommandMethod("GetPoint")]?
????????public?void?PointTest()

????????
{
????????????Editor?ed?=?Application.DocumentManager.MdiActiveDocument.Editor;
????????????PromptPointOptions?ptopts?=?new?PromptPointOptions("Enter?start?point?of?the?line");
????????????ptopts.BasePoint?=?new?Point3d(1,1,1);
????????????ptopts.UseDashedLine?=?true;
????????????ptopts.Message?=?"Enter?start?point?of?the?line";
????????????
????????????ed.PromptingForPoint?+=?new?PromptPointOptionsEventHandler(handle_promptPointOptions);
????????????ed.PromptedForPoint?+=?new?PromptPointResultEventHandler(handle_promptPointResult);
????????????PromptPointResult?ptRes?=?ed.GetPoint(ptopts);
????????????ed.PromptingForPoint?-=?new?PromptPointOptionsEventHandler(handle_promptPointOptions);
????????????ed.PromptedForPoint?-=?new?PromptPointResultEventHandler(handle_promptPointResult);
????????????

????????????Point3d?start?=?ptRes.Value;
????????????if(ptRes.Status?==?PromptStatus.Cancel)

????????????
{
????????????????ed.WriteMessage("Taking?(0,0,0)?as?the?start?point");
????????????}

????????????ptopts.Message?="Enter?end?point?of?the?line:?";
????????????ptRes?=?ed.GetPoint(ptopts);
????????????Point3d?end?=?ptRes.Value;
????????????if(ptRes.Status?==?PromptStatus.Cancel)

????????????
{
????????????????ed.WriteMessage("Taking?(0,0,0)?as?the?end?point");
????????????}
????????????
????????????Database?db?=?Application.DocumentManager.MdiActiveDocument.Database;
????????????Autodesk.AutoCAD.DatabaseServices.TransactionManager?tm?=?db.TransactionManager;
????????????
????????????using?(Transaction?myT?=?tm.StartTransaction())

????????????
{???
????????????????BlockTable?bt?=?(BlockTable)tm.GetObject(db.BlockTableId,OpenMode.ForRead,false);
????????????????BlockTableRecord?btr?=?(BlockTableRecord)tm.GetObject(bt[BlockTableRecord.ModelSpace],OpenMode.ForWrite,false);
????????????????using?(Line?myline?=?new?Line(start,?end))

????????????????
{
????????????????????btr.AppendEntity(myline);
????????????????????tm.AddNewlyCreatedDBObject(myline,?true);
????????????????}
????????????????myT.Commit();
????????????}
????????????
????????}


????????[CommandMethod("usepoint")]?
????????public?void?UsingPointOptionsAndResults()

????????
{
????????????Editor?ed?=?Application.DocumentManager.MdiActiveDocument.Editor;
????????????//Verify?GetPoint?has?been?ran?once?before?executing?this?one
????????????if?(?useThisPointOption?==?null?||?useThisPointResult?==?null?)

????????????
{
????????????????ed.WriteMessage("Please?run?GetPoint?command?first");
????????????????return?;
????????????}
????????????//Using?the?stored?PromptPointOption.
????????????PromptPointResult?res1?=?ed.GetPoint(useThisPointOption);
????????????//Using?the?stored?PromptPointResult.
????????????PromptPointResult?res2?=?useThisPointResult;
????????????
????????????if(res1.Status?!=?PromptStatus.Cancel?&&?res2.Status?!=?PromptStatus.Cancel)

????????????
{
????????????????Point3d?start?=?res1.Value;
????????????????Point3d?end?=?res2.Value;
????????????????Database?db?=?Application.DocumentManager.MdiActiveDocument.Database;
????????????????Autodesk.AutoCAD.DatabaseServices.TransactionManager?tm?=?db.TransactionManager;
????????????????using?(Transaction?myT?=?tm.StartTransaction())

????????????????
{?????????????
????????????????????BlockTable?bt?=?(BlockTable)tm.GetObject(db.BlockTableId,OpenMode.ForRead,false);
????????????????????BlockTableRecord?btr?=?(BlockTableRecord)tm.GetObject(bt[BlockTableRecord.ModelSpace],OpenMode.ForWrite,false);
????????????????????using?(Line?myline?=?new?Line(start,?end))

????????????????????
{
????????????????????????myline.ColorIndex?=?3;
????????????????????????btr.AppendEntity(myline);
????????????????????????myT.AddNewlyCreatedDBObject(myline,?true);
????????????????????}
????????????????????myT.Commit();
????????????????}
????????????????
????????????}
????????}
????????
????????//Here?the?user?is?prompted?for?a?string?that?could?be?used?as?a?keyword.
????????//We?then?test?to?see?if?the?user?entered?string?has?been?taken?as?a?valid?
????????//keyword?or?not?by?asking?the?user?to?enter?that?string?as?a?keyword?in?the
????????//command?prompt.?If?it?is?not?a?valid?one?then?the?user?is?prompted?for?a?
????????//different?value?(which?is?a?bug).
????????[CommandMethod("GetKW")]?
????????public?void?KWandStringTest()

????????
{
????????????Editor?ed?=?Application.DocumentManager.MdiActiveDocument.Editor;
????????????PromptStringOptions?stropts?=?new?PromptStringOptions("Enter?a?string?you?want?to?use?as?keyword");
????????????PromptKeywordOptions?kwopts?=?new?PromptKeywordOptions("Enter?a?string?you?want?to?use?as?keyword");
????????????stropts.AllowSpaces?=?false;
????????????PromptResult?str?=?ed.GetString(stropts);
????????????kwopts.Keywords.Add(str.StringResult);
????????????kwopts.Keywords.Add("onemore");
????????????kwopts.Message?=?"Enter?the?word?that?u?just?enterd?to?test?if?its?a?valid?keyword?or?not";
????????????PromptResult?kw?=?ed.GetKeywords(kwopts);
????????????if(kw.Status?==?PromptStatus.OK)

????????????
{
????????????????ed.WriteMessage("You?entered?a?valid?keyword");
????????????}
????????????else

????????????
{
????????????????ed.WriteMessage("You?didn't?enter?a?valid?keyword:?"+kw.Status.ToString());
????????????}
????????}

????????//Try?to?draw?a?few?entities?in?the?drawing?for?the?user?to?select.
????????//It?prompts?the?user?to?select?some?entities?and?finally?types
????????//the?name?of?the?selected?entity?at?the?command?prompt.
????????//Also?added?the?two?input?context?reactor?events:
????????//PromptingForEntity?and?PromptedForEntity
????????[CommandMethod("Getentity")]?
????????public?void?EntityTest()

????????
{
????????????Editor?ed?=?Application.DocumentManager.MdiActiveDocument.Editor;
????????????PromptEntityOptions?entopts?=?new?PromptEntityOptions("Pick?an?entity?of?your?choice?from?the?drawing");
????????????entopts.Message?=?"Pick?an?entity?of?your?choice?from?the?drawing";
????????????PromptEntityResult?ent=null;
????????????//ADDED?INPUT?CONTEXT?REACTOR????
????????????ed.PromptingForEntity?+=?new?PromptEntityOptionsEventHandler(handle_promptEntityOptions);
????????????ed.PromptedForEntity?+=?new?PromptEntityResultEventHandler(handle_promptEntityResult);
????????????????
????????????try

????????????
{
????????????????ent?=?ed.GetEntity(entopts);
????????????}
????????????catch

????????????
{
????????????????ed.WriteMessage("You?did?not?select?a?valid?entity");
????????????????ed.PromptingForEntity?-=?new?PromptEntityOptionsEventHandler(handle_promptEntityOptions);
????????????????ed.PromptedForEntity?-=?new?PromptEntityResultEventHandler(handle_promptEntityResult);
????????????????????
????????????}
????????????ed.PromptingForEntity?-=?new?PromptEntityOptionsEventHandler(handle_promptEntityOptions);
????????????ed.PromptedForEntity?-=?new?PromptEntityResultEventHandler(handle_promptEntityResult);
????
????????????if(ent.Status?!=?PromptStatus.Error)

????????????
{
????????????????ObjectId?entid?=?ent.ObjectId;
????????????????Database?db?=?Application.DocumentManager.MdiActiveDocument.Database;
????????????????Autodesk.AutoCAD.DatabaseServices.TransactionManager?tm?=?db.TransactionManager;
????????????????using?(Transaction?myT?=?tm.StartTransaction())

????????????????
{?????????????
????????????????????Entity?entity?=?(Entity)tm.GetObject(entid,OpenMode.ForRead,true);
????????????????????ed.WriteMessage("You?selected:?"+entity.GetType().FullName);
????????????????????myT.Commit();
????????????????}
????????????}
????????????
????????}

????

????????//This?method?just?makes?use?of?the?entity?option?and?entity?result
????????//that?was?stored?in?a?static?variable?when?the?PromptingForEntity
????????//and?PromptingForEntity?events?where?fired?from?EntityTest()?function.
????????[CommandMethod("useentity")]?
????????public?void?UsingEntityOptionsAndResults()

????????
{
????????????Editor?ed?=?Application.DocumentManager.MdiActiveDocument.Editor;
????????????//Using?the?stored?PromptEntityOption.
????????????PromptEntityResult?res1?=?ed.GetEntity(useThisEntityOption);
????????????//Using?the?stored?PromptEntityResult.
????????????PromptEntityResult?res2?=?useThisEntityResult;
????????????ed.WriteMessage("\nCHANGING?THE?ALREADY?SELECTED?ENTITIES?COLORS?TO?GREEN");
????????????if(res2.Status?!=?PromptStatus.Error)

????????????
{
????????????????ObjectId?entid?=?res2.ObjectId;
????????????????Database?db?=?Application.DocumentManager.MdiActiveDocument.Database;
????????????????Autodesk.AutoCAD.DatabaseServices.TransactionManager?tm?=?db.TransactionManager;
????????????????ObjectId?nowSelEntid?=?res1.ObjectId;
????????????????
????????????????using?(Transaction?myT?=?tm.StartTransaction())

????????????????
{?????????????
????????????????????Entity?Oldentity?=?(Entity)tm.GetObject(entid,OpenMode.ForWrite,true);
????????????????????Oldentity.ColorIndex?=?2;
????????????????????ed.WriteMessage("\nYou?Now?selected:?"+Oldentity.GetType().FullName);
????????????????????myT.Commit();
????????????????}
????????????}

????????}

????????//Try?to?draw?a?few?nested?entities?like?blocks?and?xrefs?in?the?drawing?for?the?user?to?select.
????????//if?the?user?selects?a?nested?entity?then?the?name?of?the?nested?entity?is?displayed.
????????//Finally?after?the?user?is?done?selecting?the?entities,?a?non?interactive?selection?is?made
????????//at?the?point?30.4,11.6,0?and?the?name?of?the?nested?entity?if?any?is?displayed.
????????[CommandMethod("GetNestentity")]?
????????public?void?NestedEntityTest()

????????
{
????????????ObjectIdCollection?coll?=?new?ObjectIdCollection();
????????????Editor?ed?=?Application.DocumentManager.MdiActiveDocument.Editor;
????????????PromptNestedEntityOptions?entopts?=?new?PromptNestedEntityOptions("prompt?nested?entity");
????????????entopts.AllowNone?=?true;
????????????entopts.Keywords.Add("Done");
????????????PromptNestedEntityResult?ent=?null;
????????????Database?db?=?Application.DocumentManager.MdiActiveDocument.Database;
????????????Autodesk.AutoCAD.DatabaseServices.TransactionManager?tm?=?db.TransactionManager;
????????????
????????????using?(Transaction?myT?=?tm.StartTransaction())

????????????
{?????
????????????????while(true)

????????????????
{

????????????????????entopts.Message?=?"\nPick?an?entity?of?your?choice?from?the?drawing?or?type?Done";

????????????????????try

????????????????????
{
????????????????????????ent?=?ed.GetNestedEntity(entopts);
????????????????????}
????????????????????catch

????????????????????
{
????????????????????????ed.WriteMessage("\nYou?did?not?select?a?valid?nested?entity");
????????????????????????break;
????????????????????}
????????????????????if(ent.Status?==?PromptStatus.Keyword)

????????????????????
{
????????????????????????if(ent.StringResult.Equals("Done"))
????????????????????????????break;
????????????????????}

????????????????????try

????????????????????
{
????????????????????????if(ent.GetContainers().Length?>?0)

????????????????????????
{
????????????????????????????Entity?entity;
????????????????????????????foreach(ObjectId?oid?in?ent.GetContainers())

????????????????????????????
{
????????????????????????????????entity?=?(Entity)tm.GetObject(oid,OpenMode.ForRead,true);
????????????????????????????????ed.WriteMessage("You?selected:?"+entity.GetType().FullName);
????????????????????????????}
????????????????????????????break;
????????????????????????}
????????????????????????else

????????????????????????
{
????????????????????????????ed.WriteMessage("You?did?not?select?any?nested?entity");
????????????????????????}
????????????????????}
????????????????????catch

????????????????????
{
????????????????????????ed.WriteMessage("You?are?Done?or?did?not?select?any?nested?entity");
????????????????????????myT.Commit();
????????????????????????return;
????????????????????}

????????????????}

????????????????entopts.NonInteractivePickPoint?=?new?Point3d(30.4,11.6,0);
????????????????entopts.UseNonInteractivePickPoint?=?true;

????????????????try

????????????????
{
????????????????????ent?=?ed.GetNestedEntity(entopts);
????????????????????if(ent.GetContainers().Length?>?0)

????????????????????
{
????????????????????????Entity?entity;
????????????????????????foreach(ObjectId?oid?in?ent.GetContainers())

????????????????????????
{
????????????????????????????entity?=?(Entity)tm.GetObject(oid,OpenMode.ForRead,true);
????????????????????????????ed.WriteMessage(entity.GetType().FullName+"?has?been?selected");
????????????????????????}
????????????????????????}
????????????????????else

????????????????????
{
????????????????????????ed.WriteMessage("No?nested?entity?was?selected");
????????????????????}
????????????????}
????????????????catch

????????????????
{
????????????????????ed.WriteMessage("\nNo?entity?was?selected");
????????????????????
????????????????}
????????????????myT.Commit();
????????????}
????????????????
????????}
????????private?static?void?handle_promptEntityOptions(object?sender,?PromptEntityOptionsEventArgs?e)

????????
{
????????????useThisEntityOption?=?e.Options;

????????}
????????private?static?void?handle_promptEntityResult(object?sender,?PromptEntityResultEventArgs?e)

????????
{
????????????useThisEntityResult?=?e.Result;

????????}
????????private?static?void?handle_promptPointOptions(object?sender,?PromptPointOptionsEventArgs?e)

????????
{
????????????useThisPointOption?=?e.Options;

????????}
????????private?static?void?handle_promptPointResult(object?sender,?PromptPointResultEventArgs?e)

????????
{
????????????useThisPointResult?=?e.Result;

????????}
????????private?static?void?handle_promptangleOptions(object?sender,?PromptAngleOptionsEventArgs?e)

????????
{
????????????useThisAngleOption?=?e.Options;

????????}
????????private?static?void?handle_promptAngleResult(object?sender,?PromptDoubleResultEventArgs?e)

????????
{
????????????useThisAngleResult?=?e.Result;
????????}

????}
}

SelectionSet - .NET API Sample
?(C) Copyright 2004-2006 by Autodesk, Inc.
This sample demonstrates the basics of using the Selectionset .NET API with AutoCAD.? It has the following commnads:
SSet - this command allows the user to do simple selection of entities and ignores
?????? all entities other than circles that are red in color.
Single - This command allows only a single entity to be picked
Implied - This command demonstrates Implied pick
CWindow? - This command demonstrates the Cwindow Selection. For this command to work, AutoCAD elements must be available
?????????? within the boundary limits 1.0,1.0,0.0 and 10.0,10.0,0.0
CPolygon - This command demonstrates CPolygon selection
OptionSel - This command has a number of keywords attached to the selection.
???????????? Based on the keyword that user enters, it does either
???????????? fence,window,windowpolygon,lastentitySelection,PreviousSelection.
SSetReactors - This command demonstrates some selection Related input context
?????????????? reactors. It exercises promptingForSelection, PromptedForSelection
?????????????? and SelectionAdded events of the editor.
useSelection - This command helps to use the previous selection options and
?????????????? selection Results that we had during the execution of SSetReactors
?????????????? command. It demonstrates how to use PromptingForSelection and
?????????????? PromptedForSelection events.
To run the sample:? Build the application adding acdbmgd.dll and acmgd.dll as references, and use NETLOAD command to load selectionset.dll from the \bin subfolder.
?
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.EditorInput
Namespace Selection
??? _
??? Public Class AcEdSSGetCommand
??????? Shared SelentityCount As Integer = 0
??????? Shared UseThisSelectionResult As PromptSelectionResult
??????? Shared UseThisSelectionOption As PromptSelectionOptions = Nothing
??????? 'This command does a simple selection and ignores all
??????? 'entities other than red circles
??????? <CommandMethod("SSet")> _
??????? Public Sub ssGetFilter()
??????????? 'Setup
??????????? Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
??????????? 'Declare our filter entries this way.
??????????? 'We build them the same way in ObjectARX.
??????????? 'DxfCode.Start (equal to 0) - This is for 'Entity Name'
??????????? 'DxfCode.Color is for ACI color - 1 = Red
??????????? Dim values() As TypedValue = { _
??????????????? New TypedValue(DxfCode.Start, "CIRCLE"), _
??????????????? New TypedValue(DxfCode.Color, 1) _
??????????????? }
??????????? Dim sfilter As New SelectionFilter(values) ' Create the filter using our values...
??????????? 'Set the selection options
??????????? Dim SelOpts As New PromptSelectionOptions()
??????????? SelOpts.MessageForAdding = "Select Red Circles:"
??????????? SelOpts.AllowDuplicates = True
??????????? 'Make the selection:
??????????? Dim res As PromptSelectionResult = ed.GetSelection(SelOpts, sfilter)
??????????? If Not res.Status = PromptStatus.OK Then Return
??????????? Dim SS As Autodesk.AutoCAD.EditorInput.SelectionSet = res.Value
??????????? Dim idarray As ObjectId() = SS.GetObjectIds()
??????????? Dim db As Database = Application.DocumentManager.MdiActiveDocument.Database
??????????? Dim tm As Autodesk.AutoCAD.DatabaseServices.TransactionManager = db.TransactionManager
??????????? 'start a transaction
??????????? Dim myT As Transaction = tm.StartTransaction()
??????????? Try
??????????????? Dim id As ObjectId
??????????????? For Each id In idarray
??????????????????? Dim entity As Entity = tm.GetObject(id, OpenMode.ForRead, True)
??????????????????? ed.WriteMessage((ControlChars.Lf + "You selected: " + entity.GetType().FullName))
??????????????? Next id
??????????? Finally
??????????????? myT.Dispose()
??????????? End Try
??????? End Sub
??????? 'This command allows only a single entity to be picked
??????? <CommandMethod("single")> _
??????? Public Sub singlePick()
??????????? Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
??????????? Dim Opts As New PromptSelectionOptions()
??????????? Opts.SingleOnly = True
??????????? Opts.MessageForAdding = "Select a single entity"
??????????? Dim res As PromptSelectionResult = ed.GetSelection(Opts)
??????????? If res.Status = PromptStatus.Error Then
??????????????? Return
??????????? End If
??????????? Dim SS As Autodesk.AutoCAD.EditorInput.SelectionSet = res.Value
??????????? Dim idarray As ObjectId() = SS.GetObjectIds()
??????????? Dim db As Database = Application.DocumentManager.MdiActiveDocument.Database
??????????? Dim tm As Autodesk.AutoCAD.DatabaseServices.TransactionManager = db.TransactionManager
??????????? Dim myT As Transaction = tm.StartTransaction()
??????????? Try
??????????????? Dim id As ObjectId
??????????????? For Each id In idarray
??????????????????? Dim entity As Entity = tm.GetObject(id, OpenMode.ForWrite, True)
??????????????????? ed.WriteMessage((ControlChars.Lf + "You selected: " + entity.GetType().FullName))
??????????????? Next id
??????????? Finally
??????????????? myT.Dispose()
??????????? End Try
??????? End Sub 'singlePick
??????? 'This command demonstrates Implied pick
??????? <CommandMethod("Implied", CommandFlags.UsePickSet)> _
??????? Public Sub ImpliedPick()
??????????? Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
??????????? Dim res As PromptSelectionResult = ed.SelectImplied()
??????????? If res.Status = PromptStatus.Error Then Return
??????????? Dim SS As Autodesk.AutoCAD.EditorInput.SelectionSet = res.Value
??????????? Dim idarray As ObjectId() = SS.GetObjectIds()
??????????? Dim db As Database = Application.DocumentManager.MdiActiveDocument.Database
??????????? Dim tm As Autodesk.AutoCAD.DatabaseServices.TransactionManager = db.TransactionManager
??????????? Dim myT As Transaction = tm.StartTransaction()
??????????? Try
??????????????? Dim id As ObjectId
??????????????? For Each id In idarray
??????????????????? Dim entity As Entity = tm.GetObject(id, OpenMode.ForWrite, True)
??????????????????? ed.WriteMessage((ControlChars.Lf + "You selected: " + entity.GetType().FullName))
??????????????? Next id
??????????? Finally
??????????????? myT.Dispose()
??????????? End Try
??????? End Sub 'ImpliedPick
??????? 'This command demonstrates the Cwindow Selection
??????? <CommandMethod("CWindow")> _
?????? Public Sub CrossWindow()
??????????? Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
??????????? Dim res As PromptSelectionResult = ed.SelectCrossingWindow(New Point3d(1.0, 1.0, 0), New Point3d(10.0, 10.0, 0))
??????????? If res.Status = PromptStatus.Error Then
??????????????? Return
??????????? End If
??????????? Dim SS As Autodesk.AutoCAD.EditorInput.SelectionSet = res.Value
??????????? Dim idarray As ObjectId() = SS.GetObjectIds()
??????????? Dim db As Database = Application.DocumentManager.MdiActiveDocument.Database
??????????? Dim tm As Autodesk.AutoCAD.DatabaseServices.TransactionManager = db.TransactionManager
??????????? 'start a transaction
??????????? Dim myT As Transaction = tm.StartTransaction()
??????????? Try
??????????????? Dim id As ObjectId
??????????????? For Each id In idarray
??????????????????? Dim entity As Entity = tm.GetObject(id, OpenMode.ForWrite, True)
??????????????????? ed.WriteMessage((ControlChars.Lf + "You selected: " + entity.GetType().FullName))
??????????????? Next id
??????????? Finally
??????????????? myT.Dispose()
??????????? End Try
??????? End Sub 'CrossWindow
??????? 'This command demonstrates CPolygon selection
??????? <CommandMethod("CPolygon")> _
??????? Public Sub CrossPlygon()
??????????? Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
??????????? Dim values(4) As Point3d
??????????? values(0) = New Point3d(1.0, 1.0, 0.0)
??????????? values(1) = New Point3d(1.0, 50.0, 0.0)
??????????? values(2) = New Point3d(50.0, 50.0, 0.0)
??????????? values(3) = New Point3d(50.0, 1.0, 0.0)
??????????? Dim polygon As New Point3dCollection(values)
??????????? Dim res As PromptSelectionResult = ed.SelectCrossingPolygon(polygon)
??????????? If res.Status = PromptStatus.Error Then
??????????????? Return
??????????? End If
??????????? Dim SS As Autodesk.AutoCAD.EditorInput.SelectionSet = res.Value
??????????? Dim idarray As ObjectId() = SS.GetObjectIds()
??????????? Dim db As Database = Application.DocumentManager.MdiActiveDocument.Database
??????????? Dim tm As Autodesk.AutoCAD.DatabaseServices.TransactionManager = db.TransactionManager
??????????? 'start a transaction
??????????? Dim myT As Transaction = tm.StartTransaction()
??????????? Try
??????????????? Dim id As ObjectId
??????????????? For Each id In idarray
??????????????????? Dim entity As Entity = tm.GetObject(id, OpenMode.ForWrite, True)
??????????????????? ed.WriteMessage((ControlChars.Lf + "You selected: " + entity.GetType().FullName))
??????????????? Next id
??????????? Finally
??????????????? myT.Dispose()
??????????? End Try
??????? End Sub 'CrossPlygon
??????? 'This command has a number of keywords attached to the selection.
??????? 'Based on the keyword that user enters, it does either fence,window,windowpolygon,
??????? 'lastentitySelection,PreviousSelection
??????? <CommandMethod("OptionSel")> _
??????? Public Sub OptionSel()
??????????? Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
??????????? Dim Opts As New PromptSelectionOptions()
??????????? Opts.Keywords.Add("myFence")
??????????? Opts.Keywords.Add("myWindow")
??????????? Opts.Keywords.Add("myWpoly")
??????????? ' Opts.Keywords.Add("Implied")
??????????? Opts.Keywords.Add("myLastSel")
??????????? Opts.Keywords.Add("myPrevSel")
??????????? AddHandler Opts.KeywordInput, AddressOf handle_KeywordInput
??????????? Dim res As PromptSelectionResult = ed.GetSelection(Opts)
??????????? If res.Status = PromptStatus.OK Then
??????????????? Dim SS As Autodesk.AutoCAD.EditorInput.SelectionSet = res.Value
??????????????? Dim idarray As ObjectId() = SS.GetObjectIds()
??????????????? Dim db As Database = Application.DocumentManager.MdiActiveDocument.Database
??????????????? Dim tm As Autodesk.AutoCAD.DatabaseServices.TransactionManager = db.TransactionManager
??????????????? Dim myT As Transaction = tm.StartTransaction()
??????????????? Try
??????????????????? Dim id As ObjectId
??????????????????? For Each id In idarray
??????????????????????? Dim entity As Entity = tm.GetObject(id, OpenMode.ForWrite, True)
??????????????????????? ed.WriteMessage((ControlChars.Lf + "You selected: " + entity.GetType().FullName))
??????????????????? Next id
??????????????? Finally
??????????????????? myT.Dispose()
??????????????? End Try
??????????? End If
??????? End Sub 'OptionSel
??????? Private Shared Sub handle_KeywordInput(ByVal sender As Object, ByVal e As SelectionTextInputEventArgs)
??????????? If e.Input.CompareTo("myFence") = 0 Then
??????????????? Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
??????????????? Dim values(4) As Point3d
??????????????? values(0) = New Point3d(5.0, 5.0, 0.0)
??????????????? values(1) = New Point3d(13.0, 15.0, 0.0)
??????????????? values(2) = New Point3d(12.0, 9.0, 0.0)
??????????????? values(3) = New Point3d(5.0, 5.0, 0.0)
??????????????? Dim Fence As New Point3dCollection(values)
??????????????? Dim res As PromptSelectionResult = ed.SelectFence(Fence)
??????????????? If res.Status = PromptStatus.Error Then
??????????????????? Return
??????????????? End If
??????????????? Dim SS As Autodesk.AutoCAD.EditorInput.SelectionSet = res.Value
??????????????? Dim idarray As ObjectId() = SS.GetObjectIds()
??????????????? e.AddObjects(idarray)
??????????? Else
??????????????? If e.Input.CompareTo("myWindow") = 0 Then
??????????????????? Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
??????????????????? Dim res As PromptSelectionResult = ed.SelectWindow(New Point3d(1.0, 1.0, 0.0), New Point3d(30.0, 20.0, 0.0))
??????????????????? If res.Status = PromptStatus.Error Then
??????????????????????? Return
??????????????????? End If
??????????????????? Dim SS As Autodesk.AutoCAD.EditorInput.SelectionSet = res.Value
??????????????????? Dim idarray As ObjectId() = SS.GetObjectIds()
??????????????????? e.AddObjects(idarray)
??????????????? Else
??????????????????? If e.Input.CompareTo("myWpoly") = 0 Then
??????????????????????? Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
??????????????????????? Dim values(4) As Point3d
??????????????????????? values(0) = New Point3d(5.0, 5.0, 0.0)
??????????????????????? values(1) = New Point3d(13.0, 15.0, 0.0)
??????????????????????? values(2) = New Point3d(12.0, 9.0, 0.0)
??????????????????????? values(3) = New Point3d(5.0, 5.0, 0.0)
??????????????????????? Dim Polygon As New Point3dCollection(values)
??????????????????????? Dim res As PromptSelectionResult = ed.SelectWindowPolygon(Polygon)
??????????????????????? If res.Status = PromptStatus.Error Then
??????????????????????????? Return
??????????????????????? End If
??????????????????????? Dim SS As Autodesk.AutoCAD.EditorInput.SelectionSet = res.Value
??????????????????????? Dim idarray As ObjectId() = SS.GetObjectIds()
??????????????????????? e.AddObjects(idarray)
??????????????????? Else
??????????????????????? If e.Input.CompareTo("myLastSel") = 0 Then
??????????????????????????? Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
??????????????????????????? Dim res As PromptSelectionResult = ed.SelectLast()
??????????????????????????? If res.Status = PromptStatus.Error Then
??????????????????????????????? Return
??????????????????????????? End If
??????????????????????????? Dim SS As Autodesk.AutoCAD.EditorInput.SelectionSet = res.Value
??????????????????????????? Dim idarray As ObjectId() = SS.GetObjectIds()
??????????????????????????? e.AddObjects(idarray)
??????????????????????? Else
??????????????????????????? If e.Input.CompareTo("myPrevSel") = 0 Then
??????????????????????????????? Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
??????????????????????????????? Dim res As PromptSelectionResult = ed.SelectPrevious()
??????????????????????????????? If res.Status = PromptStatus.Error Then
??????????????????????????????????? Return
??????????????????????????????? End If
??????????????????????????????? Dim SS As Autodesk.AutoCAD.EditorInput.SelectionSet = res.Value
??????????????????????????????? Dim idarray As ObjectId() = SS.GetObjectIds()
??????????????????????????????? e.AddObjects(idarray)
??????????????????????????? End If
??????????????????????? End If
??????????????????? End If
??????????????? End If
??????????? End If
??????? End Sub
??????? 'This command demonstrates some selection Related input context reactors.
??????? 'It exercises promptingForSelection, PromptedForSelection and SelectionAdded
??????? 'events of the editor.
??????? <CommandMethod("SSetReactors")> _
????????? Public Sub SSetAddFilterTest()
??????????? Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
??????????? Dim db As Database = Application.DocumentManager.MdiActiveDocument.Database
??????????? Dim tm As Autodesk.AutoCAD.DatabaseServices.TransactionManager = db.TransactionManager
??????????? Dim SelOpts As New PromptSelectionOptions()
??????????? SelOpts.AllowDuplicates = True
??????????? AddHandler ed.PromptingForSelection, AddressOf handle_promptSelectionOptions
??????????? AddHandler ed.PromptedForSelection, AddressOf handle_promptSelectionResult
??????????? AddHandler ed.SelectionAdded, AddressOf handle_SelectionAdded
??????????? Dim SelRes As PromptSelectionResult = ed.GetSelection(SelOpts)
??????????? RemoveHandler ed.SelectionAdded, AddressOf handle_SelectionAdded
??????????? RemoveHandler ed.PromptingForSelection, AddressOf handle_promptSelectionOptions
??????????? RemoveHandler ed.PromptedForSelection, AddressOf handle_promptSelectionResult
??????????? If SelRes.Status = PromptStatus.OK Then
??????????????? Dim SS1 As Autodesk.AutoCAD.EditorInput.SelectionSet = SelRes.Value
??????????????? Dim idarray1 As ObjectId() = SS1.GetObjectIds()
??????????????? Dim myT As Transaction = tm.StartTransaction()
??????????????? Try
??????????????????? Dim id As ObjectId
??????????????????? For Each id In idarray1
??????????????????????? Dim entity As Entity = tm.GetObject(id, OpenMode.ForWrite, True)
??????????????????????? ed.WriteMessage((ControlChars.Lf + "You selected: " + entity.GetType().FullName))
??????????????????? Next id
??????????????? Finally
??????????????????? myT.Dispose()
??????????????? End Try
??????????? End If
??????? End Sub 'SSetAddFilterTest
?
??????? Private Shared Sub handle_SelectionAdded(ByVal sender As Object, ByVal e As SelectionAddedEventArgs)
??????????? Dim ss As Autodesk.AutoCAD.EditorInput.SelectionSet = e.Selection
??????????? SelentityCount += 1
??????????? If SelentityCount = 2 Then
??????????????? e.Remove(0)
??????????? End If
??????? End Sub 'handle_SelectionAdded
??????? Private Shared Sub handle_promptSelectionResult(ByVal sender As Object, ByVal e As PromptSelectionResultEventArgs)
??????????? UseThisSelectionResult = e.Result
??????? End Sub 'handle_promptSelectionResult
??????? Private Shared Sub handle_promptSelectionOptions(ByVal sender As Object, ByVal e As PromptSelectionOptionsEventArgs)
??????????? UseThisSelectionOption = e.Options
??????? End Sub 'handle_promptSelectionOptions
??????? 'This command helps to use the previous selection options and selection Results
??????? 'that we had during the execution of SSetReactors command. It demonstrates how to
??????? 'use PromptingForSelection and PromptedForSelection events.
??????? <CommandMethod("useSelection")> _
???????? Public Sub UsingSelectionOptionsAndResults()
??????????? Dim db As Database = Application.DocumentManager.MdiActiveDocument.Database
??????????? Dim tm As Autodesk.AutoCAD.DatabaseServices.TransactionManager = db.TransactionManager
??????????? 'start a transaction
??????????? Dim myT As Transaction = tm.StartTransaction()
??????????? Try
??????????????? Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
??????????????? If UseThisSelectionOption Is Nothing Then
??????????????????? Exit Sub
??????????????? End If
??????????????? Dim res2 As PromptSelectionResult = ed.GetSelection(UseThisSelectionOption)
??????????????? Dim res1 As PromptSelectionResult = UseThisSelectionResult
??????????????? Dim SS2 As Autodesk.AutoCAD.EditorInput.SelectionSet = res2.Value
??????????????? Dim idarray2 As ObjectId() = SS2.GetObjectIds()
??????????????? Dim SS1 As Autodesk.AutoCAD.EditorInput.SelectionSet = res1.Value
??????????????? Dim idarray1 As ObjectId() = SS1.GetObjectIds()
??????????????? Dim id As ObjectId
??????????????? For Each id In idarray1
??????????????????? Dim entity As Entity = tm.GetObject(id, OpenMode.ForWrite, True)
??????????????????? ed.WriteMessage((ControlChars.Lf + "You selected:" + entity.GetType().FullName))
??????????????? Next id
??????????????? Dim id1 As ObjectId
??????????????? For Each id1 In idarray2
??????????????????? Dim entity2 As Entity = tm.GetObject(id1, OpenMode.ForWrite, True)
??????????????????? ed.WriteMessage((ControlChars.Lf + "You selected:" + entity2.GetType().FullName))
??????????????? Next id1
??????????? Finally
??????????????????? myT.Dispose()
??????????? End Try
??????? End Sub 'UsingSelectionOptionsAndResults
??? End Class 'AcEdSSGetCommand
End Namespace 'Selection
?
?
以上實(shí)例見objectarx sdk \sample\dotnet
?