明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 5872|回复: 5

在VBA中应用ucs小结,深入学习AUTOCAD二次开发第九章.

[复制链接]
发表于 2009-7-21 09:27:00 | 显示全部楼层 |阅读模式
本帖最后由 作者 于 2009-7-22 8:43:21 编辑

Sub A()
    Dim P1(2) As Double, P2(2) As Double, P3(2) As Double, P4(2) As Double
    Dim UCS As AcadUCS, Op(2) As Double, Xp(2) As Double, Yp(2) As Double
    With ThisDrawing
        '下面4个点用于定义二维填充(solid)对象
        P1(0) = 0: P1(1) = 0: P1(2) = 0
        '下面3个点用于定义新的UCS
        Op(0) = 0: Op(1) = 0: Op(2) = 0 '新UCS的原点
        Xp(0) = 1: Xp(1) = 0: Xp(2) = 0 '新UCS的X方向
        Yp(0) = 0: Yp(1) = 0: Yp(2) = 1 '新UCS的Y方向
        '新建UCS
        Set UCS = .UserCoordinateSystems.Add(Op, Xp, Yp, "AAA")
        '新UCS置为当前
        .ActiveUCS = UCS
        '创建二维填充
        Set objCircle = .ModelSpace.AddCircle(P1, 10)

    End With
End Sub

Xp(0)=1:Yp(2)=1 相当于执行UCS命令--- UCS →X →90

Xp(0)=1:Yp(2)=-1 相当于执行UCS命令--- UCS →X →-90

Xp(2)=1:Yp(1)=1 相当于执行UCS命令--- UCS →X →-90

希望有兴趣的大侠共同整理,谢谢.

 楼主| 发表于 2009-7-21 10:44:00 | 显示全部楼层
下面程序是将VBA示例程序,填加了circle,arc和arraypolar命令,便于各位大侠理解.
  1. Sub Example_UserCoordinateSystems()
  2.     ' This example finds the current UserCoordinateSystems collection and
  3.     ' adds a new UCS to that collection.
  4.     Dim pp(2) As Double
  5.     Dim UCSColl As AcadUCSs
  6.     Set UCSColl = ThisDrawing.UserCoordinateSystems
  7.    
  8.     ' Create a UCS named "TEST" in the current drawing
  9.     Dim ucsObj As AcadUCS
  10.     Dim origin(0 To 2) As Double
  11.     Dim xAxisPnt(0 To 2) As Double
  12.     Dim yAxisPnt(0 To 2) As Double
  13.     Dim pp1(2) As Double
  14.     pp1(1) = 20
  15.     ' Define the UCS
  16.     origin(0) = 0#: origin(1) = 0#: origin(2) = 0#
  17.     xAxisPnt(0) = 0: xAxisPnt(1) = 1#: xAxisPnt(2) = 0
  18.     yAxisPnt(0) = 0: yAxisPnt(1) = 0#: yAxisPnt(2) = 1
  19.     Set objLine = ThisDrawing.ModelSpace.AddLine(xAxisPnt, yAxisPnt)
  20.        objLine.color = 3
  21.     ' Add the UCS to the UserCoordinatesSystems collection
  22.     Dim objCircle As AcadCircle
  23.     Set ucsObj = UCSColl.Add(origin, xAxisPnt, yAxisPnt, "TEST")
  24.     ThisDrawing.ActiveUCS = ucsObj
  25.     Set objCircle = ThisDrawing.ModelSpace.AddCircle(pp, 20)
  26.     Set objCircle = ThisDrawing.ModelSpace.AddCircle(pp1, 0.5)
  27.     Set objArc = ThisDrawing.ModelSpace.AddArc(pp, 3, 0, 1.5)
  28.     'Set objcy = ThisDrawing.ModelSpace.AddCylinder(pp, 5, 20)
  29.     objAng = (Atn(1) * 4 / 180) * 360
  30.      objC = objCircle.ArrayPolar(6, objAng, pp)
  31.     'MsgBox "A new UCS called " & ucsObj.Name & " has been added to the UserCoordinateSystems collection.", vbInformation, "UserCoordinateSystems 示例"
  32. End Sub

发表于 2009-7-21 21:48:00 | 显示全部楼层
在UCS中有一个已经定义好的UCS为"aa"
采用UserCoordianteSystems.Item方法.
用UserCoordinateSystems.Item("aa")
  1. Sub LLL()
  2.   Dim UUS As AcadUCSs
  3.   Dim UU As AcadUCS
  4.   Set UU = ThisDrawing.UserCoordinateSystems.Item("aa")
  5.   ThisDrawing.ActiveUCS = UU
  6.   transMatrix = UU.GetUCSMatrix()
  7.   Dim objLine As AcadLine, objCircle As AcadCircle
  8.   Set objCircle = ThisDrawing.HandleToObject("8E")
  9.   objCircle.TransformBy (transMatrix)
  10. End Sub
 楼主| 发表于 2009-7-22 08:46:00 | 显示全部楼层
