明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2963|回复: 10

CAD打开文件问题

[复制链接]
发表于 2009-6-15 19:39 | 显示全部楼层 |阅读模式

看了论坛中的cad二次开发,受益匪浅,但是只能在dwg文件被打开后,手动执行C#程序获取被打开DWG文件的相关信息,

现在我想想在cad  filedlg打开文件的时候就获取到文件的信息,请问如何获取,最好能做到实时,如打开多个文件,依次都能获取到信息,谢谢

发表于 2009-6-15 19:46 | 显示全部楼层
 楼主| 发表于 2009-6-15 19:57 | 显示全部楼层

你是指这个Application.DocumentManager.DocumentCreated += new DocumentCollectionEventHandler(documentCreated); 么

能做到同步等待么

 楼主| 发表于 2009-6-15 22:06 | 显示全部楼层

你是指这个Application.DocumentManager.DocumentCreated += new DocumentCollectionEventHandler(documentCreated); 么

能做到同步等待么

发表于 2009-6-15 22:25 | 显示全部楼层

不要重复发帖

DocumentCreated 顾名思义 文档创建后

 楼主| 发表于 2009-6-21 13:40 | 显示全部楼层

斑竹你好,我参考了你的代码,但是在实际应用当中还是有很多的疑问

比如MSITStore:C:\Documents%20and%20Settings\Administrator\桌面\arxref(.net).chm::/1/Autodesk.AutoCAD.Windows.html">Autodesk.AutoCAD.Windows.OpenFileDialog这个类,好像对原有的cad原有的filedlg没有什么影响

请再指教指教,怎么能做到捕获类似于c#中 OpenFileDialog.ShowDialog() == DialogResult.OK 这个消息,

方便的话请给点示例代码

发表于 2009-6-21 14:56 | 显示全部楼层

你具体想实现什么功能?

详细说下吧

 楼主| 发表于 2009-6-21 15:07 | 显示全部楼层

想在cad打开文件的时候获取到被打开文件的信息,

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

发表于 2009-6-21 16:13 | 显示全部楼层
直接处理DocumentCreated 事件就好了
  1.     class TlsApplication : IExtensionApplication
  2.     {
  3.         void IExtensionApplication.Initialize()
  4.         {
  5.             TTest.Start();
  6.         }
  7.         void IExtensionApplication.Terminate()
  8.         {
  9.         }
  10.     }
  11.     class TTest
  12.     {
  13.         public static void Start()
  14.         {
  15.             Application.DocumentManager.DocumentCreated += new DocumentCollectionEventHandler(documentCreated);
  16.         }
  17.         static void documentCreated(object sender, DocumentCollectionEventArgs e)
  18.         {
  19.             Document doc = e.Document;
  20.             Database db = doc.Database;
  21.             string s = "";
  22.             using (Transaction tr = db.TransactionManager.StartTransaction())
  23.             {
  24.                 BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
  25.                 BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);
  26.                 foreach (ObjectId id in btr)
  27.                 {
  28.                     Entity ent = (Entity)tr.GetObject(id, OpenMode.ForRead);
  29.                     s += "\n" + ent.GetType().Name;
  30.                 }
  31.             }
  32.             doc.Editor.WriteMessage(s);
  33.         }
  34.     }
  1. Polyline
  2. Polyline
  3. Polyline
  4. Polyline
  5. Line
  6. Line
  7. Line
  8. Line
  9. Line
  10. Line
  11. Line
  12. Spline
  13. Arc
  14. Arc
  15. Circle
  16. Circle
  17. Circle
  18. Arc
  19. Polyline3d
  20. Polyline3d
  21. Ellipse
  22. Ellipse
  23. Arc
  24. Circle
  25. Ellipse
  26. Ellipse
  27. Line
复制代码
发表于 2017-8-14 08:53 | 显示全部楼层
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
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-4-27 08:41 , Processed in 0.207013 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表