明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 863|回复: 1

[图元] 模拟CAD中,框选实体对象制作块,并插入到模型空间

[复制链接]
发表于 2019-12-14 18:23 | 显示全部楼层 |阅读模式
如题所示,想模拟cad中的block命令,类似 <燕秀工具箱> 里面的 创建 匿名块在cad中已经画好的图元,通过框选实体对象,制成块表然后插入块参照(普通块)

不知道在cad内部是怎么样的一个操作流程,在我选择的实体中,插入到数据库中时,提示已经存在
网上到处也没有找到类似或者是没有找到搜的方式
求大神指点一下,C# 语言 谢谢


发表于 2019-12-14 22:19 | 显示全部楼层
本帖最后由 caiqs 于 2019-12-16 18:39 编辑

using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.ApplicationServices;

[assembly: CommandClass(typeof(MakeBlock.Commands))]

namespace MakeBlock
{
    /// <summary>
    /// Summary description for Commands.
    /// </summary>
    public class Commands
    {
        public Commands()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        // Define Command "AsdkCmd1"
        //师兄 QQ361865648,一直都在用VC,好长时间没写C#了
        [CommandMethod("AsdkCmd1")]
        static public void test() // This method can have any name
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            PromptSelectionResult ssres = ed.GetSelection();
            if (ssres.Status == PromptStatus.OK)
            {
                Point3d basept = new Point3d(0, 0, 0);
                PromptPointResult ptres = ed.GetPoint("\n指定基点:");
                if (ptres.Status == PromptStatus.OK)
                    basept = ptres.Value;
                if (ptres.Status == PromptStatus.Cancel) return;
                Transaction tr = db.TransactionManager.StartTransaction();
                ObjectIdCollection ids = new ObjectIdCollection(ssres.Value.GetObjectIds());
                BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;
                BlockTableRecord mspbtr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
                BlockTableRecord btr = new BlockTableRecord();
                btr.Name = "*U";
                btr.Origin = basept;

                ObjectId id = bt.Add(btr);
                tr.AddNewlyCreatedDBObject(btr, true);
                IdMapping idm = new IdMapping();
                db.DeepCloneObjects(ids, id, idm, false);
                BlockReference bref = new BlockReference(basept, id);
                mspbtr.AppendEntity(bref);
                tr.AddNewlyCreatedDBObject(bref, true);
                //此处删除原实体
                foreach (ObjectId Oldid in ids)
                {
                    DBObject old = tr.GetObject(Oldid, OpenMode.ForWrite);
                    old.Erase();
                }
                tr.Commit();
            }

            // Put your command code here
        }

    }
}

本帖子中包含更多资源

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

x
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-5-4 11:18 , Processed in 0.280590 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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