CAD 二次开发----- 块

Posted civil

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CAD 二次开发----- 块相关的知识,希望对你有一定的参考价值。

        /// <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 二次开发----- 块的主要内容,如果未能解决你的问题,请参考以下文章

怎样用VB对AutoCAD 进行二次开发?急!急!急!

请问 哪位大哥熟悉CAD二次开发ObjectARX CAD请进

CAD 中二次开发

求教大神 我现在想学ARX编译CAD图形,恩。也就是CAD二次开发(一定是用ObjectARX开发环境)

求教CAD用VBA二次开发怎样得到一个图块自定义属性的问题。

编程基于.NET的CAD二次开发(入门篇)