- UID
- 658062
- 积分
- 2147
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2008-10-22
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
[CommandMethod("hat", CommandFlags.Modal)]
public static void IntersectHatchWithCurves()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
Transaction tr = db.TransactionManager.StartTransaction();
using (tr)
{
try
{
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite, false) as BlockTableRecord;
PromptEntityOptions peo = new PromptEntityOptions("\nSelect a hatch: ");
peo.SetRejectMessage("Select hatch only!");
peo.AddAllowedClass(typeof(Hatch), true);
PromptEntityResult res = doc.Editor.GetEntity(peo);
if (res.Status != PromptStatus.OK) return;
// Point3d pt = res.PickedPoint;
Entity ent = tr.GetObject(res.ObjectId, OpenMode.ForRead, false) as Entity;
Hatch hat = ent as Hatch;
if (hat == null)
{
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("No luck, mate...");
return;
}
// get outer loop of hatch
HatchLoop loops = hat.GetLoopAt(0);
Polyline pline = new Polyline();
if (loops.IsPolyline)
{
DoubleCollection bulgcoll=new DoubleCollection();
Point2dCollection pts=new Point2dCollection();
BulgeVertexCollection bulges = loops.Polyline;
for (int i=0;i<bulges.Count;i++)
{
BulgeVertex bulg = bulges;
bulgcoll.Add(bulg.Bulge);
pts.Add(bulg.Vertex);
}
// create polyline around of the hatch
pline = new Polyline(bulges.Count);
for (int n = 0; n < pts.Count; n++)
{
pline.AddVertexAt(n, pts[n], bulgcoll[n], 0, 0);
}
// commented lines used just for debug only
//btr.AppendEntity(pline);
//tr.AddNewlyCreatedDBObject(pline, true);
//pline.ColorIndex = 5;
}
// select all intersection curves on layer "0"
TypedValue[] tvs = new TypedValue[] {
new TypedValue((int) DxfCode.Start, "arc,circle,line,lwpolyline,spline"),
new TypedValue((int) DxfCode.LayerName, "0")
};
SelectionFilter sf = new SelectionFilter(tvs);
PromptSelectionOptions pso = new PromptSelectionOptions();
pso.MessageForRemoval="\nwrong object selected!";
pso.MessageForAdding = "\nSelect curves separately one by another: ";
PromptSelectionResult psr = ed.GetSelection(pso,sf);
int count = 0;
if (psr.Status == PromptStatus.OK)
{
count = psr.Value.GetObjectIds().Length;
ed.WriteMessage("\nSelected: " + count.ToString());
}
else
{
ed.WriteMessage("\nBad selection");
return;
}
foreach (SelectedObject selobj in psr.Value)
{
DBObject obj = tr.GetObject(selobj.ObjectId, OpenMode.ForRead) as DBObject;
Curve curv = obj as Curve;
if (obj.ObjectId != pline.ObjectId)
{
Point3dCollection ipts = new Point3dCollection();
// next line is may have another syntax for other Acad release
pline.IntersectWith(curv, Intersect.OnBothOperands, ipts, (int)IntPtr.Zero, (int)IntPtr.Zero);
if (ipts.Count > 0)
{
foreach (Point3d pt in ipts)
{
// draw circles in intersection for dispaying the result
Circle circ = new Circle(pt, Vector3d.ZAxis, 0.5);
btr.AppendEntity(circ);
tr.AddNewlyCreatedDBObject(circ, true);
circ.ColorIndex = 2;
}
}
}
}
tr.Commit();
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
ed.WriteMessage("\n" + ex.Message + "\n" + ex.StackTrace);
}
finally
{
}
}
}
|
|