Updating a specific attribute inside a folder of AutoCAD drawings using .NET

本文介绍了一种使用C#批量处理AutoCAD DWG文件的方法,通过命令行输入目标文件夹及属性信息,程序能够搜索指定块内的特定属性并进行更新,同时将修改后的文件另存为新名称。

 

In the last post we looked at some code to search the current drawing for a particular attribute and update its value. In this post - as promised - we're going to look at how to extend this application to work on a folder of drawings, updating those that contain the attribute and saving them to a new filename.

Rather than implement a fancy, graphical user interface, I've stuck with my approach of using the command-line for input and output. If you wish to implement your own UI, please do - it's really easy using .NET. I get the occasional request to do this myself, but I prefer to keep my posts focused - there are other posts on this blog that focus specifically on UI-related issues.

I ended up changing the code somewhat - I had been printing the results of the batching process from within the UpdateAttributesInDatabase() function. As I get closer to extracting the functionality to a RealDWG application, I've started reducing the dependencies on the Editor (as this will not be available later on). The WriteMessage() calls are now in the commands themselves, instead of in the processing functions.

In addition to the previous UA (UpdateAttribute) command, I've added a new one called UAIF (UpdateAttributeInFiles). This one queries for some additional data (such as the path to the folder to process), and then uses some handy .NET Framework functionality to iterate through the drawings stored in a particular location. Rather than overwrite the originals, the updated files get saved to a new name (with "-updated" added to the filename). It is left as an exercise to put these in a separate folder or to save back to the original location (I'd rather not get blamed for overwriting valuable data :-).

Here's the C# code:

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Runtime;

using System.IO;

using System;


namespace AttributeUpdater

{

  public class Commands

  {

    [CommandMethod("UAIF")]

    public void UpdateAttributeInFiles()

    {

      Document doc =

        Application.DocumentManager.MdiActiveDocument;

      Editor ed = doc.Editor;


      // Have the user choose the block and attribute

      // names, and the new attribute value


      PromptResult pr =

        ed.GetString(

          "/nEnter folder containing DWGs to process: "

        );

      if (pr.Status != PromptStatus.OK)

        return;

      string pathName = pr.StringResult;


      pr =

        ed.GetString(

          "/nEnter name of block to search for: "

        );

      if (pr.Status != PromptStatus.OK)

        return;

      string blockName = pr.StringResult.ToUpper();


      pr =

        ed.GetString(

          "/nEnter tag of attribute to update: "

        );

      if (pr.Status != PromptStatus.OK)

        return;

      string attbName = pr.StringResult.ToUpper();


      pr =

        ed.GetString(

          "/nEnter new value for attribute: "

        );

      if (pr.Status != PromptStatus.OK)

        return;

      string attbValue = pr.StringResult;


      string[] fileNames =

        Directory.GetFiles(pathName,"*.dwg");


      // We'll use some counters to keep track

      // of how the processing is going


      int processed = 0, saved = 0, problem = 0;


      foreach (string fileName in fileNames)

      {

        if (fileName.EndsWith(

              ".dwg",

              StringComparison.CurrentCultureIgnoreCase

            )

        )

        {

          string outputName =

            fileName.Substring(

              0,

              fileName.Length - 4) +

            "_updated.dwg";

          Database db = new Database(false, false);

          using (db)

          {

            try

            {

              ed.WriteMessage(

                "/n/nProcessing file: " + fileName

              );


              db.ReadDwgFile(

                fileName,

                FileShare.ReadWrite,

                false,

                ""

              );

              int attributesChanged =

                UpdateAttributesInDatabase(

                  db,

                  blockName,

                  attbName,

                  attbValue

                );


              // Display the results


              ed.WriteMessage(

                "/nUpdated {0} instance{1} of " +

                "attribute {2}.",

                attributesChanged,

                attributesChanged == 1 ? "" : "s",

                attbName

              );


              // Only save if we changed something


              if (attributesChanged > 0)

              {

                ed.WriteMessage(

                  "/nSaving to file: {0}", outputName

                );


                db.SaveAs(

                  outputName,

                  DwgVersion.Current

                );

                saved++;

              }

              processed++;

            }

            catch (System.Exception ex)

            {

              ed.WriteMessage(

                "/nProblem processing file: {0} - /"{1}/"",

                fileName,

                ex.Message

              );

              problem++;

            }

          }

        }

      }

      ed.WriteMessage(

        "/n/nSuccessfully processed {0} files, of which {1} had " +

        "attributes to update and an additional {2} had errors " +

        "during reading/processing.",

        processed,

        saved,

        problem

      );

    }


