乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 74|回复: 1

插入的文件是块,怎么调整为文件

[复制链接]

1

主题

1

帖子

1

银币

初来乍到

Rank: 1

铜币
5
发表于 2021-12-1 09:54:00 | 显示全部楼层 |阅读模式
以下代码是插入一个外部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]
回复

使用道具 举报

75

主题

306

帖子

10

银币

中流砥柱

Rank: 25

铜币
606
发表于 2021-12-11 15:59:00 | 显示全部楼层
主要步骤
  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)
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-12 22:17 , Processed in 0.386056 second(s), 56 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表