获取选择集的中心点(包括用程序创建图块对象)
本帖最后由 lzx838 于 2012-7-3 09:14 编辑public static void FindCenterPoint()
{
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
try
{
using (Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument())
{
PromptSelectionOptions pso = new PromptSelectionOptions();
pso.MessageForAdding = "\n请选取要查找中心点的对象:";
PromptSelectionResult ents = ed.GetSelection(pso);
if (ents.Status == PromptStatus.OK)
{
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead, false);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt, OpenMode.ForWrite, false);
Extents3d box = new Extents3d();
foreach (ObjectId id in ents.Value.GetObjectIds())
{
Entity entity = trans.GetObject(id, OpenMode.ForWrite, true) as Entity;
if (entity != null && !(entity is Spline))
{
box.AddExtents(entity.GeometricExtents);
}
else
{
Spline spLineObject = entity as Spline;
box.AddExtents(spLineObject.ToPolyline(1000).GeometricExtents);
}
}
int intIndex = 0;
double douWidth = 0;
//创建边界线
Polyline polyLineObject = new Polyline();
polyLineObject.AddVertexAt(intIndex++, new Point2d(box.MinPoint.X, box.MinPoint.Y), 0, douWidth, douWidth);
polyLineObject.AddVertexAt(intIndex++, new Point2d(box.MinPoint.X, box.MaxPoint.Y), 0, douWidth, douWidth);
polyLineObject.AddVertexAt(intIndex++, new Point2d(box.MaxPoint.X, box.MaxPoint.Y), 0, douWidth, douWidth);
polyLineObject.AddVertexAt(intIndex++, new Point2d(box.MaxPoint.X, box.MinPoint.Y), 0, douWidth, douWidth);
polyLineObject.LayerId = LzxLibrary.LzxSymbolTables.LzxLayerTable.LzxLayerTableRecord.CreateLayer("实体边界", 4);
polyLineObject.Closed = true;
btr.AppendEntity(polyLineObject);
trans.AddNewlyCreatedDBObject(polyLineObject, true);
//插入中心点图块
BlockReference blockObject = new BlockReference(
LzxLibrary.LzxLibraryFunction.Midpoint(box.MinPoint, box.MaxPoint),
COM.LzxCreateBlock.CreateBlock_CenterPoint());
btr.AppendEntity(blockObject);
trans.AddNewlyCreatedDBObject(blockObject, true);
trans.Commit();
}
}
}
}
catch (Exception ex)
{
ed.WriteMessage("获取中心点发生错误:" + ex.Message);
}
} 创建图块
/// <summary>
/// 创建图块_中心点
/// </summary>
/// <returns>ObjectId</returns>
public static ObjectId CreateBlock_CenterPoint()
{
ObjectId objectId = ObjectId.Null;
Point3d insPoint = Point3d.Origin;
List<Entity> entitys = new List<Entity>();
//创建圆
Circle circleObject = new Circle(insPoint, Vector3d.ZAxis, 0.2);
circleObject.ColorIndex = 7;
entitys.Add(circleObject);
//创建直线1
Line lineObject1 = new Line(new Point3d(-0.2, 0, 0), new Point3d(+0.2, 0, 0));
lineObject1.ColorIndex = 2;
entitys.Add(lineObject1);
//创建直线2
Line lineObject2 = new Line(new Point3d(0, -0.2, 0), new Point3d(0, +0.2, 0));
lineObject2.ColorIndex = 2;
entitys.Add(lineObject2);
objectId = LzxLibrary.LzxSymbolTables.LzxBlockTableClass.AppendEntityToBlockTable("CenterBlock", insPoint, entitys);
return objectId;
} /// <summary>
/// 将实体对象加入到块表中,创建图块对象。
/// </summary>
/// <param name="BlockName">图块名称</param>
/// <param name="InsPoint">图块插入点</param>
/// <param name="entitys">要加入图块的实体对象</param>
/// <returns>实体对象ObjectId</returns>
public static ObjectId AppendEntityToBlockTable(string BlockName, Point3d InsPoint, List<Entity> entitys)
{
ObjectId blockId = ObjectId.Null; //用于返回所创建的块的对象Id
try
{
if (entitys.Count > 0)
{
Database db = HostApplicationServices.WorkingDatabase;
BlockTableRecord record = new BlockTableRecord(); //创建一个BlockTableRecord类的对象,表示所要创建的块
record.Name = BlockName; //设置块名
using (Transaction trans = db.TransactionManager.StartTransaction())
{
//设置块的基点,也就是块的插入点
record.Origin = InsPoint;
//将实体对象加入到新建的BlockTableRecord对象
foreach (var item in entitys)
{
record.AppendEntity(item);
}
//以写的方式打开块表
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForWrite);
//判断是否存在相同的块名
if (!bt.Has(BlockName))
{
blockId = bt.Add(record); //在块表中加入块
trans.AddNewlyCreatedDBObject(record, true);//通知事务处理
}
else
{
blockId = bt;
}
trans.Commit();
}
}
}
catch (Exception ex)
{
throw ex;
}
return blockId;
} 支持一个,第一个坐上沙发 本帖最后由 xman00 于 2012-10-14 22:30 编辑
楼主程序在08中测试,返回:no function definition: AUTODESK,
二、三楼程序在08中测试no function definition: INSPOINT
请求确认,正在求教本类问题,两位若看到,请到本版”快速建块的问题,知道的进“贴中进行讨论
页:
[1]