乐筑天下

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

[编程交流] [vb.net]UCS问题

[复制链接]

10

主题

29

帖子

21

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
68
发表于 2022-7-6 17:06:12 | 显示全部楼层 |阅读模式
嗯,我在vb方面进展很慢。网
 
这是我最近的一期:
 
我有一个程序,它会提示输入两个端点,然后根据这些端点插入一条线。插入直线后,程序还会在直线的中心点插入一个块。
 
当UCS处于正常位置(与WCS对齐?)时,该程序工作正常。
 
但是,如果我以任何方式移动UCS(例如旋转15度),线和块最终会插入到图形上的奇数位置。它们的大小和相对方向正确,但它们的整体插入点不正确。
 
我会发布一些代码,但这不是很直截了当。但是,我使用标准方法从编辑器中获取用户输入,并将线条和块插入到活动图形数据库中。
 
我的猜测是,当用户输入这两个点时,AutoCAD正在将这些点转换为某些WCS坐标,当插入线和块时,这些坐标不会转换回当前UCS。不幸的是,我不知道如何解决这个问题(假设这就是问题所在)。
 
你知道这可能是什么原因吗?
回复

使用道具 举报

10

主题

973

帖子

909

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
118
发表于 2022-7-6 17:26:30 | 显示全部楼层
是的,这是一个翻译问题,需要与VBA类似的步骤。
 
下面是一个vb。net演示。与大多数演示代码一样,错误检查应该修改到所需的任何级别。
 
  1. Imports Autodesk.AutoCAD.Runtime
  2. Imports Autodesk.AutoCAD.ApplicationServices
  3. Imports Autodesk.AutoCAD.DatabaseServices
  4. Imports Autodesk.AutoCAD.EditorInput
  5. Imports Autodesk.AutoCAD.Geometry
  6. Public Class STSCCommands
  7.    <CommandMethod("BTM")> _
  8.    Public Sub Tester()
  9.        Dim strBlkName As String = "Arrow_E" 'change to suit
  10.        Dim strBlkPath As String = "C:\STCustomCommon\Arrow_E.dwg" 'change to suit
  11.        If Block2Mid(strBlkName, strBlkPath) Then
  12.            Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(vbCr & "Block insertion successful.")
  13.        End If
  14.    End Sub
  15.    Public Function Block2Mid(ByVal strBlkName As String, ByVal strBlkPath As String) As Boolean
  16.        Dim db As Database = HostApplicationServices.WorkingDatabase
  17.        Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
  18.        Dim ucsMat As Matrix3d = ed.CurrentUserCoordinateSystem
  19.        Dim bt As BlockTable
  20.        Dim btr As BlockTableRecord
  21.        Dim btrSpace As BlockTableRecord
  22.        Dim entBref As BlockReference
  23.        Dim ppo As PromptPointOptions = New PromptPointOptions("Select first point: ")
  24.        ppo.AllowNone = False
  25.        ppo.AllowArbitraryInput = False
  26.        Dim ppr As PromptPointResult = ed.GetPoint(ppo)
  27.        If ppr.Status = PromptStatus.Cancel Or ppr.Status = PromptStatus.Error Then Exit Function
  28.        Dim p3d1 As Point3d = ppr.Value
  29.        ppo.UseBasePoint = True
  30.        ppo.BasePoint = p3d1
  31.        ppo.Message = vbCr & "Select second point: "
  32.        ppr = ed.GetPoint(ppo)
  33.        If ppr.Status = PromptStatus.Cancel Or ppr.Status = PromptStatus.Error Then Exit Function
  34.        Dim p3d2 As Point3d = ppr.Value
  35.        Dim v3d1 As Vector3d = p3d1.GetAsVector()
  36.        Dim v3d2 As Vector3d = p3d2.GetAsVector()
  37.        v3d2 = v3d2 - v3d1
  38.        v3d2 = v3d1 + (v3d2 / 2.0)
  39.        Dim midPt As Point3d = New Point3d(v3d2.ToArray())
  40.        p3d1 = p3d1.TransformBy(ucsMat)
  41.        p3d2 = p3d2.TransformBy(ucsMat)
  42.        midPt = midPt.TransformBy(ucsMat)
  43.        Dim entLine As Line = New Line(p3d1, p3d2)
  44.        Using tr As Transaction = db.TransactionManager.StartTransaction()
  45.            bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead)
  46.            If bt.Has(strBlkName) Then
  47.                btr = tr.GetObject(bt.Item(strBlkName), OpenMode.ForRead)
  48.            Else
  49.                btr = RtrvBlk(strBlkName, strBlkPath, ed, db)
  50.                If btr = Nothing Then
  51.                    Block2Mid = False
  52.                    Exit Function
  53.                End If
  54.            End If
  55.            Try
  56.                btrSpace = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite)
  57.                btrSpace.AppendEntity(entLine)
  58.                entBref = New BlockReference(midPt, btr.ObjectId)
  59.                entBref.Normal = ucsMat.CoordinateSystem3d.Zaxis
  60.                entBref.Position = midPt 'requires re-positioning after Normal mod
  61.                btrSpace.AppendEntity(entBref)
  62.                tr.AddNewlyCreatedDBObject(entLine, True)
  63.                tr.AddNewlyCreatedDBObject(entBref, True)
  64.                tr.Commit()
  65.            Catch ex As System.Exception
  66.                ed.WriteMessage(ex.Message)
  67.                Block2Mid = False
  68.            End Try
  69.            Block2Mid = True
  70.        End Using
  71.    End Function
  72.    Public Function RtrvBlk(ByVal strBlkName As String, ByVal strBlkPath As String, ByRef ed As Editor, ByRef db As Database) As BlockTableRecord
  73.        Dim bt As BlockTable
  74.        Dim btr As BlockTableRecord = Nothing
  75.        Dim id As ObjectId
  76.        Dim actDoc As Document = Application.DocumentManager.MdiActiveDocument
  77.        Using tr As Transaction = db.TransactionManager.StartTransaction()
  78.            Try
  79.                bt = tr.GetObject(db.BlockTableId, OpenMode.ForWrite)
  80.                Using importDb As Database = New Database(False, False)
  81.                    Try
  82.                        importDb.ReadDwgFile(strBlkPath, IO.FileShare.Read, True, "")
  83.                        id = db.Insert(strBlkPath, importDb, True)
  84.                        btr = tr.GetObject(id, OpenMode.ForWrite)
  85.                        btr.Name = strBlkName
  86.                        btr.DowngradeOpen()
  87.                    Catch ex As System.Exception
  88.                        ed.WriteMessage(ex.Message)
  89.                        Return Nothing
  90.                        Exit Function
  91.                    Finally
  92.                        importDb.Dispose()
  93.                    End Try
  94.                End Using
  95.                tr.Commit()
  96.            Catch ex As System.Exception
  97.                ed.WriteMessage(ex.Message)
  98.            End Try
  99.        End Using
  100.        Return btr
  101.    End Function
  102. End Class
