乐筑天下

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

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

[复制链接]

9

主题

25

帖子

16

银币

初来乍到

Rank: 1

铜币
45
发表于 2022-7-6 21:52:13 | 显示全部楼层 |阅读模式
我真的刚刚进入。NET编程,我很难弄清楚如何请求管道信息。
 
我这里有一些简单的东西,我在图纸中计算了管道。现在,我最终尝试通过管道循环并列出其坡度,但我不太确定如何做到这一点。如您所见,我试图首先列出不同的管道ID,我得到了以下错误。。。任何帮助或解释都将不胜感激,因为API参考在提供示例方面做得不是很好。
 
  1. Imports Autodesk.AutoCAD.Runtime
  2. Imports Autodesk.AutoCAD.EditorInput
  3. Imports Autodesk.AutoCAD.DatabaseServices
  4. Imports Autodesk.AutoCAD.ApplicationServices
  5. Imports Autodesk.Civil.DatabaseServices
  6. Public Class PipeSlope
  7.    <CommandMethod("GetPipeSlope")>
  8.    Public Sub cmdGetPipeSlope()
  9.        Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
  10.        Dim civDoc As Autodesk.Civil.ApplicationServices.CivilDocument
  11.        civDoc = Autodesk.Civil.ApplicationServices.CivilApplication.ActiveDocument
  12.        Dim pipeIds As ObjectIdCollection = civDoc.GetPipeNetworkIds
  13.        ed.WriteMessage("There are " & pipeIds.Count & " pipes in the drawing.")
  14.        Dim oCurrentPipe = civDoc.NetworkState.CurrentPipeId
  15.        For i = 0 To pipeIds.Count
  16.            ed.WriteMessage(oCurrentPipe)
  17.        Next
  18.    End Sub
  19. End Class
回复

使用道具 举报

20

主题

338

帖子

323

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
100
发表于 2022-7-6 22:00:05 | 显示全部楼层
我希望这能帮助你朝着正确的方向前进。如果您有任何疑问,请随时询问代码中发生了什么。
 
  1. [color="red"]Imports Autodesk.Civil.ApplicationServices[/color]
  2. <CommandMethod("GetPipeSlope")>
  3.        Public Sub cmdGetPipeSlope()
  4.            Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
  5.            Dim civDoc As civilDocument = Autodesk.Civil.ApplicationServices.CivilApplication.ActiveDocument
  6.            Dim aDoc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
  7.            Dim db As Database = aDoc.Database
  8.            Dim NetworkIds As ObjectIdCollection = civDoc.GetPipeNetworkIds
  9.            Using trans As Transaction = db.TransactionManager.StartTransaction
  10.                For Each NetId As ObjectId In NetworkIds
  11.                    Dim net As Network = trans.GetObject(NetId, OpenMode.ForRead)
  12.                    ed.WriteMessage(vbCrLf + net.Name)
  13.                    Dim PipeIds As ObjectIdCollection = net.GetPipeIds
  14.                    Using trans2 As Transaction = db.TransactionManager.StartTransaction
  15.                        Dim cnt As Integer = 1
  16.                        For Each PipeId As ObjectId In PipeIds
  17.                            Dim pipe As Pipe = trans2.GetObject(PipeId, OpenMode.ForRead)
  18.                            ed.WriteMessage(vbCrLf + cnt.ToString + " : " + pipe.Name + "   -   " + (pipe.Slope * 100.0).ToString("N2") + "%")
  19.                            cnt = cnt + 1
  20.                        Next
  21.                        trans2.Commit()
  22.                    End Using
  23.                Next
  24.                trans.Commit()
  25.            End Using
  26.        End Sub
回复

使用道具 举报

9

主题

25

帖子

16

银币

初来乍到

Rank: 1

铜币
45
发表于 2022-7-6 22:03:00 | 显示全部楼层
太棒了,非常感谢。
 
我主要来自VBS,从数据库连接/读取有点不同;在VBS中,您可以使用ADO连接连接到MS Access数据库,但它就像任何文件一样。。。你必须打开阅读,然后在完成后关闭连接。您是否也必须关闭或断开与DB的连接?
回复

使用道具 举报

20

主题

338

帖子

323

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
100
发表于 2022-7-6 22:08:20 | 显示全部楼层
通过事务访问数据库对象。由于我使用的是“使用”语句,因此我不需要处置交易,因为这是在“使用结束”时自动完成的。如果我没有使用“using”语句,那么我将不得不调用trans。dispose可处置交易对象。
回复

使用道具 举报

9

主题

25

帖子

16

银币

初来乍到

Rank: 1

铜币
45
发表于 2022-7-6 22:11:47 | 显示全部楼层
哦,太好了!因此,使用类似于以只读方式访问数据库。。。这真的很方便。
回复

使用道具 举报

20

主题

338

帖子

323

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
100
发表于 2022-7-6 22:13:54 | 显示全部楼层
 
