明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 464|回复: 1

[基础] 插入的文件是块,怎么调整为文件

[复制链接]
发表于 2021-12-1 09:54 | 显示全部楼层 |阅读模式
本帖最后由 k1239752201 于 2021-12-1 09:59 编辑

以下代码是插入一个外部cad文件,但是插入进去的是一个块,怎么调整为插入文件

[code=.net]public void insertBlockFunc(string blockName, string layerName, bool isShow, bool isCover)
{
    Document doc = Application.DocumentManager.MdiActiveDocument;
    Database db = doc.Database;

    // 指定块的插入位置
    PromptPointResult pPtRes;
    PromptPointOptions pPtOpts = new PromptPointOptions("");
    //提示起点
    pPtOpts.Message = "\n请选择插入点: ";
    pPtRes = doc.Editor.GetPoint(pPtOpts);
    Point3d point = pPtRes.Value;


    // 新建块文件数据库对象 读取块
    using (Database blockDb = new Database(false, true))
    {
        // 读取外部块 dwg
        blockDb.ReadDwgFile(blockName, System.IO.FileShare.Read, true, null);
        // 关闭文件
        blockDb.CloseInput(true);

        // 先锁定文档 多文档的情况需要先锁定
        using (DocumentLock lockDoc = doc.LockDocument())
        {
            // 新建图层id
            ObjectId layId = ObjectId.Null;
            // 开启事务处理
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
               
                // 指定插入块后的新名称
                string newName = System.IO.Path.GetFileNameWithoutExtension(blockName);                     

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

                if ((bt.Has(newName)) && isCover)
                {
                    // 插入块 insert it as a new block
                    ObjectId idBtr = doc.Database.Insert(newName, blockDb, false);

                    if (isShow)
                    {
                        BlockTableRecord curBtr = trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;

                        LayerTable lt = trans.GetObject(db.LayerTableId, OpenMode.ForWrite) as LayerTable;

                        if (!lt.Has("layerName"))
                        {
                            LayerTableRecord ltr = new LayerTableRecord();
                            ltr.Name = "layerName";
                            layId = lt.Add(ltr);
                            trans.AddNewlyCreatedDBObject(ltr, true);
                        }
                        curBtr.LayoutId = layId;

                        using (BlockReference blockRef = new BlockReference(point, idBtr))
                        {
                            curBtr.AppendEntity(blockRef);
                            trans.AddNewlyCreatedDBObject(blockRef, true);
                        }
                    }
                }

                else if (!(bt.Has(newName)))
                {
                    // 插入块 insert it as a new block
                    ObjectId idBtr = doc.Database.Insert(newName, blockDb, false);

                    if (isShow)
                    {
                        BlockTableRecord curBtr = trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;


                        LayerTable lt = trans.GetObject(db.LayerTableId, OpenMode.ForWrite) as LayerTable;

                        if (!lt.Has("layerName"))
                        {
                            LayerTableRecord ltr = new LayerTableRecord();
                            ltr.Name = "layerName";
                            layId = lt.Add(ltr);
                            trans.AddNewlyCreatedDBObject(ltr, true);
                        }
                        curBtr.LayoutId = layId;

                        using (BlockReference blockRef = new BlockReference(point, idBtr))
                        {
                            curBtr.AppendEntity(blockRef);
                            trans.AddNewlyCreatedDBObject(blockRef, true);
                        }
                    }
                }
                trans.Commit();
            }
        }
    }
}[/code]
发表于 2021-12-11 15:59 | 显示全部楼层
主要步骤

  1. Dim ID As ObjectId = Doc.Database.AttachXref("x:\xxxxx.dwg", "新的名称")
  2. Dim Ref As New BlockReference(Point3d.Origin, ID)
  3. ModelSpace.AppendEntity(Ref)
  4. Trans.AddNewlyCreatedDBObject(Ref, True)


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

本版积分规则

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

GMT+8, 2024-5-7 06:13 , Processed in 0.289369 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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