RMS 发表于 2022-7-6 22:55:06

问题循环通过我的cad f

我正在做一些。net测试,我遇到了很多问题。我正在遍历一个包含多个Autocad文件的目录。然后我一次打开一个,并尝试做一个选择集,但在绘图打开默认绘图1时,出现了我的问题。dwg(空白)图纸在其顶部打开;如果我把它关闭,同样的事情会发生在下一幅画上。
 
如果有人想解决这个问题,这是我的代码。谢谢
 


Public Class Class1

   Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor

   <CommandMethod("comp")> _
   Public Sub createLine()

       Dim mPath As String = ""
       Dim mFxdNm As String = ""

       ' First create a FolderBrowserDialog object
       Dim FolderBrowserDialog1 As New Forms.FolderBrowserDialog
       With FolderBrowserDialog1
         .RootFolder = Environment.SpecialFolder.Desktop
         .SelectedPath = "c:\dxfTest"
         .Description = "Select the source directory"
         If .ShowDialog = Forms.DialogResult.OK Then
               mPath = .SelectedPath.ToString
               Dim last_char = mPath.Substring(mPath.Length - 1)
               If last_char <> "\" Then
                   mPath = (mPath & "\")
               End If
         End If
       End With

       Dim files() As String ' keeps a list of all nest sums
       files = System.IO.Directory.GetFiles(mPath, "*.dwg") ' gets list of files names in folder
       For Each fname In files' Loops Through files() array and parces every nest sum

         Dim strFileName As String = fname
         Dim acDocMgr As DocumentCollection = Application.DocumentManager
         If (File.Exists(strFileName)) Then
               acDocMgr.Open(strFileName, False)

               ' Get the current document and database
               Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
               Dim acCurDb As Database = acDoc.Database

               ' Start a transaction
               Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()

                   ' Request for objects to be selected in the drawing area
                   Dim acSSPrompt As PromptSelectionResult = acDoc.Editor.GetSelection()

                   ' If the prompt status is OK, objects were selected
                   If acSSPrompt.Status = PromptStatus.OK Then

                     Dim acSSet As SelectionSet = acSSPrompt.Value

                     ' Step through the objects in the selection set
                     For Each acSSObj As SelectedObject In acSSet

                           ' Check to make sure a valid SelectedObject object was returned
                           If Not IsDBNull(acSSObj) Then

                               ' Open the selected object for write
                               Dim acEnt As Entity = acTrans.GetObject(acSSObj.ObjectId, OpenMode.ForWrite)

                               If Not IsDBNull(acEnt) Then
                                 ' Change the object's color to Green
                                 acEnt.ColorIndex = 3

                               End If
                           End If
                     Next

                     ' Save the new object to the database
                     acTrans.Commit()

                   End If
                   ' Dispose of the transaction
               End Using
         Else
               acDocMgr.MdiActiveDocument.Editor.WriteMessage("File " & strFileName & " does not exist.")
         End If
       Next
   End Sub

End Class

和导入:

Imports System
Imports System.IO
Imports System.Windows
Imports Autodesk.AutoCAD.Colors
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.EditorInput
Imports System.Runtime.InteropServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.Windows

SEANT 发表于 2022-7-6 23:04:06

如果将CommandMethod设置为这样,会发生什么情况:
 

SEANT 发表于 2022-7-6 23:12:25

我无法测试这个概念,但类似的东西也可能有帮助:
 

         If (File.Exists(strFileName)) Then
               Dim acDoc As Document = acDocMgr.Open(strFileName, False)

               ' Get the current document and database
               Application.DocumentManager.MdiActiveDocument = acDoc
               Dim acCurDb As Database = acDoc.Database

 
以这种方式打开的文件也可能需要“Document.LockDocument”。

RMS 发表于 2022-7-6 23:29:13

 
这似乎成功了!我现在可以在每个文件上进行选择,但我遇到了私奔违规;所以我有更多的问题需要解决。我还尝试了你第二次发布的代码块,但没有运气,虽然这给了我一些线索。
 
谢谢你的帮助!

SEANT 发表于 2022-7-6 23:36:13

 
 
 
这就是“Document.LockDocument”发挥作用的时候。

RMS 发表于 2022-7-6 23:39:02

 
嗯,我正在尝试一些不同的锁定方法,但我认为我的程序在这个循环中如何查看每个新的dxf文件/图形,但Drawing1存在一个潜在的问题。dwg仍然影响着我(尝试的编码方法),我的意思是,我在一个目录中有4个dxf文件;但我的代码正在计算Drawing1,然后提前停止循环,并且没有完全查看最后一个dxf文件。在VBA中使用此绘图,从不查看绘图1。完全是dwg!(wtf)。。。。。哎呀,刚刚把我的电脑砸了!!

SEANT 发表于 2022-7-6 23:52:54

 
这是应得的:眨眼:
 
 
我说不出问题出在哪里,但如果最后期限迫在眉睫的话,一个。NET例程可以通过互操作使用与VBA相同的方法。这种方法可能更为常见。
我知道这种类型的程序是可能的。虽然我在这方面没有太多经验。
这个线程(大约从中线程开始)有一些关于互操作的信息:
http://www.cadtutor.net/forum/showthread.php?39166

RMS 发表于 2022-7-6 23:56:29

好吧,这似乎是粗糙的PC工作!
 
我在网上又搜索了一些,找到了我丢失的拼图。这是我找到的代码块,也许这里的其他人也会发现它很有用。
 
<br>'//Get a database from a file <br>      Public Function GetDatabaseFromFile(ByVal fileName As String) As Database<br>            Dim newDB As Database = New Database(False, True)<br>            newDB.ReadDwgFile(fileName, System.IO.FileShare.None, False, Nothing) <br>            Return newDB<br>      End Function<br>
 
学分归fixo:
http://forums.augi.com/showthread.php?t=62473
页: [1]
查看完整版本: 问题循环通过我的cad f