回复

使用道具 举报

10

主题

29

帖子

21

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
68
发表于 2022-7-6 17:42:09 | 显示全部楼层
好的,听起来所有的点输入都是根据WCS进行的,然后使用转换矩阵转换为当前UCS。
 
感谢您提供示例代码。是你写的还是从其他资源获得的?
 
-乔
回复

使用道具 举报

10

主题

973

帖子

909

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
118
发表于 2022-7-6 17:53:53 | 显示全部楼层
一般来说,我发布的所有内容都是自己写的。我不一定会因为这样一个相当通用的例程而受到赞扬(当我说通用时,我指的是一个可能已经编写过多次的例程,不一定是一个容易编写的例程)。这种通用性(因为这项任务的需求非常普遍)意味着有用的代码片段相当丰富,我可以到处借用——呵呵——部分内容。
 
UCS的大部分内容与VBA非常相似,因此以前的经验非常有用。
 
既然我已经承认了这本《汇编》的大部分所有权,我希望真正的专业人士会过来告诉我,我真的应该做些什么才能让它变得更好。
回复

使用道具 举报

10

主题

29

帖子

21

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
68
发表于 2022-7-6 18:03:49 | 显示全部楼层
哈,对我来说已经够好了。
 
可以看出,我仍在努力找出在后台处理AutoCAD的细节。有时试图记住事物是如何结构的可能有点令人畏惧。
 
我刚刚开始学习转换,这是很好的知识。
 
谢谢你的帮助。
回复

使用道具 举报

10

主题

973

帖子

909

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
118
发表于 2022-7-6 18:12:58 | 显示全部楼层
尽管灾难的严重性。NET api,转换是简化过程的一个领域;至少与VBA相比。一旦定位,很多。NET工具随时可用。
 
 
不客气。
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

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

GMT+8, 2025-3-4 19:40 , Processed in 0.424903 second(s), 64 queries .

© 2020-2025 乐筑天下

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