.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

GetSelection as..

5 REPLIES 5
Reply
Message 1 of 6
StuartMartin6768
1882 Views, 5 Replies

GetSelection as..

I'm reasonably familiar with C# so I was pretty damn happy to find that this is an option for Autocad. We're all using 2006 here, however the referenced dll's seem to provide most of the functionality needed so i'm guessing that this is still available to 2006 users.

My question is, how to I request, then return a selection as a collection of say, points? So regardless of whether the user selects lines or what have you, it will return the points of that selection. I'm then going to do something to the position of each of those points in a for loop.

This doesn't quite work:

{code}
PromptSelectionOptions getSelectionOption = new PromptSelectionOptions();
PromptSelectionResult getSelectionResult = ed.GetSelection(getSelectionOption);

if (getSelectionOption.GetType() == typeof(Point2dCollection))
{
Point2dCollection pointSelection = ed.GetSelection(getSelectionOption) as Point2dCollection;
}
{code}

Edited by: StuartMartin6768 on Oct 27, 2009 3:10 AM Edited by: StuartMartin6768 on Oct 27, 2009 4:05 AM
5 REPLIES 5
Message 2 of 6
NathTay
in reply to: StuartMartin6768

Look in the documentation for SelectionFilter.
Message 3 of 6
Anonymous
in reply to: StuartMartin6768

Stuart,
As Nathtay suggests, you can filter selection sets for any object type that
you need, but don't confuse the objects in the Geometry namespace with
objects in the DatabaseServices namespace. Objects in the Geometry
namespace do not reside in the drawing database. The DBPoint object is what
resides in the drawing.
--
Bobby C. Jones
http://bobbycjones.spaces.live.com
Message 4 of 6
Anonymous
in reply to: StuartMartin6768

Selections do not contain geometric points. They contain entities,
and can consist of combinations of any kind of entity that you can
draw in AutoCAD.

Lines have two 'points'. Circles have one; polylines have many, so
'a collection of say, points' is a bit ambiguous.

What exactly do you need, or are you just experimenting? If so,
I would suggest reviewing the API docs and samples, and any
other materials you can find that can help you become more
familiar with the API.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD
Supporting AutoCAD 2000 through 2010

http://www.acadxtabs.com

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");

wrote in message news:6278498@discussion.autodesk.com...
I'm reasonably familiar with C# so I was pretty damn happy to find that this
is an option for Autocad. We're all using 2006 here, however the referenced
dll's seem to provide most of the functionality needed so i'm guessing that
this is still available to 2006 users.

My question is, how to I request, then return a selection as a collection of
say, points? So regardless of whether the user selects lines or what have
you, it will return the points of that selection. I'm then going to do
something to the position of each of those points in a for loop.

This doesn't quite work:

{code}
PromptSelectionOptions getSelectionOption = new
PromptSelectionOptions();
PromptSelectionResult getSelectionResult =
ed.GetSelection(getSelectionOption);

if (getSelectionOption.GetType() ==
typeof(Point2dCollection))
{
Point2dCollection pointSelection =
ed.GetSelection(getSelectionOption) as Point2dCollection;
}
{code}

Edited by: StuartMartin6768 on Oct 27, 2009 3:10 AM

Edited by: StuartMartin6768 on Oct 27, 2009 4:05 AM
Message 5 of 6

Thanks for the responses. What i'm doing is making a plugin that rounds off the selected points (start/end of lines, i'll probably make it only work with lines) to the nearest fixed, or user input. So I have the command "RoundSel" and "RoundSelO" (which has the user input option). I went over some more docs yesterday and while I haven't yet tested it, I seem to have gotten as far as returning and looping through the selected lines. Now i'm just trying to find out how to grab its start and end points and alter their positions (probably world space, but maybe relative to origin or UCS origin).

{code}
using System;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;

// Ensure this works with UCS!
// ie. if you run it on an angled line it should keep the angle and effectively alter
// that lines local X/Y if the user has set their UCS to the same angle as that line

public class RoundSelected
{
[CommandMethod("RoundSel")]
public void RoundSel()
{
// fixed rounding of 10mm
}

[CommandMethod("RoundSelO")]
public void RoundSelO()
{
// rounding distance as user input
}

// Requests user distance
private double GetDistance()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;

PromptDistanceOptions getDistanceOption = new PromptDistanceOptions("Round to the nearest..?");
PromptResult getDistanceResult = ed.GetDistance(getDistanceOption);

if (getDistanceResult.GetType() == typeof(DistanceUnitFormat))
{
double roundDistance = ed.GetDistance(getDistanceOption);

return 1000;
}
}

// Returns line selection
private void GetSelection()
{
Document activeDocument = Application.DocumentManager.MdiActiveDocument;
Database activeDB = activeDocument.Database;

Editor ed = activeDocument.Editor;

using (Transaction trans = activeDB.TransactionManager.StartTransaction())
{
TypedValue[] selectionTypeValue = new TypedValue[1];
selectionTypeValue.SetValue(new TypedValue((int)DxfCode.Start, "LINE"), 0);

SelectionFilter selectionFilter = new SelectionFilter(selectionTypeValue);

PromptSelectionResult getSelectionResult;
getSelectionResult = ed.GetSelection(selectionFilter);

// If selection actually contains something
if (getSelectionResult.Status == PromptStatus.OK)
{
SelectionSet selectionSet = getSelectionResult.Value;

foreach (SelectedObject selectedObject in selectionSet)
{
if (selectedObject != null)
{
Entity selectedEntity = trans.GetObject(selectedObject.ObjectId, OpenMode.ForWrite) as Entity;

if (selectedEntity != null)
{
// do shit to said line here
Rounder(0.01, selectedEntity);
}
}
}

trans.Commit();
}
}
}

