foreach (ObjectId id in btr)
{
Line line = tr.GetObject(id, OpenMode.ForRead) as Line;
if (line != null)
{
double dist = pt.DistanceTo(line.GetClosestPointTo(pt, false));
[b]if (dist == 0)
continue;[/b]
if (closest == null)
{
closest = line;
shortest = dist;
}
else if (shortest > dist)
{
closest = line;
shortest = dist;
}
}
}
By the way.The codes in your If...else if ... are the same.Why this?You're right.
public static void UseCrossingWindow()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
PromptPointResult ppr = ed.GetPoint("\n Sepecify the first Point:");
if (ppr.Status != PromptStatus.OK) return;
Point3d pt1 = ppr.Value.TransformBy(ed.CurrentUserCoordinateSystem);
//Get another point to form a SeltectCrossingWindow
ppr = ed.GetPoint("\n Sepecify the second Point:");
if (ppr.Status != PromptStatus.OK) return;
Point3d pt2 = ppr.Value.TransformBy(ed.CurrentUserCoordinateSystem);
// Create a crossing window based on the two selected points
PromptSelectionResult acSSPrompt;
acSSPrompt = ed.SelectCrossingWindow(pt1, pt2);
// If the prompt status is not OK, do nothing.
if (acSSPrompt.Status != PromptStatus.OK)
return;
SelectionSet acSSet = acSSPrompt.Value;
ObjectId[] ids = acSSet.GetObjectIds();
Line closest = null;
double shortest = 0.0;
foreach (ObjectId id in ids)
{
Line line = trans.GetObject(id, OpenMode.ForRead) as Line;
if (line != null)
{
double dist = pt1.DistanceTo(line.GetClosestPointTo(pt1, false));
if (dist == 0)
continue;
if (closest == null)
{
closest = line;
shortest = dist;
}
else if (shortest > dist)
{
closest = line;
shortest = dist;
}
}
}
if (closest != null)
{
closest.Highlight();
ed.WriteMessage("\nDistance = {0}", shortest);
}
/*
double shortest = 0.0;
foreach (ObjectId id in ids)
{
Entity ent = (Entity)trans.GetObject(id, OpenMode.ForWrite);
if (ent.GetType().Name == "Line")
{
Line myLine = (Line)trans.GetObject(id, OpenMode.ForWrite);
double dist = pt1.DistanceTo(myLine.GetClosestPointTo(pt1,false));
if (dist!=0 && shortest > dist)
{
shortest = dist;
}
}
} //End of foreach loop
ed.WriteMessage("\nDistance = {0}", shortest);
*/
trans.Commit();
}
}
By the way.The codes which have be commented out don't work. The result is "0". using (Transaction tr = doc.Database.TransactionManager.StartTransaction())
{
IEnumerable<Entities> filteredEntities = objectIDs.GetObjects<Entity>(OpenMode.ForRead, false, true).Where( predicate).Select(predicate) etc. etc. ;
}
public static IEnumerable<T> GetObjects<T>(
this IEnumerable<ObjectId> ids,
OpenMode mode = OpenMode.ForRead,
bool openErased = false,
bool forceOpenOnLockedLayers = false) where T : DBObject
{
if (ids.Any())
{
TransactionManager tm = ids.First().Database.TransactionManager;
RXClass rxclass = RXClass.GetClass(typeof(T));
if (typeof(T) == typeof(DBObject))
{
foreach (ObjectId id in ids)
{
yield return (T)tm.GetObject(id, mode, openErased,
forceOpenOnLockedLayers);
}
}
else if (rxclass.GetHasChildren())
{
foreach (ObjectId id in ids)
{
if (id.ObjectClass.IsDerivedFrom(rxclass))
yield return (T)tm.GetObject(id, mode, openErased,
forceOpenOnLockedLayers);
}
}
else
{
foreach (ObjectId id in ids)
{
if (id.ObjectClass == rxclass)
yield return (T)tm.GetObject(id, mode, openErased,
forceOpenOnLockedLayers);
}
}
}
}
// Returns true if at least one RXClass is derived from
// the given RXClass:
public static bool GetHasChildren(this RXClass rxclass)
{
foreach (DictionaryEntry e in SystemObjects.ClassDictionary)
{
if (rxclass == ((RXClass)e.Value).MyParent)
return true;
}
return false;
}
Hi,
Here's another way, certainly slower if there're many entities in the current space, but much more easy to code. :evil:Code - C#: [Select]
using System.Linq; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.Runtime; namespace ClosestLineSample { public class Commands { [CommandMethod("Test")] public void Test() { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; PromptPointResult ppr = ed.GetPoint("\nSpecify a point: "); if (ppr.Status != PromptStatus.OK) return; Point3d pt = ppr.Value.TransformBy(ed.CurrentUserCoordinateSystem); using (Transaction tr = db.TransactionManager.StartTransaction()) { BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForRead); var closest = btr .Cast<ObjectId>() .Select(id => tr.GetObject(id, OpenMode.ForRead) as Line) .Where(ent => ent != null) .Aggregate((l1, l2) => l1.Dist < l2.Dist ? l1 : l2); if (closest != null) { closest.Line.Highlight(); ed.WriteMessage("\nDistance = {0}", closest.Dist); } tr.Commit(); } } } }