Tác giả: hmc@lisp.vn
Hàm ZoomHighlight
1 Thêm hàm ZoomHighlight
Lưu mã sau
Code:
public static void ZoomHighlight(ObjectId id)
{
// Lấy tài liệu hiện hành
Document doc = AcadApp.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
using (Transaction tr = doc.TransactionManager.StartTransaction())
{
try
{
// Lấy đối tượng từ ObjectId
Entity ent = tr.GetObject(id, OpenMode.ForRead) as Entity;
if (ent != null)
{
// Lấy giới hạn hình học của đối tượng
Extents3d extents = ent.GeometricExtents;
Point3d minPoint = extents.MinPoint;
Point3d maxPoint = extents.MaxPoint;
Point2d center = new Point2d(
(minPoint.X + maxPoint.X) / 2.0,
(minPoint.Y + maxPoint.Y) / 2.0
);
double height = maxPoint.Y - minPoint.Y;
double width = maxPoint.X - minPoint.X;
var cview = ed.GetCurrentView();
ed.WriteMessage("\n Chiều cao của view trước zoom: " + cview.Height);
// Tính tỷ lệ khung hình của viewport hiện tại
double aspectRatio = cview.Width / cview.Height;
// Điều chỉnh chiều cao và chiều rộng để phù hợp với khung nhìn
if (height * aspectRatio < width)
{
height = width / aspectRatio;
}
// đặt scale nếu muốn zoom nhỏ hơn object một ít
double scale = 1.5;
cview.CenterPoint = center;
cview.Height = height * scale;
cview.Width = height * aspectRatio * scale;
// Thiết lập view cho editor
ed.SetCurrentView(cview);
ent.Highlight();
ed.WriteMessage("\n Chiều cao của view sau zoom: " + cview.Height);
}
else
{
ed.WriteMessage("\nKhông thể lấy đối tượng từ ObjectId.");
}
tr.Commit();
}
catch (System.Exception ex)
{
ed.WriteMessage($"\nLỗi: {ex.Message}");//Tác giả: hmc@lisp.vn } } }
Tác giả: hmc@lisp.vn
Cách sử dụng
2 Thêm class MyCommands.cs
Code:
[CommandMethod("ZoomHighlight")]
public static void GiongblockCMD()
{
Document doc = AcadApp.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
using (DocumentLock lk = doc.LockDocument())
{
PromptEntityOptions peo = new PromptEntityOptions("\nPick object");
peo.SetRejectMessage("Please pick object");
var per = ed.GetEntity(peo);
if (per.Status != PromptStatus.OK)
{ return; }
var id = per.ObjectId;
ZoomHighlight(id);//Tác giả: hmc@lisp.vn } }
Tác giả: hmc@lisp.vn
Nhóm AutoCAD dotNet
Link tham gia nhóm Zalo: http://dnz.lisp.vn
---------------------------------------------------------------------------------------------
Mọi thông tin xin liên hệ Fanpage AutoLISP Thật là đơn giản!
Cảm ơn bạn đã theo dõi!

Không có nhận xét nào:
Đăng nhận xét