    [CommandMethod("UA")]

    public void UpdateAttribute()

    {

      Document doc =

        Application.DocumentManager.MdiActiveDocument;

      Database db = doc.Database;

      Editor ed = doc.Editor;


      // Have the user choose the block and attribute

      // names, and the new attribute value


      PromptResult pr =

        ed.GetString(

          "/nEnter name of block to search for: "

        );

      if (pr.Status != PromptStatus.OK)

        return;

      string blockName = pr.StringResult.ToUpper();


      pr =

        ed.GetString(

          "/nEnter tag of attribute to update: "

        );

      if (pr.Status != PromptStatus.OK)

        return;

      string attbName = pr.StringResult.ToUpper();


      pr =

        ed.GetString(

          "/nEnter new value for attribute: "

        );

      if (pr.Status != PromptStatus.OK)

        return;

      string attbValue = pr.StringResult;


      ed.WriteMessage(

        "/nProcessing file: " + db.Filename

      );


      int count =

        UpdateAttributesInDatabase(

          db,

          blockName,

          attbName,

          attbValue

        );


      ed.Regen();


      // Display the results


      ed.WriteMessage(

        "/nUpdated {0} instance{1} of " +

        "attribute {2}.",

        count,

        count == 1 ? "" : "s",

        attbName

      );

    }


    private int UpdateAttributesInDatabase(

      Database db,

      string blockName,

      string attbName,

      string attbValue

    )

    {

      Document doc =

        Application.DocumentManager.MdiActiveDocument;

      Editor ed = doc.Editor;


      // Get the IDs of the spaces we want to process

      // and simply call a function to process each


      ObjectId msId, psId;

      Transaction tr =

        db.TransactionManager.StartTransaction();

      using (tr)

      {

        BlockTable bt =

          (BlockTable)tr.GetObject(

            db.BlockTableId,

            OpenMode.ForRead

          );

        msId =

          bt[BlockTableRecord.ModelSpace];

        psId =

          bt[BlockTableRecord.PaperSpace];


        // Not needed, but quicker than aborting

        tr.Commit();

      }

      int msCount =

        UpdateAttributesInBlock(

          msId,

          blockName,

          attbName,

          attbValue

        );

      int psCount =

        UpdateAttributesInBlock(

          psId,

          blockName,

          attbName,

          attbValue

        );

      return msCount + psCount;

    }


    private int UpdateAttributesInBlock(

      ObjectId btrId,

      string blockName,

      string attbName,

      string attbValue

    )

    {

      // Will return the number of attributes modified


      int changedCount = 0;

      Document doc =

        Application.DocumentManager.MdiActiveDocument;

      Database db = doc.Database;

      Editor ed = doc.Editor;


      Transaction tr =

        doc.TransactionManager.StartTransaction();

      using (tr)

      {

        BlockTableRecord btr =

          (BlockTableRecord)tr.GetObject(

            btrId,

            OpenMode.ForRead

          );


        // Test each entity in the container...


        foreach (ObjectId entId in btr)

        {

          Entity ent =

            tr.GetObject(entId, OpenMode.ForRead)

            as Entity;


          if (ent != null)

          {

            BlockReference br = ent as BlockReference;

            if (br != null)

            {

              BlockTableRecord bd =

                (BlockTableRecord)tr.GetObject(

                  br.BlockTableRecord,

                  OpenMode.ForRead

              );


              // ... to see whether it's a block with

              // the name we're after


              if (bd.Name.ToUpper() == blockName)

              {


                // Check each of the attributes...


                foreach (

                  ObjectId arId in br.AttributeCollection

                )

                {

                  DBObject obj =

                    tr.GetObject(

                      arId,

                      OpenMode.ForRead

                    );

                  AttributeReference ar =

                    obj as AttributeReference;

                  if (ar != null)

                  {

                    // ... to see whether it has

                    // the tag we're after


                    if (ar.Tag.ToUpper() == attbName)

                    {

                      // If so, update the value

                      // and increment the counter


                      ar.UpgradeOpen();

                      ar.TextString = attbValue;

                      ar.DowngradeOpen();

                      changedCount++;

                    }

                  }

                }

              }


              // Recurse for nested blocks

              changedCount +=

                UpdateAttributesInBlock(

                  br.BlockTableRecord,

                  blockName,

                  attbName,

                  attbValue

                );

            }

          }

        }

        tr.Commit();

      }

      return changedCount;

    }

  }

}