// Loops through vectors and alters
private void Rounder(double roundDistance, Entity entity)
{


// do loop here
entity.GetStretchPoints();

Vector2d pointPos;
roundDistance = 1.00;

Math.Round(5.5);

}

// Returns the rounded value
private double RoundValue(double roundDistance, double number)
{
double n;

n = number * Math.Pow(10, roundDistance);

n = Math.Sign(n) * Math.Abs(Math.Floor(n + 0.5));

return n / Math.Pow(10, roundDistance);
}
}


{code} Edited by: StuartMartin6768 on Oct 28, 2009 3:11 AM
Message 6 of 6
Anonymous
in reply to: StuartMartin6768

My suggestion is to look at the docs for the API methods you are calling,
and see what the type of the object they return is, as your GetDistance()
method seems to suggest that you're taking wild guesses. Using any
API requires one know what parameter and result types are for methods
being used.

For example, look at the docs for the Editor's GetDistance() method,
and note what type it returns. It never returns a DistanceUnitFormat, so
there is no point to comparing it to that.

In your GetSelection() method, you create the array of TypedValue[] in
a somewhat roundabout way:

{code}

TypedValue[] selectionTypeValue = new TypedValue[1];
selectionTypeValue.SetValue(new TypedValue((int)DxfCode.Start,
"LINE"), 0);

{code}

You can just do this:

{code}

TypedValue[] typeValueArray =
new TypedValue[] { new TypedValue((int) DxfCode.Start, "LINE") };

{code}


The return type of the Transaction's GetObject() method is declared as
DBObject, but that is the base type of all objects that it returns, not the
actual type.

So, the Autodesk.AutoCAD.DatabaseServices.Line class is used to
represent a line in an AutoCAD drawing, and that is the type of the
object that GetObject() returns when you give it the ObjectId of a line.

So, the way to go about testing the result of GetObject() to see if it is
a type of interest is:

{code}

Transaction tr = ....

DBObject obj = tr.GetObject( .... );

Line myLine = obj as Line; // try to cast the result to a Line
object

if( myLine != null ) // then the cast succeeded
{
// use myLine here
}

{code}

As far as how to get/set the endpoints of lines, if you were able to
get this far, I'm confident that once you've got the result of GetObject()
cast to the correct type (a Line object) as per the exampole above,
you will be able to figure out the rest quite easly. 🙂

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD
Supporting AutoCAD 2000 through 2010

http://www.acadxtabs.com

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");

wrote in message news:6279308@discussion.autodesk.com...
Thanks for the responses. What i'm doing is making a plugin that rounds off
the selected points (start/end of lines, i'll probably make it only work
with lines) to the nearest fixed, or user input. So I have the command
"RoundSel" and "RoundSelO" (which has the user input option). I went over
some more docs yesterday and while I haven't yet tested it, I seem to have
gotten as far as returning and looping through the selected lines. Now i'm
just trying to find out how to grab its start and end points and alter their
positions (probably world space, but maybe relative to origin or UCS
origin).

{code}
using System;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;

// Ensure this works with UCS!
// ie. if you run it on an angled line it should keep the angle and
effectively alter
// that lines local X/Y if the user has set their UCS to the same angle as
that line

public class RoundSelected
{
[CommandMethod("RoundSel")]
public void RoundSel()
{
// fixed rounding of 10mm
}

[CommandMethod("RoundSelO")]
public void RoundSelO()
{
// rounding distance as user input
}

// Requests user distance
private double GetDistance()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;

PromptDistanceOptions getDistanceOption = new
PromptDistanceOptions("Round to the nearest..?");
PromptResult getDistanceResult = ed.GetDistance(getDistanceOption);

if (getDistanceResult.GetType() == typeof(DistanceUnitFormat))
{
double roundDistance = ed.GetDistance(getDistanceOption);

return 1000;
}
}

// Returns line selection
private void GetSelection()
{
Document activeDocument =
Application.DocumentManager.MdiActiveDocument;
Database activeDB = activeDocument.Database;

Editor ed = activeDocument.Editor;

using (Transaction trans =
activeDB.TransactionManager.StartTransaction())
{
TypedValue[] selectionTypeValue = new TypedValue[1];
selectionTypeValue.SetValue(new TypedValue((int)DxfCode.Start,
"LINE"), 0);

SelectionFilter selectionFilter = new
SelectionFilter(selectionTypeValue);

PromptSelectionResult getSelectionResult;
getSelectionResult = ed.GetSelection(selectionFilter);

// If selection actually contains something
if (getSelectionResult.Status == PromptStatus.OK)
{
SelectionSet selectionSet = getSelectionResult.Value;

foreach (SelectedObject selectedObject in selectionSet)
{
if (selectedObject != null)
{
Entity selectedEntity =
trans.GetObject(selectedObject.ObjectId, OpenMode.ForWrite) as Entity;

if (selectedEntity != null)
{
// do shit to said line here
Rounder(0.01, selectedEntity);
}
}
}

trans.Commit();
}
}
}

// Loops through vectors and alters
private void Rounder(double roundDistance, Entity entity)
{


// do loop here
entity.GetStretchPoints();

Vector2d pointPos;
roundDistance = 1.00;

Math.Round(5.5);

}

// Returns the rounded value
private double RoundValue(double roundDistance, double number)
{
double n;

n = number * Math.Pow(10, roundDistance);

n = Math.Sign(n) * Math.Abs(Math.Floor(n + 0.5));

return n / Math.Pow(10, roundDistance);
}
}


{code}

Edited by: StuartMartin6768 on Oct 28, 2009 3:11 AM

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost