乐筑天下

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

[编程交流] 创建一个组

[复制链接]

4

主题

8

帖子

4

银币

初来乍到

Rank: 1

铜币
20
发表于 2022-7-6 22:38:55 | 显示全部楼层 |阅读模式
下面的代码选择图形中的图元。
我现在需要创建一个名为groupa mad up的新组,该组由选定的实体组成。
我需要在代码中添加什么
  1. Public Class Class1
  2. <CommandMethod("selectwindowa")> _
  3. Public Sub selectwindowa()
  4. Dim mydb As Database = HostApplicationServices.WorkingDatabase
  5. Dim mydwg As Document = DocumentManager.MdiActiveDocument
  6. Dim myeditor As Editor = DocumentManager.MdiActiveDocument.Editor
  7. Dim myPPR As Point3d = myeditor.GetPoint("Select 1st Point: ").Value
  8. Dim myPPR1 As Point3d = myeditor.GetCorner("Select 2nd Point: ", myPPR).Value
  9. Dim mypsr As PromptSelectionResult = mydwg.Editor.SelectWindow( _
  10. myPPR, myPPR1)
  11. If mypsr.Status = PromptStatus.OK Then
  12. Using myTrans As Transaction = mydwg.TransactionManager.StartTransaction
  13. For Each myObjectID As ObjectId In mypsr.Value.GetObjectIds
  14. Dim myEnt As Entity = myObjectID.GetObject(OpenMode.ForRead)
  15. 'Insert Code Here
  16. Next
  17. End Using
  18. End If
  19. End Sub
  20.  
  21. End Class
回复

使用道具 举报

4

主题

8

帖子

4

银币

初来乍到

Rank: 1

铜币
20
发表于 2022-7-6 23:30:08 | 显示全部楼层
我现在有一些代码可以创建一个组
但我收到一条警告信息,上面写着
 
“Function”selectobjectsforgroup“不会在所有代码路径上返回值
使用结果时,运行时可能会发生空引用异常。”
 
我已经运行了代码,它导致autocad崩溃。
 
我需要做什么改变
 
这是我到目前为止的代码
 
  1. Public Class Class2
  2. <CommandMethod("CG")> _
  3. Public Sub CreateGroup()
  4.  
  5.  
  6. Dim mydwg As Document = Application.DocumentManager.MdiActiveDocument
  7. Dim db As Database = mydwg.Database
  8. Dim myeditor As Editor = mydwg.Editor
  9.  
  10.  
  11.  
  12. Dim tr As Transaction = db.TransactionManager.StartTransaction()
  13. Using tr
  14.  
  15. ' Get the group dictionary from the drawing
  16. Dim gd As DBDictionary = DirectCast(tr.GetObject(db.GroupDictionaryId, OpenMode.ForRead), DBDictionary)
  17.  
  18.  
  19. ' Check the group name, to see whether it's
  20. ' already in use
  21. Dim pso As New PromptStringOptions(vbLf & "Enter new group name: ")
  22. pso.AllowSpaces = True
  23. ' A variable for the group's name
  24. Dim grpName As String = ""
  25. Do
  26. Dim pr As PromptResult = myeditor.GetString(pso)
  27.  
  28.  
  29. ' Just return if the user cancelled
  30. ' (will abort the transaction as we drop out of the using
  31. ' statement's scope)
  32. If pr.Status <> PromptStatus.OK Then
  33. Return
  34. End If
  35. Try
  36. ' Validate the provided symbol table name
  37. SymbolUtilityServices.ValidateSymbolName(pr.StringResult, False)
  38. ' Only set the block name if it isn't in use
  39. If gd.Contains(pr.StringResult) Then
  40. myeditor.WriteMessage(vbLf & "A group with this name already exists.")
  41. Else
  42. grpName = pr.StringResult
  43. End If
  44. Catch
  45.  
  46. ' An exception has been thrown, indicating the
  47. ' name is invalid
  48. myeditor.WriteMessage(vbLf & "Invalid group name.")
  49.  
  50.  
  51. End Try
  52. Loop While grpName = ""
  53. ' Create our new group...
  54.  
  55.  
  56. Dim grp As New Group("Test group", True)
  57. ' Add the new group to the dictionary
  58.  
  59.  
  60. gd.UpgradeOpen()
  61. Dim grpId As ObjectId = gd.SetAt(grpName, grp)
  62. tr.AddNewlyCreatedDBObject(grp, True)
  63. ' Open the model-space
  64.  
  65. Dim bt As BlockTable = DirectCast(tr.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
  66.  
  67. Dim ms As BlockTableRecord = DirectCast(tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite), BlockTableRecord)
  68.  
  69.  
  70. ' Add some lines to the group to form a square
  71. ' (the entities belong to the model-space)
  72. Dim ids As New ObjectIdCollection()
  73. Dim ents As DBObjectCollection = Selectobjectsforgroup(0)
  74. For Each ent As Entity In ents
  75.  
  76. Dim id As ObjectId = ms.AppendEntity(ent)
  77. ids.Add(id)
  78. tr.AddNewlyCreatedDBObject(ent, True)
  79. Next
  80. grp.InsertAt(0, ids)
  81. ' Commit the transaction
  82. tr.Commit()
  83. ' Report what we've done
  84. myeditor.WriteMessage(vbLf & "Created group named ""{0}"" containing {1} entities.", grpName, ents.Count)
  85. End Using
  86. End Sub
  87. Private Function Selectobjectsforgroup(ByVal size As Double) As DBObjectCollection
  88.  
  89.  
  90.  
  91.  
  92.  
  93. Dim mydwg As Document = DocumentManager.MdiActiveDocument
  94. Dim db As Database = mydwg.Database
  95. Dim myeditor As Editor = DocumentManager.MdiActiveDocument.Editor
  96. Dim myPPR As Point3d = myeditor.GetPoint("Select 1st Point: ").Value
  97. Dim myPPR1 As Point3d = myeditor.GetCorner("Select 2nd Point: ", myPPR).Value
  98. Dim mypsr As PromptSelectionResult = mydwg.Editor.SelectWindow( _
  99. myPPR, myPPR1)
  100. If mypsr.Status = PromptStatus.OK Then
  101. Using myTrans As Transaction = mydwg.TransactionManager.StartTransaction
  102. For Each myObjectID As ObjectId In mypsr.Value.GetObjectIds
  103. Dim myEnt As Entity = myObjectID.GetObject(OpenMode.ForRead)
  104. Next
  105. End Using
  106. End If
  107. End Function
  108.  
  109. End Class
