【源码】选取模型空间多段线框,生成布局视口
本帖最后由 qq1254582201 于 2020-3-15 15:15 编辑public void MakeVport()
{ Document dc = Application.DocumentManager.MdiActiveDocument;
Database dtb = dc.Database;
Transaction tr = dtb.TransactionManager.StartTransaction();
var pl = Select("\n请选择多段线");
var Scale = GetScale();
Application.SetSystemVariable("TILEMODE", 0);
dc.Editor.SwitchToPaperSpace();
using (tr)
{
BlockTable bta = tr.GetObject(dtb.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = tr.GetObject(bta, OpenMode.ForWrite) as BlockTableRecord;
Polyline newpl = pl.Clone() as Polyline;
Point3d maxPoint = newpl.GeometricExtents.MaxPoint;
Point3d minPoint = newpl.GeometricExtents.MinPoint;
Point3d centerPoint = new Point3d((maxPoint.X + minPoint.X) / 2, (maxPoint.Y + minPoint.Y) / 2, 0);
newpl.Closed = true;
btr.AppendEntity(newpl);
tr.AddNewlyCreatedDBObject(newpl, true);
Viewport vp = new Viewport();
btr.AppendEntity(vp);
tr.AddNewlyCreatedDBObject(vp, true);
vp.NonRectClipEntityId = newpl.Id;
vp.CenterPoint = centerPoint;//视口插入位置
var pt = new Point2d(centerPoint.X, centerPoint.Y);
vp.ViewCenter = pt; //模型空间中心位置
vp.ViewHeight = maxPoint.Y - minPoint.Y; //模型空间高度
vp.Height = Scale * vp.ViewHeight;//视口高度
vp.NonRectClipOn = true;
vp.Locked = true;
vp.On = true;
tr.Commit();
}
}
/// <summary>
/// 选择多段线
/// </summary>
/// <param name="word">提示选择文字</param>
/// <returns>多段线实体</returns>
public static Polyline Select(string word)
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database; Editor ed = doc.Editor;
Polyline entity = null;
PromptEntityResult ent = ed.GetEntity(word);
if (ent.Status == PromptStatus.OK)
{
using (Transaction transaction = db.TransactionManager.StartTransaction())
{
entity = (Polyline)transaction.GetObject(ent.ObjectId, OpenMode.ForWrite, true);
transaction.Commit();
}
}
return entity;
}
/// <summary>
/// 获取设定视口的比例
/// </summary>
/// <returns></returns>
public double GetScale()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
PromptDoubleOptionsoptDou = new PromptDoubleOptions ("\n请输入视口比例");
optDou.AllowNegative=false;
optDou.AllowZero=false;
optDou.UseDefaultValue = true;
optDou.DefaultValue = 1.00;
PromptDoubleResult resDou = ed.GetDouble(optDou);
if (resDou.Status == PromptStatus.OK)
{
double Scale = resDou.Value;
return Scale;
}
else
return 1.00;
}
演示图片:
`中微子 发表于 2023-2-18 16:11
这不是AUTOLISP代码,里面怎么还有大括号。我保存为LSP文件加载不了。
这里是NET板块 `中微子 发表于 2023-2-18 16:11
这不是AUTOLISP代码,里面怎么还有大括号。我保存为LSP文件加载不了。
人家这是C#代码 不是 lsp 代码 `中微子 发表于 2023-2-18 16:11
这不是AUTOLISP代码,里面怎么还有大括号。我保存为LSP文件加载不了。
这是c#代码 随便把图框图名图号给布置了就比较更方便了 楼主,怎么使用 不知道该咋使用,这个复制后保存成什么格式呢,然后怎么加载呢,目前只会加载lsp的 感谢楼主分享 下载试试! 支持支持,,目前对CAD的认知有限,对布局还不知道怎么利用,先收藏一下,谢谢楼主分享 感谢楼主分享下载试试! 顶起,谢谢楼主分享 请教下怎么使用,感谢!! 感谢楼主分享下载试试!
页:
[1]
2