乐筑天下

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

[求助]在vba中如何编程将坐标系转换到一根直线上?

[复制链接]

1

主题

2

帖子

1

银币

初来乍到

Rank: 1

铜币
6
发表于 2008-3-23 21:23:00 | 显示全部楼层 |阅读模式
如题。在下刚学vba。如何编程获得用户输入两点后,画一直线,然后将坐标系转换到该直线上?谢谢!

3cgv5vxc1s2.JPG

3cgv5vxc1s2.JPG

回复

使用道具 举报

1

主题

2

帖子

1

银币

初来乍到

Rank: 1

铜币
6
发表于 2008-3-25 22:39:00 | 显示全部楼层

Sub Example_ActiveUCS()
   
    Dim ucsObj As AcadUCS
    Dim origin As Variant
    Dim xAxisPoint(0 To 2) As Double
    Dim yAxisPoint(0 To 2) As Double
    Dim viewportObj As AcadViewport
    Dim x As Double, y As Double, z As Double, aa As Double, bb As Double, dd As Double
    Dim dist As Double
    ' Set the viewportObj variable to the activeviewport
    Set viewportObj = ThisDrawing.ActiveViewport
   
    Dim startPnt As Variant
    Dim endPnt As Variant
    Dim prompt1 As String
    Dim prompt2 As String
    prompt1 = vbCrLf & "Enter the start point of the line: "
    prompt2 = vbCrLf & "Enter the end point of the line: "
    ' 获取第一点
    startPnt = ThisDrawing.Utility.GetPoint(, prompt1)
   
    ' 获取第二点
    endPnt = ThisDrawing.Utility.GetPoint(, prompt2)
    ' 使用输入的两个点创建一条直线
    ThisDrawing.ModelSpace.AddLine startPnt, endPnt
    ThisDrawing.Application.ZoomAll
    ' 计算 point1 和 point2 之间的距离
   
    x = startPnt(0) - endPnt(0)
    y = startPnt(1) - endPnt(1)
    z = startPnt(2) - endPnt(2)
   
    dist = Sqr((Sqr((x ^ 2) + (y ^ 2)) ^ 2) + (z ^ 2))
    '显示计算出来的距离
    MsgBox "The distance between the points is: " _
                  & dist, , "Calculate Distance"
    aa = Abs(endPnt(1) - startPnt(1))
   
    bb = Abs(endPnt(0) - startPnt(0))
   
    dd = (aa * aa) / bb
   
   
    origin = startPnt
   xAxisPoint(0) = origin(0) + 1: xAxisPoint(1) = origin(1): xAxisPoint(2) = 0
   yAxisPoint(0) = origin(0): yAxisPoint(1) = origin(1) + 1: yAxisPoint(2) = 0
   Set ucsObj = ThisDrawing.UserCoordinateSystems.Add(origin, xAxisPoint, yAxisPoint, "UCS1")
  
    xAxisPoint(0) = bb: xAxisPoint(1) = aa: xAxisPoint(2) = 0
    yAxisPoint(0) = -dd: yAxisPoint(1) = aa: yAxisPoint(2) = 0
   

    Set ucsObj = ThisDrawing.UserCoordinateSystems.Add(origin, xAxisPoint, yAxisPoint, "TestUCS")
   
    ThisDrawing.ActiveUCS = ucsObj
   
    MsgBox "The new UCS is " & ucsObj.Name, vbInformation, "ActiveUCS Example"
    aa = 0
    bb = 0
    dd = 0
End Sub
但是以上程序仅在第一点选择wcs原点时才有效,否则就会提示y轴与x轴不垂直.能不能用translatecoordinates转换呢?但是我不会用,请斑竹赐教.不胜感激!
回复

使用道具 举报

72

主题

2726

帖子

9

银币

社区元老

Rank: 75Rank: 75Rank: 75

铜币
3014
发表于 2008-3-26 11:30:00 | 显示全部楼层
xAxisPoint和yAxisPoint是两轴上的点,而不是向量
Sub Example_ActiveUCS()
   
    Dim ucsObj As AcadUCS
    Dim origin As Variant
    Dim xAxisPoint
    Dim yAxisPoint
   
    Dim startPnt As Variant
    Dim endPnt As Variant
    Dim oLine As AcadLine
    Dim prompt1 As String
    Dim prompt2 As String
    prompt1 = vbCrLf & "Enter the start point of the line: "
    prompt2 = vbCrLf & "Enter the end point of the line: "
   
    startPnt = ThisDrawing.Utility.GetPoint(, prompt1)
    endPnt = ThisDrawing.Utility.GetPoint(startPnt, prompt2)
    Set oLine = ThisDrawing.ModelSpace.AddLine(startPnt, endPnt)
   
   
    Dim pnt(2) As Double
   
    origin = startPnt
    xAxisPoint = ThisDrawing.Utility.PolarPoint(startPnt, oLine.Angle, 1)
    yAxisPoint = ThisDrawing.Utility.PolarPoint(startPnt, oLine.Angle + Atn(1) * 2, 1)

    Set ucsObj = ThisDrawing.UserCoordinateSystems.Add(origin, xAxisPoint, yAxisPoint, "TestUCS")
   
    ThisDrawing.ActiveUCS = ucsObj
   
    MsgBox "The new UCS is " & ucsObj.Name, vbInformation, "ActiveUCS Example"
End Sub
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

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

GMT+8, 2025-7-5 03:38 , Processed in 0.494624 second(s), 72 queries .

© 2020-2025 乐筑天下

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