Here's what happens when I run it on my temp folder (which is full of crud, and that's putting it politely):

Command: UAIF

Enter folder containing DWGs to process: c:/temp

Enter name of block to search for: TEST

Enter tag of attribute to update: ONE

Enter new value for attribute: 1234



Processing file: c:/temp/-old_recover.dwg

Problem processing file: c:/temp/-old_recover.dwg - "eBadDwgHeader"



Processing file: c:/temp/4076612-2.DWG

Updated 0 instances of attribute ONE.



Processing file: c:/temp/attributes.dwg

Updated 5 instances of attribute ONE.

Saving to file: c:/temp/attributes_updated.dwg



[Deleted a lot of uninteresting reports]



Successfully processed 42 files, of which 1 had attributes to update and an

additional 3 had errors during reading/processing.

You'll see that some DWGs have failed to load: hopefully because they're in need of recovery rather than the workings of this code. The above technique should handle this gracefully, allowing you to go back and fix the problematic ones.

Update:

Norman Yuan pointed out a mistake in this code - I've been using the TransactionManager from the current document, rather than from the db. This isn't dramatically bad, but would have become more of a problem as we move to RealDWG. What I should really have done sooner is remove the doc and ed variables from the UpdateAttributesIn...() functions, so that I would not have been in a position to use them by mistake. I now pass in the Database as a parameter, which allows us to use it to get the appropriate TransactionManager. One other benefit this code enables is the choice of constructing the database without an associated document:

Database db = new Database(false, true);

This makes the code work more efficiently (as well as being cleaner).

Here are the updated functions - not the entire code:

    private int UpdateAttributesInDatabase(

      Database db,

      string blockName,

      string attbName,

      string attbValue

    )

    {

      // Get the IDs of the spaces we want to process

      // and simply call a function to process each


      ObjectId msId, psId;

      Transaction tr =

        db.TransactionManager.StartTransaction();

      using (tr)

      {

        BlockTable bt =

          (BlockTable)tr.GetObject(

            db.BlockTableId,

            OpenMode.ForRead

          );

        msId =

          bt[BlockTableRecord.ModelSpace];

        psId =

          bt[BlockTableRecord.PaperSpace];


        // Not needed, but quicker than aborting

        tr.Commit();

      }

      int msCount =

        UpdateAttributesInBlock(

          db,

          msId,

          blockName,

          attbName,

          attbValue

        );

      int psCount =

        UpdateAttributesInBlock(

          db,

          psId,

          blockName,

          attbName,

          attbValue

        );

      return msCount + psCount;

    }


    private int UpdateAttributesInBlock(

      Database db,

      ObjectId btrId,

      string blockName,

      string attbName,

      string attbValue

    )

    {

      // Will return the number of attributes modified


      int changedCount = 0;


      Transaction tr =

        db.TransactionManager.StartTransaction();

      using (tr)

      {

        BlockTableRecord btr =

          (BlockTableRecord)tr.GetObject(

            btrId,

            OpenMode.ForRead

          );


        // Test each entity in the container...


        foreach (ObjectId entId in btr)

        {

          Entity ent =

            tr.GetObject(entId, OpenMode.ForRead)

            as Entity;


          if (ent != null)

          {

            BlockReference br = ent as BlockReference;

            if (br != null)

            {

              BlockTableRecord bd =

                (BlockTableRecord)tr.GetObject(

                  br.BlockTableRecord,

                  OpenMode.ForRead

              );


              // ... to see whether it's a block with

              // the name we're after


              if (bd.Name.ToUpper() == blockName)

              {


                // Check each of the attributes...


                foreach (

                  ObjectId arId in br.AttributeCollection

                )

                {

                  DBObject obj =

                    tr.GetObject(

                      arId,

                      OpenMode.ForRead

                    );

                  AttributeReference ar =

                    obj as AttributeReference;

                  if (ar != null)

                  {

                    // ... to see whether it has

                    // the tag we're after


                    if (ar.Tag.ToUpper() == attbName)

                    {

                      // If so, update the value

                      // and increment the counter


                      ar.UpgradeOpen();

                      ar.TextString = attbValue;

                      ar.DowngradeOpen();

                      changedCount++;

                    }

                  }

                }

              }


              // Recurse for nested blocks

              changedCount +=

                UpdateAttributesInBlock(

                  db,

                  br.BlockTableRecord,

                  blockName,

                  attbName,

                  attbValue

                );

            }

          }

        }

        tr.Commit();

      }

      return changedCount;

    }

  }

