20033535 发表于 2010-9-27 17:12:00

[求助]如何批量处理一个目录下的所有DWG文件

本帖最后由 作者 于 2010-9-28 19:16:50 编辑

如题,如下代码

public void BatchAddTitle()
{
string patch = @"C:\Documents and Settings\ABC\桌面\test\";
            try
            {
                string[] dirs = Directory.GetFiles(patch, "*.dwg");
                //创建文件夹
                string newFolder = "转换后";
                if (!Directory.Exists(patch + newFolder))
                {
                  Directory.CreateDirectory(patch + newFolder);
                }
                foreach (string item in dirs)
                {
                  ed.WriteMessage(item + "\n");
                  try
                  {
                        Document doc = Application.DocumentManager.Open(item, false);
                        //执行一系列操作,比如一个addTitle函数,接受Document参数。
                  DocumentLock doclock = doc.LockDocument();
                  addTitle(doc);
                  doclock.Dispose();
                        //改文件名
                        string newFileName = Path.GetDirectoryName(item) + "\\" + newFolder + "\\" + Path.GetFileName(item);
                        //存储的文件版本
                        //doc.Database.SaveAs(newFileName, DwgVersion.Current);
                        ed.WriteMessage("newFileName:<" + newFileName + ">");
                        //doc.CloseAndSave(newFileName);
                  }
                  catch (System.Exception ex)
                  {
                        ed.WriteMessage("内:" + ex.Message + ":" + ex.StackTrace + "\n");
                  }
                }
            }
            catch (System.Exception ex)
            {
                ed.WriteMessage("外:" + ex.Message + "\n");
            }
}

打开test文件夹下的DWG文件后,addTitle(doc)也没有报错,但是没有预期后的执行效果;
如下处理单个文件:   

      public void bxcl()
      {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            addTitle(doc);
      }
可以正常执行。

加上参数CommandFlags.Session以后,程序可以正常运行,感谢lzh741206的指点
但是我觉得即使不加CommandMethod 的参数CommandFlags.Session程序也应该能够正常运行啊。
我感觉即使在一个session里面,打开操作其他Document并保存也是可行的,但是根据我代码的实际运行情况来看不可行。
我的代码哪里有问题?
对于cad的session正确理解是什么?

20033535 发表于 2010-9-27 17:25:00

谁能给个打开某一目录下所有的dwg文件并处理的例子啊?

20033535 发表于 2010-9-27 17:52:00

是不是要用到长事务?

雪山飞狐_lzh 发表于 2010-9-27 17:54:00

<font face="Verdana"></font>

20033535 发表于 2010-9-27 20:11:00

本帖最后由 作者 于 2010-9-28 19:05:25 编辑 <br /><br /> lzh741206发表于2010-9-27 17:54:00static/image/common/back.gif

<p></p>一言中的啊<br/>问题解决,看来我的要去了解示例方法和静态方法的知识了<br/>我还是想知道我那代码不能运行的详细原因。<br/>

雪山飞狐_lzh 发表于 2010-9-27 20:45:00

