fish27jo 发表于 2009-6-15 19:39:00

CAD打开文件问题

<p>看了论坛中的cad二次开发,受益匪浅,但是只能在dwg文件被打开后,手动执行C#程序获取被打开DWG文件的相关信息,</p><p>现在我想想在cad&nbsp; filedlg打开文件的时候就获取到文件的信息,请问如何获取,最好能做到实时,如打开多个文件,依次都能获取到信息,谢谢</p>

雪山飞狐_lzh 发表于 2009-6-15 19:46:00

<p>看下这里11楼的代码</p><p>使用文档事件</p><p><a href="http://www.mjtd.com/bbs/dispbbs.asp?boardid=33&amp;replyid=10314&amp;id=75996&amp;page=2&amp;skin=0&amp;landlord=0&amp;Star=2">http://www.mjtd.com/bbs/dispbbs.asp?boardid=33&amp;replyid=10314&amp;id=75996&amp;page=2&amp;skin=0&amp;landlord=0&amp;Star=2</a></p>

fish27jo 发表于 2009-6-15 19:57:00

<p>你是指这个Application.DocumentManager.DocumentCreated += new DocumentCollectionEventHandler(documentCreated); 么</p><p>能做到同步等待么</p>

fish27jo 发表于 2009-6-15 22:06:00

<div style="min-height: 200px; font-size: 12pt; line-height: normal; text-indent: 0px; margin-top: 10px; word-wrap: break-word;"><p>你是指这个Application.DocumentManager.DocumentCreated += new DocumentCollectionEventHandler(documentCreated); 么</p><p>能做到同步等待么</p></div>

雪山飞狐_lzh 发表于 2009-6-15 22:25:00

<p>不要重复发帖</p><p>DocumentCreated 顾名思义 文档创建后</p>

fish27jo 发表于 2009-6-21 13:40:00

<p>斑竹你好,我参考了你的代码,但是在实际应用当中还是有很多的疑问</p><p>比如<a href="mk:@MSITStore:C:\Documents%20and%20Settings\Administrator\桌面\arxref(.net).chm::/1/Autodesk.AutoCAD.Windows.html">Autodesk.AutoCAD.Windows</a>.OpenFileDialog这个类,好像对原有的cad原有的filedlg没有什么影响</p><p>请再指教指教,怎么能做到捕获类似于c#中 OpenFileDialog.ShowDialog() == DialogResult.OK 这个消息,</p><p>方便的话请给点示例代码</p>

雪山飞狐_lzh 发表于 2009-6-21 14:56:00

<p>你具体想实现什么功能?</p><p>详细说下吧</p>

fish27jo 发表于 2009-6-21 15:07:00

<p>想在cad打开文件的时候获取到被打开文件的信息,</p><p>所以就想在fileopendlg.ShowDialog() == DialogResult.OK的时候获取到被打开dwg文件信息,可以做到么?</p>

雪山飞狐_lzh 发表于 2009-6-21 16:13:00

直接处理DocumentCreated 事件就好了
    class TlsApplication : IExtensionApplication
    {
      void IExtensionApplication.Initialize()
      {
            TTest.Start();
      }
      void IExtensionApplication.Terminate()
      {
      }
    }
    class TTest
    {
      public static void Start()
      {
            Application.DocumentManager.DocumentCreated += new DocumentCollectionEventHandler(documentCreated);
      }
      static void documentCreated(object sender, DocumentCollectionEventArgs e)
      {
            Document doc = e.Document;
            Database db = doc.Database;
            string s = "";
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt, OpenMode.ForRead);
                foreach (ObjectId id in btr)
                {
                  Entity ent = (Entity)tr.GetObject(id, OpenMode.ForRead);
                  s += "\n" + ent.GetType().Name;
                }
            }
            doc.Editor.WriteMessage(s);
      }
    }Polyline
Polyline
Polyline
Polyline
Line
Line
Line
Line
Line
Line
Line
Spline
Arc
Arc
Circle
Circle
Circle
Arc
Polyline3d
Polyline3d
Ellipse
Ellipse
Arc
Circle
Ellipse
Ellipse
Line

wz0406 发表于 2017-8-14 08:53:23

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Windows
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Namespace ManagedApplication
    Class TlsApplication
      Implements IExtensionApplication

      Public Sub Initialize() Implements IExtensionApplication.Initialize

            TTest.Start()
      End Sub

      Public Sub Terminate() Implements IExtensionApplication.Terminate

      End Sub

    End Class
    Class TTest
      Public Shared Sub Start()
            AddHandler Application.DocumentManager.DocumentCreated, New DocumentCollectionEventHandler(AddressOf documentCreated)
      End Sub
      Shared Sub documentCreated(ByVal sender As Object, ByVal e As DocumentCollectionEventArgs)
            Dim doc As Document = e.Document
            Dim db As Database = doc.Database
            Dim s As String = ""
            Dim tr As Transaction = db.TransactionManager.StartTransaction()
            doc.Editor.WriteMessage("111")
            Using tr
                Dim bt As BlockTable = CType(tr.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
                Dim btr As BlockTableRecord = CType(tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForRead), BlockTableRecord)
                Dim id As ObjectId
                For Each id In btr
                  Dim ent As Entity = CType(tr.GetObject(id, OpenMode.ForRead), Entity)
                  s += "\n" + ent.GetType().Name
                  doc.Editor.WriteMessage(s)
                Next
            End Using
      End Sub
    End Class
End Namespace
页: [1] 2
查看完整版本: CAD打开文件问题