马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
直接上代码了
[C#] 纯文本查看 复制代码 using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
namespace Fsxm.Acad
{
public class BreakCurve
{
//全局变量,默认等分的段数
static int n = 2;
static double dist = 5;
static string Mod = "N";
//等分曲线选择集
[CommandMethod("AveragesCurve", CommandFlags.UsePickSet)]
public void AveragesCurveSs()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
#region 用户输入=================================================
while (true)
{
if (Mod == "D")
{
PromptDistanceOptions op = new PromptDistanceOptions("\n输入定距等分的距离[定数等分(N)]", "N");
op.DefaultValue = dist;
op.AllowZero = false;
PromptDoubleResult drst = ed.GetDistance(op);
if (drst.Status == PromptStatus.OK)
{
Mod = "D";
dist = drst.Value;
break;
}
else if (drst.Status == PromptStatus.Keyword)
Mod = "N";
else if (drst.Status == PromptStatus.Cancel)
return;
else
break;
}
else if (Mod == "N")
{
PromptIntegerOptions iop = new PromptIntegerOptions("\n输入定数等分的段数[定距等分(D)]", "D");
iop.DefaultValue = n;
PromptIntegerResult irst = ed.GetInteger(iop);
if (irst.Status == PromptStatus.OK && irst.Value >= 2)
{
Mod = "N";
n = irst.Value;
break;
}
else if (irst.Status == PromptStatus.Keyword)
Mod = "D";
else if (irst.Status == PromptStatus.Cancel)
return;
else
break;
}
}
PromptSelectionResult srt = ed.GetSelection
(new SelectionFilter(new TypedValue[] { new TypedValue(0, "arc,circle,ellipse,*line") }));
SelectionSet ss;
if (srt.Status == PromptStatus.OK)
ss = srt.Value;
else
return;
#endregion ======================================================
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTableRecord Spase = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
if (Mod == "N")
{
foreach (SelectedObject obj in ss)
{
Curve cv = (Curve)tr.GetObject(obj.ObjectId, OpenMode.ForWrite);
foreach (DBObject dbobj in AveragesCurve(cv, n))
{
Spase.AppendEntity((Entity)dbobj);
tr.AddNewlyCreatedDBObject(dbobj, true);
}
}
}
else//定距等分
{
foreach (SelectedObject obj in ss)
{
Curve cv = (Curve)tr.GetObject(obj.ObjectId, OpenMode.ForWrite);
Point3d pt = new Point3d(); bool sel = true;
double dist_Ok = dist;
if (obj is CrossingOrWindowSelectedObject)
{
var cobj = (CrossingOrWindowSelectedObject)obj;
pt = cobj.GetPickPoints()[0].PointOnLine;
}
else if (obj is FenceSelectedObject)
{
var cobj = (FenceSelectedObject)obj;
pt = cobj.GetIntersectionPoints()[0].PointOnLine;
}
else if (obj is PickPointSelectedObject)
{
var cobj = (PickPointSelectedObject)obj;
pt = cobj.PickPoint.PointOnLine;
}
else
{
sel = false;
}
if (sel && cv.StartPoint.DistanceTo(pt) > cv.EndPoint.DistanceTo(pt))
{
dist_Ok = -dist;
}
foreach (DBObject dbobj in AveragesCurve(cv, dist_Ok))
{
Spase.AppendEntity((Entity)dbobj);
tr.AddNewlyCreatedDBObject(dbobj, true);
}
}
}
tr.Commit();
}
}
//定数等分单条曲线
public DBObjectCollection AveragesCurve(Curve cv, int n)
{
double ep = cv.EndParam;
double len = cv.GetDistanceAtParameter(ep);
double split = len / n;
DoubleCollection pas = new DoubleCollection();
if (cv.Closed) //闭合曲线要先断开
pas.Add(cv.StartParam);
for (int i = 1; i < n; i++)
{
pas.Add(cv.GetParameterAtDistance(i * split));
}
cv.Erase();
return cv.GetSplitCurves(pas);
}
//等距等分单条曲线(dist<0反向等分)
public DBObjectCollection AveragesCurve(Curve cv, Double dist)
{
double ep = cv.EndParam;
double len = cv.GetDistanceAtParameter(ep);
double dst = 0;
//从最后开始等分
if (dist < 0)
{
dist = -dist;
dst = (cv.GetDistanceAtParameter(cv.EndParam) % dist) - dist;
}
DoubleCollection pas = new DoubleCollection();
if (cv.Closed) pas.Add(cv.StartParam);//闭合曲线要先断开
while (true)
{
dst += dist;
if (dst >= len) break;
pas.Add(cv.GetParameterAtDistance(dst));
}
cv.Erase();
return cv.GetSplitCurves(pas);
}
}
} |