明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 3874|回复: 16

[打印] 【源码】选取模型空间多段线框,生成布局视口

  [复制链接]
发表于 2020-3-14 23:45 | 显示全部楼层 |阅读模式
本帖最后由 qq1254582201 于 2020-3-15 15:15 编辑
  1.   [CommandMethod("CPP")]
  2.         public void MakeVport()
  3.          {            Document dc = Application.DocumentManager.MdiActiveDocument;
  4.             Database dtb = dc.Database;
  5.             Transaction tr = dtb.TransactionManager.StartTransaction();
  6.             var pl = Select("\n请选择多段线");
  7.             var Scale = GetScale();
  8.             Application.SetSystemVariable("TILEMODE", 0);
  9.             dc.Editor.SwitchToPaperSpace();
  10.             using (tr)
  11.             {
  12.                 BlockTable bta = tr.GetObject(dtb.BlockTableId, OpenMode.ForRead) as BlockTable;
  13.                 BlockTableRecord btr = tr.GetObject(bta[BlockTableRecord.PaperSpace], OpenMode.ForWrite) as BlockTableRecord;
  14.                 Polyline newpl = pl.Clone() as Polyline;
  15.                 Point3d maxPoint = newpl.GeometricExtents.MaxPoint;
  16.                 Point3d minPoint = newpl.GeometricExtents.MinPoint;
  17.                 Point3d centerPoint = new Point3d((maxPoint.X + minPoint.X) / 2, (maxPoint.Y + minPoint.Y) / 2, 0);
  18.                 newpl.Closed = true;
  19.                 btr.AppendEntity(newpl);
  20.                 tr.AddNewlyCreatedDBObject(newpl, true);   
  21.                 Viewport vp = new Viewport();
  22.                 btr.AppendEntity(vp);
  23.                 tr.AddNewlyCreatedDBObject(vp, true);
  24.                 vp.NonRectClipEntityId = newpl.Id;
  25.                 vp.CenterPoint = centerPoint;  //视口插入位置
  26.                 var pt = new Point2d(centerPoint.X, centerPoint.Y);
  27.                 vp.ViewCenter = pt; //模型空间中心位置
  28.                 vp.ViewHeight = maxPoint.Y - minPoint.Y; //模型空间高度
  29.                 vp.Height = Scale * vp.ViewHeight;  //视口高度
  30.                 vp.NonRectClipOn = true;
  31.                 vp.Locked = true;
  32.                 vp.On = true;
  33.                 tr.Commit();
  34.             }
  35.         }


  36.         /// <summary>
  37.         /// 选择多段线
  38.         /// </summary>
  39.         /// <param name="word">提示选择文字</param>
  40.         /// <returns>多段线实体</returns>

  41.         public static Polyline Select(string word)
  42.         {
  43.             Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
  44.             Database db = doc.Database; Editor ed = doc.Editor;
  45.             Polyline entity = null;
  46.             PromptEntityResult ent = ed.GetEntity(word);
  47.             if (ent.Status == PromptStatus.OK)
  48.             {
  49.                 using (Transaction transaction = db.TransactionManager.StartTransaction())
  50.                 {
  51.                     entity = (Polyline)transaction.GetObject(ent.ObjectId, OpenMode.ForWrite, true);
  52.                     transaction.Commit();
  53.                 }
  54.             }
  55.             return entity;
  56.         }
  57.         /// <summary>
  58.         /// 获取设定视口的比例
  59.         /// </summary>
  60.         /// <returns></returns>
  61.         public double GetScale()
  62.         {
  63.             Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
  64.             PromptDoubleOptions  optDou = new PromptDoubleOptions ("\n请输入视口比例");
  65.             optDou.AllowNegative=false;
  66.             optDou.AllowZero=false;
  67.             optDou.UseDefaultValue = true;
  68.             optDou.DefaultValue = 1.00;
  69.             PromptDoubleResult resDou = ed.GetDouble(optDou);
  70.             if (resDou.Status == PromptStatus.OK)
  71.             {
  72.                 double Scale = resDou.Value;
  73.                 return Scale;
  74.             }
  75.             else
  76.                 return 1.00;
  77.         }

演示图片:



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x

评分

参与人数 3明经币 +2 金钱 +20 收起 理由
d1742647821 + 1 评论里lisp区的不要来捣乱好吗??
Civil3D + 20 赞一个!
USER2128 + 1 赞一个!

查看全部评分

发表于 2023-2-21 20:42 | 显示全部楼层
`中微子 发表于 2023-2-18 16:11
这不是AUTOLISP代码,里面怎么还有大括号。我保存为LSP文件加载不了。

这里是NET板块
回复 支持 1 反对 0

使用道具 举报

发表于 2023-9-2 01:27 来自手机 | 显示全部楼层
`中微子 发表于 2023-2-18 16:11
这不是AUTOLISP代码,里面怎么还有大括号。我保存为LSP文件加载不了。

人家这是C#代码 不是 lsp 代码
发表于 2023-9-2 01:25 来自手机 | 显示全部楼层
`中微子 发表于 2023-2-18 16:11
这不是AUTOLISP代码,里面怎么还有大括号。我保存为LSP文件加载不了。

这是c#代码
发表于 2020-3-15 20:18 | 显示全部楼层
随便把图框图名图号给布置了就比较更方便了
发表于 2020-6-12 10:23 | 显示全部楼层
楼主,怎么使用
发表于 2020-9-5 12:40 | 显示全部楼层
不知道该咋使用,这个复制后保存成什么格式呢,然后怎么加载呢,目前只会加载lsp的
发表于 2020-9-5 16:05 | 显示全部楼层
感谢楼主分享 下载试试!
发表于 2020-9-5 16:58 | 显示全部楼层
支持支持,,目前对CAD的认知有限,对布局还不知道怎么利用,先收藏一下,谢谢楼主分享
发表于 2020-9-23 10:20 | 显示全部楼层
感谢楼主分享下载试试!
发表于 2020-9-23 10:30 | 显示全部楼层
顶起,谢谢楼主分享
发表于 2021-3-10 13:09 | 显示全部楼层
请教下怎么使用,感谢!!
发表于 2021-3-12 15:52 | 显示全部楼层
感谢楼主分享下载试试!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-5-6 09:22 , Processed in 0.561817 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表