lxdnn 发表于 2008-3-23 21:23:00

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

本帖最后由 作者 于 2008-3-23 21:24:36 编辑 <br /><br /> <p>&nbsp;如题。在下刚学vba。如何编程获得用户输入两点后,画一直线,然后将坐标系转换到该直线上?谢谢!</p>

lxdnn 发表于 2008-3-25 22:39:00

本帖最后由 作者 于 2008-3-25 22:44:42 编辑 <br /><br /> <p>Sub Example_ActiveUCS()<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; Dim ucsObj As AcadUCS<br/>&nbsp;&nbsp;&nbsp; Dim origin As Variant<br/>&nbsp;&nbsp;&nbsp; Dim xAxisPoint(0 To 2) As Double<br/>&nbsp;&nbsp;&nbsp; Dim yAxisPoint(0 To 2) As Double<br/>&nbsp;&nbsp;&nbsp; Dim viewportObj As AcadViewport<br/>&nbsp;&nbsp;&nbsp; Dim x As Double, y As Double, z As Double, aa As Double, bb As Double, dd As Double<br/>&nbsp;&nbsp;&nbsp; Dim dist As Double</p><p>&nbsp;&nbsp;&nbsp; ' Set the viewportObj variable to the activeviewport<br/>&nbsp;&nbsp;&nbsp; Set viewportObj = ThisDrawing.ActiveViewport<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; Dim startPnt As Variant<br/>&nbsp;&nbsp;&nbsp; Dim endPnt As Variant<br/>&nbsp;&nbsp;&nbsp; Dim prompt1 As String<br/>&nbsp;&nbsp;&nbsp; Dim prompt2 As String<br/>&nbsp;&nbsp;&nbsp; prompt1 = vbCrLf &amp; "Enter the start point of the line: "<br/>&nbsp;&nbsp;&nbsp; prompt2 = vbCrLf &amp; "Enter the end point of the line: "</p><p>&nbsp;&nbsp;&nbsp; ' 获取第一点</p><p>&nbsp;&nbsp;&nbsp; startPnt = ThisDrawing.Utility.GetPoint(, prompt1)<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; ' 获取第二点</p><p>&nbsp;&nbsp;&nbsp; endPnt = ThisDrawing.Utility.GetPoint(, prompt2)</p><p>&nbsp;&nbsp;&nbsp; ' 使用输入的两个点创建一条直线</p><p>&nbsp;&nbsp;&nbsp; ThisDrawing.ModelSpace.AddLine startPnt, endPnt</p><p>&nbsp;&nbsp;&nbsp; ThisDrawing.Application.ZoomAll</p><p>&nbsp;&nbsp;&nbsp; ' 计算 point1 和 point2 之间的距离</p><p>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; x = startPnt(0) - endPnt(0)</p><p>&nbsp;&nbsp;&nbsp; y = startPnt(1) - endPnt(1)</p><p>&nbsp;&nbsp;&nbsp; z = startPnt(2) - endPnt(2)<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; dist = Sqr((Sqr((x ^ 2) + (y ^ 2)) ^ 2) + (z ^ 2))</p><p>&nbsp;&nbsp;&nbsp; '显示计算出来的距离</p><p>&nbsp;&nbsp;&nbsp; MsgBox "The distance between the points is: " _<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &amp; dist, , "Calculate Distance"</p><p>&nbsp;&nbsp;&nbsp; aa = Abs(endPnt(1) - startPnt(1))<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; bb = Abs(endPnt(0) - startPnt(0))<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; dd = (aa * aa) / bb<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; origin = startPnt</p><p>&nbsp;&nbsp; xAxisPoint(0) = origin(0) + 1: xAxisPoint(1) = origin(1): xAxisPoint(2) = 0</p><p>&nbsp;&nbsp; yAxisPoint(0) = origin(0): yAxisPoint(1) = origin(1) + 1: yAxisPoint(2) = 0</p><p>&nbsp;&nbsp; Set ucsObj = ThisDrawing.UserCoordinateSystems.Add(origin, xAxisPoint, yAxisPoint, "UCS1")<br/>&nbsp; <br/>&nbsp;&nbsp;&nbsp; xAxisPoint(0) = bb: xAxisPoint(1) = aa: xAxisPoint(2) = 0<br/>&nbsp;&nbsp;&nbsp; yAxisPoint(0) = -dd: yAxisPoint(1) = aa: yAxisPoint(2) = 0<br/>&nbsp;&nbsp; <br/>&nbsp;<br/>&nbsp;&nbsp;&nbsp; Set ucsObj = ThisDrawing.UserCoordinateSystems.Add(origin, xAxisPoint, yAxisPoint, "TestUCS")<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; ThisDrawing.ActiveUCS = ucsObj<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; MsgBox "The new UCS is " &amp; ucsObj.Name, vbInformation, "ActiveUCS Example"<br/>&nbsp;&nbsp;&nbsp; aa = 0<br/>&nbsp;&nbsp;&nbsp; bb = 0<br/>&nbsp;&nbsp;&nbsp; dd = 0<br/>End Sub</p><p>但是以上程序仅在第一点选择wcs原点时才有效,否则就会提示y轴与x轴不垂直.能不能用translatecoordinates转换呢?但是我不会用,请斑竹赐教.不胜感激!</p>