我想我不会那样看待“使用”。“使用”是一种或多或少轻松处理一次性物品的方法。当您使用完事务实例时,需要正确地处理它。我可以在事务中使用“using”,并在何时写入数据库。事务只是与AutoCAD数据库交互的正确方式。“使用”是在使用完物品后处置物品的一种简单方法。
回复

使用道具 举报

9

主题

25

帖子

16

银币

初来乍到

Rank: 1

铜币
45
发表于 2022-7-6 22:21:23 | 显示全部楼层
好吧,我更进一步。。。不确定我是否应该为此创建新线程。。。如果我需要。。。无论如何
 
该代码允许用户使用交叉窗口选择对象。然后,它将获取所有对象ID,并将其与管网对象集合中的对象ID进行比较。如果他们匹配,这是一个管道。然后,它将在屏幕上显示每个管道的流量方法。
 
我想做的是将一个变量设为“ForWrite”,将方法更改为3。我尝试过这样做,但它给了我一个错误,关于实体的名称太宽,我不知道如何修复它:
  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.            Dim cadSSPrompt As PromptSelectionResult = cadDOC.Editor.GetSelection()
  18.            '// If user pressed enter after prompt
  19.            If cadSSPrompt.Status = PromptStatus.OK Then
  20.                Dim cadSSet As SelectionSet = cadSSPrompt.Value
  21.                For Each item As SelectedObject In cadSSet
  22.                    ed.WriteMessage(vbCrLf & "Object in CAD DB: " & item.ObjectId.ToString)
  23.                    '// Check if the item is in the DB
  24.                    If Not IsDBNull(item) Then
  25.                        '// Loop through the networks and see if the object ID is a pipe
  26.                        For Each netID As ObjectId In pipeNetIDs
  27.                            Dim net As Network = cadTrans.GetObject(netID, OpenMode.ForRead)
  28.                            Dim pipeIDs As ObjectIdCollection = net.GetPipeIds
  29.                            Using cadTrans2 As Transaction = cadDB.TransactionManager.StartTransaction
  30.                                For Each pipeID As ObjectId In pipeIDs
  31.                                    Dim pipeOBJ As Pipe = cadTrans2.GetObject(pipeID, OpenMode.ForRead)
  32.                                    If pipeID = item.ObjectId Then
  33.                                        Select Case pipeOBJ.FlowDirectionMethod
  34.                                            Case 0
  35.                                                ed.WriteMessage(vbCrLf & pipeOBJ.Name & " flow method: Bi-Directional")
  36.                                            Case 1
  37.                                                ed.WriteMessage(vbCrLf & pipeOBJ.Name & " flow method: Start to End")
  38.                                            Case 2
  39.                                                ed.WriteMessage(vbCrLf & pipeOBJ.Name & " flow method: End to Start")
  40.                                            Case 3
  41.                                                ed.WriteMessage(vbCrLf & pipeOBJ.Name & " flow method: By Slope")
  42.                                        End Select
  43.                                        '// This is where the "Entity" error is
  44.                                        Dim pipeOBJwrite As Entity = cadTrans2.GetObject(pipeID, OpenMode.ForWrite)
  45.                                    End If
  46.                                Next
  47.                            End Using
  48.                        Next
  49.                    End If
  50.                Next
  51.            End If
  52.        End Using
  53.    End Sub
  54. End Class
回复

使用道具 举报

20

主题

338

帖子

323

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
100
发表于 2022-7-6 22:22:27 | 显示全部楼层
如果你知道你只会使用管道。然后只允许用户选择管道。
 
 
  1. Dim acTypValAr(0) As TypedValue
  2. acTypValAr.SetValue(New TypedValue(DxfCode.Start, "AECC_PIPE"), 0)
  3. Dim pSelFil As SelectionFilter = New SelectionFilter(acTypValAr)
  4. Dim cadSSPrompt As PromptSelectionResult = cadDOC.Editor.GetSelection(pSelFil)
回复

使用道具 举报

20

主题

338

帖子

323

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
100
发表于 2022-7-6 22:30:05 | 显示全部楼层
您可以从已打开以供写入的对象中读取。
 
还有一种升级开放方法。如果您之前已打开以进行读取,则可以升级为打开以进行写入。
 
如果你知道你正在使用管道,你应该
  1. Dim dbobj as Pipe = trans.GetObject(ObjID,openmode.forwrite)
回复

使用道具 举报

20

主题

338

帖子

323

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
100
发表于 2022-7-6 22:31:09 | 显示全部楼层
***注***
 
不要忘记:
 
  1. trans.commit()

 
否则,您的事务将回滚,就像什么都没有发生一样。这可能令人沮丧,因为代码似乎都是正确的,但什么都不起作用。
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

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

GMT+8, 2025-3-4 12:45 , Processed in 0.502859 second(s), 72 queries .

© 2020-2025 乐筑天下

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