1. 程式人生 > >CAD 二次開發----- 塊

CAD 二次開發----- 塊

isp ace spa ddn 旋轉 return eat his ret

        /// <summary>
        /// 插入一個塊參照到CAD圖形中
        /// </summary>
        /// <param name="spaceId">模型空間Id</param>
        /// <param name="layer">要插入的圖層名</param>
        /// <param name="blockName">塊名</param>
        /// <param name="postion">插入點</param>
        /// <param name="scale">縮放比例</param>
        /// <param name="rotateAngle">旋轉角度</param>
        /// <returns></returns>
        public static ObjectId InsertBlockTableRecord(this ObjectId spaceId, string layer, string blockName, Point3d postion, Scale3d scale, double rotateAngle)
        {

            ObjectId blockRefId;//存儲要插入的塊參照的Id

            Database db = spaceId.Database;//獲取數據庫對象

            BlockTable bt = db.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable;

            if (!bt.Has(blockName))

                return ObjectId.Null;//如果沒有blockName的塊,程序返回

            //以寫的方式打開空間
            BlockTableRecord space = spaceId.GetObject(OpenMode.ForWrite) as BlockTableRecord;
            //創建塊參照的插入點
            BlockReference br = new BlockReference(postion, bt[blockName]);
            br.ScaleFactors = scale;

            br.Layer = layer;

            br.Rotation = rotateAngle;
            blockRefId = space.AppendEntity(br);

            db.TransactionManager.AddNewlyCreatedDBObject(br, true);

            space.DowngradeOpen();

            return blockRefId;//返回添加的塊參照的ID 

        }

  

        /// <summary>
        /// 插入一個塊參照到CAD圖形中
        /// </summary>
        /// <param name="spaceId">模型空間Id</param>
        /// <param name="layer">要插入的圖層名</param>
        /// <param name="blockName">塊名</param>
        /// <param name="postion">插入點</param>
        /// <param name="scale">縮放比例</param>
        /// <param name="rotateAngle">旋轉角度</param>
        /// <returns></returns>
        public static ObjectId InsertBlockTableRecord(this ObjectId spaceId, string layer, string blockName, Point3d postion, Scale3d scale, double rotateAngle)
        {

            ObjectId blockRefId;//存儲要插入的塊參照的Id

            Database db = spaceId.Database;//獲取數據庫對象

            BlockTable bt = db.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable;

            if (!bt.Has(blockName))

                return ObjectId.Null;//如果沒有blockName的塊,程序返回

            //以寫的方式打開空間
            BlockTableRecord space = spaceId.GetObject(OpenMode.ForWrite) as BlockTableRecord;
            //創建塊參照的插入點
            BlockReference br = new BlockReference(postion, bt[blockName]);
            br.ScaleFactors = scale;

            br.Layer = layer;

            br.Rotation = rotateAngle;
            blockRefId = space.AppendEntity(br);

            db.TransactionManager.AddNewlyCreatedDBObject(br, true);

            space.DowngradeOpen();

            return blockRefId;//返回添加的塊參照的ID 

        }

 
 
在CAD中插入一個塊參照


/// <summary>
        /// 創建一個塊,添加到數據庫中
        /// </summary>
        /// <param name="db"></param>
        /// <param name="blockName"></param>
        /// <param name="ents"></param>
        /// <returns></returns>
        public static ObjectId AddBlockTableRecord(this Database db, string blockName, List<Entity> ents)
        {
            BlockTable bt = (BlockTable)db.BlockTableId.GetObject(OpenMode.ForRead);

            if (!bt.Has(blockName))
            {
                BlockTableRecord btr = new BlockTableRecord();

                btr.Name = blockName;
                //將列表中的實體加入到新建的BlockTableRecord
                ents.ForEach(ent => btr.AppendEntity(ent));
               
                bt.UpgradeOpen();//切換塊為寫的狀態

                bt.Add(btr);

                db.TransactionManager.AddNewlyCreatedDBObject(btr, true);

                bt.DowngradeOpen();//切換塊為讀的狀態

            }

            return bt[blockName];
        }

  

CAD 二次開發----- 塊