雪山飞狐_lzh 发表于 2008-3-26 11:30:00

<p>xAxisPoint和yAxisPoint是两轴上的点,而不是向量</p><p></p><p>Sub Example_ActiveUCS()<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; Dim ucsObj As AcadUCS<br/>&nbsp;&nbsp;&nbsp; Dim origin As Variant<br/>&nbsp;&nbsp;&nbsp; Dim xAxisPoint<br/>&nbsp;&nbsp;&nbsp; Dim yAxisPoint<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; Dim startPnt As Variant<br/>&nbsp;&nbsp;&nbsp; Dim endPnt As Variant<br/>&nbsp;&nbsp;&nbsp; Dim oLine As AcadLine<br/>&nbsp;&nbsp;&nbsp; Dim prompt1 As String<br/>&nbsp;&nbsp;&nbsp; Dim prompt2 As String<br/>&nbsp;&nbsp;&nbsp; prompt1 = vbCrLf &amp; "Enter the start point of the line: "<br/>&nbsp;&nbsp;&nbsp; prompt2 = vbCrLf &amp; "Enter the end point of the line: "<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; startPnt = ThisDrawing.Utility.GetPoint(, prompt1)<br/>&nbsp;&nbsp;&nbsp; endPnt = ThisDrawing.Utility.GetPoint(startPnt, prompt2)<br/>&nbsp;&nbsp;&nbsp; Set oLine = ThisDrawing.ModelSpace.AddLine(startPnt, endPnt)<br/>&nbsp;&nbsp;&nbsp; </p><p>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; Dim pnt(2) As Double<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; origin = startPnt<br/>&nbsp;&nbsp;&nbsp; xAxisPoint = ThisDrawing.Utility.PolarPoint(startPnt, oLine.Angle, 1)<br/>&nbsp;&nbsp;&nbsp; yAxisPoint = ThisDrawing.Utility.PolarPoint(startPnt, oLine.Angle + Atn(1) * 2, 1)<br/>&nbsp;<br/>&nbsp;&nbsp;&nbsp; Set ucsObj = ThisDrawing.UserCoordinateSystems.Add(origin, xAxisPoint, yAxisPoint, "TestUCS")<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; ThisDrawing.ActiveUCS = ucsObj<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; MsgBox "The new UCS is " &amp; ucsObj.Name, vbInformation, "ActiveUCS Example"</p><p>End Sub</p><p></p>
页: [1]
查看完整版本: [求助]在vba中如何编程将坐标系转换到一根直线上?