Ad

Tuesday, February 05, 2013

Select Viewport

Here’s some code to select a viewport. I’ve made an extension to the Editor class. this way I can easily call it from other code. Most of the rest of the code was taken from an internet search when I was coding for viewport selection. I’m unsure of the original source, although a search of words finds it in numerous locations.

        public static ObjectId SelectViewport(this Editor ed, string promptString)
{
ObjectId viewportObjId = ObjectId.Null;
PromptEntityOptions opt = new PromptEntityOptions("\n" + promptString);
opt.SetRejectMessage("\nObject must be a viewport.\n");
opt.AddAllowedClass(typeof(Viewport), true);
//next lines are to allow for non-rectangular viewport selection
opt.AddAllowedClass(typeof(Circle), true);
opt.AddAllowedClass(typeof(Polyline), true);
opt.AddAllowedClass(typeof(Polyline2d), true);
opt.AddAllowedClass(typeof(Polyline3d), true);
opt.AddAllowedClass(typeof(Ellipse), true);
opt.AddAllowedClass(typeof(Autodesk.AutoCAD.DatabaseServices.Region), true);
opt.AddAllowedClass(typeof(Spline), true);
opt.AddAllowedClass(typeof(Face), true);
PromptEntityResult viewportResult = ed.GetEntity(opt);

if (viewportResult.Status == PromptStatus.OK)
{
Entity ent = viewportResult.ObjectId.GetObject(OpenMode.ForRead) as Entity;
// It is a rectangular viewport.
if (ent.GetType() == typeof(Viewport))
{
viewportObjId = viewportResult.ObjectId;
}
else if (true)
{
//Viewport is non-rectangular, attempt to get it from the selected clip entity
ObjectId vpId = LayoutManager.Current.GetNonRectangularViewportIdFromClipId(viewportResult.ObjectId);
if (vpId != ObjectId.Null)
{
viewportObjId = vpId;
}
}
}
return viewportObjId;
}



It also provides a nice example to limit different types of objects during selection. Just remove the opt.AddAllowedClass(typeof(Spline), true);  you don’t need and add the classes you want the user to select. It makes it easy so you have to worry less about the user selecting the wrong type of object. Just make sure to provide the error warning message first.

No comments:

LinkWithin

Blog Widget by LinkWithin

Ad