Example #1
0
        public void autoca()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db  = HostApplicationServices.WorkingDatabase;
            var ed  = doc.Editor;

            try
            {
                var per1 = ed.GetEntity("选择测试设备:\n");
                if (per1.Status != PromptStatus.OK)
                {
                    return;
                }
                using (var tr = doc.TransactionManager.StartTransaction())
                {
                    var bt   = (BlockTable)(tr.GetObject(db.BlockTableId, OpenMode.ForRead));
                    var btr  = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                    var ent1 = tr.GetObject(per1.ObjectId, OpenMode.ForRead) as Entity;
                    if (ent1 == null)
                    {
                        return;
                    }
                    if (ent1 is Polyline)
                    {
                        var pp = ent1 as Polyline;
                        ed.WriteMessage(pp.Closed);
                        ed.WriteMessage("\n");
                    }
                    else if (ent1 is BlockReference)
                    {
                        var rr = ent1.Clone() as BlockReference;
                        if (rr != null)
                        {
                            // 记录当前角度
                            var rolat = rr.Rotation;
                            // 把角度设置为0
                            rr.Rotation = 0;
                            // 得到角度为0的实体范围
                            var ext = rr.GeometricExtents;

                            // 生成一个测试矩形,颜色为黄色
                            var pl = new Polyline();
                            pl.CreateRectangle(new Point2d(ext.MaxPoint.X, ext.MaxPoint.Y), new Point2d(ext.MinPoint.X, ext.MinPoint.Y));
                            pl.ColorIndex = 2;

                            // 旋转实体范围
                            Matrix3d mt = Matrix3d.Rotation(rolat, Vector3d.ZAxis, GeTools.MidPoint(ext.MaxPoint, ext.MinPoint));
                            pl.TransformBy(mt);

                            // 加入到模型空间
                            btr.AppendEntity(pl);
                            tr.AddNewlyCreatedDBObject(pl, true);
                        }
                    }
                    tr.Commit();
                }
            }
            catch (Exception ex) { ed.WriteMessageWithReturn(ex); }
        }