明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 726|回复: 1

[资源] 创建视口

[复制链接]
发表于 2020-10-19 00:04 | 显示全部楼层 |阅读模式
  1. [code=csharp][CommandMethod("cjsk")]
  2.         public void MakeVport()
  3.         {

  4.             Document doc = Application.DocumentManager.MdiActiveDocument;
  5.             Editor ed = doc.Editor;
  6.             Database db = doc.Database;

  7.             Entity MsClipBorder;
  8.             Entity PsClipBorder = new acDbSvs.Polyline();
  9.             acDbSvs.Viewport Vp = new acDbSvs.Viewport();
  10.             Point3d centerPoint = Point3d.Origin;
  11.             Point3d SelPoint0 = Point3d.Origin;
  12.             Point3d SelPoint1 = Point3d.Origin;
  13.             ObjectId ClipObjectId = ObjectId.Null;
  14.             double Scale = 96.0;
  15.             object orthomode = Application.GetSystemVariable("orthomode");//保存系统变量
  16.             string DrawingSalePrexfix = "1:";//注释比例前缀
  17.             //ini文件名称
  18.             string IniFileName = Directory.GetParent(StaticFileSystem.GetCurrentPath()) + "\\sys\\arch_tcc.ini";
  19.             string defaultText = "1:";

  20.             //判断英制还是公制
  21.             if (File.Exists(IniFileName))
  22.             {
  23.                 StringBuilder temp = new StringBuilder(1024);
  24.                 StaticFileSystem.GetPrivateProfileString("绘图单位", "绘图单位", defaultText, temp, 1024, IniFileName);
  25.                 if (temp.ToString() == "英制")
  26.                     DrawingSalePrexfix = "['"]";
  27.                 else
  28.                     DrawingSalePrexfix = "1:";
  29.             }
  30.             else
  31.                 DrawingSalePrexfix = "1:";

  32.             #region
  33.             //LayoutManager lm =  LayoutManager.Current;
  34.             //ObjectId cur_layoutid = lm.GetLayoutId(lm.CurrentLayout);
  35.             //string layoutname = LayoutManager.Current.CurrentLayout;
  36.             #endregion

  37.             //创建图层,设置颜色
  38.             layermanager.CreateLayer("viewport_border", 252);
  39.             layermanager.layerIsPlottable("viewport_border", false);

  40.             //切换到模型空间
  41.             object tilemode = Application.GetSystemVariable("tilemode");
  42.             if (System.Convert.ToInt16(tilemode) == 0)
  43.                 Application.SetSystemVariable("TILEMODE", 1);

  44.             //选择视口角点
  45.             PromptPointOptions Ppo = new PromptPointOptions("\n选择视口第一角点,[右键选择多段线、椭圆、圆]");
  46.             Ppo.AllowNone = true;
  47.             PromptPointResult Ppr = ed.GetPoint(Ppo);
  48.             if (Ppr.Status == PromptStatus.Cancel)
  49.             {
  50.                 PsClipBorder.Dispose();
  51.                 Vp.Dispose();
  52.                 return;
  53.             }

  54.             //用多段线、椭圆、圆作为视口边界
  55.             if (Ppr.Status == PromptStatus.None)
  56.             {
  57.                 PromptEntityOptions peo = new PromptEntityOptions("\n选择视口裁切的多段线、椭圆、圆");
  58.                 peo.SetRejectMessage("\n必须是多段线、圆、椭圆");
  59.                 peo.AddAllowedClass(typeof(acDbSvs.Polyline), false);
  60.                 peo.AddAllowedClass(typeof(acDbSvs.Circle), false);
  61.                 peo.AddAllowedClass(typeof(acDbSvs.Ellipse), false);
  62.                 PromptEntityResult per = ed.GetEntity(peo);
  63.                 if (per.Status != PromptStatus.OK)
  64.                 {
  65.                     PsClipBorder.Dispose();
  66.                     Vp.Dispose();
  67.                     return;
  68.                 }
  69.                 ClipObjectId = per.ObjectId;
  70.             }

  71.             //选择视口另一角点
  72.             if (Ppr.Status == PromptStatus.OK)
  73.             {
  74.                 SelPoint0 = Ppr.Value;
  75.                 PromptCornerOptions Pco = new PromptCornerOptions("\n选择视口另一角点", SelPoint0);
  76.                 PromptPointResult Pcr = ed.GetCorner(Pco);
  77.                 if (Pcr.Status == PromptStatus.Cancel)
  78.                 {
  79.                     PsClipBorder.Dispose();
  80.                     Vp.Dispose();
  81.                     return;
  82.                 }
  83.                 SelPoint1 = Pcr.Value;

  84.             }

  85.             //视口比例
  86.             if (GetScale() == false)//按取消键,退出
  87.             {
  88.                 PsClipBorder.Dispose();
  89.                 Vp.Dispose();
  90.                 return;
  91.             }
  92.             Scale = 1 / Scale;//注释比例,1/96

  93.             //切换至图纸空间
  94.             Application.SetSystemVariable("TILEMODE", 0);
  95.             doc.Editor.SwitchToPaperSpace();

  96.             //事务管理器tr
  97.             Transaction tr = db.TransactionManager.StartTransaction();
  98.             ////!!!此处用openclosetransaction无法通过!!!
  99.             //OpenCloseTransaction tr = db.TransactionManager.StartOpenCloseTransaction();
  100.             using (tr)
  101.             {
  102.                 try
  103.                 {
  104.                     BlockTable tbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
  105.                     // open 图纸空间块表
  106.                     BlockTableRecord btr = tr.GetObject(tbl[BlockTableRecord.PaperSpace], OpenMode.ForWrite) as BlockTableRecord;

  107.                     //选择图元作为视口裁切编辑
  108.                     if (ClipObjectId != ObjectId.Null)
  109.                     {
  110.                         MsClipBorder = tr.GetObject(ClipObjectId, OpenMode.ForRead) as Entity;
  111.                         PsClipBorder = MsClipBorder.Clone() as Entity;
  112.                     }

  113.                     //用户选择point,创建pline
  114.                     else
  115.                     {
  116.                         acDbSvs.Polyline pl = new acDbSvs.Polyline();
  117.                         pl.AddVertexAt(0, new Point2d(SelPoint0.X, SelPoint0.Y), 0, 0, 0);
  118.                         pl.AddVertexAt(0, new Point2d(SelPoint1.X, SelPoint0.Y), 0, 0, 0);
  119.                         pl.AddVertexAt(0, new Point2d(SelPoint1.X, SelPoint1.Y), 0, 0, 0);
  120.                         pl.AddVertexAt(0, new Point2d(SelPoint0.X, SelPoint1.Y), 0, 0, 0);
  121.                         PsClipBorder = pl as Entity;
  122.                     }

  123.                     //视口角点坐标
  124.                     Point3d maxPoint = PsClipBorder.GeometricExtents.MaxPoint;
  125.                     Point3d minPoint = PsClipBorder.GeometricExtents.MinPoint;

  126.                     //视口中心点
  127.                     centerPoint = new Point3d((maxPoint.X + minPoint.X) / 2, (maxPoint.Y + minPoint.Y) / 2, 0);
  128.                     //多段线闭合
  129.                     if (PsClipBorder is acDbSvs.Polyline)
  130.                     {
  131.                         acDbSvs.Polyline my_pl = PsClipBorder as acDbSvs.Polyline;
  132.                         my_pl.Closed = true;
  133.                     }

  134.                     //改到viewport_border层,不打印图层
  135.                     PsClipBorder.Layer = "viewport_border";
  136.                     //视口边界缩放到图纸空间
  137.                     PsClipBorder.TransformBy(Matrix3d.Scaling(Scale, centerPoint));

  138.                     //添加多段线到数据库
  139.                     btr.AppendEntity(PsClipBorder);
  140.                     tr.AddNewlyCreatedDBObject(PsClipBorder, true);


  141.                     //添加视口到数据库
  142.                     //vp.Annotative = AnnotativeStates.True;
  143.                     btr.AppendEntity(Vp);
  144.                     tr.AddNewlyCreatedDBObject(Vp, true);

  145.                     //设置视口参数
  146.                     Vp.NonRectClipEntityId = PsClipBorder.Id;
  147.                     var pt = new Point2d(centerPoint.X, centerPoint.Y);//视口在模型空间插入位置
  148.                     Vp.ViewCenter = pt; //模型空间中心位置
  149.                     Vp.ViewHeight = maxPoint.Y - minPoint.Y; //模型空间高度
  150.                     Vp.CenterPoint = centerPoint;  //视口插入位置
  151.                     Vp.Height = Scale * Vp.ViewHeight;  // 视口高度
  152.                     //vp.CustomScale = Scale;                    
  153.                     Vp.NonRectClipOn = true;
  154.                     Vp.Locked = true;
  155.                     //vp.Annotative = AnnotativeStates.True;//不能用
  156.                     Vp.On = true;

  157.                     //设置注释比例
  158.                     ObjectContextManager ocm = db.ObjectContextManager;
  159.                     ObjectContextCollection occ = ocm.GetContextCollection("ACDB_ANNOTATIONSCALES");
  160.                     foreach (AnnotationScale objcon in occ) //AnnotationScale
  161.                     {
  162.                         if (objcon.Scale.Equals(Scale) && Regex.IsMatch(objcon.Name, DrawingSalePrexfix))
  163.                         {
  164.                             Vp.AnnotationScale = objcon;
  165.                             //ed.WriteMessage("\n" + objcon.Name.ToString());
  166.                             break;
  167.                         }
  168.                     }

  169.                     tr.Commit();//提交更改
  170.                 }// end of try
  171.                 catch (System.Exception ex)
  172.                 {
  173.                     Vp.Dispose();
  174.                     PsClipBorder.Dispose();
  175.                     ed.WriteMessage("\ncatch" + ex.Message);
  176.                 }
  177.             }// end of using tr

  178.             //*************Jig 拖动**************************
  179.             Application.SetSystemVariable("orthomode", 0);
  180.             DragDrawJig jig = new DragDrawJig(centerPoint);
  181.             using (jig)
  182.             {
  183.                 try
  184.                 {
  185.                     jig.BasePnt = centerPoint;//拖动基点
  186.                     jig.PrmpStr = "\n视口中心位置";//提示字符                     

  187.                     //事务管理器
  188.                     using (Transaction tr1 = db.TransactionManager.StartTransaction())
  189.                     {
  190.                         BlockTable tbl = tr1.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
  191.                         BlockTableRecord btr = tr1.GetObject(tbl[BlockTableRecord.PaperSpace], OpenMode.ForWrite) as BlockTableRecord;

  192.                         #region
  193.                         //foreach (ObjectId id in objectIds)
  194.                         //{
  195.                         //    Entity ent = (Entity)tr.GetObject(id, OpenMode.ForWrite);
  196.                         //    jig.AddEntity(ent);
  197.                         //}                        
  198.                         //Entity vp_entity = tr1.GetObject(vp.ObjectId, OpenMode.ForWrite) as Entity;
  199.                         //Entity vp_entity = newpl.Clone() as Entity;                        
  200.                         //btr.AppendEntity(vp_entity);
  201.                         //tr.AddNewlyCreatedDBObject(vp_entity,false);e
  202.                         #endregion

  203.                         //以写方式打开视口裁切多段线
  204.                         tr1.GetObject(PsClipBorder.ObjectId, OpenMode.ForWrite);
  205.                         jig.AddEntity(PsClipBorder);

  206.                         // Draw Jig 交互
  207.                         PromptResult pr;
  208.                         pr = doc.Editor.Drag(jig);

  209.                         #region
  210.                         //if (pr.Status == PromptStatus.Keyword)
  211.                         //{
  212.                         //    doc.Editor.WriteMessage(pr.StringResult);
  213.                         //    doc.Editor.WriteMessage(pr.ToString());
  214.                         //}
  215.                         #endregion

  216.                         // 结果
  217.                         if (pr.Status == PromptStatus.OK)
  218.                         {
  219.                             //视口至鼠标选定位置
  220.                             jig.TransFormEntities();
  221.                             tr1.Commit();
  222.                             doc.Editor.WriteMessage("\n视口已创建");
  223.                             Application.SetSystemVariable("orthomode", orthomode);
  224.                             return;
  225.                         }
  226.                         else
  227.                         {
  228.                             //删除新建视口
  229.                             PsClipBorder.Erase();
  230.                             tr1.Commit();
  231.                             Application.SetSystemVariable("orthomode", orthomode);
  232.                             doc.Editor.WriteMessage("\n用户取消!");
  233.                             return;
  234.                         }
  235.                     }// end of using tr1

  236.                 }//end of try
  237.                 catch (System.Exception ex)
  238.                 {
  239.                     ed.WriteMessage("\nJig出错了");
  240.                     ed.WriteMessage("\ncatch" + ex.Message);
  241.                     Application.SetSystemVariable("orthomode", orthomode);
  242.                     return;
  243.                 }
  244.             }// end of using Jig

  245.             //输入视口比例函数
  246.             bool GetScale()
  247.             {
  248.                 //Editor editor = Application.DocumentManager.MdiActiveDocument.Editor;
  249.                 PromptDoubleOptions pdo = new PromptDoubleOptions("\n输入视口比例");
  250.                 pdo.AllowNegative = false;
  251.                 pdo.AllowZero = false;
  252.                 pdo.UseDefaultValue = true;
  253.                 pdo.DefaultValue = 96.0;
  254.                 PromptDoubleResult pdr = ed.GetDouble(pdo);
  255.                 if (pdr.Status == PromptStatus.Cancel)
  256.                 {
  257.                     //ed.WriteMessage("\ncancel");
  258.                     return false;
  259.                 }
  260.                 if (pdr.Status == PromptStatus.OK)
  261.                 {
  262.                     Scale = pdr.Value;
  263.                     return true;
  264.                 }
  265.                 else
  266.                 {
  267.                     Scale = pdo.DefaultValue;
  268.                     return true;
  269.                 }
  270.             }
  271.         }
[/code]

本帖子中包含更多资源

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

x
发表于 2020-10-19 21:43 | 显示全部楼层
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-5-5 10:24 , Processed in 0.275094 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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