<table class="Table4" cellspacing="0">
<tbody>
<tr>
<td class="Element262" valign="top" width="50%">
<div class="Element263">ActionMacro = 0x8000000&nbsp;</div></td>
<td class="Element266" valign="top" width="50%">
<div class="Element267">This bit commands that are defined as a result of recording a series of actions with the ActionRecorder.&nbsp;</div></td></tr>
<tr>
<td class="Element262" valign="top" width="50%">
<div class="Element263">Defun = 0x800&nbsp;</div></td>
<td class="Element266" valign="top" width="50%">
<div class="Element267">This flag can only be set by the Visual LISP engine. Command can be invoked as a LISP function and can therefore use acedGetArgs() to receive arguments from LISP and can use the acedRetXxx() functions to return values to LISP.&nbsp;</div></td></tr>
<tr>
<td class="Element262" valign="top" width="50%">
<div class="Element263">DocExclusiveLock = 0x100000&nbsp;</div></td>
<td class="Element266" valign="top" width="50%">
<div class="Element267">Document will be exclusively locked when command is invoked. If this bit is not set, then the document will be share locked when the command is invoked.&nbsp;</div></td></tr>
<tr>
<td class="Element262" valign="top" width="50%">
<div class="Element263">DocReadLock = 0x80000&nbsp;</div></td>
<td class="Element266" valign="top" width="50%">
<div class="Element267">Document will be read locked when command is invoked. If this bit is not set, then the document will be write locked when the command is invoked.&nbsp;</div></td></tr>
<tr>
<td class="Element262" valign="top" width="50%">
<div class="Element263">Interruptible = 0x400000&nbsp;</div></td>
<td class="Element266" valign="top" width="50%">
<div class="Element267">The command may be interrupted when prompting for user input while it is calling any of the acedGetxxx methods.&nbsp;</div></td></tr>
<tr>
<td class="Element262" valign="top" width="50%">
<div class="Element263">Modal = 0&nbsp;</div></td>
<td class="Element266" valign="top" width="50%">
<div class="Element267">Command cannot be invoked while another command is active.&nbsp;</div></td></tr>
<tr>
<td class="Element262" valign="top" width="50%">
<div class="Element263">NoActionRecording = 0x4000000&nbsp;</div></td>
<td class="Element266" valign="top" width="50%">
<div class="Element267">This flag causes the ActionRecorder to ignore the associated command&nbsp;</div></td></tr>
<tr>
<td class="Element262" valign="top" width="50%">
<div class="Element263">NoBlockEditor = 0x2000000&nbsp;</div></td>
<td class="Element266" valign="top" width="50%">
<div class="Element267">The command is blocked during a bedit session.&nbsp;</div></td></tr>
<tr>
<td class="Element262" valign="top" width="50%">
<div class="Element263">NoHistory = 0x800000&nbsp;</div></td>
<td class="Element266" valign="top" width="50%">
<div class="Element267">The command will not be added to AutoCAD’s repeat-last-command functionality.&nbsp;</div></td></tr>
<tr>
<td class="Element262" valign="top" width="50%">
<div class="Element263">NoPaperSpace = 0x40&nbsp;</div></td>
<td class="Element266" valign="top" width="50%">
<div class="Element267">Command cannot be invoked when in paperspace.&nbsp;</div></td></tr>
<tr>
<td class="Element262" valign="top" width="50%">
<div class="Element263">NoTileMode = 0x20&nbsp;</div></td>
<td class="Element266" valign="top" width="50%">
<div class="Element267">Command cannot be invoked when TILEMODE == 1.&nbsp;</div></td></tr>
<tr>
<td class="Element262" valign="top" width="50%">
<div class="Element263">NoUndoMarker = 0x1000000&nbsp;</div></td>
<td class="Element266" valign="top" width="50%">
<div class="Element267">The command and its undo markers will not be added to the undo file. This is intended for commands that don't modify the database, and therefore should not show up in the undo file.&nbsp;</div></td></tr>
<tr>
<td class="Element262" valign="top" width="50%">
<div class="Element263">Redraw = 4&nbsp;</div></td>
<td class="Element266" valign="top" width="50%">
<div class="Element267">When the pickfirst set or grip set are retrieved, they are not cleared within AutoCAD.<br/>Command can retrieve the pickfirst set and the grip set by using the ads_ssgetfirst function.<br/>Command can retrieve the pickfirst set via ads_ssget("I.").<br/>Command can set both the pickfirst and grip sets using the ads_sssetfirst function. Objects in these sets are redrawn with the proper grip handles and highlighting upon completion of the command.&nbsp;</div></td></tr>
<tr>
<td class="Element262" valign="top" width="50%">
<div class="Element263">Session = 0x200000&nbsp;</div></td>
<td class="Element266" valign="top" width="50%">
<div class="Element267">The command will be run in the application execution context rather than the current document context, with the different capabilities and limitations that entails. It should be used sparingly.&nbsp;</div></td></tr>
<tr>
<td class="Element262" valign="top" width="50%">
<div class="Element263">Transparent = 1&nbsp;</div></td>
<td class="Element266" valign="top" width="50%">
<div class="Element267">Command can be invoked while another command is active.<br/>ads_sssetfirst() cannot be used when this flag is set&nbsp;</div></td></tr>
<tr>
<td class="Element262" valign="top" width="50%">
<div class="Element263">Undefined = 0x200&nbsp;</div></td>
<td class="Element266" valign="top" width="50%">
<div class="Element267">Command can only be invoked via the cmdGroupName.cmdGlobalName syntax.&nbsp;</div></td></tr>
<tr>
<td class="Element262" valign="top" width="50%">
<div class="Element263">UsePickSet = 2&nbsp;</div></td>
<td class="Element266" valign="top" width="50%">
<div class="Element267">When the pickfirst set is retrieved, it is cleared within AutoCAD.<br/>Command is able to retrieve the pickfirst set via the functions ads_ssgetfirst() or ads_ssget("I.").<br/>Command can set the pickfirst set via ads_sssetfirst(), but it only stays set until the command ends.<br/>Command cannot retrieve nor set grips.&nbsp;</div></td></tr></tbody></table>

20033535 发表于 2010-9-28 19:24:00

对每一个Document执行下面的操作<br/>DocumentLock doclock = doc.LockDocument();<br/>addTitle(doc);<br/>doclock.Dispose();<br/>没有预期的效果。(执行过程没有报错误,但是执行不能反映到该doc中去)<br/>但是对于当前打开的文档<br/>Document doc = Application.DocumentManager.MdiActiveDocument;<br/>addTitle(doc);<br/>又能够正常运行。<br/><br/>
页: [1]
查看完整版本: [求助]如何批量处理一个目录下的所有DWG文件