thereisnotime 发表于 2022-7-6 22:35:18

好的,作为一个测试,我点击了一个我只知道是“By slope”的管道,我在这条线上得到了一个“InvalidoOperationException”错误:
 
**编辑:等等,试着做点什么。。。

thereisnotime 发表于 2022-7-6 22:41:51

好的,没有更多的错误,但CAD崩溃每次我运行命令lol
 
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput

Imports Autodesk.Civil.ApplicationServices
Imports Autodesk.Civil.DatabaseServices

Public Class DisplayPipeInfoSingleSelection
   <CommandMethod("DisplayPipeInfo")>
   Public Sub cmdDisplayPipeInfo()
       Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
       Dim civilDOC As CivilDocument = Autodesk.Civil.ApplicationServices.CivilApplication.ActiveDocument
       Dim cadDOC As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
       Dim cadDB As Database = cadDOC.Database
       Dim pipeNetIDs As ObjectIdCollection = civilDOC.GetPipeNetworkIds

       '// Open the DB query
       Using cadTrans As Transaction = cadDB.TransactionManager.StartTransaction()
         '// Use this to filter out ONLY pipes
         Dim acTypValAr(0) As TypedValue
         acTypValAr.SetValue(New TypedValue(DxfCode.Start, "AECC_PIPE"), 0)
         Dim pSelFil As SelectionFilter = New SelectionFilter(acTypValAr)
         Dim pSelFilOpt As New PromptSelectionOptions
         With pSelFilOpt
               .AllowDuplicates = False
               .AllowSubSelections = False
               .RejectObjectsFromNonCurrentSpace = True
               .RejectObjectsOnLockedLayers = False
         End With

         Dim cadSSPrompt As PromptSelectionResult = cadDOC.Editor.GetSelection(pSelFilOpt, pSelFil)
         Dim cadSSet As SelectionSet = cadSSPrompt.Value

         For Each item As SelectedObject In CadSSet
               If cadSSPrompt.Status = PromptStatus.OK Then
                   For Each netID As ObjectId In pipeNetIDs
                     Dim net As Network = cadTrans.GetObject(netID, OpenMode.ForWrite)
                     Dim pipeIDs As ObjectIdCollection = net.GetPipeIds

                     For Each pipeID As ObjectId In pipeIDs
                           Dim pipeOBJ As Pipe = cadTrans.GetObject(pipeID, OpenMode.ForWrite)

                           If pipeID = item.ObjectId Then
                               Select Case pipeOBJ.FlowDirectionMethod
                                 Case 0
                                       ed.WriteMessage(vbCrLf & pipeOBJ.Name & " flow method: Bi-Directional")
                                       pipeOBJ.FlowDirectionMethod = 3
                                       cadTrans.Commit()
                                 Case 1
                                       ed.WriteMessage(vbCrLf & pipeOBJ.Name & " flow method: Start to End")
                                       pipeOBJ.FlowDirectionMethod = 3
                                       cadTrans.Commit()
                                 Case 2
                                       ed.WriteMessage(vbCrLf & pipeOBJ.Name & " flow method: End to Start")
                                       pipeOBJ.FlowDirectionMethod = 3
                                       cadTrans.Commit()
                                 Case 3
                                       ed.WriteMessage(vbCrLf & pipeOBJ.Name & " flow method: By Slope")
                               End Select
                           End If
                     Next
                   Next
               End If
         Next
       End Using
   End Sub
End Class

Hippe013 发表于 2022-7-6 22:47:09

你做了一些奇怪的事情,但似乎没有什么意义。我已经根据我认为你试图做的事情重写了你的代码。
 