回复

使用道具 举报

4

主题

8

帖子

4

银币

初来乍到

Rank: 1

铜币
20
发表于 2022-7-6 23:48:34 | 显示全部楼层
我现在有了一个代码,它更接近于对我选择的对象进行分组。
 
唯一的问题是,当我运行它时,autocad中出现了一个错误,即
“应用程序中的组件发生未处理的异常”
 
我该如何修复它?
 
 
  1. Public Class Class2
  2. <CommandMethod("CG")> _
  3. Public Sub CreateGroup()
  4. Dim mydwg As Document = Application.DocumentManager.MdiActiveDocument
  5. Dim db As Database = mydwg.Database
  6. Dim myeditor As Editor = mydwg.Editor
  7. Dim tr As Transaction = db.TransactionManager.StartTransaction()
  8. Using tr
  9. ' Get the group dictionary from the drawing
  10. Dim gd As DBDictionary = DirectCast(tr.GetObject(db.GroupDictionaryId, OpenMode.ForRead), DBDictionary)
  11. ' Check the group name, to see whether it's
  12. ' already in use
  13. Dim pso As New PromptStringOptions(vbLf & "Enter new group name: ")
  14. pso.AllowSpaces = True
  15. ' A variable for the group's name
  16. Dim grpName As String = ""
  17. Do
  18. Dim pr As PromptResult = myeditor.GetString(pso)
  19. ' Just return if the user cancelled
  20. ' (will abort the transaction as we drop out of the using
  21. ' statement's scope)
  22. If pr.Status <> PromptStatus.OK Then
  23. Return
  24. End If
  25. Try
  26. ' Validate the provided symbol table name
  27. SymbolUtilityServices.ValidateSymbolName(pr.StringResult, False)
  28. ' Only set the block name if it isn't in use
  29. If gd.Contains(pr.StringResult) Then
  30. myeditor.WriteMessage(vbLf & "A group with this name already exists.")
  31. Else
  32. grpName = pr.StringResult
  33. End If
  34. Catch
  35.  
  36. ' An exception has been thrown, indicating the
  37. ' name is invalid
  38. myeditor.WriteMessage(vbLf & "Invalid group name.")
  39.  
  40.  
  41. End Try
  42. Loop While grpName = ""
  43. ' Create our new group...
  44.  
  45.  
  46. Dim grp As New Group("Test group", True)
  47. ' Add the new group to the dictionary
  48.  
  49.  
  50. gd.UpgradeOpen()
  51. Dim grpId As ObjectId = Selectobjectsforgroup()
  52. tr.AddNewlyCreatedDBObject(grp, True)
  53. ' Open the model-space
  54.  
  55. Dim bt As BlockTable = DirectCast(tr.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
  56.  
  57. Dim ms As BlockTableRecord = DirectCast(tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite), BlockTableRecord)
  58.  
  59.  
  60. ' Commit the transaction
  61. tr.Commit()
  62.  
  63. End Using
  64. End Sub
  65. Private Function Selectobjectsforgroup() As ObjectId
  66. Dim mydwg As Document = DocumentManager.MdiActiveDocument
  67. Dim db As Database = mydwg.Database
  68. Dim myeditor As Editor = DocumentManager.MdiActiveDocument.Editor
  69. Dim myPPR As Point3d = myeditor.GetPoint("Select 1st Point: ").Value
  70. Dim myPPR1 As Point3d = myeditor.GetCorner("Select 2nd Point: ", myPPR).Value
  71. Dim mypsr As PromptSelectionResult = mydwg.Editor.SelectWindow( _
  72. myPPR, myPPR1)
  73. If mypsr.Status = PromptStatus.OK Then
  74. Using myTrans As Transaction = mydwg.TransactionManager.StartTransaction
  75. For Each myObjectID As ObjectId In mypsr.Value.GetObjectIds
  76. Dim myEnt As Entity = myObjectID.GetObject(OpenMode.ForWrite)
  77. Next
  78. End Using
  79. End If
  80. End Function
  81.  
  82. End Class
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

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

GMT+8, 2025-1-31 21:00 , Processed in 0.267011 second(s), 68 queries .

© 2020-2025 乐筑天下

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