- UID
- 658062
- 积分
- 2147
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2008-10-22
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
//----------------------------------------------------------------------------------------------//
[CommandMethod("POLA", CommandFlags.Modal)]
public void PolyAreas()
{
var doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
var db = doc.Database;
var ed = doc.Editor;
Matrix3d ucs = ed.CurrentUserCoordinateSystem;
var sf = new SelectionFilter(new TypedValue[] { new TypedValue(0, "LWPOLYLINE"), new TypedValue(70, 1) });
var pso = new PromptSelectionOptions();
pso.MessageForAdding = "Select polylines (or enter for all): ";
pso.AllowDuplicates = false;
var psr = ed.GetSelection(pso, sf);
// If nothing selected then select all
if (psr.Status == PromptStatus.Error) psr = ed.SelectAll(sf);
// make sure the selection isn't empty
if (psr.Status == PromptStatus.OK && psr.Value.Count > 0)
{
using (var tr = db.TransactionManager.StartTransaction())
{
var btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
foreach (SelectedObject so in psr.Value)
{
var pline = (Polyline)tr.GetObject(so.ObjectId, OpenMode.ForRead);
Point3d cp = GetPseudoCentroid(db, pline).TransformBy(ucs);
string strObjId = pline.ObjectId.ToString();
strObjId = strObjId.Trim(new char[] { '(', ')' });
var mtx = new MText();
mtx.Contents = "%<\\AcObjProp Object(%<\\_ObjId " + strObjId + ">%).Area \\f \"%pr3%lu2%ct4%qf1 SQ. FT.\">%";
mtx.Location = cp;
mtx.Rotation = 0;
mtx.Attachment = AttachmentPoint.MiddleCenter;
btr.AppendEntity(mtx);
tr.AddNewlyCreatedDBObject(mtx, true);
tr.TransactionManager.QueueForGraphicsFlush();
}
doc.TransactionManager.FlushGraphics();
tr.Commit();
ed.Regen();
}
}
}
//----------------------------------------------------------------------------------------------//
public Point3d GetPseudoCentroid(Database db, Polyline pline)
{
Point3d cpt = new Point3d();
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
try
{
using (Transaction tr = db.TransactionManager.StartOpenCloseTransaction())
{
Point3d maxp = pline.GeometricExtents.MaxPoint;
Point3d minp = pline.GeometricExtents.MinPoint;
cpt = new Point3d((maxp.X + minp.X) / 2, (maxp.Y + minp.Y) / 2, (maxp.Z + minp.Z) / 2);
tr.Commit();
return cpt;
}
}
catch
{
// ed.WriteMessage("\nError on getting centroid");//debug only
return cpt;
}
}
//----------------------------------------------------------------------------------------------// |
|