<CommandMethod("SetPipeFlowMethodToSlope")>
       Public Sub setPipeFlowMethodToSlope()
         'Get Documents and Database
         Dim aDoc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
         Dim ed As Editor = aDoc.Editor
         Dim db As Database = aDoc.Database

         'Build SelectionSet Options and Filter
         Dim acTypValAr(0) As TypedValue
         acTypValAr.SetValue(New TypedValue(DxfCode.Start, "AECC_PIPE"), 0)
         Dim pSelFil As SelectionFilter = New SelectionFilter(acTypValAr)
         Dim pSelFilOpt As New PromptSelectionOptions
         With pSelFilOpt
               .AllowDuplicates = False
               .AllowSubSelections = False
               .RejectObjectsFromNonCurrentSpace = True
               .RejectObjectsOnLockedLayers = False
         End With
         'Prompt for Selection
         Dim PrmptSelRes As PromptSelectionResult = aDoc.Editor.GetSelection(pSelFilOpt, pSelFil)
         'Test for a Good Selection
         If PrmptSelRes.Status = PromptStatus.OK Then
               'Get the SelectionSet
               Dim ss As SelectionSet = PrmptSelRes.Value
               'Begin a database transaction
               Using trans As Transaction = db.TransactionManager.StartTransaction
                   For Each item As SelectedObject In ss
                     'Open each pipe in the selectionset for write
                     Dim pipeObj As Pipe = trans.GetObject(item.ObjectId, OpenMode.ForWrite)
                     'Print the Network Name, Pipe Name & Current Flow Method.
                     ed.WriteMessage(vbCrLf + "Network: " + pipeObj.NetworkName + "Name: " + pipeObj.Name + " - " + pipeObj.FlowDirectionMethod.ToString)
                     'Set the Flow Direction to BySlope
                     pipeObj.FlowDirectionMethod = FlowDirectionMethodType.BySlope
                     'Print what we just did.
                     ed.WriteMessage(vbCrLf + "Name: " + pipeObj.Name + " FlowDirectionMethod has been set to BySlope.")
                   Next
                   'Commit the transaction.
                   trans.Commit()
               End Using
         End If
         ed.WriteMessage(vbCrLf + "Command has completed sucessfully.")
       End Sub

thereisnotime 发表于 2022-7-6 22:48:15

啊,是的,我知道我错在哪里了。我这方面的“拼凑”太多了。非常感谢。这对我来说是一个很好的起点
 
这很有趣,为什么你不能只说流方向法的整数。。。你必须说“BySlope”,但我想这是有道理的,因为你必须将其分配给属性。我想我现在明白这一切是怎么回事了;程序会给你一个属性的数值,但你不能只说“这个属性,而不是2,让它变成3”,你必须在物理上使它等于该对象的原始属性。
 
与Visual Basic脚本lol有很大不同,但现在有了意义。

Hippe013 发表于 2022-7-6 22:53:10

实际上,您可以使用枚举值(在本例中为3)而不是枚举。我更喜欢在可能的情况下使用枚举,以提高代码的可读性。
 
<CommandMethod("SetPipeFlowMethodToSlope")>
       Public Sub setPipeFlowMethodToSlope()
         'Get Documents and Database
         Dim aDoc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
         Dim ed As Editor = aDoc.Editor
         Dim db As Database = aDoc.Database

         'Build SelectionSet Options and Filter
         Dim acTypValAr(0) As TypedValue
         acTypValAr.SetValue(New TypedValue(DxfCode.Start, "AECC_PIPE"), 0)
         Dim pSelFil As SelectionFilter = New SelectionFilter(acTypValAr)
         Dim pSelFilOpt As New PromptSelectionOptions
         With pSelFilOpt
               .AllowDuplicates = False
               .AllowSubSelections = False
               .RejectObjectsFromNonCurrentSpace = True
               .RejectObjectsOnLockedLayers = False
         End With
         'Prompt for Selection
         Dim PrmptSelRes As PromptSelectionResult = aDoc.Editor.GetSelection(pSelFilOpt, pSelFil)
         'Test for a Good Selection
         If PrmptSelRes.Status = PromptStatus.OK Then
               'Get the SelectionSet
               Dim ss As SelectionSet = PrmptSelRes.Value
               'Begin a database transaction
               Using trans As Transaction = db.TransactionManager.StartTransaction
                   For Each item As SelectedObject In ss
                     'Open each pipe in the selectionset for write
                     Dim pipeObj As Pipe = trans.GetObject(item.ObjectId, OpenMode.ForWrite)
                     'Print the Network Name, Pipe Name & Current Flow Method.
                     ed.WriteMessage(vbCrLf + "Network: " + pipeObj.NetworkName + "Name: " + pipeObj.Name + " - " + pipeObj.FlowDirectionMethod.ToString)
                     'Set the Flow Direction to BySlope
                     pipeObj.FlowDirectionMethod = 3
                     'Print what we just did.
                     ed.WriteMessage(vbCrLf + "Name: " + pipeObj.Name + " FlowDirectionMethod has been set to BySlope.")
                   Next
                   'Commit the transaction.
                   trans.Commit()
               End Using
         End If
         ed.WriteMessage(vbCrLf + "Command has completed successfully.")
       End Sub
 
https://msdn.microsoft.com/en-us/library/3sd4y2w7.aspx
页: 1 [2]
查看完整版本: 帮助介绍一个简单的示例