乐筑天下

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

[编程交流] 帮助介绍一个简单的示例

[复制链接]

9

主题

25

帖子

16

银币

初来乍到

Rank: 1

铜币
45
发表于 2022-7-6 22:35:18 | 显示全部楼层
好的,作为一个测试,我点击了一个我只知道是“By slope”的管道,我在这条线上得到了一个“InvalidoOperationException”错误:
 
**编辑:等等,试着做点什么。。。
回复

使用道具 举报

9

主题

25

帖子

16

银币

初来乍到

Rank: 1

铜币
45
发表于 2022-7-6 22:41:51 | 显示全部楼层
好的,没有更多的错误,但CAD崩溃每次我运行命令lol
 
  1. Imports Autodesk.AutoCAD.Runtime
  2. Imports Autodesk.AutoCAD.ApplicationServices
  3. Imports Autodesk.AutoCAD.DatabaseServices
  4. Imports Autodesk.AutoCAD.EditorInput
  5. Imports Autodesk.Civil.ApplicationServices
  6. Imports Autodesk.Civil.DatabaseServices
  7. Public Class DisplayPipeInfoSingleSelection
  8.    <CommandMethod("DisplayPipeInfo")>
  9.    Public Sub cmdDisplayPipeInfo()
  10.        Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
  11.        Dim civilDOC As CivilDocument = Autodesk.Civil.ApplicationServices.CivilApplication.ActiveDocument
  12.        Dim cadDOC As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
  13.        Dim cadDB As Database = cadDOC.Database
  14.        Dim pipeNetIDs As ObjectIdCollection = civilDOC.GetPipeNetworkIds
  15.        '// Open the DB query
  16.        Using cadTrans As Transaction = cadDB.TransactionManager.StartTransaction()
  17.            '// Use this to filter out ONLY pipes
  18.            Dim acTypValAr(0) As TypedValue
  19.            acTypValAr.SetValue(New TypedValue(DxfCode.Start, "AECC_PIPE"), 0)
  20.            Dim pSelFil As SelectionFilter = New SelectionFilter(acTypValAr)
  21.            Dim pSelFilOpt As New PromptSelectionOptions
  22.            With pSelFilOpt
  23.                .AllowDuplicates = False
  24.                .AllowSubSelections = False
  25.                .RejectObjectsFromNonCurrentSpace = True
  26.                .RejectObjectsOnLockedLayers = False
  27.            End With
  28.            Dim cadSSPrompt As PromptSelectionResult = cadDOC.Editor.GetSelection(pSelFilOpt, pSelFil)
  29.            Dim cadSSet As SelectionSet = cadSSPrompt.Value
  30.            For Each item As SelectedObject In CadSSet
  31.                If cadSSPrompt.Status = PromptStatus.OK Then
  32.                    For Each netID As ObjectId In pipeNetIDs
  33.                        Dim net As Network = cadTrans.GetObject(netID, OpenMode.ForWrite)
  34.                        Dim pipeIDs As ObjectIdCollection = net.GetPipeIds
  35.                        For Each pipeID As ObjectId In pipeIDs
  36.                            Dim pipeOBJ As Pipe = cadTrans.GetObject(pipeID, OpenMode.ForWrite)
  37.                            If pipeID = item.ObjectId Then
  38.                                Select Case pipeOBJ.FlowDirectionMethod
  39.                                    Case 0
  40.                                        ed.WriteMessage(vbCrLf & pipeOBJ.Name & " flow method: Bi-Directional")
  41.                                        pipeOBJ.FlowDirectionMethod = 3
  42.                                        cadTrans.Commit()
  43.                                    Case 1
  44.                                        ed.WriteMessage(vbCrLf & pipeOBJ.Name & " flow method: Start to End")
  45.                                        pipeOBJ.FlowDirectionMethod = 3
  46.                                        cadTrans.Commit()
  47.                                    Case 2
  48.                                        ed.WriteMessage(vbCrLf & pipeOBJ.Name & " flow method: End to Start")
  49.                                        pipeOBJ.FlowDirectionMethod = 3
  50.                                        cadTrans.Commit()
  51.                                    Case 3
  52.                                        ed.WriteMessage(vbCrLf & pipeOBJ.Name & " flow method: By Slope")
  53.                                End Select
  54.                            End If
  55.                        Next
  56.                    Next
  57.                End If
  58.            Next
  59.        End Using
  60.    End Sub
  61. End Class
回复

使用道具 举报

20

主题

338

帖子

