CAD获取一个图形上所有直线的交点
本帖最后由 jiangfei200809 于 2012-7-20 11:08 编辑#region 相交判断
public void CrossCAD()
{
TypedValue[] pTypeValue = new TypedValue;
pTypeValue = new TypedValue((int)DxfCode.LayerName, “测试”);
SelectionFilter pSelectionFilter=new SelectionFilter(pTypeValue);
PromptSelectionResult selectResult = pDocument.Editor.SelectAll(pSelectionFilter);
SelectionSet pSelectionSet = selectResult.Value;
using (Transaction tran = pDatabase.TransactionManager.StartTransaction())
{
List crossPoint = new List();//存放相交点
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(pSelectionSet.Count.ToString());
for(int i=0;i= minX && y >= minY && x <= maxX && y<= maxY)
{
#region 确定第二条线的最大最小坐标
if (secondLine.StartPoint.X < secondLine.EndPoint.X)
{
minX = secondLine.StartPoint.X;
maxX = secondLine.EndPoint.X;
}
else
{
minX = secondLine.EndPoint.X;
maxX = secondLine.StartPoint.X;
}
if (secondLine.StartPoint.Y < secondLine.EndPoint.Y)
{
minY = secondLine.StartPoint.Y;
maxY = secondLine.EndPoint.Y;
}
else
{
maxY = secondLine.StartPoint.Y;
minY = secondLine.EndPoint.Y;
}
#endregion
if (x >= minX && y >= minY && x <= maxX && y <= maxY)
{
crossPoint.Add(new Point2d(x, y));
}
}
}
#endregion
}
}
}
}
if (crossPoint.Count == 1)
{
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("交点为:"+crossPoint.X.ToString()+","+crossPoint.Y.ToString());
return;
}
BlockTable pBlockTable = tran.GetObject(pDatabase.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord pBlockTableRecord = tran.GetObject(pBlockTable, OpenMode.ForWrite) as BlockTableRecord;
for (int i = 0; i < crossPoint.Count; i++)
{
Circle pCircle = new Circle(new Point3d(crossPoint.X, crossPoint.Y, 0), Vector3d.ZAxis, 1);
pCircle.ColorIndex = 0;
pBlockTableRecord.AppendEntity(pCircle);
tran.AddNewlyCreatedDBObject(pCircle, true);
}
tran.Commit();
}
}
#endregion
图层名称我用的是“测试”,大家根据自己需要改下。
通过以上代码可以将一个图层上所有直线相交点用圆的形式标出,希望大家有用
本帖最后由 雪山飞狐_lzh 于 2012-7-20 12:23 编辑
纯数学的方法? 不过好像差点代码。。。
试下这段代码
public static void CrossCAD()
{
var doc = Application.DocumentManager.MdiActiveDocument;
var db = doc.Database;
var ed = doc.Editor;
var sf =
new SelectionFilter
(
new TypedValue[]{ new TypedValue(0, "line")}
);
var res = ed.SelectAll(sf);
if (res.Status != PromptStatus.OK) return;
using (var tr = doc.TransactionManager.StartTransaction())
{
var lines = new List<Line>();
foreach(ObjectId id in res.Value.GetObjectIds())
{
lines.Add((Line)tr.GetObject(id, OpenMode.ForRead));
}
List<Point3d> pts = new List<Point3d>();
for (int m = 0; m < lines.Count; m++)
{
LineSegment3d ls1 = new LineSegment3d(lines.StartPoint, lines.EndPoint);
for (int n = m + 1; n < lines.Count; n++)
{
LineSegment3d ls2 = new LineSegment3d(lines.StartPoint, lines.EndPoint);
CurveCurveIntersector3d cc3d = new CurveCurveIntersector3d(ls1, ls2, Vector3d.ZAxis);
if (cc3d.NumberOfIntersectionPoints == 1)
{
pts.Add(cc3d.GetIntersectionPoint(0));
}
}
}
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
foreach (var pt in pts)
{
Circle c = new Circle(pt, Vector3d.ZAxis, 1);
btr.AppendEntity(c);
tr.AddNewlyCreatedDBObject(c, true);
}
tr.Commit();
}
}
雪山飞狐_lzh 发表于 2012-7-20 12:17 static/image/common/back.gif
纯数学的方法? 不过好像差点代码。。。
试下这段代码
缺代码吗 亲?纯数学运算的方法做的 通过构建二元一次方程 然后 判断相交点是否在线上...代码完全可行哈 当然你的代码要简洁点 哈哈 不是AUTOLISP?看不懂,能否做成一个插件LSP 学习了 感谢楼主
页:
[1]