你要注意的是我写参数的顺序,因为我试过参数弄乱了,反而多边形视口出问题了
【贵妃】[c#lisp]惊惊(540762622) 17:40:17
视口的构造继承了Entity,允许用户坐标系….
【贵妃】[c#lisp]惊惊(540762622) 17:41:10
而用户坐标系可以重新定义视口的原点,重新定义视口的原点之后,视点就会发生根据用户原点来计算可视位置
【贵妃】[c#lisp]惊惊(540762622) 17:41:52
不知道我说明白了没有,我第一次摸索的时候感觉有点复杂
【群主】Lisp#小轩轩(515357067) 17:43:13
也就是说你new 视口对象时候直接指定ucs原点,然后视口对象是跟着UCS走的
【贵妃】[c#lisp]惊惊(540762622) 17:43:14
/// <summary>/// 保存视口信息的类型/// </summary>public struct StructViewport{public Point3d ViewTarget { get; set; }public Point2d ViewCenter { get; set; }public double ViewHeight { get; set; }public double ViewWidth { get; set; }public Color ViewColor { get; set; }public string ViewLayerName { get; set; }public double ViewCustomScale { get; set; }public string ViewLineType { get; set; }public double ViewLineTypeScale { get; set; }public Vector3d ViewDirection { get; set; }public Point3d ViewCenterPoint { get; set; }public string[] ViewLayerNames { get; set; }public double ViewTwistAngle { get; set; }public bool ViewSnapmode { get; set; }public Entity Entity { get; set; }/// <summary>/// 生成视口的必须元素/// </summary>/// <param name="centerPoint">视口中点</param>/// <param name="width">视口宽</param>/// <param name="height">视口高</param>/// <param name="target">视口原点</param>/// <param name="center">视点中心</param>/// <param name="color">视口颜色</param>/// <param name="layerName">视口图层名字</param>/// <param name="lineTypeName">视口线型名称</param>/// <param name="lineTypeScale">视口线型比例</param>/// <param name="customScale">视口比例</param>/// <param name="direction">视图方向</param>/// <param name="layerNames">视口冻结的图层名称</param>/// <param name="twistAngle">视口角度</param>/// <param name="snapmode">视口捕捉</param>/// <param name="entity">克隆的图元</param>public StructViewport(Point3d centerPoint, double width, double height, Point3d target, Point2d center,Color color, string layerName, string lineTypeName, double lineTypeScale,double customScale, Vector3d direction,string[] layerNames, double twistAngle, bool snapmode,Entity entity = null){ViewCenterPoint = centerPoint;ViewHeight = height;ViewWidth = width;ViewTarget = target;ViewCenter = center;ViewColor = color;ViewLayerName = layerName;ViewLineType = lineTypeName;ViewLineTypeScale = lineTypeScale;ViewCustomScale = customScale;ViewDirection = direction;ViewLayerNames = layerNames;ViewTwistAngle = twistAngle;ViewSnapmode = snapmode;Entity = entity;}}
/// <summary>
/// 创建视口
/// </summary>
/// <param name="db">数据库</param>
/// <param name="vpa">保存视口的信息</param>
/// <returns></returns>
/// https://www.keanw.com/2009/12/creating-non-rectangular-paperspace-viewports-in-autocad-using-net.html
public static ObjectId AddViewportToMsPs(this Database db, StructViewport vpa)
{
ObjectId id = ObjectId.Null;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
//当前图纸生成一遍需要冻结的图层之后,再获取这些名字相同的图层id
var ids = new ObjectIdCollection();
var lay = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
foreach (var item in vpa.ViewLayerNames)
{
var layRec = tr.GetObject(lay[item], OpenMode.ForRead) as LayerTableRecord;
ids.Add(layRec.ObjectId);
}
//创建视口
Viewport vp = new Viewport();
id = tr.AddEntityToMsPs(db, vp); //加入数据库
//如果是多边形视口,那么就通过点表新建多段线
if (vpa.Entity != null)
{
var ent = vpa.Entity.Clone() as Entity; //重复执行要克隆一次,以保证有个母本不提交状态
ent.Layer = vpa.ViewLayerName; //图层
ent.Color = vpa.ViewColor; //颜色
ent.Linetype = vpa.ViewLineType;//线型名称
ent.LinetypeScale = vpa.ViewLineTypeScale;//线型比例
vp.NonRectClipEntityId = tr.AddEntityToMsPs(db, ent);//多边形线
vp.NonRectClipOn = true;//不受约束的,多边形视口所需
}
vp.On = true;//激活视口
vp.Locked = false;//视口锁定
{
vp.Width = vpa.ViewWidth; //视口宽度
vp.Height = vpa.ViewHeight; //视口高度
vp.CenterPoint = vpa.ViewCenterPoint;//插入点
vp.ViewTarget = vpa.ViewTarget; //视口原点
vp.ViewCenter = vpa.ViewCenter; //视口中心
vp.ViewDirection = vpa.ViewDirection;//观察方向
vp.CustomScale = vpa.ViewCustomScale;//视口比例
vp.FreezeLayersInViewport(ids.GetEnumerator()); //冻结视口内图层
vp.TwistAngle = vpa.ViewTwistAngle;//视口角度
vp.SnapOn = vpa.ViewSnapmode; //视口捕捉
vp.Layer = vpa.ViewLayerName; //图层
vp.Color = vpa.ViewColor; //颜色
vp.Linetype = vpa.ViewLineType; //线型名称
vp.LinetypeScale = vpa.ViewLineTypeScale;//线型比例
}
vp.Locked = true;//视口锁定
tr.Commit();
}
return id;
}