323

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
100
发表于 2022-7-6 22:47:09 | 显示全部楼层
你做了一些奇怪的事情,但似乎没有什么意义。我已经根据我认为你试图做的事情重写了你的代码。
 
  1. <CommandMethod("SetPipeFlowMethodToSlope")>
  2.        Public Sub setPipeFlowMethodToSlope()
  3.            'Get Documents and Database
  4.            Dim aDoc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
  5.            Dim ed As Editor = aDoc.Editor
  6.            Dim db As Database = aDoc.Database
  7.            'Build SelectionSet Options and Filter
  8.            Dim acTypValAr(0) As TypedValue
  9.            acTypValAr.SetValue(New TypedValue(DxfCode.Start, "AECC_PIPE"), 0)
  10.            Dim pSelFil As SelectionFilter = New SelectionFilter(acTypValAr)
  11.            Dim pSelFilOpt As New PromptSelectionOptions
  12.            With pSelFilOpt
  13.                .AllowDuplicates = False
  14.                .AllowSubSelections = False
  15.                .RejectObjectsFromNonCurrentSpace = True
  16.                .RejectObjectsOnLockedLayers = False
  17.            End With
  18.            'Prompt for Selection
  19.            Dim PrmptSelRes As PromptSelectionResult = aDoc.Editor.GetSelection(pSelFilOpt, pSelFil)
  20.            'Test for a Good Selection
  21.            If PrmptSelRes.Status = PromptStatus.OK Then
  22.                'Get the SelectionSet
  23.                Dim ss As SelectionSet = PrmptSelRes.Value
  24.                'Begin a database transaction
  25.                Using trans As Transaction = db.TransactionManager.StartTransaction
  26.                    For Each item As SelectedObject In ss
  27.                        'Open each pipe in the selectionset for write
  28.                        Dim pipeObj As Pipe = trans.GetObject(item.ObjectId, OpenMode.ForWrite)
  29.                        'Print the Network Name, Pipe Name & Current Flow Method.
  30.                        ed.WriteMessage(vbCrLf + "Network: " + pipeObj.NetworkName + "  Name: " + pipeObj.Name + " - " + pipeObj.FlowDirectionMethod.ToString)
  31.                        'Set the Flow Direction to BySlope
  32.                        pipeObj.FlowDirectionMethod = FlowDirectionMethodType.BySlope
  33.                        'Print what we just did.
  34.                        ed.WriteMessage(vbCrLf + "Name: " + pipeObj.Name + " FlowDirectionMethod has been set to BySlope.")
  35.                    Next
  36.                    'Commit the transaction.
  37.                    trans.Commit()
  38.                End Using
  39.            End If
  40.            ed.WriteMessage(vbCrLf + "Command has completed sucessfully.")
  41.        End Sub
回复

使用道具 举报

9

主题

25

帖子

16

银币

初来乍到

Rank: 1

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

使用道具 举报

20

主题

338

帖子

323

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
100
发表于 2022-7-6 22:53:10 | 显示全部楼层
实际上,您可以使用枚举值(在本例中为3)而不是枚举。我更喜欢在可能的情况下使用枚举,以提高代码的可读性。
 
  1. <CommandMethod("SetPipeFlowMethodToSlope")>
  2.        Public Sub setPipeFlowMethodToSlope()
  3.            'Get Documents and Database
  4.            Dim aDoc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
  5.            Dim ed As Editor = aDoc.Editor
  6.            Dim db As Database = aDoc.Database
  7.            'Build SelectionSet Options and Filter
  8.            Dim acTypValAr(0) As TypedValue
  9.            acTypValAr.SetValue(New TypedValue(DxfCode.Start, "AECC_PIPE"), 0)
  10.            Dim pSelFil As SelectionFilter = New SelectionFilter(acTypValAr)
  11.            Dim pSelFilOpt As New PromptSelectionOptions
  12.            With pSelFilOpt
  13.                .AllowDuplicates = False
  14.                .AllowSubSelections = False
  15.                .RejectObjectsFromNonCurrentSpace = True
  16.                .RejectObjectsOnLockedLayers = False
  17.            End With
  18.            'Prompt for Selection
  19.            Dim PrmptSelRes As PromptSelectionResult = aDoc.Editor.GetSelection(pSelFilOpt, pSelFil)
  20.            'Test for a Good Selection
  21.            If PrmptSelRes.Status = PromptStatus.OK Then
  22.                'Get the SelectionSet
  23.                Dim ss As SelectionSet = PrmptSelRes.Value
  24.                'Begin a database transaction
  25.                Using trans As Transaction = db.TransactionManager.StartTransaction
  26.                    For Each item As SelectedObject In ss
  27.                        'Open each pipe in the selectionset for write
  28.                        Dim pipeObj As Pipe = trans.GetObject(item.ObjectId, OpenMode.ForWrite)
  29.                        'Print the Network Name, Pipe Name & Current Flow Method.
  30.                        ed.WriteMessage(vbCrLf + "Network: " + pipeObj.NetworkName + "  Name: " + pipeObj.Name + " - " + pipeObj.FlowDirectionMethod.ToString)
  31.                        'Set the Flow Direction to BySlope
  32.                        [color="red"]pipeObj.FlowDirectionMethod = 3[/color]
  33.                        'Print what we just did.
  34.                        ed.WriteMessage(vbCrLf + "Name: " + pipeObj.Name + " FlowDirectionMethod has been set to BySlope.")
  35.                    Next
  36.                    'Commit the transaction.
  37.                    trans.Commit()
  38.                End Using
  39.            End If
  40.            ed.WriteMessage(vbCrLf + "Command has completed successfully.")
  41.        End Sub

 
https://msdn.microsoft.com/en-us/library/3sd4y2w7.aspx
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

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

GMT+8, 2025-3-4 12:50 , Processed in 0.584304 second(s), 60 queries .

© 2020-2025 乐筑天下

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