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