乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 108|回复: 7

[编程交流] 问题循环通过我的cad f

[复制链接]
RMS

9

主题

38

帖子

29

银币

初来乍到

Rank: 1

铜币
45
发表于 2022-7-6 22:55:06 | 显示全部楼层 |阅读模式
我正在做一些。net测试,我遇到了很多问题。我正在遍历一个包含多个Autocad文件的目录。然后我一次打开一个,并尝试做一个选择集,但在绘图打开默认绘图1时,出现了我的问题。dwg(空白)图纸在其顶部打开;如果我把它关闭,同样的事情会发生在下一幅画上。
 
如果有人想解决这个问题,这是我的代码。谢谢
 
  1. Public Class Class1
  2.    Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
  3.    <CommandMethod("comp")> _
  4.    Public Sub createLine()
  5.        Dim mPath As String = ""
  6.        Dim mFxdNm As String = ""
  7.        ' First create a FolderBrowserDialog object
  8.        Dim FolderBrowserDialog1 As New Forms.FolderBrowserDialog
  9.        With FolderBrowserDialog1
  10.            .RootFolder = Environment.SpecialFolder.Desktop
  11.            .SelectedPath = "c:\dxfTest"
  12.            .Description = "Select the source directory"
  13.            If .ShowDialog = Forms.DialogResult.OK Then
  14.                mPath = .SelectedPath.ToString
  15.                Dim last_char = mPath.Substring(mPath.Length - 1)
  16.                If last_char <> "" Then
  17.                    mPath = (mPath & "")
  18.                End If
  19.            End If
  20.        End With
  21.        Dim files() As String ' keeps a list of all nest sums
  22.        files = System.IO.Directory.GetFiles(mPath, "*.dwg") ' gets list of files names in folder
  23.        For Each fname In files  ' Loops Through files() array and parces every nest sum
  24.            Dim strFileName As String = fname
  25.            Dim acDocMgr As DocumentCollection = Application.DocumentManager
  26.            If (File.Exists(strFileName)) Then
  27.                acDocMgr.Open(strFileName, False)
  28.                ' Get the current document and database
  29.                Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
  30.                Dim acCurDb As Database = acDoc.Database
  31.                ' Start a transaction
  32.                Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
  33.                    ' Request for objects to be selected in the drawing area
  34.                    Dim acSSPrompt As PromptSelectionResult = acDoc.Editor.GetSelection()
  35.                    ' If the prompt status is OK, objects were selected
  36.                    If acSSPrompt.Status = PromptStatus.OK Then
  37.                        Dim acSSet As SelectionSet = acSSPrompt.Value
  38.                        ' Step through the objects in the selection set
  39.                        For Each acSSObj As SelectedObject In acSSet
  40.                            ' Check to make sure a valid SelectedObject object was returned
  41.                            If Not IsDBNull(acSSObj) Then
  42.                                ' Open the selected object for write
  43.                                Dim acEnt As Entity = acTrans.GetObject(acSSObj.ObjectId, OpenMode.ForWrite)
  44.                                If Not IsDBNull(acEnt) Then
  45.                                    ' Change the object's color to Green
  46.                                    acEnt.ColorIndex = 3
  47.                                End If
  48.                            End If
  49.                        Next
  50.                        ' Save the new object to the database
  51.                        acTrans.Commit()
  52.                    End If
  53.                    ' Dispose of the transaction
  54.                End Using
  55.            Else
  56.                acDocMgr.MdiActiveDocument.Editor.WriteMessage("File " & strFileName & " does not exist.")
  57.            End If
  58.        Next
  59.    End Sub
  60. End Class
和导入:
  1. Imports System
  2. Imports System.IO
  3. Imports System.Windows
  4. Imports Autodesk.AutoCAD.Colors
  5. Imports Autodesk.AutoCAD.Runtime
  6. Imports Autodesk.AutoCAD.Geometry
  7. Imports Autodesk.AutoCAD.EditorInput
  8. Imports System.Runtime.InteropServices
  9. Imports Autodesk.AutoCAD.DatabaseServices
  10. Imports Autodesk.AutoCAD.ApplicationServices
  11. Imports Autodesk.AutoCAD.Windows
回复

使用道具 举报

10

主题

973

帖子

909

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
118
发表于 2022-7-6 23:04:06 | 显示全部楼层
如果将CommandMethod设置为这样,会发生什么情况:
 
回复

使用道具 举报

10

主题

973

帖子

909

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
118
发表于 2022-7-6 23:12:25 | 显示全部楼层
我无法测试这个概念,但类似的东西也可能有帮助:
 
  1.            If (File.Exists(strFileName)) Then
  2.                Dim acDoc As Document = acDocMgr.Open(strFileName, False)
  3.                ' Get the current document and database
  4.                Application.DocumentManager.MdiActiveDocument = acDoc
  5.                Dim acCurDb As Database = acDoc.Database

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

使用道具 举报

RMS

9

主题

38

帖子

29

银币

初来乍到

Rank: 1

铜币
45
发表于 2022-7-6 23:29:13 | 显示全部楼层
 
这似乎成功了!我现在可以在每个文件上进行选择,但我遇到了私奔违规;所以我有更多的问题需要解决。我还尝试了你第二次发布的代码块,但没有运气,虽然这给了我一些线索。
 
谢谢你的帮助!
回复

使用道具 举报

10

主题

973

帖子

909

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
118
发表于 2022-7-6 23:36:13 | 显示全部楼层
 
 
 
这就是“Document.LockDocument”发挥作用的时候。
回复

使用道具 举报

RMS

9

主题

38

帖子

29

银币

初来乍到

Rank: 1

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

使用道具 举报

10

主题

973

帖子

909

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

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

使用道具 举报

RMS

9

主题

38

帖子

29

银币

初来乍到

Rank: 1

铜币
45
发表于 2022-7-6 23:56:29 | 显示全部楼层
好吧,这似乎是粗糙的PC工作!
 
我在网上又搜索了一些,找到了我丢失的拼图。这是我找到的代码块,也许这里的其他人也会发现它很有用。
 
  1. <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
[/code]
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-4 05:49 , Processed in 0.794709 second(s), 68 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表