栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

AutoCAD.NET c# 旋转视口

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

AutoCAD.NET c# 旋转视口

**
视口不能通过Entity.TransformBy来进行旋转操作,通过修改Viewport的属性TwistAngle、ViewCenter,同时旋转NonRectClipEntityId实体,达到旋转视口的目的。
**

[CommandMethod("RotateVP")]
public void RotateViewPort()
{
	ObjectId viewportId = ObjectId.Null;
	while (true)
    {
    	PromptSelectionOptions pso = new PromptSelectionOptions
        {
        	MessageForAdding = "n请选择视口:",
            SingleOnly = true
        };
        PromptSelectionResult psr = MCommands.CurrentEditor.GetSelection(pso);
        if (psr.Status != PromptStatus.OK) return;
    
        foreach (var id in psr.Value.GetObjectIds())
        {
        	if (id.ObjectClass.DxfName.ToUpper().Equals("VIEWPORT"))
            {
            	viewportId = id;
                break;
            }
        }
    
        if (viewportId.IsValid) break;
   }
    
	double rotateAngle = Math.PI * 0.25;//设置旋转角度45°
    
	using (Transaction tr = MCommands.CurrentDoc.TransactionManager.StartTransaction())
	{
		Viewport vp = tr.GetObject(viewportId, OpenMode.ForWrite) as Viewport;
    
        if (vp.NonRectClipEntityId.IsNull)
        {
        	//说明视口是矩形视口
        	ObjectId plId = ObjectId.Null; 
            using (BlockTableRecord btr = tr.GetObject(vp.BlockId, OpenMode.ForWrite) as BlockTableRecord)
           	{
				//生成视口裁剪区域
	            Point2d ptLeftBottom = new Point2d(vp.CenterPoint.X - vp.Width * 0.5, vp.CenterPoint.Y -
    vp.Height * 0.5);
                Point2d ptRightBottom = new Point2d(vp.CenterPoint.X + vp.Width * 0.5, vp.CenterPoint.Y -
    vp.Height * 0.5);
                Point2d ptRightTop = new Point2d(vp.CenterPoint.X + vp.Width * 0.5, vp.CenterPoint.Y +
    vp.Height * 0.5);
                Point2d ptLeftTop = new Point2d(vp.CenterPoint.X - vp.Width * 0.5, vp.CenterPoint.Y +
    vp.Height * 0.5);
                Polyline pl = new Polyline();
                pl.AddVertexAt(0, ptLeftBottom, 0, 0, 0);
                pl.AddVertexAt(1, ptRightBottom, 0, 0, 0);
                pl.AddVertexAt(2, ptRightTop, 0, 0, 0);
                pl.AddVertexAt(3, ptLeftTop, 0, 0, 0);
                pl.Closed = true;
    
                plId= btr.AppendEntity(pl);    
                tr.AddNewlyCreatedDBObject(pl, true);
			}    
            vp.NonRectClipEntityId = plId;
            vp.NonRectClipOn = true;
		}
		
		vp.TwistAngle += rotateAngle;
        vp.ViewCenter = vp.ViewCenter.RotateBy(rotateAngle, Point2d.Origin);
        Entity ent = tr.GetObject(vp.NonRectClipEntityId, OpenMode.ForWrite) as Entity;
        ent.TransformBy(Matrix3d.Rotation(rotateAngle, Vector3d.ZAxis, vp.CenterPoint));
    
        tr.Commit();
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/955668.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号