基于粒子群优化算法的p-Hub选址优化(Matlab代码实现)内容概要:本文介绍了基于粒子群优化算法(PSO)的p-Hub选址优化问题的研究与实现,重点利用Matlab进行算法编程和仿真。p-Hub选址是物流与交通网络中的关键问题,旨在通过确定最优的枢纽节点位置和非枢纽节点的分配方式,最小化网络总成本。文章详细阐述了粒子群算法的基本原理及其在解决组合优化问题中的适应性改进,结合p-Hub中转网络的特点构建数学模型,并通过Matlab代码实现算法流程,包括初始化、适应度计算、粒子更新与收敛判断等环节。同时可能涉及对算法参数设置、收敛性能及不同规模案例的仿真结果分析,以验证方法的有效性和鲁棒性。; 适合人群:具备一定Matlab编程基础和优化算法理论知识的高校研究生、科研人员及从事物流网络规划、交通系统设计等相关领域的工程技术人员。; 使用场景及目标:①解决物流、航空、通信等网络中的枢纽选址与路径优化问题;②学习并掌握粒子群算法在复杂组合优化问题中的建模与实现方法;③为相关科研项目或实际工程应用提供算法支持与代码参考。; 阅读建议:建议读者结合Matlab代码逐段理解算法实现逻辑,重点关注目标函数建模、粒子编码方式及约束处理策略,并尝试调整参数或拓展模型以加深对算法性能的理解。
### RStudio Launch Error 'Choose a Specific' Solution The error message "choose a specific" in RStudio typically occurs when there is ambiguity or misconfiguration in the environment setup, such as multiple versions of R installed on the system or incorrect paths configured for RStudio to locate the appropriate R version. Here are some detailed solutions to address this issue: #### Verify R Installation Ensure that only one version of R is installed or that the desired version is correctly specified in the system's PATH variable. This can prevent RStudio from being confused about which R installation to use[^1]. Use the following command in your terminal to check the R version: ```bash R --version ``` If multiple versions are present, remove unnecessary installations or adjust the PATH so that the preferred R version is prioritized. #### Configure RStudio to Use the Correct R Version Sometimes, even if the correct R version is installed, RStudio might not be pointing to it. To resolve this, edit the RStudio desktop configuration file (on Linux systems, this is often located at `~/.config/RStudio/desktop.ini`), ensuring the path to the R executable is explicitly defined[^1]. For example: ```ini [General] rsession-which-r=/usr/bin/R ``` On Windows systems, ensure the registry keys point to the correct R installation directory. #### Update RStudio Outdated versions of RStudio may also cause compatibility issues with newer R versions. Updating RStudio to the latest release can help mitigate these problems[^1]. Download and install the newest version from the official RStudio website. #### Debugging Environment Variables Environment variables play a crucial role in how RStudio interacts with R. If certain variables like `R_HOME` are not set properly, RStudio might fail to launch correctly. Set the `R_HOME` variable to the root directory of your R installation. For Unix-based systems: ```bash export R_HOME=/path/to/R ``` For Windows: Add `R_HOME` to the system environment variables and set its value to the location of your R installation. #### Check Logs for Detailed Errors RStudio generates logs that can provide more insight into what might be causing the issue. Locate the log files (commonly found in `~/.rstudio/monitored/log/` on Unix-like systems) and review them for any additional error messages or warnings that could indicate the root cause of the problem. ```python # Example Python code to read log files with open("~/.rstudio/monitored/log/session.log", "r") as file: print(file.read()) ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值