TheSwamp
Code Red => .NET => Topic started by: waterharbin on November 19, 2011, 02:29:15 AM
-
Hello,there. I want to get the cross point of two lines,which property should I access? There are three casese, see the image. Point A and Point B are the endpoint of the lines,but I want get point C.
-
Without code
Determine the StartPoint and EndPoint of each Line object.
The Geometry Namespace has a Line2d and a Line3d Class.
These classes are derived from LinearEntity2d or LinearEntity3d which have several methods named IntersectWith
The signatures for the LinearEntity3d Methos are
public Point3d[] IntersectWith(LinearEntity3d line);
public Point3d[] IntersectWith(PlanarEntity plane);
public Point3d[] IntersectWith(LinearEntity3d line, Tolerance tolerance);
public Point3d[] IntersectWith(PlanarEntity plane, Tolerance tolerance);
One option may be something like
private static Point3d lineIntersectors(Point3d pt1, Point3d pt2, Point3d pt3, Point3d pt4)
{
using (Line3d l1 = new Line3d(pt1, pt2),
l2 = new Line3d(pt3, pt4))
{
return l1.IntersectWith(l2)[0];
}
}
-
Or you may choose to use the IntersectWith Method from the Entity Class
if you want explicit control over the Intersection Type
Signatures :
public void IntersectWith(Entity entityPointer,
Intersect intersectType,
Point3dCollection points,
IntPtr thisGraphicSystemMarker,
IntPtr otherGraphicSystemMarker)
{
}
public enum Intersect
{
OnBothOperands,
ExtendThis,
ExtendArgument,
ExtendBoth
}
-
Here is something to play with ..
//
// CodeHimBelonga KDUB 2011.11.19.
using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
[assembly: CommandClass(typeof (KDUB.IntersectExample.IntersectionTest))]
namespace KDUB.IntersectExample
{
public class IntersectionTest
{
[CommandMethod("Test", CommandFlags.Modal)]
public void IntersectionTest01() {
Document doc = AcadApp.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
Point3d IntersectionPoint;
using (Transaction tr = db.TransactionManager.StartTransaction()) {
ObjectId id1 = SelectSpecificObjectType("\nSelect Line entity: ",
typeof (Line));
if (id1.IsNull) {
return;
}
ObjectId id2 = SelectSpecificObjectType("\nSelect next Line entity: ",
typeof (Line));
if (id2.IsNull) {
return;
}
Line line1 = (Line) tr.GetObject(id1, OpenMode.ForRead);
Line line2 = (Line) tr.GetObject(id2, OpenMode.ForRead);
IntersectionPoint = lineIntersectors(line1.StartPoint,
line1.EndPoint, line2.StartPoint,
line2.EndPoint);
tr.Commit();
}
AcadApp.ShowAlertDialog(string.Format("X: {0} \nY: {1} \nZ: {2} \n",
IntersectionPoint.X, IntersectionPoint.Y,
IntersectionPoint.Z));
}
private static Point3d lineIntersectors(Point3d spt1, Point3d ept1, Point3d spt2,
Point3d ept2) {
using (Line3d line1 = new Line3d(spt1, ept1),
line2 = new Line3d(spt2, ept2)) {
return line1.IntersectWith(line2)[0];
}
}
private static ObjectId SelectSpecificObjectType(string prompt,
System.Type objectClass) {
Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
using (
Transaction tr =
ed.Document.Database.TransactionManager.StartTransaction()) {
while (true) {
PromptEntityResult res = ed.GetEntity(prompt);
if (res.Status != PromptStatus.OK) {
return ObjectId.Null;
}
DBObject ob = tr.GetObject(res.ObjectId, OpenMode.ForRead);
if (objectClass.IsAssignableFrom(ob.GetType())) {
return res.ObjectId;
}
ed.WriteMessage("\nInvalid selection, {0} entity expected",
RXClass.GetClass(objectClass).DxfName);
}
}
}
}
}
And a piccy
and the source solution zipped
-
Hellp,Kerry.I appreciate it very much.Thanks for help me so much.Happy everyday! You are a great man!
-
You are a great man!
++
-
Thank you waterharbin ... I'm pleased the code and post helps.
and thank you Dan.
Regards