- 积分
- 4400
- 明经币
- 个
- 注册时间
- 2015-1-16
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
发表于 2021-4-28 19:41:23
|
显示全部楼层
- [CommandMethod("MyWBlock", CommandFlags.Modal)]
- public static void ExecuteViewSetting()
- {
- Document doc = Application.DocumentManager.MdiActiveDocument;
- Database db = doc.Database;
- Editor ed = doc.Editor;
- PromptSelectionResult prRes = ed.GetSelection();
- if (prRes.Status != PromptStatus.OK) return;
- ObjectIdCollection objIds = new ObjectIdCollection();
- ObjectId[] objIdArray = prRes.Value.GetObjectIds();
- double dMinX = 0, dMinY = 0, dMaxX = 0, dMaxY = 0;
- /// Copy objectIds to objectIdCollection
- foreach (ObjectId id in objIdArray)
- {
- using (Transaction txn = id.Database.TransactionManager.StartTransaction())
- {
- Entity acEnt = txn.GetObject(id, OpenMode.ForRead) as Entity;
- if (null != acEnt)
- {
- try
- {
- if ((dMinX == 0) && (dMinY == 0) && (dMaxX == 0) && (dMaxY == 0))
- {
- dMinX = acEnt.GeometricExtents.MinPoint.X;
- dMinY = acEnt.GeometricExtents.MinPoint.Y;
- dMaxX = acEnt.GeometricExtents.MaxPoint.X;
- dMaxY = acEnt.GeometricExtents.MaxPoint.Y;
- }
- else
- {
- if (dMinX > acEnt.GeometricExtents.MinPoint.X) dMinX = acEnt.GeometricExtents.MinPoint.X;
- if (dMinY > acEnt.GeometricExtents.MinPoint.Y) dMinY = acEnt.GeometricExtents.MinPoint.Y;
- if (dMaxX < acEnt.GeometricExtents.MaxPoint.X) dMaxX = acEnt.GeometricExtents.MaxPoint.X;
- if (dMaxY < acEnt.GeometricExtents.MaxPoint.Y) dMaxY = acEnt.GeometricExtents.MaxPoint.Y;
- }
- }
- catch { }
- }
- txn.Commit();
- }
- objIds.Add(id);
- }
- using (Database acDatabase = new Database(true, false))
- {
- db.Wblock(acDatabase, objIds, Point3d.Origin, DuplicateRecordCloning.Ignore);
- ///translate to (0,0,0)
- using (Transaction txn = acDatabase.TransactionManager.StartTransaction())
- {
- Matrix3d mat = Matrix3d.Displacement(new Point3d((dMinX + dMaxX) * 0.5, (dMinY + dMaxY) * 0.5, 0).GetVectorTo(Point3d.Origin));
- BlockTable acBlkTbl = (BlockTable)txn.GetObject(acDatabase.BlockTableId, OpenMode.ForRead);
- BlockTableRecord acBlkTblRcd = (BlockTableRecord)txn.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
- foreach (ObjectId objId in acBlkTblRcd)
- {
- Entity acEnt = txn.GetObject(objId, OpenMode.ForWrite) as Entity;
- if (null != acEnt)
- {
- acEnt.TransformBy(mat);
- }
- }
- ed.ZoomExtens();
- txn.Commit();
- }
- ///up to here
- string FileName = "C:\\temp\\wblock.dwg";
- acDatabase.SaveAs(FileName, DwgVersion.Current);
- }
- }
|
|