曲线转换类
本帖最后由 雪山飞狐_lzh 于 2014-8-29 19:15 编辑Ge库曲线在Net中见到的很少,ObjectArx有很多,在XD找到过
下面的代码有小部分是参考XD的代码移植来的:)
参考
http://www.xdcad.net/forum/showthread.php?s=&threadid=57844&perpage=15&display=&pagenumber=1
http://www.xdcad.net/forum/showthread.php?s=&threadid=19335&perpage=15&display=&pagenumber=1
http://www.xdcad.net/forum/showthread.php?postid=66202
2014/8/26更新版本 修正样条曲线的Bug
2014/8/29更新版本 修正Ge曲线打断
收获满满,共享伟大 <p>学一学!谢斑竹!</p><p>不知道能不能用得上!</p><p>以前碰到过CircularArc2d或还是别的圆弧类,记不清了,认为有Bug,是StartAngle和EndAnlge角度方面的,AutoCAD 2006版本</p><p></p> 发几个使用的例子:)
1、Ge创建圆弧(中点、起点,圆心)
同理实现起点,弧上点、中点,GeArc有相应的构筑函数
public static void CCicByDb()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Point3d p1 = new Point3d(10,0,10);
Point3d p2 = new Point3d(0,200,0);
Plane plane = new Plane(p1, Point3d.Origin, p2);
Matrix3d mat = Matrix3d.WorldToPlane(plane);
p1 = p1.TransformBy(mat);
p2 = p2.TransformBy(mat);
using (Transaction tr = db.TransactionManager.StartTransaction())
{
Curve arc = new Arc(Point3d.Origin, p1.GetAsVector().Length, p1.GetAsVector().AngleOnPlane(new Plane()), p2.GetAsVector().AngleOnPlane(new Plane()));
arc.TransformBy(mat.Inverse());
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
btr.AppendEntity(arc);
tr.AddNewlyCreatedDBObject(arc,true);
tr.Commit();
}
}
public static void CCirByGe()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Vector3d startvec = new Vector3d(10, 0, 10);
Vector3d endvec = new Vector3d(0, 200, 0);
endvec = endvec.GetNormal() * startvec.Length;
Vector3d othervec = startvec + endvec;
othervec = othervec.GetNormal() * startvec.Length;
CircularArc3d ca3d = new CircularArc3d(Point3d.Origin + startvec, Point3d.Origin + othervec, Point3d.Origin + endvec);
using (Transaction tr = db.TransactionManager.StartTransaction())
{
Curve arc = ConvertCurve.ToArc(ca3d);
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
btr.AppendEntity(arc);
tr.AddNewlyCreatedDBObject(arc, true);
tr.Commit();
}
}
2、填充边界(以前发过)
public static void HatchLoop()
{
PromptSelectionResult res = Helper.Editor.GetSelection(
new PromptSelectionOptions(),
new SelectionFilter(new TypedValue[] { new TypedValue(0, "Hatch") }));
if (res.Status != PromptStatus.OK)
return;
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTableRecord btr =
(BlockTableRecord)tr.GetObject(
db.CurrentSpaceId,
OpenMode.ForWrite,
false);
foreach (ObjectId id in res.Value.GetObjectIds())
{
Hatch h = (Hatch)tr.GetObject(id, OpenMode.ForRead);
Matrix3d mat = Matrix3d.PlaneToWorld(h.GetPlane());
for (int i = 0; i < h.NumberOfLoops; i++)
{
HatchLoop loop = h.GetLoopAt(i);
if (loop.IsPolyline)
{
BulgeVertexCollection bvs = loop.Polyline;
Autodesk.AutoCAD.DatabaseServices.Polyline pl = new Autodesk.AutoCAD.DatabaseServices.Polyline();
for (int j = 0; j < bvs.Count; j++)
{
BulgeVertex bv = bvs;
pl.AddVertexAt(j, bv.Vertex, bv.Bulge, 0, 0);
}
pl.TransformBy(mat);
btr.AppendEntity(pl);
tr.AddNewlyCreatedDBObject(pl, true);
}
else
{
foreach (Curve2d curve in loop.Curves)
{
Curve c = ConvertCurve.ToCurve(curve, mat);
btr.AppendEntity(c);
tr.AddNewlyCreatedDBObject(c, true);
}
}
}
}
tr.Commit();
}
} <p>没用过,看不明白</p><p>还请楼主多多指教</p> 本帖最后由 雪山飞狐_lzh 于 2014-8-26 20:44 编辑
试着用扩展方法重写了,调用起来确实很简单 简单的示例,直接把飞诗的代码改写了
public static void Test()
{
PromptEntityResult rt1 = Helper.Editor.GetEntity("\n选择曲线1:");
if (rt1.Status != PromptStatus.OK) return;
PromptEntityResult rt2 = Helper.Editor.GetEntity("\n选择曲线2:");
if (rt2.Status != PromptStatus.OK) return;
using (DBTransaction tr = new DBTransaction())
{
Curve cv1 = rt1.ObjectId.GetObject<Curve>();
if (cv1 == null) return;
Curve cv2 = rt2.ObjectId.GetObject<Curve>();
if (cv2 == null || cv1 == cv2) return;
Helper.Editor.WriteMessage("距离是:{0}", cv1.ToCurve3d().GetDistanceTo(cv2.ToCurve3d()));
PointOnCurve3d[] pocs = cv1.ToCurve3d().GetClosestPointTo(cv2.ToCurve3d());
Line l = new Line(pocs.Point, pocs.Point);
tr.OpenCurrentSpace(OpenMode.ForWrite);
tr.AddEntity(l);
}
} <p>看不大明白!</p><p>版主能否解释一下什么是Ge库曲线和Db曲线,有什么区别和联系,分别用于什么场合?</p> <p>感谢楼主!</p> 看不太懂