- public static void FindCenterPoint()
- {
- Database db = HostApplicationServices.WorkingDatabase;
- Editor ed = Autodesk..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[BlockTableRecord.ModelSpace], 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);
- }
- }
|