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