在UCS下画线出现问题
<p>如题,为什么我第二次画的线和第三次画的线重合?代码如下:</p><p><font style="BACKGROUND-COLOR: #ffffff" face="Verdana">'CAD<br/>Imports Autodesk.AutoCAD.Interop<br/>Imports Autodesk.AutoCAD.Interop.Common<br/>Public Class Form1<br/> Dim AcadApp As AcadApplication<br/> Dim thisdrawing As AcadDocument<br/> Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click<br/> Try<br/> AcadApp = GetObject("autocad.application")<br/> Catch ex As Exception<br/> Try<br/> AcadApp = CreateObject("autocad.application")<br/> Catch ex1 As Exception<br/> MsgBox("无法打开AutoCAD")<br/> Exit Sub<br/> End Try<br/> End Try<br/> AcadApp.Visible = True<br/> AppActivate(AcadApp.Caption)<br/> thisdrawing = AcadApp.ActiveDocument<br/> For i = 1 To 3<br/> Dim origin(0 To 2) As Double<br/> Call AddLine_UCS(1, 1, 0, 12, 12, 0)<br/> 'Dim origin(0 To 2) As Double<br/> origin(0) = 8 : origin(1) = 8 : origin(2) = 0<br/> Call MoveOriginUCS(origin, "MyUcs")<br/> Next</font></p>
<p><font style="BACKGROUND-COLOR: #ffffff" face="Verdana"> End Sub<br/> '通过移动坐标原点定义坐标系<br/> Public Function MoveOriginUCS(ByVal originWcs As Object, ByVal ucsName As String) As AcadUCS<br/> ' 获得新UCS原点在当前UCS中的坐标<br/> Dim originUcs As Object<br/> originUcs = TranslatePointWcsToUcs(originWcs)</font></p>
<p><font style="BACKGROUND-COLOR: #ffffff" face="Verdana"> ' 获得X、Y正半轴上任一点的UCS坐标<br/> Dim ptXUcs(0 To 2) As Double, ptYUcs(0 To 2) As Double<br/> ptXUcs(0) = originUcs(0) + 1<br/> ptXUcs(1) = originUcs(1)<br/> ptXUcs(2) = originUcs(2)<br/> ptYUcs(0) = originUcs(0)<br/> ptYUcs(1) = originUcs(1) + 1<br/> ptYUcs(2) = originUcs(2)</font></p>
<p><font style="BACKGROUND-COLOR: #ffffff" face="Verdana"> ' 获得X、Y正半轴上任一点的WCS坐标<br/> Dim ptXWcs As Object, ptYWcs As Object<br/> originWcs = TranslatePointUcsToWcs(originUcs)<br/> ptXWcs = TranslatePointUcsToWcs(ptXUcs)<br/> ptYWcs = TranslatePointUcsToWcs(ptYUcs)</font></p>
<p><font style="BACKGROUND-COLOR: #ffffff" face="Verdana"> ' 创建UCS<br/> MoveOriginUCS = thisdrawing.UserCoordinateSystems.Add(originWcs, ptXWcs, ptYWcs, ucsName)<br/> 'MoveOriginUCS = thisdrawing.UserCoordinateSystems.Add(originUcs, ptXUcs, ptYUcs, ucsName)<br/> '' 显示 UCS 图标<br/> 'thisdrawing.ActiveViewport.UCSIconAtOrigin = True<br/> 'thisdrawing.ActiveViewport.UCSIconOn = True<br/> '' 使新的 UCS 成为活动的 UCS<br/> 'thisdrawing.ActiveUCS = MoveOriginUCS<br/> End Function<br/> ' 将点的坐标从UCS转换到WCS<br/> Public Function TranslatePointUcsToWcs(ByVal ucsPoint As Object) As Object<br/> Debug.Assert(VarType(ucsPoint) = vbArray + vbDouble)<br/> Debug.Assert(LBound(ucsPoint) = 0 And UBound(ucsPoint) = 2)<br/> TranslatePointUcsToWcs = thisdrawing.Utility.TranslateCoordinates(ucsPoint, AcCoordinateSystem.acUCS, AcCoordinateSystem.acWorld, False)<br/> End Function</font></p>
<p><font style="BACKGROUND-COLOR: #ffffff" face="Verdana"> ' 将点的坐标从WCS转换到UCS<br/> Public Function TranslatePointWcsToUcs(ByVal wcsPoint As Object) As Object<br/> Debug.Assert(VarType(wcsPoint) = vbArray + vbDouble)<br/> Debug.Assert(LBound(wcsPoint) = 0 And UBound(wcsPoint) = 2)<br/> TranslatePointWcsToUcs = thisdrawing.Utility.TranslateCoordinates(wcsPoint, AcCoordinateSystem.acWorld, AcCoordinateSystem.acUCS, False)<br/> End Function<br/> Public Function AddLine_UCS(ByVal p1x As Double, ByVal p1y As Double, ByVal p1z As Double, ByVal p2x As Double, ByVal p2y As Double, ByVal p2z As Double) As AcadLine<br/> ' 保存当前的UCS<br/> Dim curUcs As AcadUCS<br/> curUcs = GetActiveUCS()<br/> ' 返回到WCS<br/> thisdrawing.ActiveUCS = GetWCS()<br/> Dim ptStart(2) As Double<br/> Dim ptEnd(2) As Double<br/> ptStart(0) = p1x<br/> ptStart(1) = p1y<br/> ptStart(2) = p1z</font></p>
<p><font style="BACKGROUND-COLOR: #ffffff" face="Verdana"> ptEnd(0) = p2x<br/> ptEnd(1) = p2y<br/> ptEnd(2) = p2z</font></p>
<p><font style="BACKGROUND-COLOR: #ffffff" face="Verdana"> ' 在WCS中创建轻量多段线<br/> Dim objLine As AcadLine<br/> objLine = thisdrawing.ModelSpace.AddLine(ptStart, ptEnd)</font></p>
<p><font style="BACKGROUND-COLOR: #ffffff" face="Verdana"> ' 恢复保存的UCS<br/> thisdrawing.ActiveUCS = curUcs</font></p>
<p><font style="BACKGROUND-COLOR: #ffffff" face="Verdana"> ' 对长方体进行变换<br/> Dim transMatrix As Object<br/> transMatrix = curUcs.GetUCSMatrix()<br/> objLine.TransformBy(transMatrix)<br/> objLine.Update()</font></p>
<p><font style="BACKGROUND-COLOR: #ffffff" face="Verdana"> AddLine_UCS = objLine<br/> End Function<br/> Public Function GetWCS() As AcadUCS<br/> ' 定义创建UCS的三个点<br/> Dim ptOrigin(2) As Double, ptXAxis(2) As Double, ptYAxis(2) As Double<br/> ptOrigin(0) = 0 : ptOrigin(1) = 0 : ptOrigin(2) = 0<br/> ptXAxis(0) = 1 : ptXAxis(1) = 0 : ptXAxis(2) = 0<br/> ptYAxis(0) = 0 : ptYAxis(1) = 1 : ptYAxis(2) = 0<br/> GetWCS = thisdrawing.UserCoordinateSystems.Add(ptOrigin, ptXAxis, ptYAxis, "WCS")<br/> End Function<br/> Public Function GetActiveUCS() As AcadUCS<br/> If thisdrawing.GetVariable("UCSNAME") = "" Then<br/> Dim ptOrigin(2) As Double ' 要创建的UCS的原点<br/> Dim ptXAxis(2) As Double ' UCS的X轴正半轴上一点<br/> Dim ptYAxis(2) As Double ' UCS的Y轴正半轴上一点<br/> Dim xDir, yDir, org As Object ' 当前UCS的参数</font></p>
<p><font style="BACKGROUND-COLOR: #ffffff" face="Verdana"> ' 获得当前UCS的参数<br/> xDir = thisdrawing.GetVariable("UCSXDIR")<br/> yDir = thisdrawing.GetVariable("UCSYDIR")<br/> org = thisdrawing.GetVariable("UCSORG")</font></p>
<p><font style="BACKGROUND-COLOR: #ffffff" face="Verdana"> ' UCS的原点<br/> ptOrigin(0) = org(0)<br/> ptOrigin(1) = org(1)<br/> ptOrigin(2) = org(2)</font></p>
<p><font style="BACKGROUND-COLOR: #ffffff" face="Verdana"> ' 获得UCS的X轴正半轴上的一点<br/> ptXAxis(0) = org(0) + xDir(0)<br/> ptXAxis(1) = org(1) + xDir(1)<br/> ptXAxis(2) = org(2) + xDir(2)</font></p>
<p><font style="BACKGROUND-COLOR: #ffffff" face="Verdana"> ' 获得UCS的Y轴正半轴上的一点<br/> ptYAxis(0) = org(0) + yDir(0)<br/> ptYAxis(1) = org(1) + yDir(1)<br/> ptYAxis(2) = org(2) + yDir(2)</font></p>
<p><font style="BACKGROUND-COLOR: #ffffff" face="Verdana"> ' 创建和当前UCS重合的UCS<br/> GetActiveUCS = thisdrawing.UserCoordinateSystems.Add(ptOrigin, ptXAxis, ptYAxis, "MyUCS")</font></p>
<p><font style="BACKGROUND-COLOR: #ffffff" face="Verdana"> thisdrawing.ActiveUCS = GetActiveUCS<br/> Else<br/> GetActiveUCS = thisdrawing.ActiveUCS<br/> End If<br/> End Function<br/>End Class</font></p> 问题已经解决了! 楼上是怎么解决的啊?
页:
[1]