重新体会AutoCAD二次开发第九章的9.1到9.2
上面所述全在下面程序中解决.
  1. Sub test_AddOrgUCS()
  2.     '原点UCS调用示例
  3.     Dim myUCS As AcadUCS, NewOrgPt As Variant
  4.     NewOrgPt = ThisDrawing.Utility.GetPoint(, vbCrLf & "请输入新原点:")
  5.     Set myUCS = AddOrgUCS(NewOrgPt, "abc")
  6.     ThisDrawing.ActiveUCS = myUCS
  7. End Sub
  8. Sub test_AddXAngUCS()
  9.     'X轴旋转UCS调用示例
  10.     Dim myUCS As AcadUCS, Ang As Double
  11.     Ang = ThisDrawing.Utility.GetAngle(, vbCrLf & "请指定绕 X 轴的旋转角度:")
  12.     Set myUCS = AddXAngUCS(Ang, "abc")
  13.     ThisDrawing.ActiveUCS = myUCS
  14. End Sub
  15. Sub test_AddYAngUCS()
  16.     'Y轴旋转UCS调用示例
  17.     Dim myUCS As AcadUCS, Ang As Double
  18.     Ang = ThisDrawing.Utility.GetAngle(, vbCrLf & "请指定绕 Y 轴的旋转角度:")
  19.     Set myUCS = AddYAngUCS(Ang, "abc")
  20.     ThisDrawing.ActiveUCS = myUCS
  21. End Sub
  22. Sub test_AddZAngUCS()
  23.     'Z轴旋转UCS调用示例
  24.     Dim myUCS As AcadUCS, Ang As Double
  25.     Ang = ThisDrawing.Utility.GetAngle(, vbCrLf & "请指定绕 Z 轴的旋转角度:")
  26.     Set myUCS = AddZAngUCS(Ang, "abc")
  27.     ThisDrawing.ActiveUCS = myUCS
  28. End Sub
  29. ' 移动原点创建UCS
  30. ' ptOriginWcs:新UCS的原点在WCS中的坐标
  31. Public Function AddOrgUCS(ptOriginWcs As Variant, strUcsName As String) As AcadUCS
  32.     ' 获得新UCS原点在当前UCS中的坐标
  33.     Dim ptOriginUcs As Variant
  34.     ptOriginUcs = PtWcs2Ucs(ptOriginWcs)
  35.     'Debug.Print ptOriginWcs(0)
  36.     ' 获得X、Y正半轴上任一点的UCS坐标
  37.     Dim ptXUcs(0 To 2) As Double, ptYUcs(0 To 2) As Double
  38.     ptXUcs(0) = ptOriginUcs(0) + 1
  39.     ptXUcs(1) = ptOriginUcs(1)
  40.     ptXUcs(2) = ptOriginUcs(2)
  41.     ptYUcs(0) = ptOriginUcs(0)
  42.     ptYUcs(1) = ptOriginUcs(1) + 1
  43.     ptYUcs(2) = ptOriginUcs(2)
  44.    
  45.     ' 获得X、Y正半轴上任一点的WCS坐标
  46.     Dim ptXWcs As Variant, ptYWcs As Variant
  47.     ptOriginWcs = PtUcs2Wcs(ptOriginUcs)
  48.     ptXWcs = PtUcs2Wcs(ptXUcs)
  49.     ptYWcs = PtUcs2Wcs(ptYUcs)
  50.     'Debug.Print ptOriginWcs(0)
  51.     ' 创建UCS
  52.     Set AddOrgUCS = ThisDrawing.UserCoordinateSystems.Add(ptOriginWcs, ptXWcs, ptYWcs, strUcsName)
  53. End Function
  54. ' 旋转X轴创建新的UCS
  55. ' angle:弧度值,绕X轴旋转的角度(旋转方向由右手定则决定)
  56. Public Function AddXAngUCS(angle As Double, strUcsName As String) As AcadUCS
  57.     ' 定义新UCS原点的三维WCS坐标
  58.     Dim ptOriginUcs(0 To 2) As Double
  59.     ptOriginUcs(0) = 0: ptOriginUcs(1) = 0: ptOriginUcs(2) = 0
  60.     Dim ptOriginWcs As Variant
  61.     ptOriginWcs = PtUcs2Wcs(ptOriginUcs)
  62.    
  63.     ' 定义新UCS在X轴正方向上一个点的三维WCS坐标
  64.     Dim ptXUcs(0 To 2) As Double
  65.     ptXUcs(0) = 1: ptXUcs(1) = 0: ptXUcs(2) = 0
  66.     Dim ptXWcs As Variant
  67.     ptXWcs = PtUcs2Wcs(ptXUcs)
  68.    
  69.     ' 定义新UCS在Y轴正方向上一个点的三维WCS坐标
  70.     Dim ptYUcs(0 To 2) As Double
  71.     ptYUcs(0) = 0: ptYUcs(1) = Cos(angle): ptYUcs(2) = Sin(angle)
  72.     Dim ptYWcs As Variant
  73.     ptYWcs = PtUcs2Wcs(ptYUcs)
  74.    
  75.     Set AddXAngUCS = ThisDrawing.UserCoordinateSystems.Add(ptOriginWcs, ptXWcs, ptYWcs, strUcsName)
  76. End Function
  77. ' 旋转Y轴创建新的UCS
  78. ' angle:弧度值,绕Y轴旋转的角度(旋转方向由右手定则决定)
  79. Public Function AddYAngUCS(angle As Double, strUcsName As String) As AcadUCS
  80.     ' 定义新UCS原点的三维WCS坐标
  81.     Dim ptOriginUcs(0 To 2) As Double
  82.     ptOriginUcs(0) = 0: ptOriginUcs(1) = 0: ptOriginUcs(2) = 0
  83.     Dim ptOriginWcs As Variant
  84.     ptOriginWcs = PtUcs2Wcs(ptOriginUcs)
  85.    
  86.     ' 定义新UCS在X轴正方向上一个点的三维WCS坐标
  87.     Dim ptXUcs(0 To 2) As Double
  88.     ptXUcs(0) = Cos(angle): ptXUcs(1) = 0: ptXUcs(2) = -Sin(angle)
  89.     Dim ptXWcs As Variant
  90.     ptXWcs = PtUcs2Wcs(ptXUcs)
  91.    
  92.     ' 定义新UCS在Y轴正方向上一个点的三维WCS坐标
  93.     Dim ptYUcs(0 To 2) As Double
  94.     ptYUcs(0) = 0: ptYUcs(1) = 1: ptYUcs(2) = 0
  95.     Dim ptYWcs As Variant
  96.     ptYWcs = PtUcs2Wcs(ptYUcs)
  97.    
  98.     Set AddYAngUCS = ThisDrawing.UserCoordinateSystems.Add(ptOriginWcs, ptXWcs, ptYWcs, strUcsName)
  99. End Function
  100. ' 旋转Z轴创建新的UCS
  101. ' angle:弧度值,绕Z轴旋转的角度(旋转方向由右手定则决定)
  102. Public Function AddZAngUCS(angle As Double, strUcsName As String) As AcadUCS
  103.     ' 定义新UCS原点的三维WCS坐标
  104.     Dim ptOriginUcs(0 To 2) As Double
  105.     ptOriginUcs(0) = 0: ptOriginUcs(1) = 0: ptOriginUcs(2) = 0
  106.     Dim ptOriginWcs As Variant
  107.     ptOriginWcs = PtUcs2Wcs(ptOriginUcs)
  108.    
  109.     ' 定义新UCS在X轴正方向上一个点的三维WCS坐标
  110.     Dim ptXUcs(0 To 2) As Double
  111.     ptXUcs(0) = Cos(angle): ptXUcs(1) = Sin(angle): ptXUcs(2) = 0
  112.     Dim ptXWcs As Variant
  113.     ptXWcs = PtUcs2Wcs(ptXUcs)
  114.    
  115.     ' 定义新UCS在Y轴正方向上一个点的三维WCS坐标
  116.     Dim ptYUcs(0 To 2) As Double
  117.     ptYUcs(0) = -Sin(angle): ptYUcs(1) = Cos(angle): ptYUcs(2) = 0
  118.     Dim ptYWcs As Variant
  119.     ptYWcs = PtUcs2Wcs(ptYUcs)
  120.    
  121.     Set AddZAngUCS = ThisDrawing.UserCoordinateSystems.Add(ptOriginWcs, ptXWcs, ptYWcs, strUcsName)
  122. End Function
  123. ' 将点的UCS坐标转化到WCS坐标
  124. Private Function PtUcs2Wcs(ptUcs As Variant) As Variant
  125.     PtUcs2Wcs = ThisDrawing.Utility.TranslateCoordinates(ptUcs, acUCS, acWorld, False)
  126. End Function
  127. ' 将点的WCS坐标转化到UCS坐标
  128. Private Function PtWcs2Ucs(ptWcs As Variant) As Variant
  129.     PtWcs2Ucs = ThisDrawing.Utility.TranslateCoordinates(ptWcs, acWorld, acUCS, False)
  130. End Function
发表于 2011-12-9 23:58:58 | 显示全部楼层
兰州人 发表于 2009-7-22 08:46
重新体会AutoCAD二次开发第九章的9.1到9.2
上面所述全在下面程序中解决.

非常感谢楼主
发表于 2013-5-16 08:00:31 | 显示全部楼层
感谢版主。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-12-23 15:41 , Processed in 0.168371 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表