码迷,mamicode.com
首页 > Web开发 > 详细

CAD.net二次开发--自动调成图框位置

时间:2016-07-24 17:34:47      阅读:281      评论:0      收藏:0      [点我收藏+]

标签:

[CommandMethod("mm")]
        public void mm()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            TypedValue[] values = new TypedValue[]
            {
                new TypedValue((int)DxfCode.LayerName,"TK")
            };
            SelectionFilter selfilter = new SelectionFilter(values);
            PromptSelectionResult psr = ed.SelectAll(selfilter);
            if (psr.Status == PromptStatus.OK)
            {
                ed.WriteMessage(psr.Value.Count.ToString());
            }
            else return;
            SelectionSet ss = psr.Value;
            List<Polyline> pls=new List<Polyline>();
            Point2dCollection points = new Point2dCollection();
            Polyline mypl = new Polyline();
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                foreach (ObjectId id in ss.GetObjectIds())
                {
                    Polyline ent = trans.GetObject(id, OpenMode.ForWrite) as Polyline;
                    if(ent!=null)
                    {
                        if (!ent.Closed) ent.Closed=true;
                        mypl = ent;
                        for (int i = 0; i < ent.NumberOfVertices; i++)
                        {
                            points.Add(ent.GetPoint2dAt(i));

                        }
                    }
                }
                trans.Commit();
            }
            double xmin = points[0].X;
            double xmax = points[0].X;
            double ymin = points[0].Y; 
            double ymax = points[0].Y;
            double basex = 0; double basex1 = 0;
            double basey = 0; double basey1 = 0;
            
            ed.WriteMessage(mypl.NumberOfVertices.ToString());
            for (int i = 0; i < mypl.NumberOfVertices; i++)
            {
                ed.WriteMessage(points[i].ToString());
            }
                for (int i = 1; i < mypl.NumberOfVertices; i++)
                {
                    
                    if (xmin > points[i].X) xmin = points[i].X;
                    if (xmax < points[i].X) xmax = points[i].X;
                    if (ymin > points[i].Y) ymin = points[i].Y;
                    if (ymax < points[i].Y) ymax = points[i].Y;
                }
            ed.WriteMessage("\n" + xmin.ToString() + "\n" + ymin.ToString() + "\n" );
            basex = xmin + 6.0;
            basey = ymin + 6.0;
            basex1 = ((int)(basex * 0.1 + 0.5))*10;
            basey1 = ((int)(basey * 0.1 + 0.5))*10;
            ed.WriteMessage("\n改后:"+basex1.ToString());
            ed.WriteMessage("\n改后:" + basey1.ToString());
            Vector3d vec = new Vector3d(basex1 - basex, basey1 - basey,0);
            ////////////////////////////////////////////////////
            Point3d[] p = new Point3d[4];
            p[0] = new Point3d(xmin + 6.0, ymin + 6.0, 0);
            p[1] = new Point3d(xmax - 6.0, ymin + 6.0, 0); double basex2 = ((int)(p[1].X * 0.1 + 0.5)) * 10; double basey2 = ((int)(p[1].Y * 0.1 + 0.5)) * 10;
            p[2] = new Point3d(xmax - 6.0, ymax - 6.0, 0); double basex3 = ((int)(p[2].X * 0.1 + 0.5)) * 10; double basey3 = ((int)(p[2].Y * 0.1 + 0.5)) * 10;
            p[3] = new Point3d(xmin + 6.0, ymax - 6.0, 0); double basex4 = ((int)(p[3].X * 0.1 + 0.5)) * 10; double basey4 = ((int)(p[3].Y * 0.1 + 0.5)) * 10;
             ObjectId[] text1id = Class2.fun(p[0]);
             //ed.WriteMessage("字符串长度:"+str.Length.ToString());
             using (Transaction tr = db.TransactionManager.StartTransaction())
             {                
                     DBText thistext1 = tr.GetObject(text1id[0], OpenMode.ForWrite) as DBText;
                     thistext1.TextString = basey1.ToString();
                     DBText thistext2 = tr.GetObject(text1id[1], OpenMode.ForWrite) as DBText;
                     thistext2.TextString = basex1.ToString();                
                 tr.Commit();
             }

             ObjectId[] text2id = Class2.fun(p[1]);
             //ed.WriteMessage("字符串长度:"+str.Length.ToString());
             using (Transaction tr = db.TransactionManager.StartTransaction())
             {
                 DBText thistext1 = tr.GetObject(text2id[0], OpenMode.ForWrite) as DBText;
                 thistext1.TextString = basey2.ToString();
                 DBText thistext2 = tr.GetObject(text2id[1], OpenMode.ForWrite) as DBText;
                 thistext2.TextString = basex2.ToString();
                 tr.Commit();
             }

             ObjectId[] text3id = Class2.fun(p[2]);
             //ed.WriteMessage("字符串长度:"+str.Length.ToString());
             using (Transaction tr = db.TransactionManager.StartTransaction())
             {
                 DBText thistext1 = tr.GetObject(text3id[0], OpenMode.ForWrite) as DBText;
                 thistext1.TextString = basey3.ToString();
                 DBText thistext2 = tr.GetObject(text3id[1], OpenMode.ForWrite) as DBText;
                 thistext2.TextString = basex3.ToString();
                 tr.Commit();
             }

             ObjectId[] text4id = Class2.fun(p[3]);
             //ed.WriteMessage("字符串长度:"+str.Length.ToString());
             using (Transaction tr = db.TransactionManager.StartTransaction())
             {
                 DBText thistext1 = tr.GetObject(text4id[0], OpenMode.ForWrite) as DBText;
                 thistext1.TextString = basex4.ToString();
                 DBText thistext2 = tr.GetObject(text4id[1], OpenMode.ForWrite) as DBText;
                 thistext2.TextString = basey4.ToString();
                 tr.Commit();
             }
            /////////////////////////修改图框表头下面的代号////////////////////////
             Point3d pointmid = new Point3d((xmin+xmax)*0.5,ymax,0);
             ObjectId[] idmid = Class2.fun(pointmid);
             using (Transaction tr = db.TransactionManager.StartTransaction())
             {
                 DBText textmid = tr.GetObject(idmid[0], OpenMode.ForWrite) as DBText;
                 string strtemp = basey1.ToString() + basex1.ToString();
                 strtemp = strtemp.Insert(3, ".");
                 strtemp = strtemp.Insert(7, "-");
                 strtemp = strtemp.Insert(11, ".");
                 textmid.TextString = strtemp;                 
                 tr.Commit();
             }
            ///////////////////////////////////////////////////
            using (Transaction tran1 = db.TransactionManager.StartTransaction())
            {
                Matrix3d mt = Matrix3d.Displacement(vec);
                foreach (ObjectId id in ss.GetObjectIds())
                {
                    Entity ent = tran1.GetObject(id, OpenMode.ForWrite) as Entity;

                    if (ent != null)
                    {
                        ent.TransformBy(mt);
                    }
                }
                tran1.Commit();
            }
        }

1、选择整个图框,找到图框线4个节点坐标,并排序

2、将图框放在特定位置,并修改新位置坐标,程序的fun(point3d p)函数在令一篇日志。

本文主要供新手学习。

 

CAD.net二次开发--自动调成图框位置

标签:

原文地址:http://www.cnblogs.com/w-lumi/p/5701052.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!