Accessing DWG files not open in the AutoCAD editor using .NET

This topic was briefly introduced almost a year ago, in this post. I then looked into the code a little further in a follow-up post. But at the time this topic wasn't the main thrust of the post, it was really more of an implementation detail.

So now it's time to do the topic a little more justice. :-)

Let's start with some terminology. What we're talking about today are often referred to as "side databases" or "external databases". Basically they're DWG files that are not open in the AutoCAD editor, but ones we want to access programmatically to load geometry or settings. You may also hear the term "lazy loaded" - these DWG files are not loaded completely into memory (unless you choose to access all the data stored in them), they are brought in, as needed, making the access very efficient.

Side databases have been available through ObjectARX since it was introduced in R13, but were introduced more recently in COM (the AxDb implementation was introduced several releases back, although I can't remember exactly when... perhaps it was in AutoCAD 2000 but it might have been later) and .NET (when we introduced the managed layer in 2005, I believe). One interesting point to note, is that any code you write using ObjectARX or .NET to access side databases can be used with almost no modification on top of RealDWG to access the same data outside AutoCAD. Although any code that's intermingled which accesses AutoCAD-resident functionality will not work, of course.

The basic technique is to create a Database object, and read a DWG file into it. Please remember to use the appropriate constructor: the standard constructor without arguments creates a Database object that cannot be used in this manner (it creates one that you would typically use to create a new DWG file). When reading a DWG you will need to pass False as the first argument - what you pass as the second depends on your need.

From there you work with the Database object, accessing the header variables and the objects stored in the various dictionaries and symbol tables, just as you would inside AutoCAD.

Let's take a very simple example, where we open a particular file on disk, and iterate through the model-space, listing a little bit of data about each object:

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Runtime;


namespace ExtractObjects

{

  public class Commands

  {

    [CommandMethod("EOF")]

    static public void ExtractObjectsFromFile()

    {

      Document doc =

        Application.DocumentManager.MdiActiveDocument;

      Editor ed = doc.Editor;


      // Ask the user to select a file

      PromptResult res =

        ed.GetString(

          "/nEnter the path of a DWG or DXF file: "

        );


      if (res.Status == PromptStatus.OK)

      {

        // Create a database and try to load the file

        Database db = new Database(false, true);

        using (db)

        {

          try

          {

            db.ReadDwgFile(

              res.StringResult,

              System.IO.FileShare.Read,

              false,

              ""

            );

          }

          catch (System.Exception)

          {

            ed.WriteMessage(

              "/nUnable to read drawing file."

            );

            return;

          }


          Transaction tr =

            db.TransactionManager.StartTransaction();

          using (tr)

          {

            // Open the blocktable, get the modelspace

            BlockTable bt =

              (BlockTable)tr.GetObject(

                db.BlockTableId,

                OpenMode.ForRead

              );


            BlockTableRecord btr =

              (BlockTableRecord)tr.GetObject(

                bt[BlockTableRecord.ModelSpace],

                OpenMode.ForRead

              );



            // Iterate through it, dumping objects

            foreach (ObjectId objId in btr)

            {

              Entity ent =

                (Entity)tr.GetObject(

                  objId,

                  OpenMode.ForRead

                );


              // Let's get rid of the standard namespace

              const string prefix =

                "Autodesk.AutoCAD.DatabaseServices.";

              string typeString =

                ent.GetType().ToString();

              if (typeString.Contains(prefix))

                typeString =

                  typeString.Substring(prefix.Length);


              ed.WriteMessage(

                "/nEntity " +

                ent.ObjectId.ToString() +

                " of type " +

                typeString +

                " found on layer " +

                ent.Layer +

                " with colour " +

                ent.Color.ToString()

              );

            }

          }

        }

      }

    }

  }

}

Here's what happens when you run the code:

Command: EOF


Enter the path of a DWG or DXF file: "C:/Program Files/Autodesk/AutoCAD

2007/Sample/Sheet Sets/Architectural/S-03.dwg"


Entity (2127693096) of type Line found on layer Struc_Plan_GB_PL with colour

BYLAYER

Entity (2127693104) of type Line found on layer Struc_Plan_GB_PL with colour

BYLAYER

Entity (2127693112) of type Line found on layer Struc_Plan_GB_PL with colour

BYLAYER

Entity (2127693120) of type Line found on layer Struc_Plan_GB_PL with colour

BYLAYER

Entity (2127693128) of type Line found on layer Struc_Plan_GB_PL with colour

BYLAYER

Entity (2127693224) of type Line found on layer Struc_Plan_GB_PL with colour

BYLAYER

Entity (2127693232) of type Line found on layer Struc_Plan_GB_PL with colour

BYLAYER

Entity (2127693368) of type Line found on layer Struc_Plan_Ext with colour

BYLAYER

Entity (2127693376) of type Line found on layer Struc_Plan_Ext with colour

BYLAYER

Entity (2127693384) of type Line found on layer Struc_Plan_Ext with colour

BYLAYER

Entity (2127693392) of type Line found on layer Struc_Plan_Ext with colour

BYLAYER

Entity (2127693400) of type Line found on layer Struc_Plan_Ext with colour

BYLAYER

Entity (2127693408) of type Line found on layer Struc_Plan_Ext with colour

BYLAYER

Entity (2127693416) of type Line found on layer Struc_Plan_Ext with colour

BYLAYER

Entity (2127693424) of type Line found on layer Struc_Plan_Ext with colour

BYLAYER

Entity (2127695000) of type Line found on layer 2_Arch_Plan_Dim with colour

BYLAYER

Entity (2127695448) of type BlockReference found on layer 0 with colour BYLAYER

Entity (2127624240) of type BlockReference found on layer Building Section (2)

with colour BYLAYER

Entity (2127656048) of type BlockReference found on layer Structrual Base2 with

colour BYLAYER

Entity (2127656304) of type BlockReference found on layer grid plan with colour

BYLAYER

Entity (2127656560) of type BlockReference found on layer Stair 2 with colour

BYLAYER

I'm interested in looking at more complex scenarios where people might need to access side databases from their code. Please post any suggestions you might have as a comment (or just drop me an email).

 
内容概要:本文详细介绍了Maven的下载、安装与配置方法。Maven是基于项目对象模型(POM)的概念,用于项目管理和构建自动化的工具,能有效管理项目依赖、规范项目结构并提供标准化的构建流程。文章首先简述了Maven的功能特点及其重要性,接着列出了系统要求,包括操作系统、磁盘空间等。随后,分别针对Windows、macOS和Linux系统的用户提供了详细的下载和安装指导,涵盖了解压安装包、配置环境变量的具体操作。此外,还讲解了如何配置本地仓库和镜像源(如阿里云),以优化依赖项的下载速度。最后,给出了常见的错误解决方案,如环境变量配置错误、JDK版本不兼容等问题的处理方法。 适合人群:适用于初学者以及有一定经验的Java开发人员,特别是那些希望提升项目构建和依赖管理效率的技术人员。 使用场景及目标: ①帮助开发者掌握Maven的基本概念和功能特性; ②指导用户完成Maven在不同操作系统上的安装与配置; ③教会用户如何配置本地仓库和镜像源以加快依赖项下载; ④解决常见的安装和配置过程中遇到的问题。 阅读建议:由于Maven的安装和配置涉及多个步骤,建议读者按照文中提供的顺序逐步操作,并仔细检查每个环节的细节,尤其是环境变量的配置。同时,在遇到问题时,可参考文末提供的常见问题解决方案,确保顺利完成整个配置过程。
### Multisim 中主数据库不可用的解决方案 当遇到错误提示“Features using the Master Database will not be available”时,这通常表明软件无法正常访问其内部使用的主数据库文件。以下是可能的原因以及对应的解决方法: #### 1. 数据库路径配置不正确 如果 Multisim 的安装目录被更改或者移动过,则可能导致程序找不到默认的主数据库位置。可以尝试重新设置数据库路径来解决问题[^1]。 - 打开 **Preferences** 菜单。 - 导航到 **Database Settings** 部分。 - 确认当前指定的路径指向正确的主数据库文件夹(通常是 `C:\Users\<用户名>\AppData\Roaming\Multisim` 或者类似的默认安装路径)。 #### 2. 权限不足导致无法读取/写入数据 某些情况下,操作系统权限管理可能会阻止 Multisim 对特定文件或文件夹的操作。可以通过调整权限修复此问题。 - 右键点击 Multisim 应用快捷方式并选择以管理员身份运行。 - 如果仍然存在问题,手动检查主数据库所在文件夹的安全属性,赋予完全控制权给当前登录账户。 #### 3. 数据库损坏恢复措施 长期使用过程中可能出现意外情况造成数据库结构破坏。此时需要执行以下操作尝试修复: - 使用内置工具扫描和修正潜在的数据一致性问题。 - 备份现有项目后卸载重装最新版本的应用程序,从而替换掉任何已损毁的核心组件。 ```bash # 示例命令用于演示如何通过命令行启动应用程序作为管理员测试权限问题 runas /user:Administrator "C:\Program Files\Multisim\multisim.exe" ``` 以上步骤应该能够帮助大多数用户克服因主数据库不可用而引发的功能受限状况。然而,在极少数特殊情形下,还可能存在其他深层次的技术难题需联系官方技术支持获取进一步指导。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值